Facebook’s Libra Blockchain Programming Tutorial

Andrej
4 min readSep 25, 2019

--

According to the official documentation:

The Libra Blockchain is a decentralized, programmable database designed to support a low-volatility cryptocurrency that will have the ability to serve as an efficient medium of exchange for billions of people around the world

To validate the design of the Libra protocol, Facebook have built an open-source prototype implementation - Libra Core - in anticipation of a global collaborative effort to advance this new ecosystem. The Libra protocol allows a set of replicas, referred to as validators, from different authorities to jointly maintain a database of programmable resources. These resources are owned by different user accounts authenticated by public key cryptography and adhere to custom rules specified by the developers of these resources. Validators process transactions and interact with each other to reach consensus on the state of the database. Transactions are based on predefined and, in future versions, user-defined smart contracts in a new programming language called Move.

Let’s Start with development

First you need to clone Libra to your own machine

git clone https://github.com/libra/libra.git && cd libra

Because we are going to work on Test Network, let’s switch to Testnet. Let’s checkout the Testnet Branch.

git checkout testnet

Now we need to install some dependencies. There’s a script that will do all that job for us. It is located in /scripts and it is called dev_setup.sh

Okay, let’s run it.

./scripts/dev_setup.sh

You will see something like this

Type y (yes) and wait couple of seconds while everything is getting installed.

We are now ready! Let’s run the CLI

Run this script. Notice that first time installing will need some memory and CPU power (and couple of minutes, of course)

./scripts/cli/start_cli_testnet.sh

Once the client connects to a node on the Testnet, you will see the following output. To quit the client at any time, use the quit command.

Creating Account

We will create two accounts (Alice’s and Bob’s) for this demo. Enter this command from CLI.

libra% account create

Sample output on success:

>> Creating/retrieving next account from wallet Created/retrieved account #0 address 3ed8e5fafae4147b2a105a0be2f81972883441cfaaadf93fc0868e7a0253c4a8

#0 is the number of account. This long stream of numbers and characters is account’s public key.

Minting coins

Accounts are created by default with 0 tokens. We can mint some using this command

libra% account mint <account_#No> <how_many_coins>

Example (mint 150 LBR coins for account #0):

libra% account mint 0 150

Let’s see the balance now. It should have 150 LBR coins. Command is

libra% query balance <account_#No>

libra% query balance 0

Output should look like this.

Transfering coins

We will now transfer some money from Alice’s account (account #0) to Bob’s account (account #1). Command is

libra% transfer <FROM_account_#No> <TO_account_#No> <how_many_coins>

For example Alice will transfer 10 LBR coins to Bob.

libra% transfer 0 1 10

Sample output will

>> Transferring 
Transaction submitted to validator
To query for transaction status, run: query txn_acc_seq 0 0 <fetch_events=true|false>

You have successfully executed your transaction on the Libra testnet and transferred 10 LBR from Alice’s account to Bob’s account!

Every blockchain is made of transcations. To see the details of this particular transaction run suggested command

libra% query txn_acc_seq 0 0 true

The output will be

libra% query txn_acc_seq 0 0 true
>> Getting committed transaction by account and sequence number
Committed transaction: SignedTransaction {
raw_txn: RawTransaction {
sender: 9bb3f5486e3993bce0a852756eeb4501f1c6d69f54f611e3c89f70653f7c93e6,
sequence_number: 0,
payload: {,
transaction: peer_to_peer_transaction,
args: [
{ADDRESS: 3c0bef21d2a70352999d884ec8ddc454d1d0b20039eb57f500b61260f7da5d39},
{U64: 10000000},
]
},
max_gas_amount: 140000,
gas_unit_price: 0,
expiration_time: 1569407231s,
},
public_key: Ed25519PublicKey(
PublicKey(CompressedEdwardsY: [107, 58, 145, 55, 112, 39, 105, 161, 140, 241, 101, 74, 76, 0, 254, 173, 202, 32, 214, 5, 211, 204, 37, 62, 218, 110, 139, 16, 219, 139, 252, 164]), EdwardsPoint{
X: FieldElement51([323475165742137, 1278845330539821, 1404149086617382, 716888725530638, 1786404331834540]),
Y: FieldElement51([324837898795627, 432437678150701, 2067645217634305, 2090305065478530, 650673547970742]),
Z: FieldElement51([1, 0, 0, 0, 0]),
T: FieldElement51([2199831917337458, 1818197555666257, 34883819659738, 217540842620982, 2082579473926152])
}),
),
signature: Ed25519Signature(
Signature( R: CompressedEdwardsY: [208, 50, 156, 53, 244, 232, 129, 96, 199, 204, 163, 37, 25, 246, 86, 55, 4, 53, 53, 166, 202, 234, 72, 57, 180, 18, 128, 85, 134, 146, 220, 57], s: Scalar{
bytes: [18, 158, 15, 107, 98, 92, 195, 111, 170, 60, 119, 255, 118, 230, 146, 167, 53, 144, 21, 236, 158, 46, 189, 162, 254, 113, 50, 84, 226, 216, 221, 0],
} ),
),
}
Events:
ContractEvent { key: c4c1e53a1e2148e385b36f9dcb4e26e05a3a101c1a7aa4f8abcb3913f0375d47, index: 0, event_data: AccountEvent { account: 3c0bef21d2a70352999d884ec8ddc454d1d0b20039eb57f500b61260f7da5d39, amount: 10000000 } }
ContractEvent { key: d91a8ead1e5f7c8f81209580a52650d0313426052bb51c25f3c208996a9e69a5, index: 0, event_data: AccountEvent { account: 9bb3f5486e3993bce0a852756eeb4501f1c6d69f54f611e3c89f70653f7c93e6, amount: 10000000 } }

If you are more interesting about the meaning of each transaction property go to official documentation for more details.

Congratulations!

You are now officially a Libra Blockchain Developer.

If you are find this topic interesting 🚀 Join Our Telegram Channel because Libra Development course is coming soon… 🔥

--

--

No responses yet