Created
July 11, 2021 10:25
-
-
Save vassilyvv/bb70833bf546a5bfbd586f1aaf29b30d to your computer and use it in GitHub Desktop.
Send ERC20 Token with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from web3 import Web3 | |
# or local url | |
INFURA_URL = "https://mainnet.infura.io/v3/<some_id>" | |
PRIVATE_KEY = b'<32 characters>' | |
web3 = Web3(Web3.HTTPProvider(INFURA_URL)) | |
print(web3.isConnected()) | |
my_account = web3.eth.account.privateKeyToAccount(PRIVATE_KEY) | |
receiver_address = "0x49E90a077d0d26D16675C80C6b8cCb5EA872a83D" | |
token_abi = [ | |
{"constant": True, "inputs": [], "name": "name", "outputs": [{"name": "", "type": "string"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_upgradedAddress", "type": "address"}], "name": "deprecate", "outputs": [], | |
"payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_spender", "type": "address"}, {"name": "_value", "type": "uint256"}], | |
"name": "approve", "outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "deprecated", "outputs": [{"name": "", "type": "bool"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_evilUser", "type": "address"}], "name": "addBlackList", "outputs": [], | |
"payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "totalSupply", "outputs": [{"name": "", "type": "uint256"}], | |
"payable": False, "stateMutability": "view", "type": "function"}, {"constant": False, | |
"inputs": [{"name": "_from", "type": "address"}, | |
{"name": "_to", "type": "address"}, | |
{"name": "_value", | |
"type": "uint256"}], | |
"name": "transferFrom", "outputs": [], | |
"payable": False, | |
"stateMutability": "nonpayable", | |
"type": "function"}, | |
{"constant": True, "inputs": [], "name": "upgradedAddress", "outputs": [{"name": "", "type": "address"}], | |
"payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "", "type": "address"}], "name": "balances", | |
"outputs": [{"name": "", "type": "uint256"}], "payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "decimals", "outputs": [{"name": "", "type": "uint256"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "maximumFee", "outputs": [{"name": "", "type": "uint256"}], | |
"payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "_totalSupply", "outputs": [{"name": "", "type": "uint256"}], | |
"payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [], "name": "unpause", "outputs": [], "payable": False, | |
"stateMutability": "nonpayable", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "_maker", "type": "address"}], "name": "getBlackListStatus", | |
"outputs": [{"name": "", "type": "bool"}], "payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "", "type": "address"}, {"name": "", "type": "address"}], "name": "allowed", | |
"outputs": [{"name": "", "type": "uint256"}], "payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "paused", "outputs": [{"name": "", "type": "bool"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "who", "type": "address"}], "name": "balanceOf", | |
"outputs": [{"name": "", "type": "uint256"}], "payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [], "name": "pause", "outputs": [], "payable": False, "stateMutability": "nonpayable", | |
"type": "function"}, | |
{"constant": True, "inputs": [], "name": "getOwner", "outputs": [{"name": "", "type": "address"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "owner", "outputs": [{"name": "", "type": "address"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "symbol", "outputs": [{"name": "", "type": "string"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}], | |
"name": "transfer", "outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": False, | |
"inputs": [{"name": "newBasisPoints", "type": "uint256"}, {"name": "newMaxFee", "type": "uint256"}], | |
"name": "setParams", "outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "amount", "type": "uint256"}], "name": "issue", "outputs": [], | |
"payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "amount", "type": "uint256"}], "name": "redeem", "outputs": [], | |
"payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "_owner", "type": "address"}, {"name": "_spender", "type": "address"}], | |
"name": "allowance", "outputs": [{"name": "remaining", "type": "uint256"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "basisPointsRate", "outputs": [{"name": "", "type": "uint256"}], | |
"payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": True, "inputs": [{"name": "", "type": "address"}], "name": "isBlackListed", | |
"outputs": [{"name": "", "type": "bool"}], "payable": False, "stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_clearedUser", "type": "address"}], "name": "removeBlackList", | |
"outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": True, "inputs": [], "name": "MAX_UINT", "outputs": [{"name": "", "type": "uint256"}], "payable": False, | |
"stateMutability": "view", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "newOwner", "type": "address"}], "name": "transferOwnership", "outputs": [], | |
"payable": False, "stateMutability": "nonpayable", "type": "function"}, | |
{"constant": False, "inputs": [{"name": "_blackListedUser", "type": "address"}], "name": "destroyBlackFunds", | |
"outputs": [], "payable": False, "stateMutability": "nonpayable", "type": "function"}, { | |
"inputs": [{"name": "_initialSupply", "type": "uint256"}, {"name": "_name", "type": "string"}, | |
{"name": "_symbol", "type": "string"}, {"name": "_decimals", "type": "uint256"}], "payable": False, | |
"stateMutability": "nonpayable", "type": "constructor"}, | |
{"anonymous": False, "inputs": [{"indexed": False, "name": "amount", "type": "uint256"}], "name": "Issue", | |
"type": "event"}, | |
{"anonymous": False, "inputs": [{"indexed": False, "name": "amount", "type": "uint256"}], "name": "Redeem", | |
"type": "event"}, | |
{"anonymous": False, "inputs": [{"indexed": False, "name": "newAddress", "type": "address"}], "name": "Deprecate", | |
"type": "event"}, {"anonymous": False, "inputs": [{"indexed": False, "name": "feeBasisPoints", "type": "uint256"}, | |
{"indexed": False, "name": "maxFee", "type": "uint256"}], | |
"name": "Params", "type": "event"}, {"anonymous": False, "inputs": [ | |
{"indexed": False, "name": "_blackListedUser", "type": "address"}, | |
{"indexed": False, "name": "_balance", "type": "uint256"}], "name": "DestroyedBlackFunds", "type": "event"}, | |
{"anonymous": False, "inputs": [{"indexed": False, "name": "_user", "type": "address"}], "name": "AddedBlackList", | |
"type": "event"}, | |
{"anonymous": False, "inputs": [{"indexed": False, "name": "_user", "type": "address"}], "name": "RemovedBlackList", | |
"type": "event"}, {"anonymous": False, "inputs": [{"indexed": True, "name": "owner", "type": "address"}, | |
{"indexed": True, "name": "spender", "type": "address"}, | |
{"indexed": False, "name": "value", "type": "uint256"}], | |
"name": "Approval", "type": "event"}, {"anonymous": False, "inputs": [ | |
{"indexed": True, "name": "from", "type": "address"}, {"indexed": True, "name": "to", "type": "address"}, | |
{"indexed": False, "name": "value", "type": "uint256"}], "name": "Transfer", "type": "event"}, | |
{"anonymous": False, "inputs": [], "name": "Pause", "type": "event"}, | |
{"anonymous": False, "inputs": [], "name": "Unpause", "type": "event"}] | |
contract_address = "0xdAC17F958D2ee523a2206206994597C13D831ec7" | |
contract = web3.eth.contract(contract_address, abi=token_abi) | |
print(f"My balance: {contract.functions.balanceOf(my_account.address).call()}") | |
print(f"Receiver balance: {contract.functions.balanceOf(receiver_address).call()}") | |
raw_txn = { | |
"from": my_account.address, | |
"gasPrice": web3.eth.gasPrice, | |
"gas": 200000, | |
"to": contract_address, | |
"value": "0x0", | |
"data": contract.encodeABI('transfer', args=(receiver_address, 1234567)), | |
"nonce": web3.eth.getTransactionCount(my_account.address) | |
} | |
signed_txn = web3.eth.account.signTransaction(raw_txn, PRIVATE_KEY) | |
web3.eth.sendRawTransaction(signed_txn.rawTransaction) | |
print(f"My balance: {contract.functions.balanceOf(my_account.address).call()}") | |
print(f"Receiver balance: {contract.functions.balanceOf(receiver_address).call()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 9: Please try is_connected() instead of isConnected(). This may have been changed in Web3 6.0 release to conform with Python coding conventions ( source: https://stackoverflow.com/questions/75765117/web3-object-has-no-attribute-isconnected ).