Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Web3.py“;未知账户”;尝试将SmartContract写入Pancakeswap路由器时出错_Python_Blockchain_Smartcontracts_Web3_Web3py - Fatal编程技术网

Python Web3.py“;未知账户”;尝试将SmartContract写入Pancakeswap路由器时出错

Python Web3.py“;未知账户”;尝试将SmartContract写入Pancakeswap路由器时出错,python,blockchain,smartcontracts,web3,web3py,Python,Blockchain,Smartcontracts,Web3,Web3py,我开始开发一个小程序,允许我通过pancakeswap路由器购买代币。当我试图做交易时,我得到“未知帐户”错误。我想这可能是因为我应该在本地“登录”到我的metamask帐户,但这只是我的假设。我导出了我的私钥,并尝试使用w3.eth.account.from_key(privateKey)从中创建一个帐户,但它什么也没做。我还尝试对所有地址执行w3.toChecksumAddress(address),但它没有执行任何操作。我不知道现在我能做什么 这是我的代码: binanceRPC = '

我开始开发一个小程序,允许我通过pancakeswap路由器购买代币。当我试图做交易时,我得到“未知帐户”错误。我想这可能是因为我应该在本地“登录”到我的metamask帐户,但这只是我的假设。我导出了我的私钥,并尝试使用
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))