From 1a116f1429d2f1fb3ab6dd6ec03d1a5a37317790 Mon Sep 17 00:00:00 2001 From: Leszek Stachowski Date: Fri, 2 Feb 2024 20:55:21 +0100 Subject: [PATCH] Fix cUSD faucet transfers (#156) * remove mnemonic files * remove key_placer.sh script * Fix cUSD faucet transfers --- apps/firebase/src/celo-adapter.ts | 147 +++++++++++------------------- 1 file changed, 52 insertions(+), 95 deletions(-) diff --git a/apps/firebase/src/celo-adapter.ts b/apps/firebase/src/celo-adapter.ts index 70a02ec5c..491f09152 100644 --- a/apps/firebase/src/celo-adapter.ts +++ b/apps/firebase/src/celo-adapter.ts @@ -1,6 +1,6 @@ import { CeloTransactionObject } from '@celo/connect' import { ContractKit, newKitFromWeb3, Token } from '@celo/contractkit' -import { StableToken, StableTokenInfo } from '@celo/contractkit/lib/celo-tokens' +import { StableToken } from '@celo/contractkit/lib/celo-tokens' import { ensureLeading0x, privateKeyToAddress } from '@celo/utils/lib/address' import { Mento } from '@mento-protocol/mento-sdk' import BigNumber from 'bignumber.js' @@ -137,99 +137,76 @@ export class CeloAdapter { to: string, amount: string, alwaysTransfer: boolean = false, - ) { + ): Promise<{ [key: string]: CeloTransactionObject }> { const mento = this.mento if (!mento && this.useMento) { throw new Error('Must call init() first') } const celoToken = await this.kit.contracts.getGoldToken() - return this.kit.celoTokens.forStableCeloToken( - async (info: StableTokenInfo) => { - const token = await this.kit.celoTokens.getWrapper( - info.symbol as StableToken, - ) - const [faucetBalance, recipientBalance] = await Promise.all([ - token.balanceOf(this.defaultAddress), - token.balanceOf(to), - ]) - - const stableTokenAddr = token.address + return Object.fromEntries( + await Promise.all( + Object.keys(StableToken).map(async (symbol) => { + const token = await this.kit.celoTokens.getWrapper( + symbol as StableToken, + ) + const [faucetBalance, recipientBalance] = await Promise.all([ + token.balanceOf(this.defaultAddress), + token.balanceOf(to), + ]) - const realAmount = this.fadeOutAmount( - recipientBalance, - amount, - alwaysTransfer, - ) + const stableTokenAddr = token.address - if (realAmount.eq(0)) { - console.info( - `skipping ${ - info.symbol - } for ${to} balance already ${recipientBalance.toString()}`, + const realAmount = this.fadeOutAmount( + recipientBalance, + amount, + alwaysTransfer, ) - return false - } - console.info( - `sending ${to} ${realAmount.toString()}${ - info.symbol - }. Balance ${recipientBalance.toString()}`, - ) - if (faucetBalance.isLessThanOrEqualTo(realAmount)) { - if (mento) { - const quoteAmountIn = await mento.getAmountIn( - celoToken.address, - stableTokenAddr, - realAmount.toString(), - ) + if (realAmount.eq(0)) { console.info( - `swap quote ${quoteAmountIn.toString()} for ${realAmount.toString()} `, - ) - const maxCeloToTrade = quoteAmountIn.div(100).mul(103).toString() // 3% slippage - await this.increaseAllowanceIfNeeded( - new BigNumber(maxCeloToTrade), - info, + `skipping ${symbol} for ${to} balance already ${recipientBalance.toString()}`, ) - const swapTxObj = await mento.swapOut( - celoToken.address, - stableTokenAddr, - realAmount.toString(), - maxCeloToTrade.toString(), - ) - console.info('swap TX', swapTxObj) - await this.signer.sendTransaction(swapTxObj) - } else { - // Remove block once Broker contract issues are sorted out - console.info('Using exchange contract for token swaps') - const exchangeContract = await this.kit.contracts.getContract( - info.exchangeContract, - ) - - // this surprised me but if you want to send CELO and receive an Amount of stable, quoteGoldBuy is the function to call not quoteStableBuy - const celoBuyquote = await exchangeContract.quoteGoldBuy(realAmount) - - const maxCeloToTrade = celoBuyquote - .multipliedBy(1.05) - .integerValue(BigNumber.ROUND_UP) - await this.increaseAllowanceIfNeeded( - maxCeloToTrade as unknown as BigNumber, - info, - ) + return [symbol, false] + } + console.info( + `sending ${to} ${realAmount.toString()} ${symbol}. Balance ${recipientBalance.toString()}`, + ) - await exchangeContract - .buyStable(realAmount, maxCeloToTrade) - .sendAndWaitForReceipt() + if (faucetBalance.isLessThanOrEqualTo(realAmount)) { + if (mento) { + const quoteAmountIn = await mento.getAmountIn( + celoToken.address, + stableTokenAddr, + realAmount.toString(), + ) + console.info( + `swap quote ${quoteAmountIn.toString()} for ${realAmount.toString()} `, + ) + const maxCeloToTrade = quoteAmountIn.div(100).mul(103).toString() // 3% slippage + await this.increaseAllowanceIfNeeded( + new BigNumber(maxCeloToTrade), + ) + + const swapTxObj = await mento.swapOut( + celoToken.address, + stableTokenAddr, + realAmount.toString(), + maxCeloToTrade.toString(), + ) + console.info('swap TX', swapTxObj) + await this.signer.sendTransaction(swapTxObj) + } } - } - return token.transfer(to, realAmount.toString()) - }, + return [symbol, token.transfer(to, realAmount.toString())] + }), + ), ) } - async increaseAllowanceIfNeeded(amount: BigNumber, info: StableTokenInfo) { + async increaseAllowanceIfNeeded(amount: BigNumber) { const mento = this.mento if (!mento && this.useMento) { throw new Error('Must call init() first') @@ -254,26 +231,6 @@ export class CeloAdapter { console.log('increasedAllowance', allowanceReceipt?.transactionHash) } - } else { - // Remove block once Broker contract issues are sorted out - console.info('Increasing allowance with exchance contract') - const exchangeContractAddress = await this.kit.registry.addressFor( - info.exchangeContract, - ) - - const allowance = await celoERC20Wrapper.allowance( - this.defaultAddress, - exchangeContractAddress, - ) - if (allowance.isLessThanOrEqualTo(amount)) { - // multiply by 10 so we don't have to be setting this for every transaction - const transaction = celoERC20Wrapper.increaseAllowance( - exchangeContractAddress, - amount.multipliedBy(10).integerValue(BigNumber.ROUND_UP), - ) - const receipt = await transaction.sendAndWaitForReceipt() - console.log('increasedAllowance', receipt.transactionHash) - } } }