Building your first DApp on Tezos: Part 1 — Writing Smart Contract on SmartPy
A blockchain is an immutable, shared digital ledger that records transactions in a peer-to-peer network. It is witnessing great demand in many areas including Banking, Healthcare, and public administration.
Tezos is a decentralized, open-source blockchain network that can execute peer-to-peer transactions and serve as a platform for deploying smart contracts.
In this tutorial, we’ll be creating a DApp for posting blogs anonymously and also getting public support in the form of anonymous donations which will be secured on Tezos Blockchain.
The problem project addresses
Media is the fourth pillar of democracy. But many people believe media is controlled by the government which is suppressing their Freedom of speech.
The solution to the problem
With that keeping in mind, I thought of building a blog site where anyone can share their views anonymously and the writers can also get support in the form of anonymous donations. This will ensure no central authority controls the content of the articles.
Implementation
SmartPy is a python library for constructing Tezos Smart Contracts. SmartPy IDE has many tools which make the development and deployment of smart contracts very easy.
Quick Start
- Let's Hop over to SmartPy. In the top bar go to Online Editor. This opens up SmartPy Editor.

- In the pop-up above go to Regular Templates and from Simple Examples select Store Value and Import this contract.

Writing Smart Contract
- For using the features of the SmartPy library, we first have to import it.
- After that, we need to create a class where we’ll be writing all our functions that need to be executed. Like in the example above there is a class
StoreValue
where we have defined all the entry points. StoreValue
class needs to be inherited fromsp.Contract
class.__init__
function is the constructor ofStoreValue
class in which we are initializing thestoredValue
variable.- The next step is to create entry points for specific functions. An entry point is a method that can be called from outside. It needs to be marked with the
@sp.entrypoint
decorator. - Inside the
replace
function we have params which is an object for accessing all the parameters passed to the function and can be accessed asparams.value
for accessing thevalue
parameter. - Like most languages, SmartPy also has expressions. For example
self.data.x
represents storage fieldx
. - In the example above
replace
function is replacingstoredValue
by the value passed by the user usingself.data.storedValue = params.value
. - Now, for testing our smart contract we will write tests as given below.
Test and Scenario
c1
is an object ofStoreValue
class.scenario
describe a sequence of actions: originating contracts, computing expressions, calling entry points, etc.sp.test_scenario()
is calling for testing action on our Smart Contract.scenario.h1("Store Value")
is a heading for our test output similar to an HTML heading.scenario += c1
displays the initial state of c1.scenario += c1.replace(value = 15)
displays the state of c1 after calling the entry pointreplace
.
Running and Testing our Smart Contract
Run the code to view the result of tests on the right side.

Deploying Smart Contract
- On the right side click on Deploy Michelson Contract.

- Select Edonet as the Node.
- Select SmartPy wallet.

- Select the faucet and go to the provided link and generate your own faucet account.
- Copy and paste your faucet info here.
- Close the pop-up and then Activate the account and then Reveal it.
- Go down and click on Estimate Cost from RPC.
- Finally, click on Deploy Contract.
Woohoo! we are done with creating and deploying our Smart Contract successfully. Now, you can go to the Explore Contract page to interact with the contract.
What’s Next
In the upcoming part, we’ll build a more advanced Smart Contract and then integrate it with the front-end.