Skip to main content

Originating smart contracts with Chinstrap

At this point, the smart contracts are compiled and ready to be originated. We are ready to originate or deploy our smart contracts on to the configured network. We will make use of Flextesa sandbox as a local node, to test our originations.

Preparing origination scriptsโ€‹

Originating a contract needs origination script. Origination script usually updates the storage structure or initial data and the smart contract code. These scripts are located in the orignations directory. This works almost the same way as Truffle.

Each origination is a Python file, defining the deployment tasks, It executes Python code. Each origination starts with a number, followed by a title. Chinstrap will run the migrations in an ascending order.

An origination script defines:

  • the initial storage of the smart contract(s)
  • the contract deployment steps: the order of deployment of smart contracts, networks, accounts

These origination scripts are used the same way whether you deploy your smart contract for the first time, or you update a new version of a smart contract.

In any origination script, the first step is to import getContract function from chinstrap.originations module.

from chinstrap.originations import getContract

Next, we define a function called deploy that takes three arguments:

  • chinstrapState
  • network
  • accounts
def deploy(chinstrapState, network, accounts):

Now, inside the deploy funtion, we have to specify which smart contract is to be originated.

contract = getContract("SampleContract")

We define the storage as a json, and encode it as contract storage using contract.storage.encode function.

initial_storage = contract.storage.encode(
{"counter": 0, "owner": accounts[0].key.public_key_hash()}
)

Finally, we return initial_storage, contract from the function.

return initial_storage, contract

Here is a origination script example, defining a storage with essential types:

from chinstrap.originations import getContract

def deploy(chinstrapState, network, accounts):
contract = getContract("SampleContract")
initial_storage = contract.storage.encode(
{"counter": 0, "owner": accounts[0].key.public_key_hash()}
)
return initial_storage, contract

Originatingโ€‹

Before proceeding with originations, we have to configure accounts for originating. We can use any of the accounts generated by chinstrap sandbox and configure the accounts inside chinstrap-config.yml. We use these accounts ONLY on development network.

โžœ cat .secret 
edsk3AiSAERPfe6yqS7Q4YAxBQ5L1NLUao2H9sP34x7u1tEkXB5bwX

Configuration file:

chinstrap:
network:
development:
host: http://localhost:12345
accounts:
- privateKeyFile: ./.secret

compiler:
lang: smartpy
test: smartpy

Finally we are ready to originate to development network. Chinstrap provides `originate* sub-command to originate contracts.

โžœ chinstrap originate -h

_ _ _
___| |__ (_)_ __ ___| |_ _ __ __ _ _ __
/ __| '_ \| | '_ \/ __| __| '__/ _` | '_ \
| (__| | | | | | | \__ \ |_| | | (_| | |_) |
\___|_| |_|_|_| |_|___/\__|_| \__,_| .__/
|_|

๐Ÿง Chinstrap - a cute framework for developing Tezos Smart Contracts!
usage: chinstrap originate [-h] [-o ORIGINATE] [-d NUMBER] [-n NETWORK] [-p PORT] [-r] [-c CONTRACT]
[-l] [-s] [-e ENTRYPOINT] [-f]

optional arguments:
-h, --help show this help message and exit
-o ORIGINATE, --originate ORIGINATE
Origination script to execute. If not specified, all the originations will be
executed
-d NUMBER, --number NUMBER
Run contracts from a specific migration. The number refers to the prefix of
the migration file.
-n NETWORK, --network NETWORK
Select the configured network
-p PORT, --port PORT RPC Port of Tezos local sandbox
-r, --reset Run all originations from the beginning, instead of running from the last
completed migration
-c CONTRACT, --contract CONTRACT
Contract to compile. If not specified, all the contracts are compiled
-l, --local Use compiler from host machine. If not specified, Docker image is used
-s, --show Show addresses of originations
-e ENTRYPOINT, --entrypoint ENTRYPOINT
Entrypoint to use when compiling Ligo contracts. Default entrypoint is main
-f, --force Force originate all originations. Be careful, this will re-originate all the
contracts even if they are already deployed.

To originate all the origination scripts, we jsut run:

โžœ chinstrap originate -n development

_ _ _
___| |__ (_)_ __ ___| |_ _ __ __ _ _ __
/ __| '_ \| | '_ \/ __| __| '__/ _` | '_ \
| (__| | | | | | | \__ \ |_| | | (_| | |_) |
\___|_| |_|_|_| |_|___/\__|_| \__,_| .__/
|_|

๐Ÿง Chinstrap - a cute framework for developing Tezos Smart Contracts!
Using development network
Loaded wallet tz1PiDHTNJXhqpkbRUYNZEzmePNd21WcB8yB . Balance: ๊œฉ 20000.022856

โœ” SampleContract.py compilation successful!
โœ” SampleContract's origination transaction at: ooKGSzixRJvMGFKgGptTzHGDmGeJ5sJYZ9LPfvL2eQy9iWSGxDd
โœ” Baking successful!
โœ” SampleContract address: KT1AetSAmZW4ctNF3nnjvhW5znjg4de3JCAx
โœ” Total Cost of originations: ๊œฉ 0.139272
info

On origination, Chinstrap calculates and keeps track of the sha256 hash of the compiled contracts. This helps in preventing duplicate originations. If you want to re-originate the same contract, you can force chinstrap to do so using the -f` or --force flag.