Example Contract

This page describes how to create use the example contract that will create a tokens

Uploading a Contract

  1. Choose a .fst contract file from your computer

  2. Click Active Contracts tab (left panel)

  3. Click on the the Upload button (blue)

    1. Drag and drop the file to section provided or click the Upload button and choose a file.

    2. Choose a name that will get saved as a comment in your code - other nodes in the network will be able to read this name.

  4. Click the Enter Amount section and insert number of blocks you would like to activate contract for.

  5. Click the Activate button (blue).

  6. Click the Saved Contracts tab (left panel)

    1. You can now see the given contract name on the generic contract

Activate/Name The Contract

  1. Download the example contract f* file (.fst)

    1. Make sure to take out the .fst file from the zip

  2. Click Active Contracts tab (left panel)

  3. Click on the the Upload button (blue)

    1. Drag and drop the .fst file from above to section provided or click the Upload button and choose a file.

    2. Choose a name that will get saved as a comment in your code - other nodes in the network will be able to read this name.

  4. Click the Enter Amount section and insert number of blocks you would like to activate contract for.

  5. Click the Activate button (blue).

  6. Click the Saved Contracts tab (left panel)

    1. You can now see the given contract name on the generic contract

Saved Contracts

  1. Click on Saved Contracts tab (left panel).

    1. Note there is already a contract that has been pre-loaded for your convenience.

  2. Click Run.

  3. New page will open under the Run Contract panel

  4. Type buy in the Enter Command box

  5. Choose asset ZENP

    1. Tokens in portfolio are already hard-coded options.

  6. Enter amount of Tokens you would like to purchase (1 to 1 ratio)

  7. Click the Run button (blue)

  8. Click the Portfolio tab (left panel)

    1. You can now see your Zen Protocol and generic contract tokens in the portfolio

Run Contract

  1. Click Explore Contracts (left panel).

  2. Click the blue arrow (run) on the right side of the contract you would like to run.

  3. Click Enter Command and type buy orredeem.

  4. Choose asset (ZENP to buy and Token itself to redeem).

  5. Enter amount you like to send.

    1. You will receive a 1 to 100,000,000 ratio of Zen Tokens for Example Contract Tokens.

  6. Click the Run button (blue).

    1. Your ZENP total balance and Transactions page (left panel) should be updated accordingly.

Example Contract Written in ZF*

(* NAME_START:Example Token:NAME_END *)
open Zen.Types
open Zen.Base
open Zen.Cost
open Zen.Asset
open Zen.Data
module D = Zen.Dictionary
module W = Zen.Wallet
module RT = Zen.ResultT
module Tx = Zen.TxSkeleton
module C = Zen.Cost
module CR = Zen.ContractResult
let buy txSkeleton contractId returnAddress =
let! contractToken = Zen.Asset.getDefault contractId in
let! amount = Tx.getAvailableTokens zenAsset txSkeleton in
let! txSkeleton =
Tx.lockToContract zenAsset amount contractId txSkeleton
>>= Tx.mint amount contractToken
>>= Tx.lockToAddress contractToken amount returnAddress in
CR.ofTxSkel txSkeleton
let redeem txSkeleton contractId returnAddress wallet =
let! contractToken = Zen.Asset.getDefault contractId in
let! amount = Tx.getAvailableTokens contractToken txSkeleton in
let! txSkeleton =
Tx.destroy amount contractToken txSkeleton
>>= Tx.lockToAddress zenAsset amount returnAddress
>>= Tx.fromWallet zenAsset amount contractId wallet in
CR.ofOptionTxSkel "contract doesn't have enough zens tokens" txSkeleton
let main txSkeleton _ contractId command sender messageBody wallet state =
let! returnAddress =
messageBody >!= tryDict
>?= D.tryFind "returnAddress"
>?= tryLock
in
match returnAddress with
| Some returnAddress ->
if command = "redeem" then
redeem txSkeleton contractId returnAddress wallet
else if command = "" || command = "buy" then
buy txSkeleton contractId returnAddress
|> autoInc
else
RT.autoFailw "unsupported command"
| None ->
RT.autoFailw "returnAddress is required"
let cf _ _ _ _ _ wallet _ =
4 + 64 + 2 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 3)) + 25) + 31
|> C.ret #nat