// 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 themselves
const safes = [] // Gather some Safe handlers
safes.push((await geb.getSafe(2)).handler)
safes.push((await geb.getSafe(3)).handler)
safes.push((await geb.getSafe(4)).handler)
// Prepare the transactions
const txs = safes.map(handler =>
geb.contracts.globalSettlement.processSAFE(utils.ETH_A, handler)
txs.map(tx => await wallet.sendTransaction(tx))
// For (II) we have 2 possibilities: wait for all auctions to finish
const cooldown = await geb.contracts.globalSettlement.shutdownCooldown()
const now = BigNumber.from(Date.now()).div(1000)
const isCooldownPassed = now.gt(shutdownTime.add(cooldown))
// Or prematurely terminate each auction
const tx = geb.contracts.globalSettlement.fastTrackAuction(utils.ETH_A, auctionId)
await wallet.sendTransaction(tx)
// (III) We need to get rid of the system surplus
const accountingEngineAddress = geb.contracts.accountingEngine.address
const coin = await geb.contracts.safeEngine.coinBalance(accountingEngineAddress)
const debt = await geb.contracts.safeEngine.debtBalance(accountingEngineAddress)
const amountToSettle = coin.gte(debt) ? debt : coin
const tx = geb.contracts.accountingEngine.settleDebt(amountToSettle)
await wallet.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 successfuly
const tx = geb.contracts.accountingEngine.transferPostSettlementSurplus()
await wallet.sendTransaction(tx)