Tezos: Creating, Deploying, and Interacting with a Contract using smartPy
Tezos for future DeFi

Tezos: Creating, Deploying, and Interacting with a Contract using smartPy

By Siham lamssaoui | blockchain_news | 26 Nov 2020


Get familiar with Tezos key operations, including creating, testing, compiling, deploying, and interacting with smart contracts.

In this article, we show up how to create a simple decentralized application,deploy it and interact with the smart contract on Tezos blockchain.

Creating a Contract:

we are going to write our contract using the online editor for the sake of simplicity, https://smartpy.io/ide/, our smart contract will be developed on python then compiled to Michelson that can then be deployed at an address on Tezos blockchain, where its functionalities can be invoked.We start by defining entry point that refers selected method to interact with external calls.

We give an example of Alice for instance trying to send a deposit to Bob, allowing Bob to withdraw it.

The deposit operation will take the amount sent to the transaction, if the amount is 0, it should fail as shown in the code comments below.The transaction is valid only if the deposit is valid and the sender is Alice in this example.

                                                1164e294e8e2e4820a0cf9aa2e65ae17ba9fdd421a7c9a6e65f95cff627df767.png

The Withdraw operation takes an argument that represent the amount of Tezies to withdraw, in this example the transaction is valid only if the sender is Bob and the amount is less or equal to the deposit sent by Alice as shown in the code below:

                                                 8c2b1d2ca7a785c0eaf72b964dcf43dfdca5f9cf59522af1a95b847934b922fc.png

 

Testing the contract:

After the creation, we can proceed to test it part by part, always using the only editor we create an object of our already defined class and execute different scenarios as shown in the code below.

                                                  a7c199588133be72ac26b081bdaee7bfc6fcf28cd3eb2c393fc163491ed3cbef.png

After that we can see our contract behavior online by pressing run on the simulator.

Compiling the contract:

Now we are on the latest stage of compilation and deployment. After running the code a Michelson code is generated which we will deploy as shown in figure below.

                                                    9a00bfe7de03d0d30bd4e2b3903f1a1bcc67d114951ce4b39535632f3143c192.png

after that will lead you to a web page where you can set your node and network, I use carthagenet for test. Then follow instructions to set your private key and account by importing faucet from https://faucet.tzalpha.net/, then compute your private key, activate account, and finally reveale it on carthagenet. After deploying your contract you get your contract ID which a unique ID that you can use to interact or check your contract status.

                                                     44995f6f077217ab432405fbbf18c5ad940698efbe0c49bfeb4182c03b57bd9c.png

 

Summary:

Now that we are familiar with Tezos blockchain and how to create, compile and deploy Tezos smartPy smart contract, the next step will be to add some complexity to the contract in order to make it general and handle most of cases.

 

The code used is only for learning purpose not for production :) Good Luck.

How do you rate this article?


1

0

Siham lamssaoui
Siham lamssaoui

Blockchain Developer/ PhD student blockchain security and interoperability


blockchain_news
blockchain_news

Blockchain news

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.