Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVM] univ2 loadtest #1423

Merged
merged 32 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
322ef8f
checkpoint
jewei1997 Mar 5, 2024
820a18f
add univ2 contracts + deploy_uinv2.sh
jewei1997 Mar 5, 2024
a201f0d
able to deploy uniswapv2
jewei1997 Mar 5, 2024
a757dfa
Unifap -> Uniswap
jewei1997 Mar 5, 2024
43cb3ff
univ2 set up
jewei1997 Mar 6, 2024
2903ed5
trying to do swaps
jewei1997 Mar 7, 2024
ff1d4a1
swaps work
jewei1997 Mar 8, 2024
caeab63
swaps in both directions work
jewei1997 Mar 10, 2024
cd9fca8
increase liquidity in pool
jewei1997 Mar 10, 2024
404dd9d
remove prints
jewei1997 Mar 10, 2024
6eb2cbc
revert changes to make loadtest work locally
jewei1997 Mar 11, 2024
98dad15
add legacy to forge cmds
jewei1997 Mar 11, 2024
055a9dc
add print statement
jewei1997 Mar 11, 2024
ba58527
debug
jewei1997 Mar 11, 2024
f196b2b
add UNIV2 msg type
jewei1997 Mar 11, 2024
d73c7f7
turn off ensure tx success
jewei1997 Mar 11, 2024
922911e
comment out prints
jewei1997 Mar 11, 2024
5b0393c
print
jewei1997 Mar 11, 2024
f822c8a
print
jewei1997 Mar 11, 2024
6a16a82
cleanup
jewei1997 Mar 12, 2024
2b1e46d
cleanup
jewei1997 Mar 12, 2024
b677e7a
print script output
jewei1997 Mar 12, 2024
dba368d
debug
jewei1997 Mar 12, 2024
eef7eff
forge install + parallelize token mints and approvals
jewei1997 Mar 12, 2024
0a23822
create intermediary univ2 swapper
jewei1997 Mar 12, 2024
7c87da9
cleanup + remove bindings for univ2 pair and router
jewei1997 Mar 12, 2024
c6f0364
cleanup
jewei1997 Mar 12, 2024
0f5ffbb
Merge branch 'seiv2' into evm-univ2-loadtest3
jewei1997 Mar 12, 2024
da72b58
fix
jewei1997 Mar 12, 2024
48f48ae
cleanup
jewei1997 Mar 12, 2024
e4823fc
query wallet balance in script
jewei1997 Mar 13, 2024
077c25f
appease linter
jewei1997 Mar 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
univ2 set up
  • Loading branch information
jewei1997 committed Mar 12, 2024
commit 43cb3ffc4bdef261702228b334a8ea0de8cde480
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,evm,erc20",
"message_type": "bank,evm,erc20,univ2",
"run_oracle": false,
"metrics_port": 9695,
"wasm_msg_types": {
Expand Down
57 changes: 50 additions & 7 deletions loadtest/contracts/deploy_univ2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,60 @@ cd loadtest/contracts/evm || exit 1
# git submodule update --init --recursive

# deploy the uniswapV2 factory contract
feeCollector=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
feeCollector=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 # first anvil address, just need a random address
wallet=0xF87A299e6bC7bEba58dbBe5a5Aa21d49bCD16D52

which forge

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

echo "UniswapV2Factory deployed to $factoryAddress"

# deploy the uniswapV2 router02 contract
/root/.foundry/bin/forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/univ2/UniswapV2Router02.sol:UniswapV2Router02 --json --constructor-args $factoryAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 | jq -r '.deployedTo'
routerAddress=$(forge create -r "$evm_endpoint" --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e src/univ2/UniswapV2Router.sol:UniswapV2Router --json --constructor-args $factoryAddress $feeCollector | jq -r '.deployedTo')

echo "UniswapV2Router deployed to $routerAddress"

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

echo "Token1 deployed to $token1Address"

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

echo "Token2 deployed to $token2Address"

# mint tokens
cast send -r "$evm_endpoint" $token1Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "mint(address,uint256)" $wallet 1000000000 --legacy

cast send -r "$evm_endpoint" $token2Address --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "mint(address,uint256)" $wallet 1000000000 --legacy

balanceToken1=$(cast call -r "$evm_endpoint" $token1Address "balanceOf(address)" $wallet)
balanceToken2=$(cast call -r "$evm_endpoint" $token2Address "balanceOf(address)" $wallet)

echo "Token1 balance: $balanceToken1"
echo "Token2 balance: $balanceToken2"

# create a pool
# add liquidity
# temp
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 "Pair address: $pairAddress"

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

echo "Check allowance..."
token1ToRouterAllowance=$(cast call -r "$evm_endpoint" $token1Address "allowance(address,address)(uint256)" $wallet $routerAddress)
token2ToRouterAllowance=$(cast call -r "$evm_endpoint" $token2Address "allowance(address,address)(uint256)" $wallet $routerAddress)

echo "Token1 to router allowance: $token1ToRouterAllowance"
echo "Token2 to router allowance: $token2ToRouterAllowance"

echo "Adding liquidity to pool"
echo "cast send -r $evm_endpoint $routerAddress --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e 'addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256)' \
$token1Address $token2Address 1000000 1000000 0 0 $wallet 1000000000000000000 --legacy --json"
cast send -r "$evm_endpoint" $routerAddress --private-key 57acb95d82739866a5c29e40b0aa2590742ae50425b7dd5b5d279a986370189e "addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256)" \
$token1Address $token2Address 1000000 1000000 0 0 $wallet 1000000000000000000 --legacy --json
12 changes: 12 additions & 0 deletions loadtest/contracts/evm/src/ERC20Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract ERC20Token is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}
18 changes: 15 additions & 3 deletions loadtest/contracts/evm/src/univ2/UniswapV2Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,23 @@ contract UniswapV2Router {
factory.createPair(tokenA, tokenB);
}

