Python

In this tutorial we will be using the Factom Python library to interact with the factomd and factom-walletd APIs in order to secure some data on the Factom blockchain, also known as factomizing. This tutorial is using Python 3.6.7 that comes with a fresh Ubuntu 18.04 install, but versions 3.4, 3.5, 3.7 most likely work as well.

Requirements

  • Python 3+

  • Git

Setup

Setting up the Factom sandbox

For development it is recommended to run a local installation of factomd and factom-walletd, there are a few reasons for this:

  • No need to spend money on Entry Credits

  • Getting slightly familiar with how a Factom node works.

  • Set your own block time, default for mainnet is 600 seconds (10 minutes pr. block)

With that said, if you don't have a Factom sandbox setup, head over to the Developer Sandbox Setup Guide and follow one of the three setup methods.

Setting up the Python environment

A good idea is to create a Python virtual environment to keep your Python packages isolated to your project. This will avoid any package version conflicts between different Python projects on your computer. First, make a directory containing your virtual environments, or navigate to it if you already have one.

mkdir ~/venvs
cd ~/venvs

Now create the project specific virtual environment:

This will create a new directory in your current folder, named whatever you specified in the command. Here we named the virtual environment "hello-world-fct", you can change this to a different name that suits you. If the command fails to run, follow the instructions given in the command line and try again. Now activate the environment:

Installing the Python package(s)

We will be using the Factom API Python client library. Install it via pip:

Usage

Everything is setup and ready now. All you need is your favorite IDE and make sure you select / activate the Python interpreter you setup earlier.

Initiating Factomd and FactomWalletd instances

Retrieving wallet balances using factomd

Let’s check the wallet balances on both the FCT and EC addresses we imported and generated earlier. The FCT balance will be shown in Factoshis( Factoids * 10^-8) :

If you provided your wallet info when initiating the factomd instance you can run the above lines without providing the addresses:

You should see the following results:

At the moment we have 20000 Factoids and 0 Entry Credits. We need to fund our Entry Credit address by purchasing some Entry Credits, as those are required in order to record data on-chain.

Purchasing Entry Credits using the imported Factoid address

The wallet daemon provides a method fct_to_ec() to burn Factoids and mint Entry Credits. We will use that method, but first we need to know what the Factoid to Entry Credit exchange rate is:

Result:

This tells us we'll need to burn 1000 Factoshis (0.00001 FCT) in exchange for 1 Entry Credit, so let's purchase 10 entry credits for 10000 Factoshis (0.0001 FCT).

Result:

The transaction was successfully submitted with txid: c60e4fee59876ce5ee242d9da15d4764a2880f36efac8c18bc7306986b041fb5. Let's check the balance again on our wallets as shown earlier. The result should be:

Note that the FCT balance has been reduced by 22K instead of 10K Factoshis due to the network transaction fees.

Creating chains and entries

The wallet client has a new_chain() method that handles everything needed to create a new chain. To make a new chain you need to provide:

  • Your factomd instance, where the creation message will be submitted

  • A list of external IDs

  • Entry content

  • Your Entry Credit address

Result:

Success! Lets head over to the local Factom explorer to see if our entry exists. Search for the chainid, found in the result above, in the search box as seen below:

If nothing shows up you might need to wait a few more seconds / minutes, depending on what you set your block time to.

Our chain is successfully created and is visible in the explorer, now lets add some entries to that chain.

Result:

Success! Let's check the explorer again to see if some new entries show up on the same chain:

Reading chains and entries created

Sometimes the entries in your chain may reference each other, thus you may want to scan the entire chain in order to very its integrity. The factomd client provides a read_chain() method which iterates over all entry-containing blocks and returns a list of entries in reverse order.

Result:

Error handling

The Python library returns a number of different error codes in case of a problem. The API method you tried to call will raise a factom.exceptions.FactomAPIError with details about the error.

For example, if we try to create a new chain with the exact same parameters as a previously created chain you’ll see something similar to this:

Details about the error are attached to the exception instance, thus a good idea is to wrap each API call into try blocks such as:

As this chain already exists, it'll return the following message:

For a full list of the error codes in the library see here.

Full code

Below is the full code featuring everything mentioned above, with some extra additions.

Conclusion

You now have all the tools available and ready to start fully interacting with the Factom blockchain in Python!

Troubleshooting

This section will be filled out once users start reporting issues, problems or relevant questions they might have.

Last updated

Was this helpful?