Skip to content

Commit

Permalink
chore: update setUniswapV3Allowances method
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Oct 19, 2023
1 parent 3e77e4c commit 250b1dc
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions cmd/loadtest/uniswapv3/swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uniswapv3loadtest
import (
"context"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -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) {
Expand All @@ -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,
)
Expand All @@ -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
Expand Down

0 comments on commit 250b1dc

Please sign in to comment.