首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有0xAPI的web3py

带有0xAPI的web3py
EN

Stack Overflow用户
提问于 2022-06-22 22:35:44
回答 1查看 211关注 0票数 2

我一直试图使用0xAPI在Ropsten上执行交换,以获得最好的报价,但每次我尝试执行交换时,事务都会失败,而不会有任何反馈(至少来自Etherscan),而批准的tx可以正常运行。我可能在这里漏掉了什么东西,但我正变得盲目地盯着我的代码看。

代码语言:javascript
复制
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)

如您所见,我正试图使用0xAPI将USDC替换为DAI。

生成的第一个tx被批准为以太扫描批准tx

但是,没有任何反馈的以太扫描交换tx,实际的交换失败了。

老实说,我不知道该尝试什么.我一直在挖掘文档和阅读任何我能在Stackoverflow上找到的东西,没有任何运气。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2022-06-22 22:47:42

煤气限值及Txn用量: 134,523×132,823 (98.74%)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72722695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档