humanwhoexplores / smart-contracts

this repo contains my workbook for practicing smart contracts
0 stars 0 forks source link

Reverse engineer the code : how we deploy the code on a Test Network #14

Closed humanwhoexplores closed 3 years ago

humanwhoexplores commented 3 years ago

Complete Code :

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile')

let accounts;
let inbox;
const INITIAL_MESSAGE =  'Inbox contract';

beforeEach(async () => {
    // beforeEach runs before running each test
    // Get a list of all accounts
    accounts = await web3.eth.getAccounts();

    inbox = await new web3.eth.Contract(JSON.parse(interface))
        .deploy({data : bytecode, arguments: ['Inbox contract']})
        .send({from: accounts[0], gas: '1000000'})

    console.log("the account from which i am deploying is " , accounts[0]);
    let balance = await web3.eth.getBalance(accounts[0]);
    console.log("The balance is ", balance);   
});
humanwhoexplores commented 3 years ago

let's understand it piece - by -piece const ganache = require('ganache-cli'); const Web3 = require('web3'); const web3 = new Web3(ganache.provider()); const { interface, bytecode } = require('../compile')

i create a object of 'ganache' I create a object of web3 : While creating this i refer "Ganache Provider".
if you want to read a doc on how we declare web3 Object : - https://web3js.readthedocs.io/en/v1.2.11/web3.html#web3-instance

humanwhoexplores commented 3 years ago

inbox = await new web3.eth.Contract(JSON.parse(interface)) .deploy({data : bytecode, arguments: ['Inbox contract']}) .send({from: accounts[0], gas: '1000000'})

This is the one line that deploys the contract on Test Network. The attributes being used are :-

  1. bytecode
  2. account from which to deploy the contract
  3. Gas i am willing to pay
humanwhoexplores commented 3 years ago

Basically there are 2 different entities

  1. Network on which the contract is deployed too : this could be Ganache (local test n/w) , Rinkeby. Kovan , Mainnet etc.
  2. web3 Object: this is the portal to all things that occur in Ethereum World.

To link Deployment N/w and Web3 we need a bridge ; this bridge is the provider.

humanwhoexplores commented 3 years ago

![Uploading Screenshot 2021-09-25 at 1.21.47 PM.png…]()

humanwhoexplores commented 3 years ago

![Uploading Screenshot 2021-09-25 at 1.35.59 PM.png…]()