// require(false, string(abi.encodePacked(factory)));

(uint112 reserveA, uint112 reserveB) = UniswapV2Library.getReserves(
address(factory),
tokenA,
tokenB
);
// require(false, "after getReserves");
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
} else {
amountB = UniswapV2Library.quote(amountADesired, reserveA, reserveB);
if (amountB <= amountBDesired) {
if (amountB < amountBMin) revert InsufficientAmountB();
if (amountB < amountBMin) {
require(false, "InsufficientAmountB");
revert InsufficientAmountB();
}
amountA = amountADesired;
} else {
amountA = UniswapV2Library.quote(
Expand All @@ -141,7 +147,10 @@ contract UniswapV2Router {
);
assert(amountA <= amountADesired);

if (amountA < amountAMin) revert InsufficientAmountA();
if (amountA < amountAMin) {
require(false, "InsufficientAmountA");
revert InsufficientAmountA();
}
amountB = amountBDesired;
}
}
Expand All @@ -154,6 +163,9 @@ contract UniswapV2Router {
uint256 amount
) internal returns (bool success) {
success = IERC20(token).transferFrom(from, to, amount);
if (!success) revert SafeTransferFromFailed();
if (!success) {
require(false, "SafeTransferFromFailed");
revert SafeTransferFromFailed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.10;

import "../interfaces/IUniswapV2Pair.sol";
import "../interfaces/IUniswapV2Factory.sol";

/// @title UniswapV2Library
/// @author Uniswap Labs
Expand Down Expand Up @@ -29,9 +30,8 @@ library UniswapV2Library {
address tokenB
) internal view returns (uint112 reserveA, uint112 reserveB) {
(address token0, address token1) = sortPairs(tokenA, tokenB);
(uint112 reserve0, uint112 reserve1, ) = IUniswapV2Pair(
pairFor(factory, token0, token1)
).getReserves();
IUniswapV2Pair pair = IUniswapV2Pair(IUniswapV2Factory(factory).pairs(token0, token1));
(uint112 reserve0, uint112 reserve1, ) = pair.getReserves();
(reserveA, reserveB) = tokenA == token0
? (reserve0, reserve1)
: (reserve1, reserve0);
Expand Down
7 changes: 7 additions & 0 deletions loadtest/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (txClient *EvmTxClient) GetTxForMsgType(msgType string) *ethtypes.Transacti
return txClient.GenerateSendFundsTx()
case ERC20:
return txClient.GenerateERC20TransferTx()
case UNIV2:
return txClient.GenerateUniV2SwapTx()
default:
panic("invalid message type")
}
Expand Down Expand Up @@ -118,6 +120,11 @@ func (txClient *EvmTxClient) GenerateERC20TransferTx() *ethtypes.Transaction {
return tx
}

func (txClient *EvmTxClient) GenerateUniV2SwapTx() *ethtypes.Transaction {
// TODO
opts := txClient.getTransactOpts()
}

func (txClient *EvmTxClient) getTransactOpts() *bind.TransactOpts {
auth, err := bind.NewKeyedTransactorWithChainID(txClient.privateKey, txClient.chainId)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions loadtest/loadtest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func (c *LoadTestClient) BuildTxs(
case EVM, ERC20:
fmt.Println("In EVM/ERC20")
signedTx = SignedTx{EvmTx: c.generateSignedEvmTx(keyIndex, messageType)}
case UNIV2:
fmt.Println("In UNIV2")
signedTx = SignedTx{EvmTx: c.generateSignedEvmTx(keyIndex, messageType)}
default:
signedTx = SignedTx{TxBytes: c.generateSignedCosmosTxs(keyIndex, messageType, producedCount)}
}
Expand Down
1 change: 1 addition & 0 deletions loadtest/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
Limit string = "limit"
Market string = "market"
WasmMintNft string = "wasm_mint_nft"
UNIV2 string = "univ2"
Vortex string = "vortex"
WasmInstantiate string = "wasm_instantiate"
)
Expand Down