Creating Simple Smart Contracts

How to Create a Simple Smart Contract

Aman Agarwal
Enlear Academy

--

For this article, we will be using online IDE REMIX, which will be very helpful to compile and go through all functionality of a contract.

In the following contract, we will be looking at the admin and its role and a basic depositing, withdrawal function to deposit and withdraw from total balance, and a Balance function to check the balance.

So our contract looks like this:

Although it a self-explanatory, but will tear this apart (you can find about basics of smart contract with solidity in my previous article from here) and go through this and will see how to compile, check its functionality, and use it.

  • BalanceCheck is the name of the contract.
  • balance is a global variable that will be holding the total balance.
  • admin is another worldwide variable that will be holding the address of the admin who will be deploying this contract.
  • constructor executes at the very prior level and initializes all the values.
  • Balance function returns the current value to balance.
  • Deposite function updates the balance by the value sent along with the function call.
  • Withdrawl function does the basic work of withdrawing and reducing the balance by amount, but this power is only with the admin in our contract scenario.

Let’s move Ahead

Step 1

This is how our remix compiler will look alike after writing our contract.

Remix online compiler

Step 2

Select the latest compiler version and compile the contract.

Compiler from remix

Step 3

Select any one account to deploy it at duplicate EVM and deploy the contract for testing.

Yellow deploy button

Let’s see what we deployed and what we can do:

Functions available once you deploy successfully

Where we are till now

We can see the functions which are defined in our contracts.

Yellow color shows that these functions will create a transaction and will update the BlockChain. whereas Blue color shows that this will just show just information or some value and won’t create any transaction or blockchain.

Admin will be the account through which the contract is deployed, and you can also check the admin address through the admin button.

Testing Time

Note — Our every action is visible in logs displayed at the bottom, it also shows a green tick mark for successful execution and red cross for failure.

Case 1

Let’s first see our admin and balance

Admin will be the selected account through with contract is deployed and balance will 0 as we set in the constructor.

Case 2

Now it’s time to deposit some amount and increase the total balance.

As per after depositing 100 our total balance is 100.

Case 3

Now it’s time to change the account and withdraw 50 from a non-admin account.

Selecting any test account

As expected, it didn’t allow any non-admin account to withdraw.

Case 4

Withdraw from admin account and check updated balance.

The updated balance shows the left out amount and logs as wanted.

Conclusion

We witness a working smart contract with some basic and inside knowledge. we covered everything from creating to compiling to deploying a smart contract step by step. And the end, we also tested different cases and scenarios.

Furthur Scope

In my next article, we will be deploying contracts with golang and will maintain them with ganache, where we can also interact with them through our APIs.

You can support me and my content by buying a coffee ☕ here.

Follow me on Twitter and LinkedIn.

--

--