1

When using web3js, you will automatically have to make file to create an instance of that contract which you will use in your main project to interact with.

For example,

smartContract.js

import web3 from './web3';
/// web3 is a file which sets up the provider, such as detecting Metamask

const address = '0xabckasdhfhskdkjfsd239841eiw8912398321';

const abi = [
  {
    constant: true,
    inputs: [{ name: '', type: 'address' }],
    name: 'count',
    outputs: [{ name: '', type: 'uint256' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: true,
    inputs: [],
    name: 'getBalance',
    outputs: [{ name: '', type: 'uint256' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: true,
    inputs: [{ name: '', type: 'uint256' }],
    name: 'LastWinner',
    outputs: [{ name: '', type: 'address' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: true,
    inputs: [],
    name: 'manager',
    outputs: [{ name: '', type: 'address' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: false,
    inputs: [],
    name: 'transferRest',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    constant: true,
    inputs: [],
    name: 'getSpecialPlayers',
    outputs: [{ name: '', type: 'address[]' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: false,
    inputs: [{ name: 'anAddress', type: 'address' }],
    name: 'AssigningOwner',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    constant: true,
    inputs: [],
    name: 'pause',
    outputs: [{ name: '', type: 'bool' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: true,
    inputs: [{ name: '', type: 'uint256' }],
    name: 'specialPlayers',
    outputs: [{ name: '', type: 'address' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: false,
    inputs: [{ name: 'anInput', type: 'bool' }],
    name: 'setPause',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    constant: false,
    inputs: [{ name: 'theWinner', type: 'address' }],
    name: 'pickWinner',
    outputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'function'
  },
  {
    constant: true,
    inputs: [{ name: 'user', type: 'address' }],
    name: 'userCounter',
    outputs: [{ name: '', type: 'uint256' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: true,
    inputs: [],
    name: 'getWinners',
    outputs: [{ name: '', type: 'address[]' }],
    payable: false,
    stateMutability: 'view',
    type: 'function'
  },
  {
    constant: false,
    inputs: [],
    name: 'enter',
    outputs: [],
    payable: true,
    stateMutability: 'payable',
    type: 'function'
  },
  {
    inputs: [],
    payable: false,
    stateMutability: 'nonpayable',
    type: 'constructor'
  },
  { payable: true, stateMutability: 'payable', type: 'fallback' }
];
export default new web3.eth.Contract(abi, address);

MainApplication.js

import web3 from './web3';
import smartContract from './smartContract';
let balance = await smartContract.methods.getBalance().call();

While this code is working and I have nothing against it, I hate two things:

  • Every time I deploy a new contract I have to update the address in smartContract.js
  • Every time I deploy a new contract I have to update the abi in smartContract.js

This doesn't happen in production but when you're testing, you may be deploying improved versions of your smart contract (maybe you add a new feature). What solution, if available, would you recommend to pasting contract address and contract abi?

So far I only considered a solution: when deploying the smart contract with my own deploy script, call a function which writes a new file.js with the contract address and contract abi. Would like to know from your other options you're using.

1

If you are interest, you can learn how to use the Truffle framework for deployment.

What Truffle offers is:

  • init a new project (creates folder structure for Truffle's commands)
  • a command to compile all your contracts (inside the contracts folder of your project) which places the abis (json files) in a build folder
  • a command to migrate your contracts in a network, that can be local network (e.g. Ganache)/testnet/mainnet depending on the configuration file truffle-config.js (created during the first step). The migrate command follows a migration script inside the migration folder.
  • Mocha framework for testing
  • Javascript Console for testing

Basically, all the issues related to the project management and abi creation, are simplified with Truffle.

Hope it helps in the same way it helped me back then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.