# 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 Hangzhounet, 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

# Overview

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

  1. With Chainstack, create a public chain project.
  2. With Chainstack, join Hangzhounet.
  3. With Chainstack, access your Tezos node credentials.
  4. Fund your developer Tezos account through a faucet (opens new window).
  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 add the account to your Temple wallet.
  9. With Temple wallet, interact with the contract.

# Step-by-step

# Create a public chain project

See Create a project.

# Join Hangzhounet

See Join a public network.

# Get your Tezos node access and credentials

See View node access and credentials.

# Install LIGO

See LIGO: Installation (opens new window).

# Install and configure the Tezos client

To install the client, see Tezos Client Installation and Setup (opens new window).

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

tezos-client --endpoint ENDPOINT config update

where

Example:

tezos-client --endpoint https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d config update

# Fund your account with testnet tez

The Tezos faucet (opens new window) generates an account on Hangzhounet with test tez.

  1. Go to the faucet (opens new window).
  2. Click Get Hangzhounet tez.
  3. Download the generated JSON file.
  4. Place the generated JSON file in your project directory where the Tezos client is installed.
  5. Activate the funded account on Hangzhounet:
tezos-client activate account ALIAS with "ACCOUNT_JSON"

where

  • ALIAS — any alias to call your account.
  • ACCOUNT_JSON — the account details in a JSON file that you downloaded from the faucet.

Example:

tezos-client activate account trent with "hangzhounet.json"

Check the account balance:

tezos-client get balance for ALIAS

where

  • ALIAS — the alias you provided when activating the account.

Example:

tezos-client get balance for trent

# 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;    
end

const noOperations: list(operation) = nil;

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

// Withdraw 1 tez at a time denominated in mutez, the smallest tez unit.
// 1 tez is sent to the sender calling the withdraw function.
function withdrawFixed(var storage: storage): (list(operation) * storage) is
  block {
    const withdrawAmount: tez = 1000000mutez;  
    var _operations: list(operation) := nil;
    const receiver: contract(unit) = get_contract(sender);
    const payoutOperation: operation = transaction(unit, withdrawAmount, receiver);
    operations:= list 
      payoutOperation 
        end;       
    storage.balance := 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)
end;

# 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)" "record liquidity = 0mutez; end"
( LIST_EMPTY() , record[balance -> 10000000mutez] )

Withdraw 1 tez:

$ ligo run dry-run simplefund.ligo --syntax pascaligo --entry-point main "Withdraw(unit)" "record liquidity = 10000000mutez; end"
( CONS(Operation(0135a1ec49145785df89178dcb6e96c9a9e1e71e0a00000001c0843d0000d8276b0b00b177a2543b17e8799d31b94252c97300) ,
       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 Hangzhounet.

# Originate the contract

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

tezos-client originate contract simplefund transferring 0 from ALIAS running simplefund.tz --init 0 --burn-cap 3

where

  • ALIAS — the account alias you activated previously.

Example:

tezos-client originate contract simplefund transferring 0 from trent running simplefund.tz --init 0 --burn-cap 3

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

Example:

New contract KT1RqWGvFewTyaSaho5c5oimhDRYrW3ZXRQu originated.

You now have a working contract on Hangzhounet.

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 alias account to the contract:

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

where

  • ALIAS — the account alias you activated previously.

Example:

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

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

$ tezos-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 Hangzhounet.

  1. Install the Temple wallet (opens new window).
  2. Go to the faucet (opens new window).
  3. Click Get Hangzhounet tez.
  4. Download the generated JSON file.
  5. In your Temple wallet instance, click Import account.
  6. Select Faucet File.
  7. Provide the JSON file you have downloaded.

The Temple wallet will activate your account with test tez.

# Interact with the contract using the Temple wallet

  1. Navigate to the BCD explorer (opens new window).
  2. Click Hangzhou2net.
  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.