This code is the base in web3.py to send tokens between accounts. You can use it to send a token native to a blockchain, such as ETH, AVAX, FTM, etc.
To make this code work you will need to have installed:
You can install web3.py with:
pip install web3
Note that on Windows, you will need to install the Microsoft C++ Build Tools to make it work.
- Access to a node endpoint, I recommend to use Chainstack:
Follow these steps to sign up on Chainstack, deploy a node, and find your endpoint credentials:
Once you have access to your Chainstack endpoint credentials, you can use it to fill the node_url
variable.
This code was tested on the Fantom testnet, but it will be compatible with all the EMV based networks.
The first step is to create a new Python
file, I named it transfers.py
.
The first section of the code is to connect to a blockchain.
from web3 import Web3
node_url = "FANTOM_TESTNET_ENDPOINT" # endpoint URL
web3 = Web3(Web3.HTTPProvider(node_url)) # establish connection to a node
This creates a connection to your node so that you can send and retrieve data.
Then initialize the addresses that you want to use to make the transfer. Don't forget to include the private key from the address used to send the tokens; it is recommended to use an environment variable instead of hardcoding it like in this example!
sender = "SENDER_ADDRESS"
receiver = "RECEIVER_ADDRESS"
private_key = "YOUR_PRIVATE_KEY"
Create a nonce
variable to retrieve the sender address transactions count; this is essential to ensure that the transaction cannot be replayed by someone with malicious intent.
# nonce
nonce = web3.eth.getTransactionCount(sender)
We can now estimate the gas limit using the eth_estimateGas
method, which simulates a transfer between accounts.
# retrieve the gas limit for this simulated transaction.
gas_limit = web3.eth.estimate_gas(({"from":sender,"to":"receiver}), "latest")
#print("The gas limit is:", gas_limit)
Then we build the transaction object, where we specify the amount of FTM you want to transfer to the other account. (1 FTM in this example)
tx = {
"nonce": nonce,
"to": receiver,
"value": web3.toWei(1, "ether"), # measure unit of 1 full token
"gas": gas_limit, # 21000 is good for a simple transfer
"gasPrice": web3.toWei(10, "gwei"),
"chainId": web3.eth.chain_id,
}
The last step is to sign and send the transaction.
# sign tx
signed_tx = web3.eth.account.signTransaction(tx, private_key)
# send transaction
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print("Transaction hash:", web3.toHex(tx_hash))
This will also print the transaction hash so you can see it on the block explorer😉
Now you only need to run the script and see the magic happens!
python transfers.py