This note is a part of my Zettelkasten. What is below might not be complete or accurate. It
is also likely to change often.
23rd July, 2020
Smart Contract
Smart contracts are:
- Accounts on the Ethereum Network
- Address of a smart contract is determined at the time of its creation using the creators address and the number of transactions sent from that address or "nonce"
- Address of a user account is determined from the public key
- Both are treated equally by the EVM
- They can hold a balance in Ether
- Limited data storage
- Stored in a map of 256-bit words to 256-bit words
- Data can be updated for a cost (since work needs to be done to update them)
- Immutable
- No way of editing their code - create a new one to change
- They have code instead of a Private Key
- Object Oriented Code
- Having all OOP characteristics like inheritance
- DAPPs are one or more smart contracts working together
- activated when the contract account receives a message; it can then perform transactions.
Transactions
Smart Contracts can execute transactions
- Each transaction is a message
- with a target account (which can be same as the sender or empty)
- can include a payload of binary data
- can include ether
- If the target account is a Smart Contract, the payload is used as input data for the smart contract
- If the target account is empty or null, the transaction creates a new smart contract with the address as described above
- Payload is taken as EVM Bytecode and executed, the output of this is stored as the code of the contract
Smart contracts can create new smart contracts too (but using a function instead of sending a message to an empty address.
Gas
Smart contracts need Ether to execute.
- This is to ensure that miners are paid for their work and noone exploits the distributed processing system.
- For execution, the cost is calculated in terms of Gas as per set rules.
- Taking this Gas amount and multiplying it by a Gas price (in ether) will give us the ether needed to execute the code.
- Gas price is set by the sender but needs to be accepted by a miner.
- Higher the gas price, the more likely the transaction will be picked up by a miner and processed
- To execute a transaction, the limit you're willing to spend on it will be sent alongside as ether. The unused gas will be transferred back to your account.
Destroy
- Contracts can self-distruct
- Remaining ether will be sent to designated target
- Storage and code is removed from the state
- Once destroyed, any ether sent to that address is lost
- Contracts can deactivate
- This ensures any ether sent to their address is returned