-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbera_swap.py
60 lines (48 loc) · 13.1 KB
/
bera_swap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# example bera swap
RPC = 'https://artio.rpc.berachain.com'
CHAIN_ID = 80085
from web3 import Web3 as w, HTTPProvider as hp ; w3 = w(hp(RPC))
from os import getenv
from dotenv import load_dotenv
load_dotenv()
KEY = getenv('TKEY')
EOA = getenv('TRON')
#----------------------
ERC20_DEX = '0x0d5862FDbdd12490f9b4De54c236cff63B038074'
NATIVE = '0x0000000000000000000000000000000000000000'
USDC = '0x6581e59A1C8dA66eD0D313a0d4029DcE2F746Cc5'
# WETH = '0x8239FBb3e3D0C2cDFd7888D8aF7701240Ac4DcA4'
WBTC = '0x9DAD8A1F64692adeB74ACa26129e0F16897fF4BB'
DEX_ABI = '''
[ { "type": "function", "name": "addLiquidity", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "receiver", "type": "address", "internalType": "address" }, { "name": "assetsIn", "type": "address[]", "internalType": "address[]" }, { "name": "amountsIn", "type": "uint256[]", "internalType": "uint256[]" } ], "outputs": [ { "name": "shares", "type": "address[]", "internalType": "address[]" }, { "name": "shareAmounts", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "liquidity", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "payable" }, { "type": "function", "name": "batchSwap", "inputs": [ { "name": "kind", "type": "uint8", "internalType": "enum IERC20DexModule.SwapKind" }, { "name": "swaps", "type": "tuple[]", "internalType": "struct IERC20DexModule.BatchSwapStep[]", "components": [ { "name": "poolId", "type": "address", "internalType": "address" }, { "name": "assetIn", "type": "address", "internalType": "address" }, { "name": "amountIn", "type": "uint256", "internalType": "uint256" }, { "name": "assetOut", "type": "address", "internalType": "address" }, { "name": "amountOut", "type": "uint256", "internalType": "uint256" }, { "name": "userData", "type": "bytes", "internalType": "bytes" } ] }, { "name": "deadline", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "payable" }, { "type": "function", "name": "createPool", "inputs": [ { "name": "name", "type": "string", "internalType": "string" }, { "name": "assetsIn", "type": "address[]", "internalType": "address[]" }, { "name": "amountsIn", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "poolType", "type": "string", "internalType": "string" }, { "name": "options", "type": "tuple", "internalType": "struct IERC20DexModule.PoolOptions", "components": [ { "name": "weights", "type": "tuple[]", "internalType": "struct IERC20DexModule.AssetWeight[]", "components": [ { "name": "asset", "type": "address", "internalType": "address" }, { "name": "weight", "type": "uint256", "internalType": "uint256" } ] }, { "name": "swapFee", "type": "uint256", "internalType": "uint256" } ] } ], "outputs": [ { "name": "", "type": "address", "internalType": "address" } ], "stateMutability": "payable" }, { "type": "function", "name": "getExchangeRate", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "baseAsset", "type": "address", "internalType": "address" }, { "name": "quoteAsset", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "", "type": "uint256", "internalType": "uint256" } ], "stateMutability": "view" }, { "type": "function", "name": "getLiquidity", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "asset", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPoolName", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "", "type": "string", "internalType": "string" } ], "stateMutability": "view" }, { "type": "function", "name": "getPoolOptions", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "", "type": "tuple", "internalType": "struct IERC20DexModule.PoolOptions", "components": [ { "name": "weights", "type": "tuple[]", "internalType": "struct IERC20DexModule.AssetWeight[]", "components": [ { "name": "asset", "type": "address", "internalType": "address" }, { "name": "weight", "type": "uint256", "internalType": "uint256" } ] }, { "name": "swapFee", "type": "uint256", "internalType": "uint256" } ] } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewAddLiquidityNoSwap", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "outputs": [ { "name": "shares", "type": "address[]", "internalType": "address[]" }, { "name": "shareAmounts", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "liqOut", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewAddLiquidityStaticPrice", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "liquidity", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "outputs": [ { "name": "shares", "type": "address[]", "internalType": "address[]" }, { "name": "shareAmounts", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "liqOut", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewBatchSwap", "inputs": [ { "name": "kind", "type": "uint8", "internalType": "enum IERC20DexModule.SwapKind" }, { "name": "swaps", "type": "tuple[]", "internalType": "struct IERC20DexModule.BatchSwapStep[]", "components": [ { "name": "poolId", "type": "address", "internalType": "address" }, { "name": "assetIn", "type": "address", "internalType": "address" }, { "name": "amountIn", "type": "uint256", "internalType": "uint256" }, { "name": "assetOut", "type": "address", "internalType": "address" }, { "name": "amountOut", "type": "uint256", "internalType": "uint256" }, { "name": "userData", "type": "bytes", "internalType": "bytes" } ] } ], "outputs": [ { "name": "asset", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewBurnShares", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "asset", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewSharesForLiquidity", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "outputs": [ { "name": "shares", "type": "address[]", "internalType": "address[]" }, { "name": "shareAmounts", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "liquidity", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewSharesForSingleSidedLiquidityRequest", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "asset", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getPreviewSwapExact", "inputs": [ { "name": "kind", "type": "uint8", "internalType": "enum IERC20DexModule.SwapKind" }, { "name": "pool", "type": "address", "internalType": "address" }, { "name": "baseAsset", "type": "address", "internalType": "address" }, { "name": "baseAssetAmount", "type": "uint256", "internalType": "uint256" }, { "name": "quoteAsset", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "asset", "type": "address", "internalType": "address" }, { "name": "amount", "type": "uint256", "internalType": "uint256" } ], "stateMutability": "view" }, { "type": "function", "name": "getRemoveLiquidityExactAmountOut", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "assetIn", "type": "address", "internalType": "address" }, { "name": "assetAmount", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getRemoveLiquidityOneSideOut", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "assetOut", "type": "address", "internalType": "address" }, { "name": "sharesIn", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "getTotalShares", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "view" }, { "type": "function", "name": "removeLiquidityBurningShares", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "withdrawAddress", "type": "address", "internalType": "address" }, { "name": "assetIn", "type": "address", "internalType": "address" }, { "name": "amountIn", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "liquidity", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "payable" }, { "type": "function", "name": "removeLiquidityExactAmount", "inputs": [ { "name": "pool", "type": "address", "internalType": "address" }, { "name": "withdrawAddress", "type": "address", "internalType": "address" }, { "name": "assetOut", "type": "address", "internalType": "address" }, { "name": "amountOut", "type": "uint256", "internalType": "uint256" }, { "name": "sharesIn", "type": "address", "internalType": "address" }, { "name": "maxSharesIn", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "shares", "type": "address[]", "internalType": "address[]" }, { "name": "shareAmounts", "type": "uint256[]", "internalType": "uint256[]" }, { "name": "liquidity", "type": "address[]", "internalType": "address[]" }, { "name": "liquidityAmounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "payable" }, { "type": "function", "name": "swap", "inputs": [ { "name": "kind", "type": "uint8", "internalType": "enum IERC20DexModule.SwapKind" }, { "name": "poolId", "type": "address", "internalType": "address" }, { "name": "assetIn", "type": "address", "internalType": "address" }, { "name": "amountIn", "type": "uint256", "internalType": "uint256" }, { "name": "assetOut", "type": "address", "internalType": "address" }, { "name": "amountOut", "type": "uint256", "internalType": "uint256" }, { "name": "deadline", "type": "uint256", "internalType": "uint256" } ], "outputs": [ { "name": "assets", "type": "address[]", "internalType": "address[]" }, { "name": "amounts", "type": "uint256[]", "internalType": "uint256[]" } ], "stateMutability": "payable" } ]
'''
dex = w3.eth.contract(address=ERC20_DEX, abi=DEX_ABI)
swap = dex.functions.batchSwap(
0,
[
('0x3F2090dc46629eeb36Db545f0e9E5084b67D11aE', NATIVE, 10000000000000, WBTC, 0, '0x'),
('0xD316eC27FDc736B9f3e625644c98221469288Da6', WBTC, 0, USDC, 0, '0x')
],
99999999
).build_transaction({
'value': 10000000000000000,
'from': EOA,
'chainId': CHAIN_ID,
'gas': 500000,
'maxFeePerGas': w3.eth.gas_price * 2,
'maxPriorityFeePerGas': w3.eth.max_priority_fee * 2,
'nonce': w3.eth.get_transaction_count(EOA)
})
w3.eth.call(swap) # should throw if there is an issue
# helpers
def sign_tx(tx, key):
return w3.eth.account.sign_transaction(tx, private_key=key)
def send_tx(signed_tx):
return w3.eth.send_raw_transaction(signed_tx.rawTransaction)
def main():
('[-] Attempting swap...')
tx_hash = send_tx(sign_tx(swap, KEY))
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print (f'[>] Hash of attempt: {tx_hash}\n[>] {receipt}')
if __name__ == '__main__':
main()