Getting Hands-on Tezster-CLI (Shopping Contract Example)

Pawan Dhanwani
Tezos India
Published in
5 min readJul 28, 2020

In this article, we are using Tezster-CLI version 0.2.7

Basic Setup

Installing Tezster-CLI

npm install -g tezster@latest --unsafe

Please use “sudo ”in front of this command if you are using OS other than Windows.

Confirming Installation

tezster --version

If this command yields a version as output you are good to go.

Setting up RPC node provider

RPC node is the node on testnet through which our requests are sent to the blockchain.

tezster set-rpc-node

Select the node to which you want to interact with.

In this article, we will be using a remote node. I recommend you check the current node by using “get-rpc-node” command before proceeding.

Account Operations

Adding a new account

Proceed to Tezos testnet faucet to get a file containing mnemonics which is in JSON format.

Store the JSON file somewhere on the system.

tezster add-testnet-account <account-label> <absolute-path-to-json-file>

Similarly, we can create multiple accounts using the same command. I have also created another account named “TestAccount”. I would recommend you to do the same to follow along.

Activating Account

After adding the account we need to activate it. Activating means we are originating account on the blockchain using mnemonics.

tezster activate-testnet-account <Account-Alias-Created>

List Accounts

tezster list-accounts

This commands outputs all the available accounts.

Transfer XTZ

This command allows you to transfer XTZ from one account to another.

tezster amount from-account to-account

Create a Wallet

This is an alternative to creating an account using faucet’s JSON and activating it, but you will not get any XTZ with that. Alternatively, you can transfer XTZ from other accounts to wallet account. Please save the mnemonic generated after this command for the future.

tezster create-wallet <Account-Alias>

Restore Wallet

You can restore an account if you have mnemonics of that account. If you don’t have a mnemonic and want to try this feature you can get a 24-word mnemonic from here.

tezster restore-wallet <Account-Alias> '<24 Words of mnemonic>'

Contract Operations

Deploying Contract

Here is the shopping contract which we will be deploying and interacting with.

import smartpy as spclass Shopping(sp.Contract):
def __init__(self,params):
self.init(orders = sp.map(tkey = sp.TInt , tvalue = sp.TRecord(units = sp.TInt, contactPerson = sp.TString , deliveryAddress = sp.TString , fulfilled = sp.TBool , item=sp.TString)), counter = sp.int(0),admin=params)

def incrementCounter(self):
self.data.counter += 1

@sp.entry_point
def purchase(self,params):
self.incrementCounter()
orderData = sp.record(units = params.units , contactPerson = params.contactPerson , deliveryAddress = params.deliveryAddress , item = params.item , fulfilled = False)

self.data.orders[self.data.counter] = orderData

@sp.entry_point
def closeOrder(self,params):
sp.verify(sp.sender == self.data.admin)
self.data.orders[params.orderID].fulfilled = True

@add_test(name="ShoppingContractTest")
def test():

shopping = Shopping(sp.address("tz1WgZqDQcYBeG35GupoNgGAqexBaFJLPJut"))
scenario = sp.test_scenario()
scenario += shopping
scenario += shopping.purchase(units = 3 , contactPerson = "Pawan" , deliveryAddress="301 ABCD Street XYZ City" , item = "Mango")
scenario += shopping.closeOrder(orderID = 1).run(sender = sp.address("tz1WgZqDQcYBeG35GupoNgGAqexBaFJLPJut"))

Please copy this code and open the Smartpy editor to generate Michelson and storage.

Store the copied Michelson code in a file with (.tz) extension. Something like “ShoppingContract.tz”. Store the copied Initial Storage somewhere because we will use it at the time of deploying.

Now let's deploy the contract using Tezster CLI

tezster deploy

This will ask you the following things -

Contract Label: Just a name to be used as an alias to contract address for easy operation.

Absolute Path: Path to “ShoppingContract.tz” file

Initial Storage: Initial storage copied from Smartpy editor.

Label/pkh : You can use a stored account or provide a public key hash of an existing account. Here I have used “MyAccount” we created few steps earlier.

Amount: If you want to provide some XTZ to contract then you can provide it here.

Origination Fee: Origination fee to be paid to create a contract on network.

Storage Limit: For each byte data to stored in the contract we need to pay 1000 Mutez. Depending upon your requirements you can set up the storage limits and pay the price accordingly. As this is a tutorial I will proceed with defaults. You can read more about this here.

Gas Limit: The maximum amount of gas unit(s) you want to allow, after exhaustion of provided gas units the node will stop the origination operation.

Call / Invoke Contract

Before we call the contract we need to first have a look at its entrypoint(s) and understand parameters and structure perfectly.

tezster list-entry-points ShoppingContract

If you have a look at “purchase” entrypoint thoroughly. It provides the parameters and structure. Now in order to generate a valid Michelson replace the values of parameters with “$PARAM” in structure.

Here is the deduced Michelson

(Right (Pair (Pair “Pawan” “301 ABCD Street XYZ City”) (Pair “Mango” 3)))

tezster call

Provide the generated Michelson in argument value.

Get Storage

Now let's confirm that the contract call was successful

tezster get-storage <Contract-Name>

You can see that your data is stored in a JSON format.

That is all for this article, we have covered the most important commands and operation you can use the help command to have a look at other available commands.

tezster --help

Tezster is a great tool that allows you to interact with blockchain from your command line itself. You can explore more from their documentation.

Moreover, you could also use Bundle-react (the equivalent of Truffle-box) with Tezster-CLI to write SmartPy code locally, compiling, testing, and deploying it. A short video tutorial series for Bundle-react could be found here.

Follow us on Twitter or join our Telegram group for more updates.

--

--