Build a VET transaction
(Links to all parts at the bottom)
In this part a transaction will be created, signed and transfered to the VeChain Testnet using the python thor-devkit.
To send the transaction with python, “requests” needs to be installed. This can be done like:
pip3 install requests
Of course we also need a VeChain Thor Node to send the transaction to. You can choose to install a node yourself or use https://testnet.veblocks.net as provided in the demos (a list of all available nodes can be found at https://nodes.veblocks.net).
We now have a wallet with private key (Part 1) and know all the values needed for a valid transaction (Part 2), so its time to create an actual transaction. If you want to use your own wallet, you have to exchange the private key is this script. Other than that, the only value that needs to be modified is the BlockRef as shown in Part 2.
All we have to do, is so create a file (3_vet_transaction.py) and insert the following code:
from thor_devkit import cry, transaction
import requestsbody = {
"chainTag": 39,
"blockRef": '0x00634b0a00639801',
"expiration": 720,
"clauses": [
{
"to": '0x0000000000000000000000000000000000000000',
"value": 1000000000000000000,
"data": '0x'
}
],
"gasPriceCoef": 0,
"gas": 21000,
"dependsOn": None,
"nonce": 12345678
}# Construct an unsigned transaction.
tx = transaction.Transaction(body)# Sign the transaction with a private key.
priv_key = bytes.fromhex('61faba91ef7516969e885d197f59feeb2007ea2c6057908d1696d6f056ca69d4')
message_hash = tx.get_signing_hash()
signature = cry.secp256k1.sign(message_hash, priv_key)# Set the signature on the transaction.
tx.set_signature(signature)print('Created a transaction from ' + tx.get_origin() + ' to 0x0000000000000000000000000000000000000000 with TXID: ' + tx.get_id() + '.')
print('')encoded_bytes = tx.encode()
# pretty print the encoded bytes.
print('The transaction "0x' + encoded_bytes.hex() + '" will be send to the testnet node now.')
tx_headers = {'Content-Type': 'application/json', 'accept': 'application/json'}tx_data = {'raw': '0x' + encoded_bytes.hex()}send_transaction = requests.post('https://testnet.veblocks.net/transactions', json=tx_data, headers=tx_headers)print('Response from Server: ' + str(send_transaction.content))
This will result in an output like this:
In this respose you can see the encoded transaction. This transaction is then checked by the node and then -if valid - transmitted to the tx pool and the Authority Masternodes.
The TXID of this transaction is 0xb10eba82bbf24eab4930664005ed72790ad6795bae357db9f6c94da8301d0d6d
Then we take a look at this, all the values defined in the transaction body were correctly written to the blockchain.
Hurray!
Create the same transaction again
If we simply run the script again, the result will be “Response from Server: tx rejected: known tx”. To send another VET, we either need to change the BlockRef or the Nonce (as long as “BlockRef” + “Expiration” is valid).
When Nonce is changed to “12345679”, the transaction will be accepted again.
We now have a script to send VET to another address. The next part will show how to send VET to multiple addresses in one transaction (multi-task-transaction).
HowTo VeChain Blockchain — Part 1: Create a wallet
HowTo VeChain Blockchain — Part 2: What’s a transaction made of?
HowTo VeChain Blockchain — Part 3: Build a VET transaction
HowTo VeChain Blockchain — Part 4: Creating a multi-clause transaction
HowTo VeChain Blockchain — Part 5: Python-powered VET transfers
HowTo VeChain Blockchain — Part 6: Putting data on the Blockchain
HowTo VeChain Blockchain — Part 7.1: What is GAS and how to calculate it