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.