From 250b1dc7c9468e5f36f093b73fffaa2299cb9275 Mon Sep 17 00:00:00 2001 From: leovct Date: Thu, 19 Oct 2023 12:20:23 +0200 Subject: [PATCH] chore: update `setUniswapV3Allowances` method --- cmd/loadtest/uniswapv3/swapper.go | 45 ++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/cmd/loadtest/uniswapv3/swapper.go b/cmd/loadtest/uniswapv3/swapper.go index a2c88a60..46685a0a 100644 --- a/cmd/loadtest/uniswapv3/swapper.go +++ b/cmd/loadtest/uniswapv3/swapper.go @@ -3,6 +3,7 @@ package uniswapv3loadtest import ( "context" "errors" + "fmt" "math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -15,7 +16,7 @@ import ( // The amount of token to approve a spender to use on behalf of the token owner. // We use a very high amount to avoid frequent approval transactions. Don't use in production. -var approvalAmount = big.NewInt(999_999_999_999_999_999) +var allowanceAmount = big.NewInt(999_999_999_999_999_999) // Deploy an ERC20 token. func DeployERC20(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts, uniswapV3Config UniswapV3Config, tokenName string, recipient common.Address, tokenKnownAddress common.Address, blockUntilSuccessful blockUntilSuccessfulFn) (tokenConfig ContractConfig[uniswapv3.Swapper], err error) { @@ -29,11 +30,11 @@ func DeployERC20(ctx context.Context, c *ethclient.Client, tops *bind.TransactOp func(contract *uniswapv3.Swapper) error { // After the contract has been deployed, we autorize a few UniswapV3 addresses to spend those ERC20 tokens. // This is required to be able to perform swaps later. - addressesToApprove := map[string]common.Address{ + uniswapV3Addresses := map[string]common.Address{ "NFTPositionManager": uniswapV3Config.NonfungiblePositionManager.Address, "SwapRouter02": uniswapV3Config.SwapRouter02.Address, } - return approveSwapperSpendingsByUniswap(ctx, c, contract, tops, cops, tokenName, addressesToApprove, recipient, blockUntilSuccessful) + return setUniswapV3Allowances(ctx, c, contract, tops, cops, tokenName, uniswapV3Addresses, recipient, blockUntilSuccessful) }, blockUntilSuccessful, ) @@ -43,35 +44,53 @@ func DeployERC20(ctx context.Context, c *ethclient.Client, tops *bind.TransactOp return } -// Approve a slice of addresses to spend tokens on behalf of the token owner. -func approveSwapperSpendingsByUniswap(ctx context.Context, c *ethclient.Client, contract *uniswapv3.Swapper, tops *bind.TransactOpts, cops *bind.CallOpts, tokenName string, addresses map[string]common.Address, owner common.Address, blockUntilSuccessful blockUntilSuccessfulFn) error { - var err error - for spenderName, address := range addresses { +// Approve some UniswapV3 addresses to spend tokens on behalf of the token owner. +func setUniswapV3Allowances(ctx context.Context, c *ethclient.Client, contract *uniswapv3.Swapper, tops *bind.TransactOpts, cops *bind.CallOpts, tokenName string, addresses map[string]common.Address, owner common.Address, blockUntilSuccessful blockUntilSuccessfulFn) error { + // Get the ERC20 contract name. + erc20Name, err := contract.Name(cops) + if err != nil { + return err + + } + + for spenderName, spenderAddress := range addresses { // Approve the spender to spend the tokens on behalf of the owner. - if _, err = contract.Approve(tops, address, approvalAmount); err != nil { - log.Error().Err(err).Interface("address", address).Msg("Unable to set the allowance") + if _, err = contract.Approve(tops, spenderAddress, allowanceAmount); err != nil { + log.Error().Err(err). + Str("tokenName", fmt.Sprintf("%s_%s", erc20Name, tokenName)). + Interface("spenderAddress", spenderAddress).Str("spenderName", spenderName). + Interface("amount", allowanceAmount). + Msg("Unable to set the allowance") return err } // Check that the allowance is set. err = blockUntilSuccessful(ctx, c, func() (err error) { - allowance, err := contract.Allowance(cops, owner, address) + allowance, err := contract.Allowance(cops, owner, spenderAddress) if err != nil { return err } if allowance.Cmp(big.NewInt(0)) == 0 { // allowance == 0 return errors.New("allowance is set to zero") } - if allowance.Cmp(approvalAmount) == -1 { // allowance < approvalAmount + if allowance.Cmp(allowanceAmount) == -1 { // allowance < allowanceAmount return errors.New("allowance has not been set properly") } return nil }) if err != nil { - log.Error().Err(err).Msg("Unable to verify that the allowance has been set") + log.Error().Err(err). + Str("tokenName", fmt.Sprintf("%s_%s", erc20Name, tokenName)). + Interface("spenderAddress", spenderAddress).Str("spenderName", spenderName). + Interface("amount", allowanceAmount). + Msg("Unable to verify that the allowance has been set") return err } else { - log.Debug().Str("tokenName", tokenName).Interface("spenderAddress", address).Str("spenderName", spenderName).Interface("amount", approvalAmount).Msg("Allowance set") + log.Debug(). + Str("tokenName", fmt.Sprintf("%s_%s", erc20Name, tokenName)). + Interface("spenderAddress", spenderAddress).Str("spenderName", spenderName). + Interface("amount", allowanceAmount). + Msg("Allowance set") } } return nil