Author |
Topic: Smart Contract --I: Coding & Compilation via Remix |
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Smart Contract --I: Coding & Compilation via Remix |
Prerequisites: Remix IDE -- Direct access through browser via https://remix.ethereum.org/ Solidity -- One of the smart contract coding language
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Step 1. Create a new Solidity file |
Open Remix IDE via https://remix.ethereum.org/ Right-click on contracts and then click on New File Type file's name, for example, Account.sol
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Step 2. Code the contract |
Account.sol
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Account
* @dev Store & retrieve value in a variable
*/
contract Account {
uint256 balance;
/**
* @dev Deposit amount into account
* @param amount value to deposit
*/
function deposite(uint256 amount) public {
balance += amount;
}
/**
* @dev Withdraw amount from account
* @param amount value to withdraw
*/
function withdraw(uint256 amount) public {
balance += amount;
}
/**
* @dev Return balance
* @return balance value of 'number'
*/
function getBalance() public view returns (uint256){
return balance;
}
}
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Step 3. Compile the contract |
Click on the compiler icon on the very left panel Click the button Compile Account.sol Make sure the compiler icon is green checked
After successful compilation, a file named Account.json is generated under folder artifacts
Account.json
{
"deploy": {
},
"data": {
"bytecode": {
"generatedSources": [],
"linkReferences": {},
"object": "6080604...",
"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE ... ",
"sourceMap": "141:574:0:-:0;;;;;;;;;;;;;;;;;;;"
},
"deployedBytecode": {
"generatedSources": [
{ ... }
],
"immutableReferences": {},
"linkReferences": {},
"object": "60806040... ",
"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE ... ",
"sourceMap": "141:574:0:-:0; ... "
},
"gasEstimates": {
"creation": {
"codeDepositCost": "108600",
"executionCost": "153",
"totalCost": "108753"
},
"external": {
"deposite(uint256)": "infinite",
"getBalance()": "1115",
"withdraw(uint256)": "infinite"
}
},
"methodIdentifiers": {
"deposite(uint256)": "3104562b",
"getBalance()": "12065fe0",
"withdraw(uint256)": "2e1a7d4d"
}
},
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "deposite",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
}
It should be noted that two critical items abi and bytecode are listed, also present is gasEstimates.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Step 4. Save the contract |
As the Remix is an online IDE, it's important to save an off-line copy of your contact. The easy way is to backup the entire workspace into a zipped file which can be ported and restored to anywhere. Click on home icon Click the link Download all Files as backup zip
|
|
|
|
|
|
|
|