How to Deploy a Bank Smart Contract Using Solidity on Shardeum Testnet
Read our blog for a detailed step-by-step guide on how to deploy a bank smart contract on the EVM based Shardeum testnet 'Sphinx Dapp' using...
Read our blog for a detailed step-by-step guide on how to deploy a bank smart contract on the EVM based Shardeum testnet 'Sphinx Dapp' using...
For any EVM based blockchains, you can write smart contracts using a statically-typed language called Solidity, which is the most widely used smart contract programming language at the moment. Solidity is a full-featured language for writing general-purpose smart contracts, and it looks similar to JavaScript. Since there are blogs previously published about deploying smart contracts on Shardeum, let’s jump straight into how you can write and deploy a simple bank smart contract on Shardeum as lending and borrowing protocols, also known as DeFi protocols, are actively used by crypto investors/traders.
You need an environment to code, compile, test and deploy your smart contracts. Truffle is the most popular development framework for EVM based blockchains. But to keep this tutorial simple let’s use an online-based IDE called Remix. Remix IDE is a browser-based development environment for Smart Contracts. The IDE provides several plugins and compilers for different solidity versions.
To launch the Remix IDE, visit http://remix.ethereum.org
Next, let’s configure the compiler version by clicking the ‘Solidity Compiler’ button and selecting the compiler configuration shown in the image below.
Now let’s select Shardeum as the deployment environment.
Note: Shardeum Sphinx Dapp 1.X should be selected on the MetaMask in order to deploy to the Shardeum Chain.
Metamask allows users to access their EVM based blockchain wallets through a browser extension or mobile app, which can then be used to interact with decentralized applications (DApps). Click here to learn how you can add MetaMask to your browser.
You can now head over to dev.spriyo.xyz and click on the “Add to MetaMask” button, which will automatically add Shardeum Sphinx Dapp to your list of networks in MetaMask.
To make any kind of transaction/interaction, especially which modifies the data in the blockchain, we need the blockchain’s native currency, in our case it is called “Shard” with a ticker symbol “SHM”. To get free SHM, head over to https://docs.shardeum.org/faucet/claim and follow the instruction as is.
Let’s begin to code the smart contract. Note, banking contracts are used as an example here so you can find out what happens in the backend or at a code level when a user deposits and withdraws test tokens.
First, let’s create a Bank.sol file in the root directory.
This is our final code for our Bank DApp which we will divide into four parts for explanation purposes.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract Bank {
address public owner;
uint public balance;
constructor() {
owner = msg.sender;
}
function deposit() external payable {
balance += msg.value;
}
function withdraw(uint _amount) external {
require(balance >= _amount, "Insufficient balance!");
payable(owner).transfer(_amount);
balance -= _amount;
}
}
Part 1
`// SPDX-License-Identifier: GPL-3.0` — Is the Software Package Data Exchange® (SPDX®) identification used to specify the license under which the Solidity file will be distributed. pragma solidity 0.8.7, the 0.8.7 means the version that informs the compiler about the Solidity file’s compiler version.
Part 2
contract Bank is the beginning of the code, just like the Class keyword in the JS programming language. The “owner” and “balance” is a public storage variable, and the constructor is the initiator of the program which runs only once when initiated.
Part 3
The “deposit” and “withdraw” functions are just like any other functions in a programming language. The “external” keyword mentioned states the visibility of the function. More on that in the upcoming blogs so stay tuned 🙂 The keyword “payable” states that the function can accept native cryptocurrency, which will be added to the contract’s balance.
Part 4
When the function “deposit” is called, the cryptocurrency sent to it is added to the balance variable. And, when the function “withdraw” is called, which also takes a parameter, the first thing it does is to check whether the amount requested is not greater than the balance. Basically a user can’t withdraw more than he has deposited, right? This is done through “require”.
“require” takes 2 parameters, if the first parameter resolves to true it allows the code to flow and function accordingly. If the value returned is false, it throws an error and stops the code right there. In Solidity, there are three ways in which one can send cryptocurrency. Namely transfer(), send() and call(). Once the funds are transferred, we update the balance by subtracting the amount sent from the balance.
Switch to Shardeum Network on MetaMask.
Click on the “Deploy and Run” Button and select Environment to “Injected Web3”.
Make sure you have copy-pasted and saved the code. Once it is saved, ensure that there is a green tick mark above the “Solidity Compiler” button and the contract selected is Bank.sol. Once everything is done, click on the deploy button.
Metamask pops up with a confirm message. Click on the “Confirm” button. You should be able to see your contract/DApp in the ‘Deployed Contract Section’ which is below the Deploy Button. Click and expand it to see the functions and variables which we declared in the file.
Finally, we have deployed our contract to the Shardeum Network (Testnet which is known as Shardeum Sphinx Dapp 1.X)!!
Let’s interact with the DApp by depositing some Shardeum to the bank. Set the Value to 1 and Select “Ether” from the dropdown. And click on the ‘Deposit’ function button in our Contract. MetaMask will pop up with a confirmation message. You just have to click on ‘Confirm’ Button. Proceed to check your balance by clicking the balance button.
You can play around with the app by interacting with the functions.
Congrats for making it to the end of this article!
We will soon show you how to connect DApp to a frontend application which we are actually building on Shardeum Sphnix Dapp (testnet) as we speak since the network promises estimated gas fees of $0.01 with linear scalability. So if you are an Ethereum developer, note Shardeum is EVM based smart contract platform that can scale up to 1 million TPS with true decentralization and solid security. Yes Shardeum will be the first layer 1 blockchain to solve ‘scalability trilemma’ as it aims to onboard billions of daily users to Web 3 in the future.
You can also check out Shardeum Foundation’s previous blogs on how to deploy NFT smart contracts and how to mint your own ERC 20 cryptocurrency on Shardeum testnet.
What is a Governance Token | Benefits of Blockchain | What is Blockchain Security | Can Blockchain be Hacked | What is Crypto Metaverse | How to Keep Crypto Safe | Bitcoin VS Ethereum | What is a Crypto Whale | What is Staking in Crypto | Ethereum that are Compatible with the EVM | Why to Invest in DeFi Coins and Token | Mobile App Technology Stack | How to Buy Real Estate in the Metaverse | Blockchain Scalability Solutions | Public Blockchain Examples | Top Altcoins | What is Proof of Work in Blockchain | Crypto Cloud Mining | Best Place to Mint NFT | What is Stake in Crypto
Opinions expressed/content in this publication are those of the author(s). They do not necessarily purport to reflect the opinions or views of Shardeum Foundation.
About the Author : Nethaji V is a junior blockchain? developer at spriyo.xyz ?, a cross-chain NFT marketplace?, which is currently in its alpha development. You can check out the product at dev.spriyo.xyz. Follow him on Twitter and LinkedIn