Liquidity Tutorials — Hands-on Series — Part 5.2

Marty Stumpf
Oct 28, 2019 · 7 min read

This is the fifth part of a tutorial series on writing smart contracts in Liquidity. I recommend going through the first, second, third, and fourth parts if you haven’t already.

In this tutorial, we’ll deploy and call the token contract we wrote in the last tutorial via the Tezos client command line interface. I strongly recommend thorough testings of this contract before launching it in the mainnet. Deploying the contract creates a fungible token system running on the Tezos ledger. To recap, the users of this contract include the owner of the token system, the users (account holders) of the tokens, and other contracts. In this tutorial we focus on the owner and the account holders. In the next tutorial, we will write a contract that calls the transferFrom entry point of the token contract.

The owner deploys the contract to start running the token system. The account holders can call the contract to

  • make a transfer via the 1st entry point transfer.
  • make multiple tranfers in one call via the 2nd entry point multiTransfer.
  • make an approval of a transfer amount to another account holder via the 3nd entry point approve.

Deploy

Compile token.liq to token.tz with Liquidity:

liquidity [path to]/token.liq

To deploy the contract, the owner has to specify the initial storage. In the following example, the owner (bootstrap1) initiates a token system with

  • the initial set of accounts: (note that the total number of tokens from all accounts must equal the total supply below.) The owner
  • has a token balance of 10000
  • has an empty allowance map.
  • version number of the token standard is 0.
  • total supply of 10000 tokens.
  • the name of the token is tokenA and the symbol for it is A.
  • the owner’s address is tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx.

The output indicates that the token system is initiated, and is memorized as the contract named tokena:

We can check the storage of the contract using the get script storage command:

Note that the set of accounts is not shown in the get script storage command, because it is of type big map. To check the accounts, we need to use the get big map value command, and input the address we want to check. E.g., to check the owner's account:

The output indicates that the owner has a balance of 10000 tokens and an empty allowance map, as expected.

Call to transfer

Anyone can call the contract’s transfer entry point to perform a token transfer, if one has enough token balance. For example, the owner can transfer 1000 tokens to bootstrap5 by running:

We can check that bootstrap5 has 1000 tokens now:

Call to make multiple transfers

bootstrap5 (tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv) can call and transfer

  • 20 tokens to bootstrap2(tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN),
  • 30 tokens to bootstrap3(tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU)

using the multiTransfer (2nd) entry point. To choose the 2nd entry point, we start the arg input with (Right (Left .... Then we input the list of address and the amount of tokens to be transferred to that address:

We can check that bootstrap2 has 20, and bootstrap3 has 30 tokens now:

For any transfer, if the sender doesn’t have enough tokens, the call fails. For example, if bootstrap3 now transfer 50 tokens to bootstrap2, the call fails, because bootstrap3 only has 30 in the account:

Call to approve

One can call the third entry point (approve) to approve an amount for contracts to transfer tokens on their behalf. For example, bootstrap5 can approve bootstrap3 100 tokens:

Doing so updates the allowance map of bootstrap5. We can see the effect by checking its big map value:

The output shows that bootstrap5 now has an allowance map with an entry of bootstrap3’s address (in optimized form) and 100 approved tokens. Note that the call does not check that the approver has balance no less than the approved amount. We’ll make use of this approved amount in the next tutorial!

Woohoo! We just launched a token system on Tezos! And we transferred some tokens between accounts, and approved/authorized some token transfers.

In the next tutorial, we will write a contract that make use of the transferFrom entry point to swap tokens between two token systems. Stay tuned!

Cryptium Labs Tezos

Cryptium Labs offers secure and highly available digital signatures for Proof-of-Stake networks, such as Tezos, Cøsmos, and Polkadot. This blog is dedicated to anyone in the blockchain ecosystem and aims to provide educational content for all audiences on topics such as security.

Marty Stumpf

Written by

Blockchain protocol/smart contract engineer @CryptiumLabs. thealmarty.com, twitter: @MartyStumpf.

Cryptium Labs Tezos

Cryptium Labs offers secure and highly available digital signatures for Proof-of-Stake networks, such as Tezos, Cøsmos, and Polkadot. This blog is dedicated to anyone in the blockchain ecosystem and aims to provide educational content for all audiences on topics such as security.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade