簡體   English   中英

嘗試將 SmartContract 寫入 Pancakeswap 路由器時出現 Web3.py “未知帳戶”錯誤

[英]Web3.py “Unknown account” error when trying to write SmartContract to Pancakeswap router

我開始開發一個小程序,它應該允許我通過 pancakeswap 路由器購買代幣。 當我嘗試進行交易時,我收到“未知帳戶”錯誤。 我認為這可能是因為我應該在本地“登錄”到我的元掩碼帳戶,但這只是我的假設。 我導出了我的私鑰並嘗試使用w3.eth.account.from_key(privateKey)創建一個帳戶,但它沒有做任何事情。 我還嘗試對所有地址執行w3.toChecksumAddress(address)但它沒有做任何事情。 我不知道此時我能做什么。


這是我的代碼:
 binanceRPC = 'https://bsc-dataseed1.defibit.io/' w3 = Web3(Web3.HTTPProvider(binanceRPC)) PCS_V2_ADDR = w3.toChecksumAddress( '0x10ED43C718714eb63d5aA57B78B54704E256024E') PCS_ABI = #there would be pcs ABI but i needed to delete it due to character limit on stack PCS_ROUTER_CONTRACT = w3.eth.contract(address=PCS_V2_ADDR, abi=PCS_ABI) print(w3.isConnected()) # True WBNB = w3.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c') shitcoin = w3.toChecksumAddress('0x3ee2200efb3400fabb9aacf31297cbdd1d435d47') nonce = w3.eth.get_transaction_count(testAccAddr) amountIn = 0.0005 tx = { 'nonce': nonce, 'from': testAccAddr, 'to': PCS_V2_ADDR, 'gasPrice': 5, 'gas': 165250, 'value': w3.toWei(amountIn, 'ether') } w3.eth.account.privateKeyToAccount(testAccPrvKey) print(w3.eth.accounts) # [] txHash = PCS_ROUTER_CONTRACT.functions.swapExactETHForTokens(0, [w3.toChecksumAddress('0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c'), w3.toChecksumAddress( '0x3ee2200efb3400fabb9aacf31297cbdd1d435d47')], testAccAddr, 1621289953).transact(tx) # ValueError: {'code': -32000, 'message': 'unknown account'}

嘗試這樣的事情:

account = w3.eth.account.privateKeyToAccount(testAccPrvKey)
w3.eth.default_account = account.address
txn = PCS_ROUTER_CONTRACT.functions.swapExactETHForTokens(0, 
    [w3.toChecksumAddress('0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c'), 
     w3.toChecksumAddress('0x3ee2200efb3400fabb9aacf31297cbdd1d435d47')], 
     testAccAddr, 1621289953).buildTransaction({
        'chainId': 97, #This is testnet
        'value': w3.toWei(amountIn, 'ether'),
        'nonce': nonce,
    })


signed_txn = account.signTransaction(txn)
txid = w3.toHex(w3.eth.sendRawTransaction(signed_txn.rawTransaction))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM