Create and Test a Solidity Smart Contract with a Public Variable

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

The first step in this journey will be get your hands into a new language to harness the power of the blockchain. I'm talking about Solidity

**This isn't a Solidity focused course but we will review some concepts to get you up to speed with it.

The first thing to notice is that Solidity is an statically-typed language created specifically to create smart contracts for the Ethereum network.

An smart contract is like "real life" contract. Is a way to establish terms for an agreement. But the "smart" part it's because this type of contract is declared as machine code that is executed in a blockchain. This expand the idea of the original blockchain, Bitcon, aout sending and receiveing money without a centralized intermiediary to create trust and make possible to automate and dcentralize virtually any kinf of deal or transaction.

Since the contracts run in the blockchain (like Ethereum network) they offer high security, trust, inmmutability and reliability.

Since the blockchain is immutable you have to be care on what are you deploying. After deployed it cannot be change and you'll need to deploy a new contract.

For Solidity, a contract is a collection of code (a program) and data (the state) that lives inside the blockchain, it can be identify by its Ethereum address. You can leverage your previous knowledge and use some analogies to understand the smart contract.

You can think on the contract as your "backend" or "server side" program. This server expose an API and store some data (the state) into the a databse (the blockchain), but the database is not written nor query with a different language like SQL, you can imagine that Solidity is automatically connected to the database and everything you deeclare (like a variable) is stored in the database.

Instructor: [0:00] It's time to start writing Solidity. First, let's create a file for the Smart Contract. Name it as tipjar.soul. This is a Solidity file. The first thing you need to declare is the SPDX license identifier, and then choose any license. I chose GPL-3.. [0:25] Then, let's set the Solidity version using the pragma keyboard. Import a utility contract from Hardhat named console.soul. This contract allows you to print to the standard output using console.log, same as JavaScript.

[0:47] Let's create the contract by writing the contract keyword and the name of the contract. You'll notice that this is very similar to a class. Next, you'll create a public variable as integer named totalTips.

[1:06] Then a fundamental function, which is the constructor. This is an object-oriented programming language. You have constructors and the contract is like the class. Let's write the constructor, which will not accept any arguments. The only thing this constructor will do is console log that this is a smart contract.

[1:33] Let's write a little test for this. Testing smart contracts is one of the most important things you can do since the nature of blockchain is immutable, meaning that the smart contract after being deployed cannot be changed. Let's test this. Let's create a file under the test folder named tipjar.js.

[1:59] Let's require the expect library from Chai and the ethers library from Hardhat. This library will help you to interact with the smart contract. Let's describe this as the tip jar contract and write the first scenario for that.

[2:19] You will store the contract variable globally and then write the description as follows, should deploy the contract and return zero as total tips. This is an asynchronous function. The first step is to create a contract factory. This is done by using the Ethers ContractFactory function where you just pass the name of the contract that you want to deploy. In this case, tipjar is the name.

[2:50] Then, utilizing the ContractFactory, you create a contract instance, so await ContractFactory.deploy, and then we'll wait for the contract to be deployed. Now, the contract is deployed. You can expect these asynchronous functions.

[3:07] Write contract.totalTips to be equal to zero. This is what you want. If you're wondering where totalTips came from, you created a public variable earlier. Solidity will create a public getter to every public variable you make. This means it will create a public function named the same as your public variable.

[3:35] This function will return the value of that variable. In this case, it will be an unsigned integer. Now, it's time to run this test. Let's see what you have.

[3:48] Open your terminal. Be sure to cd into the tipjar folder, where the project is, and let's run hardhat test. As you can see, this fails because of some weirdness with how ES modules and CommonJS work together. What you need to do is to rename the Hardhat configuration file to be a CommonJS file.

[4:16] Then create some npm scripts inside your package.json file. To run Hardhat with this new configuration, let's make the first script to use it with the new configuration file. Then let's create one script to compile the contracts using hardhat compile. You'll do the same thing for running the test.

[4:43] Finally, a command that you'll use in the following lessons, hardhat deploy. This will just run Hardhat with a script named deploy to the local network. With this in place, it's time to run the test again.

[5:01] In your terminal run npm run hardhat test. The test will start running, compile the smart contract for you, and then check if it's passing or not. You have a green. Your smart contract is running flawlessly.

[5:18] For this lesson, you created your first contract. You made a public variable and a constructor, and you wrote your first JavaScript test using Hardhat as the development environment and ethers ES.