Smart Contract
Smart Contract
Smart contracts are self-executing lines of code that allow for parties to transact with each other without the need for any central authority. Within a smart contract is a list of defined operations that are executed when certain conditions, on or off-chain are met. Some operations could be to transfer funds to a certain address, communicate with another contract or even create a new contract.
More reading:
Smart contracts | ethereum.org
Ethereum Virtual Machine.
The Ethereum Virtual Machine (EVM) makes it easy for developers to create dapps and for the network to execute them. The purpose of the EVM is twofold:
- Allow developers to deploy smart contracts to the blockchain
- Instruct miners on how to execute EVM smart contract code embedded in the software that they run
- Stack, Memory, Storage, Env Variables, Logs, Sub-Calling
- Accepted program lang, Serpent, Solidity, Serpent, compile to EVM Code
- Function calls get comiled to transaction data using ABI
- First 4 bytes: function ID
- Next 32 nytes: first argument
- Next 32 bytes: second argument.
- Reverse Length Prefix encoding
Authoring Smart Contract
- Devcan use few languages depending on network used, most common language is Solidity.
- To interact with smart contract, we need a wallet, the most popular one is Metamask which is a browser extension. This wallet stores private keys locally.
- Before deploying to main network, test first to the testnet. Ethereum testnet including : Ropsten, Rinkeby, Kovan, Gorli.
Deploying smart contract
- After dev has written a smart contract, they can publish to mainnet. Ethereum network can use Remix Tool. Which is a cloud based IDE for Ethereum smart contract.
- After clicking deploy, remix send transaction data to Metamask which will ask for authorization.
Interacting with smart contract
-
To read, just ping the network. To write, must send transaction to the contract address.
-
All instruction with a smart contract uses application binary interface (ABI).
Read
-
Let’s read data in previous smart contract. , you should see something like this:
This figure shows the three read functions that the Guestbook smart contract has. The first function requires an input to return data, and the other two don’t.
Write
Let’s write some data, based on smart contract, it will look like this.
The MetaMask browser extension will provide you with the choice to connect to the website or not. After connecting to the website, you can start writing data to the contract. Notice that two things happen when you click Confirm:
- Etherscan generates a new transaction, populating it with the correct data, and pushes it to your MetaMask wallet.
- MetaMask then asks for authorization to send that transaction.
- After you click Confirm, your transaction gets pushed to the Ethereum network
Executing smart contract
As part of block discovery, Ethereum miners add transactions to blocks in much the same fashion as Bitcoin miners. There are two main actions a transaction can trigger:
- Payment : Send ETH from A to B
- Execution: Execute the smart contract. If these two are true, then miner will execute smart contract → receiving address is smart contract, the data payload contains data.
Gas and Pricing
- Gas is a unit of account used in the Ethereum ecosystem to calculate how much ether miners are paid to process transactions
- Miners executes opcodes, instructions at machine level. Each opcodes has a gas price:
Interacting with code
- Using web3.js
Prerequisites
- NodeJS and node package manager installed
- Open Terminal (Powershell on Windows)
- Directory with installed web3 via npm
- Ganache open
Steps
-
Write some test contract.
this smart contract have public unsigned integer variabel called myUint, and taht variable can be changed using setUint function, as in this code:
pragma solidity ^0.5.14; contract SomeContract { uint public myUint = 10; function setUint(uint _myUint) public { myUint = _myUint; } }
-
Deploy it to Ganache using Remix IDE
-
Start NodeJS environment by typing “node”
-
In command line, import web 3:
const Web3 = require('web3'); //attention CAPITAL Web3 const web3 = new Web3(new Web3.providers.HttpProvider('[http://localhost:7545](http://localhost:7545/)')); //ATTENTION THE PORT! web3.eth.call({from: 'ACCOUNT_IN_GANACHE', to:'SMART_CONTRACT_ADDRESS', data:'0x06540f7e'}).then(console.log); // Data is the hash of the function signature // SMART_CONTRACT_ADDRESS can be obtained in REMIX IDE.
-
If everything works as expected it should print “10” in hex format padded to 256 bit.
-
We know that the data field is the identifier of function. It is the first 4 bytes of keccak has of the function signature. In other words: “bytes4(keccack256(‘myUint()’))”
-
Whenever we interact with our smart contract from outside, we need to know the function names to generate these signatrues as data-fields. This is where ABI array comes-in. The ABI is a json encoded array with all information needed to encode the data field the right way
-
And enter the following code in node:
const myContract = new web3.eth.Contract(PASTE_ABI_ARRAY_HERE, 'CONTRACT_ADDRESS');
-
Then simply call via a very declarative function name
myContract.methods.myUint().call().then(console.log).catch(console.error);
- Another way is to use Infura, which provide a third party REST API instead of interacting with blockchain network directly using ABI.
-
Smart Contract Interaction Pattern
Diamond pattern : upgradable, workaround for size limit (ethereum has 24KB limit)
No Comments