Как сделать swap в Web3.py от MATIC на USDC
Как сделать swap обмен между MATIC на USDC через Metamask Router, uniswap или другое. У меня есть код и он отправляет средства тестовые но в метамаске не приходит обратно. Где мне взять abi и адрес контракта от Metamask Router, uniswap или других?
Код взял из: https://ethereum.stackexchange.com/questions/138155/interact-with-swap-function-for-sushiswap-contract
Вот мой код:
import time
from web3 import Web3
class swapExample():
def __init__(self, wallet: str, private_key: str, web3RpcUrl: str,
buy_amount: int, from_token: str, to_token: str, rourter_adress: str, abi: str):
#Setup Wallet Informations and RPC
#Usually we place these info in separate .env or .py or .json file.
self.wallet = Web3.to_checksum_address(wallet) #your wallet address
self.pk = private_key #your wallet private key
self.rpc = web3RpcUrl #your node or use public rpc
self.web3 = Web3(Web3.HTTPProvider(self.rpc)) #connect to web3
#Setup Swap Value / Gas limit / Gwei
#Change these values if you want.
self.buy_amount = buy_amount #Ether value to spend
self.gas = 500000 #gas limit
self.gwei = 200 #gwei
#Setup WETH token address (to spend) and Token address (to buy)
#In my case I'll use WBNB to buy Safemoon token (at BSC Testnet)
self.WETH = self.web3.to_checksum_address(from_token)
self.token_to_buy = self.web3.to_checksum_address(to_token)
#Setup router address (Sushiswap Router Address)
#In my case I'll use Pancakeswap Router (at BSC Testnet)
self.router = self.web3.to_checksum_address(rourter_adress)
#Setup SwapExactETHforTokens ABI
#You can get this ABI directly from Verified Contract Source Code at Etherscan
self.abi = abi
#Creating instance.
#We can call contract functions with this.
self.router_contract = self.web3.eth.contract(address=self.router, abi=self.abi)
# #Do swap.
# self.swap()
#From here we are going to create Swap function.
def swap(self):
#building transaction with swap parameters
#you can edit this if you want
swap_txn = self.router_contract.functions.swapExactETHForTokens(
0, #amountOutMin(slippage)
[self.WETH,self.token_to_buy], #path (WETH,TOKEN)
self.wallet, #to (your wallet)
(int(time.time()) + 10000) #Deadline
).build_transaction({
'from': self.wallet,
'value': self.web3.to_wei(self.buy_amount, unit='ether'),
'gas': self.gas,
'gasPrice': self.web3.to_wei(self.gwei,'gwei'),
'nonce': self.web3.eth.get_transaction_count(self.wallet),
})
#Sign swap tx and send to blockchain
sign_tx = self.web3.eth.account.sign_transaction(swap_txn, private_key=self.pk)
buy_tx = self.web3.eth.send_raw_transaction(sign_tx.rawTransaction)
print("Txn sent, awaiting for response.")
#Await for result
tx_result = self.web3.eth.wait_for_transaction_receipt(buy_tx)
# Transaction status verification
# If status == 1: Swap success.
if tx_result['status'] == 1:
print("Success! TX:", self.web3.to_hex(buy_tx))
return True
else:
print("Error! TX:", self.web3.to_hex(buy_tx))
return False
init = swapExample(
wallet='адрес кошелька',
private_key='приватный ключ',
web3RpcUrl='https://polygon-mainnet.infura.io/v3/цифры',
buy_amount=0.0001,
from_token='0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0',
to_token='0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
rourter_adress='0x1a1ec25DC08e98e5E93F1104B5e5cdD298707d31',
abi= '[{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"}]'
)
init.swap()