Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  1. STEP 5

46: ABI Decoded

An Application Binary Interface (ABI) is a set of rules that defines how two separate pieces of software communicate with each other. It's a set of functions, data types, and protocols that allow two separate pieces of software to interact. In the context of Solidity, ABIs are used to define how smart contracts interact with the Ethereum blockchain.

  • Example 1: Storing Data Consider a simple example of a smart contract that stores a value. The contract looks like this:

contract StoreValue {  
  uint public value; 
  function StoreValue() {    value = 0;  } 
  function setValue(uint _value) {    value = _value;  }
}

The ABI for this contract looks like this:

[
   {
      "constant":false,
      "inputs":[
         {
            "name":"_value",
            "type":"uint256"
         }
      ],
      "name":"setValue",
      "outputs":[
         
      ],
      "payable":false,
      "stateMutability":"nonpayable",
      "type":"function"
   },
   {
      "constant":true,
      "inputs":[
         
      ],
      "name":"value",
      "outputs":[
         {
            "name":"",
            "type":"uint256"
         }
      ],
      "payable":false,
      "stateMutability":"view",
      "type":"function"
   }
]

The ABI defines the contract's functionality, including the data types and functions that can be used to interact with the contract. In this example, the ABI defines two functions: setValue and value. The setValue function takes a uint (an unsigned integer) as an input and sets the value of the contract to the input. The value function returns the current value of the contract.

  • Example 2: Transferring Ether Now consider a contract that allows users to transfer Ether between two accounts. The contract looks like this:

contract TransferEther {  
  address public from;  
  address public to; 
  function TransferEther(address _from, address _to) {    
    from = _from;    
    to = _to;  
  } 
  function transfer(uint amount) {    
    if (msg.value > amount) {      
      from.transfer(amount);      
      to.transfer(amount);    
    }  
  }
}

The ABI for this contract looks like this:

[
   {
      "constant":false,
      "inputs":[
         {
            "name":"amount",
            "type":"uint256"
         }
      ],
      "name":"transfer",
      "outputs":[
         
      ],
      "payable":false,
      "stateMutability":"nonpayable",
      "type":"function"
   },
   {
      "constant":true,
      "inputs":[
         
      ],
      "name":"from",
      "outputs":[
         {
            "name":"",
            "type":"address"
         }
      ],
      "payable":false,
      "stateMutability":"view",
      "type":"function"
   },
   {
      "constant":true,
      "inputs":[
         
      ],
      "name":"to",
      "outputs":[
         {
            "name":"",
            "type":"address"
         }
      ],
      "payable":false,
      "stateMutability":"view",
      "type":"function"
   }
]

The ABI defines two functions: transfer and from/to. The transfer function takes a uint (an unsigned integer) as an input and transfers the specified amount of Ether from the from address to the to address. The from and to functions return the from and to addresses respectively.

That's it for the lesson 46! In the next lesson, Keccak256

Previous45: ABI EncodedNext47: Keccak256

Last updated 1 year ago