Elements code tutorial

Using Elements to perform basic operations

With both Elements daemons running, we can now use their associated clients to retrieve wallet information from them:

e1-cli getwalletinfo
e2-cli getwalletinfo

You will see that we currently have 21 million “bitcoin” in each wallet. This is because the “bitcoin” are held in outputs that are created as “anyone-can-spend”. This is done on chain initialization and it is useful for testing and also enables easy allocating of the coins initially created in a non-test environment. The amount of the default asset created can be changed using the “initialfreecoins” setting in the elements config file. The fact that the asset is named “bitcoin” can be ignored for now, it is merely a default label assigned to the first asset created by Elements. We will see how to override the default behaviour of creating 21 million of the default asset, and the label the default asset is assigned, in a later section.

NOTE: You may notice that a lot of the RPC commands you can issue to an Elements client are the same as those you can issue to a Bitcoin client. This is because the Elements code is based upon Bitcoin’s own. You can find a list of the available RPC commands for the Elements client in elements/src/rpc/client.cpp on GitHub.

Let’s claim the “anyone-can-spend” values and send half the assets to a new address generated by each Elements node. Elements has a requirement that such claims, as well as fees collected from block creation, must have a maturity of 100 blocks before they become spendable. This isn’t strictly necessary for Elements signed blockchains, but the rule is carried over from Bitcoin for maximum compatibility. We’ll also need to create addresses that will receive any funds from generating blocks for both Bitcoin and Elements nodes. Claiming the coins and maturing them:

ADDRGENB=$(b-cli getnewaddress)
ADDRGEN1=$(e1-cli getnewaddress)
ADDRGEN2=$(e2-cli getnewaddress)

e1-cli sendtoaddress $(e1-cli getnewaddress) 21000000 "" "" true
e1-cli generatetoaddress 101 $ADDRGEN1
e1-cli sendtoaddress $(e2-cli getnewaddress) 10500000 "" "" false
e1-cli generatetoaddress 101 $ADDRGEN1

As you run each step above you will see the results of executing the command written to the terminal as output. These will differ depending on the command you run (for example, the sendtoaddress command returns a transaction ID if successful). The “true” and “false” refer to fee deductions and can be ignored for now.

Check that the funds have been evenly split:

e1-cli getwalletinfo
e2-cli getwalletinfo

Both should show (amongst other values):

"bitcoin": 10500000.00000000

That confirms that our nodes are operational and able to transact.

Next: Using Confidential Transactions