Skip to content

Commit

Permalink
forge install + parallelize token mints and approvals
Browse files Browse the repository at this point in the history
  • Loading branch information
jewei1997 committed Mar 12, 2024
1 parent dba368d commit eef7eff
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
6 changes: 3 additions & 3 deletions loadtest/contracts/deploy_univ2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ echo "Deploying UniswapV2 contracts to $evm_endpoint"

cd loadtest/contracts/evm || exit 1

bigNumber=100000000000000000000000000000000 # 10^32

echo "Installing forge libaries..."
forge install

# deploy the uniswapV2 factory contract
bigNumber=100000000000000000000000000000000 # 10^32
feeCollector=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 # first anvil address, just need a random address for fees
wallet=0xF87A299e6bC7bEba58dbBe5a5Aa21d49bCD16D52

Expand Down
59 changes: 59 additions & 0 deletions loadtest/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,58 @@ func (txClient *EvmTxClient) GenerateUniV2SwapTx() *ethtypes.Transaction {
return txClient.sign(tx)
}

func (txClient *EvmTxClient) BalanceOfToken1() *big.Int {
opts := txClient.getCallOpts()
token1, err := erc20.NewErc20(txClient.evmAddresses.UniV2Token1, GetNextEthClient(txClient.ethClients))
if err != nil {
panic(fmt.Sprintf("Failed to create ERC20 contract: %v \n", err))
}
balance, err := token1.BalanceOf(opts, txClient.accountAddress)
if err != nil {
panic(fmt.Sprintf("Failed to get ERC20 balance: %v \n", err))
}
return balance
}

func (txClient *EvmTxClient) BalanceOfToken2() *big.Int {
opts := txClient.getCallOpts()
token2, err := erc20.NewErc20(txClient.evmAddresses.UniV2Token2, GetNextEthClient(txClient.ethClients))
if err != nil {
panic(fmt.Sprintf("Failed to create ERC20 contract: %v \n", err))
}
balance, err := token2.BalanceOf(opts, txClient.accountAddress)
if err != nil {
panic(fmt.Sprintf("Failed to get ERC20 balance: %v \n", err))
}
return balance
}

func (txClient *EvmTxClient) ApprovalOfToken1() *big.Int {
opts := txClient.getCallOpts()
token1, err := erc20.NewErc20(txClient.evmAddresses.UniV2Token1, GetNextEthClient(txClient.ethClients))
if err != nil {
panic(fmt.Sprintf("Failed to create ERC20 contract: %v \n", err))
}
approval, err := token1.Allowance(opts, txClient.accountAddress, txClient.evmAddresses.UniV2Router)
if err != nil {
panic(fmt.Sprintf("Failed to get ERC20 allowance: %v \n", err))
}
return approval
}

func (txClient *EvmTxClient) ApprovalOfToken2() *big.Int {
opts := txClient.getCallOpts()
token2, err := erc20.NewErc20(txClient.evmAddresses.UniV2Token2, GetNextEthClient(txClient.ethClients))
if err != nil {
panic(fmt.Sprintf("Failed to create ERC20 contract: %v \n", err))
}
approval, err := token2.Allowance(opts, txClient.accountAddress, txClient.evmAddresses.UniV2Router)
if err != nil {
panic(fmt.Sprintf("Failed to get ERC20 allowance: %v \n", err))
}
return approval
}

func (txClient *EvmTxClient) GenerateToken1MintERC20Tx() *ethtypes.Transaction {
opts := txClient.getTransactOpts()
opts.GasLimit = uint64(100000)
Expand Down Expand Up @@ -226,6 +278,13 @@ func (txClient *EvmTxClient) GenerateToken2ApproveRouterTx() *ethtypes.Transacti
return tx
}

func (txClient *EvmTxClient) getCallOpts() *bind.CallOpts {
return &bind.CallOpts{
Pending: false,
Context: context.Background(),
}
}

func (txClient *EvmTxClient) getTransactOpts() *bind.TransactOpts {
auth, err := bind.NewKeyedTransactorWithChainID(txClient.privateKey, txClient.chainId)
if err != nil {
Expand Down
50 changes: 30 additions & 20 deletions loadtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io"
"math/big"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -138,28 +139,37 @@ func deployUniswapContracts(client *LoadTestClient, config *Config) {
fmt.Println("Found UniV2Token2 Address: ", uniV2Token2Address.String())
fmt.Println("Found UniV2Pool Address: ", uniV2PoolAddress.String())

var wg sync.WaitGroup
for _, txClient := range client.EvmTxClients {
txClient.evmAddresses.UniV2Router = uniV2RouterAddress
txClient.evmAddresses.UniV2Token1 = uniV2Token1Address
txClient.evmAddresses.UniV2Token2 = uniV2Token2Address
tx1 := txClient.GenerateToken1MintERC20Tx()
txClient.SendEvmTx(tx1, func() {})
tx2 := txClient.GenerateToken2MintERC20Tx()
txClient.SendEvmTx(tx2, func() {})
approveTx1 := txClient.GenerateToken1ApproveRouterTx()
txClient.SendEvmTx(approveTx1, func() {})
approveTx2 := txClient.GenerateToken2ApproveRouterTx()
txClient.SendEvmTx(approveTx2, func() {})

// go func() {
// time.Sleep(1 * time.Second)
// txClient.EnsureTxSuccess(tx1.Hash())
// txClient.EnsureTxSuccess(tx2.Hash())
// txClient.EnsureTxSuccess(approveTx1.Hash())
// txClient.EnsureTxSuccess(approveTx2.Hash())
// }()
localTxClient := txClient
wg.Add(1)
go func() {
defer wg.Done()
localTxClient.evmAddresses.UniV2Router = uniV2RouterAddress
localTxClient.evmAddresses.UniV2Token1 = uniV2Token1Address
localTxClient.evmAddresses.UniV2Token2 = uniV2Token2Address
if localTxClient.BalanceOfToken1().Cmp(big.NewInt(0)) > 0 &&
localTxClient.BalanceOfToken2().Cmp(big.NewInt(0)) > 0 &&
localTxClient.ApprovalOfToken1().Cmp(big.NewInt(0)) > 0 &&
localTxClient.ApprovalOfToken2().Cmp(big.NewInt(0)) > 0 {
return
}
tx1 := localTxClient.GenerateToken1MintERC20Tx()
localTxClient.SendEvmTx(tx1, func() {})
tx2 := localTxClient.GenerateToken2MintERC20Tx()
localTxClient.SendEvmTx(tx2, func() {})
approveTx1 := localTxClient.GenerateToken1ApproveRouterTx()
localTxClient.SendEvmTx(approveTx1, func() {})
approveTx2 := localTxClient.GenerateToken2ApproveRouterTx()
localTxClient.SendEvmTx(approveTx2, func() {})

localTxClient.EnsureTxSuccess(tx1.Hash())
localTxClient.EnsureTxSuccess(tx2.Hash())
localTxClient.EnsureTxSuccess(approveTx1.Hash())
localTxClient.EnsureTxSuccess(approveTx2.Hash())
}()

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
}

wg.Wait()
}
}

Expand Down

0 comments on commit eef7eff

Please sign in to comment.