web3py with 0x API



我一直试图使用0x API在Ropsten上执行交换以获得最佳报价,但每次我尝试执行交换时,交易失败而没有任何反馈(至少来自Etherscan),而批准tx通过良好。我可能在这里遗漏了一些东西,但是我一直盯着我的代码都快瞎了。

import requests
import json
import time
from web3 import Web3
walletAddress = 'mywalletaddress'
wpk = 'myprivatekey'
# Setup Ropsten provider
w3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/'))
# Get the nonce to prevent from sending the transaction twice
nonce = w3.eth.getTransactionCount(walletAddress)
print('Nonce:', nonce)
# Store the generic ERC20 abi for later use
ABI = json.load(open('abi.json'))
# ROPSTEN ADDRESSES
WETH = Web3.toChecksumAddress('0xc778417e063141139fce010982780140aa0cd5ab')
DAI = Web3.toChecksumAddress('0xad6d458402f60fd3bd25163575031acdce07538d')
USDC = Web3.toChecksumAddress('0x07865c6e87b9f70255377e024ace6630c1eaa37f')

def calcAmount(initialAmount, contractAddress):
contract = w3.eth.contract(address=contractAddress, abi=ABI)
decimals = contract.functions.decimals().call()
amount = int(initialAmount * 10**decimals)
return amount
print(decimals, amount)

def getTokenSymbol(contractAddress):
contract = w3.eth.contract(address=contractAddress, abi=ABI)
tokenSymbol = contract.functions.symbol().call()
return tokenSymbol
print('Symbol:', tokenSymbol)

# Define query parameters
initialAmount = 100
slippage = '0.01'  # 0.01 = 1% slippage
sellAmount = calcAmount(initialAmount, USDC)
print('Sell:', sellAmount)

def approve(token, spenderAddress, walletAddress, wpk):
token = Web3.toChecksumAddress(token)
contract = w3.eth.contract(address=token, abi=ABI)
spender = Web3.toChecksumAddress(spenderAddress)
max_amount = Web3.toWei(2**64 - 1, 'ether')
nonce = w3.eth.getTransactionCount(walletAddress)
tx = contract.functions.approve(spender, max_amount).buildTransaction({
'from': walletAddress,
'nonce': nonce
})
signed_tx = w3.eth.account.signTransaction(tx, wpk)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
print("Approve", w3.toHex(tx_hash))
w3.eth.wait_for_transaction_receipt(tx_hash)

def getQuote(buyToken, slippage, sellToken, sellAmount):
# Make the query https://ropsten.api.0x.org/
query = 'https://ropsten.api.0x.org/swap/v1/quote?buyToken={}&slippage={}&sellToken={}&sellAmount={}'.format(buyToken, slippage, sellToken, sellAmount)
# Get the query and store the response in data
response = requests.get(query)
data = response.json()
# Print a prettiefied version of the response (optional)
print(json.dumps(data, indent=2))
# Convert the buyAmmount to integer
buyAmount = int(data['buyAmount'])
# Convert the buyAmount to a readable number
finalPrice = calcAmount(buyAmount, buyToken)
# Get the symbol of the 2 tokens
buyTokenSymbol = getTokenSymbol(buyToken)
sellTokenSymbol = getTokenSymbol(sellToken)
print(sellTokenSymbol, '->', buyTokenSymbol)
approve(data['sellTokenAddress'], data['allowanceTarget'], walletAddress, wpk)
tx = {
"from": walletAddress,
"to": Web3.toChecksumAddress(data['to']),
"data": data['data'],
"value": 0,
"gas": int(data['gas']),
"gasPrice": int(data['gasPrice']),
"nonce": nonce + 1,
"chainId": data['chainId']
}
# sign the transaction
signed_tx = w3.eth.account.sign_transaction(tx, wpk)
#
# send transaction
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
#
# get transaction hash
print(w3.toHex(tx_hash))

# Use only for testing the query
getQuote(DAI, slippage, USDC, sellAmount)

正如您所看到的,我正在尝试使用0x API将USDC交换为DAI。

第一个生成的tx被批准Etherscan approval tx

但是实际的交换在没有任何反馈的情况下失败Etherscan swap tx

我真的不知道该怎么做了…我一直在挖掘文档和阅读任何我能在Stackoverflow上找到的东西,没有任何运气。

感谢您的帮助。

气体限制&使用率:134,523 | 132,823 (98.74%)

我百分之九十九肯定是因为你的车没油了,试着增加它

相关内容

  • 没有找到相关文章

最新更新