Skip to content

Commit

Permalink
[EVM] univ2 loadtest (#1423)
Browse files Browse the repository at this point in the history
* checkpoint

* add univ2 contracts + deploy_uinv2.sh

* able to deploy uniswapv2

* Unifap -> Uniswap

* univ2 set up

* trying to do swaps

* swaps work

* swaps in both directions work

* increase liquidity in pool

* remove prints

* revert changes to make loadtest work locally

* add legacy to forge cmds

* add print statement

* debug

* add UNIV2 msg type

* turn off ensure tx success

* comment out prints

* print

* print

* cleanup

* cleanup

* print script output

* debug

* forge install + parallelize token mints and approvals

* create intermediary univ2 swapper

* cleanup + remove bindings for univ2 pair and router

* cleanup

* fix

* cleanup

* query wallet balance in script

* appease linter
  • Loading branch information
jewei1997 authored Mar 13, 2024
1 parent b2894fd commit 5a300e7
Show file tree
Hide file tree
Showing 25 changed files with 1,636 additions and 391 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "loadtest/contracts/evm/lib/openzeppelin-contracts"]
path = loadtest/contracts/evm/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "loadtest/contracts/evm/lib/solmate"]
path = loadtest/contracts/evm/lib/solmate
url = https://github.com/transmissions11/solmate
2 changes: 1 addition & 1 deletion loadtest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"begin_redelegate_percentage": "0.25"
}
},
"message_type": "bank,dex,staking,failure_dex_malformed,failure_dex_invalid,collect_rewards,distribute_rewards,wasm_instantiate",
"message_type": "bank,dex,staking,failure_dex_malformed,failure_dex_invalid,collect_rewards,distribute_rewards,wasm_instantiate,evm,univ2",
"run_oracle": false,
"metrics_port": 9695,
"wasm_msg_types": {
Expand Down
69 changes: 69 additions & 0 deletions loadtest/contracts/deploy_univ2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# This script is used to deploy the UniV2 contract to the target network
# This avoids trying to predict what address it might be deployed to
set -e

evm_endpoint=$1

echo "Deploying UniswapV2 contracts to $evm_endpoint"

cd loadtest/contracts/evm || exit 1

echo "Installing forge libaries..."
forge install

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

walletBalance=$(cast balance $wallet --rpc-url "$evm_endpoint")
if [[ $walletBalance == 0 ]]; then
echo "Please fund the wallet $wallet to continue."
exit 1
fi

echo "Deploying factory contract..."
factoryAddress=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/univ2/UniswapV2Factory.sol:UniswapV2Factory --json --legacy --constructor-args $feeCollector | jq -r '.deployedTo')
echo "factoryAddress=$factoryAddress"

echo "Deploying router contract..."
routerAddress=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/univ2/UniswapV2Router.sol:UniswapV2Router --json --legacy --constructor-args $factoryAddress $feeCollector | jq -r '.deployedTo')
echo "routerAddress=$routerAddress"

echo "Deploying token1 contract..."
token1Address=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/ERC20Token.sol:ERC20Token --json --legacy --constructor-args "Token1" "T1" | jq -r '.deployedTo')
echo "token1Address=$token1Address"

echo "Deploying token2 contract..."
token2Address=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/ERC20Token.sol:ERC20Token --json --legacy --constructor-args "Token2" "T2" | jq -r '.deployedTo')
echo "token2Address=$token2Address"

echo "Deploying UniV2Swapper contract..."
uniV2SwapperAddress=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/UniV2Swapper.sol:UniV2Swapper --json --legacy --constructor-args $token1Address $token2Address $routerAddress | jq -r '.deployedTo')
echo "uniV2SwapperAddress=$uniV2SwapperAddress"

echo "Minting tokens..."
cast send -r "$evm_endpoint" $token1Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "mint(address,uint256)" $wallet $bigNumber --legacy --json
cast send -r "$evm_endpoint" $token2Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "mint(address,uint256)" $wallet $bigNumber --legacy --json

echo "Creating a pool..."
cast send -r "$evm_endpoint" $factoryAddress --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "createPair(address,address)" $token1Address $token2Address --legacy --json

# get the pair address
pairAddress=$(cast call -r "$evm_endpoint" $factoryAddress "getAllPairsIndex(uint256)" 0)

echo "Approving router to spend tokens..."
cast send -r "$evm_endpoint" $token1Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "approve(address,uint256)" $routerAddress $bigNumber --legacy --json
cast send -r "$evm_endpoint" $token2Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "approve(address,uint256)" $routerAddress $bigNumber --legacy --json

echo "Adding liquidity to the pool..."
cast send -r "$evm_endpoint" $routerAddress --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256)" $token1Address $token2Address $bigNumber $bigNumber 0 0 $wallet 1000000000000000000 --legacy --json

# print addresses out for use in other scripts
echo "UniswapV2Factory Address: \"$factoryAddress\""
echo "UniswapV2Router Address: \"$routerAddress\""
echo "Token1 Address: \"$token1Address\""
echo "Token2 Address: \"$token2Address\""
echo "Pair Address: \"$pairAddress\""
echo "Swapper Address: \"$uniV2SwapperAddress\""
Loading

0 comments on commit 5a300e7

Please sign in to comment.