The following are examples of how you can use geb.js to facilitate the Global Settlement process and redeem your collateral which is locked in your own Safes or directly exchange system coins with collateral.
Example Flow for Global Settlement Using GEB.js
These scripts can help you go throught the steps described on the Global Settlement page.
Since a SAFE is supposed to be over-collateralized, its owner can already withdraw excess collateral. The following script assumes that the SAFE is owned by a proxy contract. It also uses the Global Settlement Proxy Actions to pack and atomically execute multiple transactions at once.
constproxy=awaitgeb.getProxyAction(wallet.address)constwethJoinAddress=geb.contracts.joinETH_A.address// Withdraw excess collateral from the Safe with ID #3consttx=proxy.freeTokenCollateralGlobalSettlement(wethJoinAddress,3)awaitwallet.sendTransaction(tx)
This fulfills step 3 and step 5 from the Global Settlement process.
Set the Final COL/COIN Exchange Rates
This part of the process consists in determining an exchange rate between the system coins that are still in circulation and each individual collateral type accepted by the system. The system needs to account for all Safes (I), terminate all ongoing collateral auctions (II) and remove all system surplus (III).
This needs to be done only once for the whole system. These steps can be taken care of by the settlement keeper bot or by anyone who is willing to pay the gas costs associated with these transactions.
// For (I) the function `processSAFE` needs to be called for all SAFEs,// particularly for under-collateralized ones since their owners are not // incentivised to call it themselvesconstsafes= [] // Gather some Safe handlers safes.push((awaitgeb.getSafe(2)).handler)safes.push((awaitgeb.getSafe(3)).handler)safes.push((awaitgeb.getSafe(4)).handler)// Prepare the transactionsconsttxs=safes.map(handler =>geb.contracts.globalSettlement.processSAFE(utils.ETH_A, handler))// Send all transactionstxs.map(tx =>awaitwallet.sendTransaction(tx))// For (II) we have 2 possibilities: wait for all auctions to finishconstcooldown=awaitgeb.contracts.globalSettlement.shutdownCooldown()constnow=BigNumber.from(Date.now()).div(1000)constisCooldownPassed=now.gt(shutdownTime.add(cooldown))// Or prematurely terminate each auctionconstauctionId=6consttx=geb.contracts.globalSettlement.fastTrackAuction(utils.ETH_A, auctionId)awaitwallet.sendTransaction(tx)// (III) We need to get rid of the system surplusconstaccountingEngineAddress=geb.contracts.accountingEngine.addressconstcoin=awaitgeb.contracts.safeEngine.coinBalance(accountingEngineAddress)constdebt=awaitgeb.contracts.safeEngine.debtBalance(accountingEngineAddress)constamountToSettle=coin.gte(debt) ? debt : coinconsttx=geb.contracts.accountingEngine.settleDebt(amountToSettle)awaitwallet.sendTransaction(tx)// In case there is a bug in the system's accounting that // created more surplus than debt, there is a backup function called// transferPostSettlementSurplus() which gets rid of that extra surplus// and allows GlobalSettlement.setOutstandingCoinSupply() to execute successfulyconsttx=geb.contracts.accountingEngine.transferPostSettlementSurplus()awaitwallet.sendTransaction(tx)
Finally, the cash price for each collateral can be set with the following steps:
consttx=geb.contracts.globalSettlement.setOutstandingCoinSupply()awaitwallet.sendTransaction(tx)// To be called once for each collateral typeconsttx=geb.contracts.globalSettlement.calculateCashPrice(utils.ETH_A)awaitwallet.sendTransaction(tx)
Redeem Collateral Against System Coins
At this stage, any system coin holder can exchange their coins against a fixed basket of collateral. This is a 2 step process that consists in locking and preparing system coins and then claiming a share of a specific collateral type.
// Prepare the system coinsconstsystemCoinBalance=awaitgeb.contracts.coin.balanceOf(wallet.address)consttx=proxy.prepareCoinsForRedeemingGlobalSettlement(systemCoinBalance)awaitwallet.sendTransaction(tx)// Redeem any collateral type you wantconsttx=proxy.redeemTokenCollateralGlobalSettlement(wethJoinAddress,utils.ETH_A, systemCoinBalance)awaitwallet.sendTransaction(tx)