Send a transaction over HTTP using the Python SDK

This example demonstrates how we run local fetch node, create a test account, give this account some FET and then send some FET to another account.

Set up

First, we need to set up the environment, which means building a local node, you can find detailed instructions for this here: v. 0.1

With a local node running, let's go ahead and download and install the Python library for interacting with the Fetch Ledger...

git clone [email protected]:fetchai/ledger-api-py.git -b develop

At this time, you need to be running on the develop branch to support the latest features.

The library can be installed to the system using the following command:

python3 setup.py install

Using this newly installed library couldn't be simpler: import it into a project and reference it. Remember that this repo will be going through many revisions so it is unwise to work within the fetchai repo.

The script we're looking at is situated in the examples folder; you can see the entirety of the script this example describes under:

ledger-api-py/examples/http_transactions.py

Creating accounts and sending FET between them

Let me briefly describe what's happening in http_transaction.py, and what's necessary for you to start creating transactions on the testnet, and all that other cool stuff.

import time

from fetchai.ledger.api import TokenApi, TransactionApi
from fetchai.ledger.crypto import Identity

HOST = '127.0.0.1'
PORT = 8000

We've imported the python package time, and imported TokenApi, TransactionApi from fetchai.ledger.api, the classes from fetchai are used to connect over HTTP to the ledger, this will be discussed in length soon in another example.

Similarly, we import the class Identity from fetchai.ledger.crypto, the process of this will be updated soon.

HOST and PORT are the host and port the local node you're running is listening on.

def wait_for_tx(txs: TransactionApi, tx: str):
    while True:
        if txs.status(tx) == "Executed":
            break
        time.sleep(1)

We've got a blocking method which uses the time library to allow us to wait for the status of a transaction to become Executed. While waiting for the transaction to be verified, we can log the status of the transaction as follows:

def wait_for_tx(txs: TransactionApi, tx: str):
    while True:
        if txs.status(tx) == "Executed":
            break
        else:
            print('Current Status:', txs.status(tx))
        time.sleep(1)

We can expect an output from a transaction as it is processed by the node to look similar to this:

Current Status: Unknown
Current Status: Pending
Current Status: Executed

Continuing to the main() method, this is how we show an example of creating an address, and sending FET to another.

http_transactions.py main() method:

def main():
    # create the APIs
    txs = TransactionApi(HOST, PORT)
    tokens = TokenApi(HOST, PORT)

    # generate a random identity
    your_identity = Identity()
    other_identity = Identity()
    print('Balance Before:', tokens.balance(your_identity.public_key))
    print (dir(your_identity))

    # create the balance
    print('Submitting wealth creation...')
    wait_for_tx(txs, tokens.wealth(your_identity.private_key_bytes, 1000))
    print('Balance after wealth:', tokens.balance(your_identity.public_key))

    # submit and wait for the transfer to be complete
    print('Submitting transfer...')
    wait_for_tx(txs, tokens.transfer(your_identity.private_key_bytes, other_identity.public_key_bytes, 250))

    print('Balance 1:', tokens.balance(your_identity.public_key))
    print('Balance 2:', tokens.balance(other_identity.public_key))
    
    print('Balance After:', tokens.balance(your_identity.public_key))

It's good to note that we're getting a reference to TransactionAPI, and the TokenAPI, and initialising both with the local nodes HOST and PORT:

# create the APIs
    txs = TransactionApi(HOST, PORT)
    tokens = TokenApi(HOST, PORT)

Then we create two identities, simplified for the purpose of this example. An identity is simple a wrapper class around a public and private key pair (it also has some nice helpers for accessing these values in various forms like a hexadecimal or base64 string)

# generate a random identity
your_identity = Identity()
other_identity = Identity()

With two identities created, we need to give your_identity some FET tokens to play with:

wait_for_tx(txs, tokens.wealth(your_identity.private_key_bytes, 1000))

We're doing a lot in a seemingly small line; we're calling the method wait_for_tx defined earlier, we're passing it a reference to the TransactionAPI object, and we're calling tokens.wealth for generating some tokens for your address. We're also telling tokens.wealth to give this identity 1000 FET.

Then, we send FET to the other created identity:

wait_for_tx(txs, tokens.transfer(your_identity.private_key_bytes, other_identity.public_key_bytes, 250))

Again, we're calling wait_for_tx, we're passing it a reference to the TransactionAPI object, and we're calling tokens.transfer, passing the private key of your account, the public key of the account you're sending to and the amount.

You can expect the output of http_transaction.py to be similar to:

Balance Before: 0
Submitting wealth creation...
Current Status: Unknown
Balance after wealth: 1000
Submitting transfer...
Current Status: Unknown
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Current Status: Pending
Balance 1: 750
Balance 2: 250
Balance After: 750

You can run the example with:

python3 http_transaction.py

That concludes this example. If you have any questions or feedback please do email us at [email protected]

Address

St. John's Innovation Centre,
Cowley Road,
Cambridge, CB4 0WS, UK

Go to map
Technical: [email protected]
Investors: [email protected]