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 Ghostnet, 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 Ghostnet, do the following:
- With Chainstack, create a public chain project.
- With Chainstack, join Ghostnet.
- With Chainstack, access your Tezos node credentials.
- Fund your developer Tezos account through a faucet.
- With LIGO, create the contract.
- With the Tezos client, compile and originate the contract through your Tezos node.
- With the Tezos client, fund the contract.
- Fund your user Tezos account through a faucet and your Temple wallet.
- With Temple wallet, interact with the contract.
Step-by-step
Create a public chain project
See Create a project.
Join Ghostnet
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 Installing binaries.
Once installed, configure the client to the Chainstack-deployed Ghostnet 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
- Go to the faucet.
- In fund any address, paste your generated address.
- 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 Ghostnet.
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 Ghostnet.
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 Ghostnet.
- Install the Temple wallet.
- Go to the faucet.
- Connect with yout Temple wallet.
- Once connected, click Request 6001 tz.
Interact with the contract using the Temple wallet
- Navigate to the BCD explorer.
- Click Ghostnet.
- In the search field, provide your originated contract address.
- On the contract page, click Interact.
- Click withdraw > Execute > Temple - Tezos Wallet.
- 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
Updated 8 months ago