DEV Community

Cover image for COMPILING THE SMART CONTRACTS
Tanisk Annpurna
Tanisk Annpurna

Posted on

COMPILING THE SMART CONTRACTS

You can read more about ABI and BYTECODE here πŸ‘‡

'understanding-smart-contract-bytecode-and-abi'

LETS START.....

πŸ’œ Recap

  • In the previous article we learnt that EVM requires ABI and BYTECODE to understand any smart contract.
  • ABI gives the power to interact with Smart Contract using JavaScript
  • BYTECODE gives commands for EVM on how to execute the Smart Contract.

if you want to understand in more depth, Please read my last article.

πŸ’œ Converting to ABI and BYTECODE

  • There are many framework through which we can convert Smart Contract into ABI and BYTECODE. Among them we will look into few of them.
  • Before that we will need smart contract to convert. So, Copy the below code into VS code or any IDE as per your preference and follow along.

SMART CONTRACT

  • Create a file named "SimpleSmartContract.sol".
  • .sol is an extension used for solidity files.
  • This Smart Contract is very simple, It updates a number, gets the number and multiply it with a number.
//SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract SimpleSmartContract {
    uint256 public number;

    function getNumber() public view returns (uint256) {
        return number;
    }

    function updateNumber(uint128 _newNumber) public {
        number = _newNumber;
    }

    function multiplyNumber(uint8 _multiple) public {
        number = number * _multiple;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Remember, There are many extensions for Solidity in VS code. You can use anyone for proper intellisense and linting. Just search solidity in extension marketplace.

πŸ’œ SOLCJS

  • Its a framework written in typescript and can provide ABI and BYTECODE with single command.
  • we have to install it using npm or yarn and for npm we need 'Node.js'. So first install Node.js if not installed.
  • Open any terminal and change the directory to the location where smart contract is stored.
  • Using NPM
npm install solc
Enter fullscreen mode Exit fullscreen mode
  • or We can also use YARN
yarn add solc
Enter fullscreen mode Exit fullscreen mode
  • To check if solc is installed , write
solcjs --help
Enter fullscreen mode Exit fullscreen mode

if no error, then solc is installed else we need to install it again. Check location of terminal pointing to.

To compile , we use this command :

solcjs --bin --abi --include-path node_modules/ --base-path . SimpleSmartContract.sol
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ SimpleSmartContract.sol is the name of the smart contract. Replace it with the name of your own smart contract.

πŸ‘‰ --bin and --abi tells the solcjs to create .bin as well as .abi file. If you need anyone then simply remove the other one from the command.

πŸ’œ BYTECODE

608060405234801561001057600080fd5b506102dd806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063297c8d7a1461005157806347a9ee3f1461006d5780638381f58a14610089578063f2c9ecd8146100a7575b600080fd5b61006b60048036038101906100669190610161565b6100c5565b005b61008760048036038101906100829190610134565b6100df565b005b6100916100fb565b60405161009e919061019d565b60405180910390f35b6100af610101565b6040516100bc919061019d565b60405180910390f35b8060ff166000546100d691906101b8565b60008190555050565b806fffffffffffffffffffffffffffffffff1660008190555050565b60005481565b60008054905090565b60008135905061011981610279565b92915050565b60008135905061012e81610290565b92915050565b60006020828403121561014a57610149610274565b5b60006101588482850161010a565b91505092915050565b60006020828403121561017757610176610274565b5b60006101858482850161011f565b91505092915050565b6101978161022e565b82525050565b60006020820190506101b2600083018461018e565b92915050565b60006101c38261022e565b91506101ce8361022e565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561020757610206610245565b5b828202905092915050565b60006fffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600060ff82169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b61028281610212565b811461028d57600080fd5b50565b61029981610238565b81146102a457600080fd5b5056fea26469706673582212206ce7a9ae1c7536d9fe3d885a363d9f98f3dc8defb9d937459835ecac88104ca664736f6c63430008070033
Enter fullscreen mode Exit fullscreen mode

πŸ’œ ABI

[{"inputs":[],"name":"getNumber","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint8","name":"_multiple","type":"uint8"}],"name":"multiplyNumber","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint128","name":"_newNumber","type":"uint128"}],"name":"updateNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Remember if you want to read ABI, just change the extension from .abi to .json, The file will update and then change it back to .abi again.

  • You can read more about solcjs here πŸ‘‰ 'solcJs'

πŸ’œ REMIX IDE

  • Remix IDE allows developing, deploying and administering smart contracts for Ethereum like blockchains.
  • You can visit it here πŸ‘‰REMIX IDE
  • Create a new file and name it "SimpleSmartContract.sol" and paste the smart contract as shown.

Screenshot 2022-07-12 211011.png

πŸ‘‰ Make sure that compiler version is set to ^0.8.7. You can set it by clicking on Solidity Compiler button at most left panel. Select Compiler from dropdown as 0.8.7+commit.. In the Smart Contract we have defined compiler version, Its best to pick the same compiler version as in Smart Contract.

Screenshot 2022-07-12 211011.png

  • After pasting the code, we can hit compile button and check if any errors. If any error Please check that compiler version is correct.
  • Below that Solidity Compiler, We have Deploy & Run Transaction. Select it. Keep the option as it is.

Screenshot 2022-07-12 211011.png

  • We hit Deploy button.
  • We can see our smart contract deploy as shown below.

Screenshot 2022-07-12 211011.png

  • Now In the Compiler tab, we can see ABI & BYTECODE button. From there one can copy the ABI/BYTECODE and have a look at it.

Screenshot 2022-07-12 213845.png

These are the ways one can convert the Smart Contract to ABI and BYTECODE.

πŸ‘‰ Remember the BYTECODE and ABI will be same for same Smart Contract. It will never be different irrespective of the method of conversion. It will only change if Smart Contract changes.

In the next article, we will see basics of solidity and how to write smart contract and deploy it.

Hello, I am Tanisk Annpurna

I post about

πŸš€web3, Blockchain, Ethereum

🐦Smart Contract, Solidity

πŸŽ‰JavaScript, ReactJS, NodeJS

Follow and like for more such posts. !!✌️!!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)