We'll be looking at a quick guide to writing and deploying a smart contract to the blockchain network.
Let's begin by looking at a simple contract program that stores and retrieves the value of a variable on the blockchain.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
The first line of code identifies the license in use for the program, the second line is a directive that specifies the version of solidity compilers that should be used for the contract.
The uint (unsigned integer) variable storedData is declared. two functions *set *and *get *are created to alter and return the variable's value.
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
We'll be interacting with our Solidity project with the use of an online Solidity IDE called Remix
Once the contract has been compiled successfully you can deploy and interact with it on the Remix IDE. When the contract has been deployed, under Deployed contracts you will see the name of the contract and the two buttons set and get representing the two functions in the contract which can be used to modify and retrieve the value of the stored variable.
In real-time use cases, contracts are deployed to blockchain networks, let's deploy this contract to the Polygon network, Contracts on the blockchain require a gas fee(cost of making a blockchain transaction) but we'll be using a test network which will not require real money. For us to pay the gas fee we'll be using a cryptocurrency wallet like Metamask , download Metamask here though you can choose to use any other non-custodial wallet. We'll be using the Polygon Mumbai Test network to host our smart contract, To connect your MetaMask Wallet to the Polygon Mumbai Testnet, visit this website https://chainlist.org/ and search for polygon, you'll see the Mumbai option under the search results, go ahead and select it.
Once you have done that, you need to get some test MATICS to start with, you will get it from here. Input your wallet address and voila you've got some cash!
To deploy your contract to the testnet, go to the Remix IDE, and instead of the default JavaScript VM, select Injected Web3.
Your wallet will pop up and require permission, your contract is now deployed and hosted on the Polygon Mumbai Testnet. Congrats on the successful deployment of your smart contract!
Top comments (0)