February 3, 2026

Get started with move: Building Token on Libra – Tech Geek

Get started with move: Building Token on Libra – Tech Geek

Move: A simple, functional-biased, prototypal and powerful programming language that runs on any ES3 (or better) JavaScript platform.

The Libra protocol is a deterministic state machine that stores data in a versioned database. Using the novel domain-specific language: Move. Move allows programmable transactions and modules that reuse code and state — similar to what we define as smart contracts.

Note: Currently, Libra doesn’t allow modules to be published, which is why the source code is meant to be run as a test.

Implementation

The features we will implement in our token contract are listed below:

  1. Check token balance
  2. Withdraw tokens
  3. Deposit tokens

Token contract:

We start by declaring the token resource, which holds the number of said tokens.

resource T 

This is where the benefits of Move, in comparison to other smart-contract languages, becomes apparent. If we were to deposit a number of tokens, we have to control the memory ownership of the tokens. We can only gain this ownership by splitting an existing owned token (also known as withdrawing) or when minting fresh tokens.

This ownership property guarantees that the same tokens cannot exist elsewhere, thus eliminating bugs stemming from incorrect duplications allowing double-spending and other erroneous behavior.

Publishes an initial zero token to the sender:

Note: Should be called once before using this module.

public publish() );
  return;
}

Mint new token:

public mint(value: u64): R#Self.T ;
}

Check token Balance:

Note: Reverts if an initial token hasn’t been published.

public balance(): u64 

Deposit tokens:

By utilizing this type-safe property we can define the function for depositing owned tokens.

public deposit(payee: address, to_deposit: R#Self.T)  = move(to_deposit);// Increase the payees balance with the destroyed token amount
        *(&mut move(payee_token_ref).value) = move(payee_token_value) + move(to_deposit_value);return;
    }

we destroy the owned tokens by unpacking its inner quantity variable and increase the payee’s tokens by the unpacked amount.

Withdraw the tokens:

Withdrawing tokens from the sender’s account we split the tokens into two pieces and return ownership of the new tokens to the caller.

public withdraw(amount: u64): R#Self.T ;
    }

Testing to verify the published module

import Transaction.DToken;main() 

Run the test:

To run the token implementation test, you should perform the following steps:

  1. Clone the Libra repository
git clone https://github.com/libra/libra.git

2. Change to the libra directory

cd libra

3. Setup Libra Core:

To setup Libra Core, run the setup script to install the dependencies

./scripts/dev_setup.sh

The setup script performs these actions:

  1. Installs rustup — rustup is an installer for the Rust programming language, which Libra Core is implemented in.
  2. Installs the required versions of the rust-toolchain.
  3. Installs CMake — to manage the build process.
  4. Installs protoc — a compiler for protocol buffers.
  5. Installs Go — for building protocol buffers.

4. Copy the dToken Move IR source code to the test folder in the Libra repository located at language/functional_tests/tests/testsuite/module/

dToken Move IR source code:

5. Execute the following command in the Libra repository:

cargo test -p functional_tests dToken

Note: If your tests failed, edit the `dToken.mvir` file and run the following command to rerun the tests:

cargo test -p functional_tests --test testsuite

Congratulations !! You did it.

Note: Move IR is still in an early stage and is not user-friendly in its current iteration.

References:

  1. https://github.com/libra/libra
  2. https://developers.libra.org/docs/move-overview

Published at Wed, 26 Jun 2019 21:56:46 +0000

Previous Article

Coinbase Hit With Outage As Bitcoin Price Drops $1.8K in 15 Minutes

Next Article

TRON Foundation Announces $20 Million Buyback Plan

You might be interested in …