Tezos: A simple fund contract in LIGO

This tutorial guides you through developing, originating, and interacting with a simple decentralized fund smart contract on Tezos.

The origination and the interaction with the contract for tutorial purposes is done on Jakartanet, which is a testnet.

The simple fund contract does the following:

  • The contract can be funded in any amount of tez by anyone on the network.
  • 1 tez at a time can be withdrawn from the funded contract by anyone on the network.

Prerequisites

  • Chainstack account to deploy a Tezos node.
  • LIGO to create and test the contract.
  • Tezos client to originate the contract and to interact with the contract through the CLI.
  • Temple wallet to interact with the contract through a web app.

Overview

To get from zero to a deployed contract on Jakartanet, do the following:

  1. With Chainstack, create a public chain project.
  2. With Chainstack, join Jakartanet.
  3. With Chainstack, access your Tezos node credentials.
  4. Fund your developer Tezos account through a faucet.
  5. With LIGO, create the contract.
  6. With the Tezos client, compile and originate the contract through your Tezos node.
  7. With the Tezos client, fund the contract.
  8. Fund your user Tezos account through a faucet and your Temple wallet.
  9. With Temple wallet, interact with the contract.

Step-by-step

Create a public chain project

See Create a project.

Join Jakartanet

See Join a public network.

Get your Tezos node access and credentials

See View node access and credentials.

Install LIGO

See LIGO: Installation.

Install and configure the Tezos client

To install the client, see Tezos Client Installation and Setup.

Once installed, configure the client to the Chainstack-deployed Jakartanet node:

tezos-client --endpoint YOUR_CHAINSTACK_ENDPOINT config update

where YOUR_CHAINSTACK_ENDPOINT — your Tezos node endpoint deployed with Chainstack

Generate an account

Run:

octez-client gen keys new

Get the generated account address:

octez-client list known addresses

Fund your account with testnet tez

  1. Go to the faucet.
  2. In fund any address, paste your generated address.
  3. Click Request 6001 tz.

Create the contract

Create a simple fund contract in the PascaLIGO syntax with two entrypoints:

  • Deposit — any account on the network can deposit any amount of tez in the contract.
  • Withdraw — any account on the network can withdraw 1 tez at a time from the contract.

Create a file called simplefund.ligo:

// Two entrypoints: deposit any, withdraw 1 tez.
type entry is 
| Deposit
| Withdraw

// Simple storage in tez.
type storage is record  
 [balance: tez;    
]

const noOperations: list(operation) = nil;
const withdrawAmount : tez = 1tez;
const receiver: contract(unit) = Tezos.get_contract(Tezos.get_sender());
const payoutOperation: operation = Tezos.transaction(unit, withdrawAmount, receiver);
const operations : list (operation) = list [payoutOperation];

// Amend the storage balance with the deposited amount.
function depositAny(var storage: storage): (list(operation) * storage) is
  block {
        storage.balance := storage.balance + Tezos.get_amount();       
      }
  with(noOperations, storage)

// Withdraw 1 tez at a time.
// 1 tez is sent to the sender calling the withdraw function.
function withdrawFixed(var storage: storage): (list(operation) * storage) is
  block {     
    storage.balance := Option.unopt(storage.balance - withdrawAmount);              
  } with(operations, storage)

function main(const action: entry; var storage: storage): (list(operation) * storage) is
  block {
    skip
  } with case action of [
    | Deposit(_param) -> depositAny(storage)
    | Withdraw(_param) -> withdrawFixed(storage)
];

Test the contract

Before compiling the contract, you can test it using the LIGO CLI.

Deposit 10 tez:

$ ligo run dry-run simplefund.ligo --syntax pascaligo --amount 10 --entry-point  main "Deposit(unit)" "0"
( LIST_EMPTY() , record[balance -> 10000000mutez] )

Withdraw 1 tez:

$ ligo run dry-run simplefund.ligo --syntax pascaligo --entry-point main "Withdraw(unit)" "10000000mutez"
( CONS(Operation(0135a1ec49145785df89178dcb6e96c9a9e1e71e0a00000001c0843d0000663549d0f7c76252056ed0600da097a3e713237a00) ,
       LIST_EMPTY()) ,
  record[balance -> 9000000mutez] )

Compile the contract

Compile the contract and save the compiled code in a .tz file:

ligo compile contract simplefund.ligo --entry-point main > simplefund.tz

You are now ready to originate the compiled contract on Jakartanet.

Originate the contract

Originate the contract using your account, initiate the contract with 0 tez, and provide the origination fee with --burn-cap:

octez-client originate contract simplefund transferring 0 from ACCOUNT_ADDRESS running simplefund.tz --init 0 --burn-cap 3

where ACCOUNT_ADDRESS — the account you generated and funded previously.

Once the contract is originated, you will see the contract address in Originated contracts.

Example:

New contract KT1NSJSJjV5HHi3dgvwDNNs72hjCe9DsYvYV originated.

You now have a working contract on Jakartanet.

Write down the contract address as you will need it later to interact with it through a web app and the Temple wallet.

Interact with the contract using the Tezos client

You can now fund the contract using the Tezos client.

Get the contract parameter for the deposit function to pass to the Tezos client.

Get the parameter for Deposit:

$ ligo compile parameter simplefund.ligo --syntax pascaligo --entry-point main "Deposit(unit)"
(Left Unit)

Deposit 10 tez from your account to the contract:

octez-client transfer 10 from ACCOUNT_ADDRESS to simplefund --arg "(Left Unit)" --burn-cap 1

where ACCOUNT_ADDRESS — the account you generated and funded previously

Once the operation is included in a block, check the contract balance by querying the storage:

$ octez-client get contract storage for simplefund
10000000

Get a Temple wallet account with test tez

You need an account in the Temple wallet with some test tez to execute operations on Jakartanet.

  1. Install the Temple wallet.
  2. Go to the faucet.
  3. Connect with yout Temple wallet.
  4. Once connected, click Request 6001 tz.

Interact with the contract using the Temple wallet

  1. Navigate to the BCD explorer.
  2. Click Jakartanet.
  3. In the search field, provide your originated contract address.
  4. On the contract page, click Interact.
  5. Click withdraw > Execute > Temple - Tezos Wallet.
  6. Click Confirm.

This will connect your account from the Temple wallet with the contract and withdraw 1 tez to the account.

Conclusion

This tutorial guided you through the basics of creating a contract in LIGO and originating the contract on a Tezos network.

You were also able to interact with the contract through the CLI with the Tezos client and through a web app with the Temple wallet.

This tutorial uses testnet, however, the exact same instructions and sequence will work on the mainnet as well.

About the author

Ake

🛠️ Developer Experience Director @ Chainstack
💸 Talk to me all things Web3 infrastructure and I'll save you the costs
Ake | Warpcast Ake | GitHub Ake | Twitter Ake | LinkedIN