简体   繁体   English

如何使用 web3.py 在钱包之间转移 ERC20 代币

[英]How to transfer an ERC20 token between wallets with web3.py

I was wondering, what is the proper way to transfer an ERC20 token between two wallets using web3.py?我想知道,使用 web3.py 在两个钱包之间转移 ERC20 代币的正确方法是什么? I am testing everything on the Rinkeby test network.我正在测试 Rinkeby 测试网络上的所有内容。

Here is what I've tried...这是我尝试过的...

from ethtoken.abi import EIP20_ABI
from web3 import Web3

token_from = "from_address"
token_to = "to_address"
token_to_private_key = "your_private_key"

#w3 = Web3(Web3.HTTPProvider(infura_url))


contractAddress = "contract_address"
infura_url = "https://rinkeby.infura.io/v3/your_infura_key"
# Fill in your infura API key here
w3 = Web3(Web3.HTTPProvider(infura_url))

contract = w3.eth.contract(address=contractAddress, abi=EIP20_ABI)

nonce = w3.eth.getTransactionCount(token_from)  



# Build a transaction that invokes this contract's function, called transfer
token_txn = contract.functions.transfer(
     token_to,
     1,
 ).buildTransaction({
     'chainId': 1,
     'gas': 70000,
     'gasPrice': w3.toWei('1', 'gwei'),
     'nonce': nonce,
 })


signed_txn = w3.eth.account.signTransaction(token_txn, private_key=token_to_private_key)

w3.eth.sendRawTransaction(signed_txn.rawTransaction)  

When I try to run this with my addresses and private key, I get the error: ValueError: {'code': -32000, 'message': 'invalid sender'}当我尝试使用我的地址和私钥运行它时,我收到错误:ValueError: {'code': -32000, 'message': 'invalid sender'}

How can I fix this?我怎样才能解决这个问题? Or... is there a better way to transfer a erc20 token with web3.py?或者......有没有更好的方法来使用 web3.py 传输 erc20 令牌?

Any help is appreciated.任何帮助表示赞赏。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM