Di strukshure of smart kontracts
Last edit: @boluwatife_4523(opens in a new tab), 14 Jun 2024
One smart kontract na program wey dey run for one address on Ethereum. Dem make dem wit data and funshons wey fit exekute as dem dey risiv one transakshon. Hia na ovaview of wetin dey make up one smart kontract.
Prerequisites
Make sure sey yu don read about smart kontracts bifor. Dis dokument assume sey yu don already sabi di programming languajis such as JavaScript abi Python.
Data
Any kontract data suppose getone lokashon: e fit bi storaj
abi memory
. E dey kost money well-well to shanj storaj for smart kontract so yu nid to konsida wia yor data supose dey.
Storaj
Data wey go dey foreva na im dem dey koll storaj and na state variabols dey rep am. Dem stor dis values on di blockchain foreva. Yu nid tell dem di type so dat di kontract go fit sabi hau much storaj im nid on top blockchain wen im dey kompile.
1// Solidity example2contract SimpleStorage {3 uint storedData; // State variable4 // ...5}Kopy
1# Vyper example2storedData: int128Kopy
If u don already program languajis wey dey for object, yu go sabi plenti types of dem. But address
suppose dey new to yu if yu dey new to Ethereum divelopment.
One address
type fit hold one Ethereum address wey dey di same to 20 bytes abi 160 bits. Im dey riturn in hexadecimal notashon wit one 0x wey dey lead.
Oda types inklude:
- boolean
- integer
- fixed point numbas
- fixed-size byte arrays
- dynamically-sized byte arrays
- Rashonal and integer literals
- String literals
- Hexadecimal literals
- Enums
If yu wont more ekplanashon, make yu look di dokuments:
Memory
Values wey dem kip only for laiftaim wey kontract funshon exekushon dey koll memory variabols. Sinse dem nor store dem on blockchain kpatakpata, dem dey sheap wella to yus.
Make yu learn more about hau di EVM dey store data (Storaj, Memory, and di Stack) in di Solidity dokuments(opens in a new tab).
Environment variabols
In adishon to di variabols wey yu difine for yor kontract, some speshial global variabols dey. Dem dey first yus dem to provide info about di blockchain abi kurent transakshon.
Eksampols:
Prop | State variabol | Deskripshon |
---|---|---|
block.timestamp | uint256 | Blok epoch taimstamp wey dey nau |
msg.sender | address | Senda of di messaj (kurent koll) |
Funshons
To tok am for simpol tams, funshons fit get informashon abi set of informashon as inkomin transakshons dey respond.
Twi types of funshon kolls dey:
internal
– dis ones nor dey kreate EVM koll- Internal funshons and state variabols fit only hapun internaly (i.e. from inside di kurent kontract abi kontracts wey dey take from am)
external
– dis ones dey kreate EVM koll- Ekstanal funshons nor dey part of di kontract interface, wey mean sey dem fit koll dem from oda kontracts and thru transakshons. Dem nor fit koll ekstanal funshon for inside
f
(i.e.f()
nor dey work, butdis one.f()
dey work).
- Ekstanal funshons nor dey part of di kontract interface, wey mean sey dem fit koll dem from oda kontracts and thru transakshons. Dem nor fit koll ekstanal funshon for inside
Dem fit also bi publik
abi private
publik
funshons fit bi anybodi from inside di kontract abi for outside thru messajisprivate
funshons dey only show for di kontract wey dem yus difine dem and nor bi inside kontracts wey dem derive
Both funshons and state variabols fit bi publik abi private
Hia na funshon wey dey update one state variabol on one kontract:
1// Solidity example2function update_name(string value) public {3 dapp_name = value;4}Kopy
- Di parameter
value
of typestring
dey passed into di funshon:update_name
- Dem go make am dey
publik
, wey mean sey anybodi fit yus am - Dem nor koll am
view
, so dem fit adjust di kontract state
View funshons
Dis kain funshons dey promise sey dem nor go shanj di data wey dey di kontract. Eksampol wey kommon na "getter" funshons – yu fit yus dis risiv one user balans for eksampol.
1// Solidity example2function balanceOf(address _owner) public view returns (uint256 _balance) {3 return ownerPizzaCount[_owner];4}Kopy
1dappName: public(string)23@view4@public5def readName() -> string:6 return dappNameKopy
Wetin dem dey konsida state tu dey yus modify:
- To dey write to state variabols.
- To dey emit events(opens in a new tab).
- To dey kreate oda kontracts(opens in a new tab).
- To dey yus
selfdestruct
. - To dey send ether thru kolls.
- To dey koll any funshon wey dem nor mark
view
abipure
. - To dey yus low-level kolls.
- To dey yus inline assembly wey get satain opcodes.
Konstructor funshons
Dem dey run konstructor
funshons only one taim wendem first riliz di kontract. Laik konstructor
for many programming languajis wey dey yus klass, dis funshons dey start set state variabols to di values wey dem want.
1// Solidity example2// Initializes the contract's data, setting the `owner`3// to the address of the contract creator.4constructor() public {5 // All smart contracts rely on external transactions to trigger its functions.6 // `msg` is a global variable that includes relevant data on the given transaction,7 // such as the address of the sender and the ETH value included in the transaction.8 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/units-and-global-variables.html#block-and-transaction-properties9 owner = msg.sender;10}Show mi evrytinKopy
1# Vyper example23@external4def __init__(_beneficiary: address, _bidding_time: uint256):5 self.beneficiary = _beneficiary6 self.auctionStart = block.timestamp7 self.auctionEnd = self.auctionStart + _bidding_timeKopy
Funkshons wey dem build-in
In adishon to di variabols and funshons wey yu difine on yor kontract, some speshial built-in funshons dey. Di eksamol wey efrione sabi na:
address.send()
– Soliditysend(address)
– Vyper
Dis dey allow kontracts to send ETH to oda akants.
To dey write funshons
Yor funshon nid:
- Parameter variabol and type (if im dey asept parameters)
- diklarashon of internal/eksternal
- diklarashon of pure/view/payabol
- riturns type (if im dey riturn one value)
1pragma solidity >=0.4.0 <=0.6.0;23contract ExampleDapp {4 string dapp_name; // state variable56 // Called when the contract is deployed and initializes the value7 constructor() public {8 dapp_name = "My Example dapp";9 }1011 // Get Function12 function read_name() public view returns(string) {13 return dapp_name;14 }1516 // Set Function17 function update_name(string value) public {18 dapp_name = value;19 }20}Show mi evrytinKopy
One komplete kontract fit look laik dis. For hia di konstructor
funshon go first provide on value for di dapp_name
variabol.
Events and logs
Events dey enabol yor smart kontract to dey tok wit yor frontend abi oda aplikashons wey dey subskribe. Wons dem don validate one transakshon kon add am to one block, smart kontracts fit emit events and log informashon, wey di frontend fit dey process and dey yus.
Eksampol wey dem annotate
Dis na some eksampols wey dem write for Solidity. If yu go laik play wit di code, yu fit interact wit dem for Remix(opens in a new tab).
Hello world
1// Specifies the version of Solidity, using semantic versioning.2// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#pragma3pragma solidity ^0.5.10;45// Defines a contract named `HelloWorld`.6// A contract is a collection of functions and data (its state).7// Once deployed, a contract resides at a specific address on the Ethereum blockchain.8// Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html9contract HelloWorld {1011 // Declares a state variable `message` of type `string`.12 // State variables are variables whose values are permanently stored in contract storage.13 // The keyword `public` makes variables accessible from outside a contract14 // and creates a function that other contracts or clients can call to access the value.15 string public message;1617 // Similar to many class-based object-oriented languages, a constructor is18 // a special function that is only executed upon contract creation.19 // Constructors are used to initialize the contract's data.20 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constructors21 constructor(string memory initMessage) public {22 // Accepts a string argument `initMessage` and sets the value23 // into the contract's `message` storage variable).24 message = initMessage;25 }2627 // A public function that accepts a string argument28 // and updates the `message` storage variable.29 function update(string memory newMessage) public {30 message = newMessage;31 }32}Show mi evrytinKopy
Token
1pragma solidity ^0.5.10;23contract Token {4 // An `address` is comparable to an email address - it's used to identify an account on Ethereum.5 // Addresses can represent a smart contract or an external (user) accounts.6 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#address7 address public owner;89 // A `mapping` is essentially a hash table data structure.10 // This `mapping` assigns an unsigned integer (the token balance) to an address (the token holder).11 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#mapping-types12 mapping (address => uint) public balances;1314 // Events allow for logging of activity on the blockchain.15 // Ethereum clients can listen for events in order to react to contract state changes.16 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#events17 event Transfer(address from, address to, uint amount);1819 // Initializes the contract's data, setting the `owner`20 // to the address of the contract creator.21 constructor() public {22 // All smart contracts rely on external transactions to trigger its functions.23 // `msg` is a global variable that includes relevant data on the given transaction,24 // such as the address of the sender and the ETH value included in the transaction.25 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/units-and-global-variables.html#block-and-transaction-properties26 owner = msg.sender;27 }2829 // Creates an amount of new tokens and sends them to an address.30 function mint(address receiver, uint amount) public {31 // `require` is a control structure used to enforce certain conditions.32 // If a `require` statement evaluates to `false`, an exception is triggered,33 // which reverts all changes made to the state during the current call.34 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/control-structures.html#error-handling-assert-require-revert-and-exceptions3536 // Only the contract owner can call this function37 require(msg.sender == owner, "You are not the owner.");3839 // Enforces a maximum amount of tokens40 require(amount < 1e60, "Maximum issuance exceeded");4142 // Increases the balance of `receiver` by `amount`43 balances[receiver] += amount;44 }4546 // Sends an amount of existing tokens from any caller to an address.47 function transfer(address receiver, uint amount) public {48 // The sender must have enough tokens to send49 require(amount <= balances[msg.sender], "Insufficient balance.");5051 // Adjusts token balances of the two addresses52 balances[msg.sender] -= amount;53 balances[receiver] += amount;5455 // Emits the event defined earlier56 emit Transfer(msg.sender, receiver, amount);57 }58}Show mi evrytinKopy
Dijital asset wey unik
1pragma solidity ^0.5.10;23// Imports symbols from other files into the current contract.4// In this case, a series of helper contracts from OpenZeppelin.5// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#importing-other-source-files67import "../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol";8import "../node_modules/@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";9import "../node_modules/@openzeppelin/contracts/introspection/ERC165.sol";10import "../node_modules/@openzeppelin/contracts/math/SafeMath.sol";1112// The `is` keyword is used to inherit functions and keywords from external contracts.13// In this case, `CryptoPizza` inherits from the `IERC721` and `ERC165` contracts.14// Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#inheritance15contract CryptoPizza is IERC721, ERC165 {16 // Uses OpenZeppelin's SafeMath library to perform arithmetic operations safely.17 // Learn more: https://docs.openzeppelin.com/contracts/2.x/api/math#SafeMath18 using SafeMath for uint256;1920 // Constant state variables in Solidity are similar to other languages21 // but you must assign from an expression which is constant at compile time.22 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constant-state-variables23 uint256 constant dnaDigits = 10;24 uint256 constant dnaModulus = 10 ** dnaDigits;25 bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;2627 // Struct types let you define your own type28 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#structs29 struct Pizza {30 string name;31 uint256 dna;32 }3334 // Creates an empty array of Pizza structs35 Pizza[] public pizzas;3637 // Mapping from pizza ID to its owner's address38 mapping(uint256 => address) public pizzaToOwner;3940 // Mapping from owner's address to number of owned token41 mapping(address => uint256) public ownerPizzaCount;4243 // Mapping from token ID to approved address44 mapping(uint256 => address) pizzaApprovals;4546 // You can nest mappings, this example maps owner to operator approvals47 mapping(address => mapping(address => bool)) private operatorApprovals;4849 // Internal function to create a random Pizza from string (name) and DNA50 function _createPizza(string memory _name, uint256 _dna)51 // The `internal` keyword means this function is only visible52 // within this contract and contracts that derive this contract53 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#visibility-and-getters54 internal55 // `isUnique` is a function modifier that checks if the pizza already exists56 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html#function-modifiers57 isUnique(_name, _dna)58 {59 // Adds Pizza to array of Pizzas and get id60 uint256 id = SafeMath.sub(pizzas.push(Pizza(_name, _dna)), 1);6162 // Checks that Pizza owner is the same as current user63 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/control-structures.html#error-handling-assert-require-revert-and-exceptions6465 // note that address(0) is the zero address,66 // indicating that pizza[id] is not yet allocated to a particular user.6768 assert(pizzaToOwner[id] == address(0));6970 // Maps the Pizza to the owner71 pizzaToOwner[id] = msg.sender;72 ownerPizzaCount[msg.sender] = SafeMath.add(73 ownerPizzaCount[msg.sender],74 175 );76 }7778 // Creates a random Pizza from string (name)79 function createRandomPizza(string memory _name) public {80 uint256 randDna = generateRandomDna(_name, msg.sender);81 _createPizza(_name, randDna);82 }8384 // Generates random DNA from string (name) and address of the owner (creator)85 function generateRandomDna(string memory _str, address _owner)86 public87 // Functions marked as `pure` promise not to read from or modify the state88 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#pure-functions89 pure90 returns (uint256)91 {92 // Generates random uint from string (name) + address (owner)93 uint256 rand = uint256(keccak256(abi.encodePacked(_str))) +94 uint256(_owner);95 rand = rand % dnaModulus;96 return rand;97 }9899 // Returns array of Pizzas found by owner100 function getPizzasByOwner(address _owner)101 public102 // Functions marked as `view` promise not to modify state103 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#view-functions104 view105 returns (uint256[] memory)106 {107 // Uses the `memory` storage location to store values only for the108 // lifecycle of this function call.109 // Learn more: https://solidity.readthedocs.io/en/v0.5.10/introduction-to-smart-contracts.html#storage-memory-and-the-stack110 uint256[] memory result = new uint256[](ownerPizzaCount[_owner]);111 uint256 counter = 0;112 for (uint256 i = 0; i < pizzas.length; i++) {113 if (pizzaToOwner[i] == _owner) {114 result[counter] = i;115 counter++;116 }117 }118 return result;119 }120121 // Transfers Pizza and ownership to other address122 function transferFrom(address _from, address _to, uint256 _pizzaId) public {123 require(_from != address(0) && _to != address(0), "Invalid address.");124 require(_exists(_pizzaId), "Pizza does not exist.");125 require(_from != _to, "Cannot transfer to the same address.");126 require(_isApprovedOrOwner(msg.sender, _pizzaId), "Address is not approved.");127128 ownerPizzaCount[_to] = SafeMath.add(ownerPizzaCount[_to], 1);129 ownerPizzaCount[_from] = SafeMath.sub(ownerPizzaCount[_from], 1);130 pizzaToOwner[_pizzaId] = _to;131132 // Emits event defined in the imported IERC721 contract133 emit Transfer(_from, _to, _pizzaId);134 _clearApproval(_to, _pizzaId);135 }136137 /**138 * Safely transfers the ownership of a given token ID to another address139 * If the target address is a contract, it must implement `onERC721Received`,140 * which is called upon a safe transfer, and return the magic value141 * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`;142 * otherwise, the transfer is reverted.143 */144 function safeTransferFrom(address from, address to, uint256 pizzaId)145 public146 {147 // solium-disable-next-line arg-overflow148 this.safeTransferFrom(from, to, pizzaId, "");149 }150151 /**152 * Safely transfers the ownership of a given token ID to another address153 * If the target address is a contract, it must implement `onERC721Received`,154 * which is called upon a safe transfer, and return the magic value155 * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`;156 * otherwise, the transfer is reverted.157 */158 function safeTransferFrom(159 address from,160 address to,161 uint256 pizzaId,162 bytes memory _data163 ) public {164 this.transferFrom(from, to, pizzaId);165 require(_checkOnERC721Received(from, to, pizzaId, _data), "Must implement onERC721Received.");166 }167168 /**169 * Internal function to invoke `onERC721Received` on a target address170 * The call is not executed if the target address is not a contract171 */172 function _checkOnERC721Received(173 address from,174 address to,175 uint256 pizzaId,176 bytes memory _data177 ) internal returns (bool) {178 if (!isContract(to)) {179 return true;180 }181182 bytes4 retval = IERC721Receiver(to).onERC721Received(183 msg.sender,184 from,185 pizzaId,186 _data187 );188 return (retval == _ERC721_RECEIVED);189 }190191 // Burns a Pizza - destroys Token completely192 // The `external` function modifier means this function is193 // part of the contract interface and other contracts can call it194 function burn(uint256 _pizzaId) external {195 require(msg.sender != address(0), "Invalid address.");196 require(_exists(_pizzaId), "Pizza does not exist.");197 require(_isApprovedOrOwner(msg.sender, _pizzaId), "Address is not approved.");198199 ownerPizzaCount[msg.sender] = SafeMath.sub(200 ownerPizzaCount[msg.sender],201 1202 );203 pizzaToOwner[_pizzaId] = address(0);204 }205206 // Returns count of Pizzas by address207 function balanceOf(address _owner) public view returns (uint256 _balance) {208 return ownerPizzaCount[_owner];209 }210211 // Returns owner of the Pizza found by id212 function ownerOf(uint256 _pizzaId) public view returns (address _owner) {213 address owner = pizzaToOwner[_pizzaId];214 require(owner != address(0), "Invalid Pizza ID.");215 return owner;216 }217218 // Approves other address to transfer ownership of Pizza219 function approve(address _to, uint256 _pizzaId) public {220 require(msg.sender == pizzaToOwner[_pizzaId], "Must be the Pizza owner.");221 pizzaApprovals[_pizzaId] = _to;222 emit Approval(msg.sender, _to, _pizzaId);223 }224225 // Returns approved address for specific Pizza226 function getApproved(uint256 _pizzaId)227 public228 view229 returns (address operator)230 {231 require(_exists(_pizzaId), "Pizza does not exist.");232 return pizzaApprovals[_pizzaId];233 }234235 /**236 * Private function to clear current approval of a given token ID237 * Reverts if the given address is not indeed the owner of the token238 */239 function _clearApproval(address owner, uint256 _pizzaId) private {240 require(pizzaToOwner[_pizzaId] == owner, "Must be pizza owner.");241 require(_exists(_pizzaId), "Pizza does not exist.");242 if (pizzaApprovals[_pizzaId] != address(0)) {243 pizzaApprovals[_pizzaId] = address(0);244 }245 }246247 /*248 * Sets or unsets the approval of a given operator249 * An operator is allowed to transfer all tokens of the sender on their behalf250 */251 function setApprovalForAll(address to, bool approved) public {252 require(to != msg.sender, "Cannot approve own address");253 operatorApprovals[msg.sender][to] = approved;254 emit ApprovalForAll(msg.sender, to, approved);255 }256257 // Tells whether an operator is approved by a given owner258 function isApprovedForAll(address owner, address operator)259 public260 view261 returns (bool)262 {263 return operatorApprovals[owner][operator];264 }265266 // Takes ownership of Pizza - only for approved users267 function takeOwnership(uint256 _pizzaId) public {268 require(_isApprovedOrOwner(msg.sender, _pizzaId), "Address is not approved.");269 address owner = this.ownerOf(_pizzaId);270 this.transferFrom(owner, msg.sender, _pizzaId);271 }272273 // Checks if Pizza exists274 function _exists(uint256 pizzaId) internal view returns (bool) {275 address owner = pizzaToOwner[pizzaId];276 return owner != address(0);277 }278279 // Checks if address is owner or is approved to transfer Pizza280 function _isApprovedOrOwner(address spender, uint256 pizzaId)281 internal282 view283 returns (bool)284 {285 address owner = pizzaToOwner[pizzaId];286 // Disable solium check because of287 // https://github.com/duaraghav8/Solium/issues/175288 // solium-disable-next-line operator-whitespace289 return (spender == owner ||290 this.getApproved(pizzaId) == spender ||291 this.isApprovedForAll(owner, spender));292 }293294 // Check if Pizza is unique and doesn't exist yet295 modifier isUnique(string memory _name, uint256 _dna) {296 bool result = true;297 for (uint256 i = 0; i < pizzas.length; i++) {298 if (299 keccak256(abi.encodePacked(pizzas[i].name)) ==300 keccak256(abi.encodePacked(_name)) &&301 pizzas[i].dna == _dna302 ) {303 result = false;304 }305 }306 require(result, "Pizza with such name already exists.");307 _;308 }309310 // Returns whether the target address is a contract311 function isContract(address account) internal view returns (bool) {312 uint256 size;313 // Currently there is no better way to check if there is a contract in an address314 // than to check the size of the code at that address.315 // See https://ethereum.stackexchange.com/a/14016/36603316 // for more details about how this works.317 // TODO Check this again before the Serenity release, because all addresses will be318 // contracts then.319 // solium-disable-next-line security/no-inline-assembly320 assembly {321 size := extcodesize(account)322 }323 return size > 0;324 }325}Show mi evrytinKopy
Further reading
Make yu shek out Solidity and Vyper dokumentashon for more komplete ovaview of smart kontracts:
Related topics
Related tutorials
- To dey downsize kontracts to fite di kontract size limit -- Some pratika tips to dey ridus di size of yor smart kontract.
- To dey login data from di smart kontracts wit events -- One introdukshon to smart kontract events and hau yu fit yus dem to log data.
- Interact wit oda kontracts from Solidity – Hau to dey show one smart kontract from kontract wey dey exist and interact wit am.