Interacting with contracts without type safety is tedious at best, and dangerous at worst. web3x-codegen generates typings for contract ABIs either local, or remote from a simple configuration file called contracts.json.
For the first it uses etherscan to download the contract ABI and initialisation code at the given address, and generates the interface at ./src/contracts/DaiContract.ts.
For the second it specifies a truffle build output and generates its interface at ./src/contracts/MyTruffleContract.ts.
For the third it reads a raw ABI file and compiled initialisation code from local files, and generates its interface at ./src/contracts/MyRawAbiContract.ts. The initDataFile property is optional but you won't be able to easily deploy the contract without it.
For an example of the code generated, take a look at this example.
Using generated contracts
The following code demonstrates how to use the generated contract class. It's a similar API as used in web3.js, only now with type safety.
console.log(`Balance of 0 address DAI: ${fromWei(daiBalance,'ether')}`);
}finally{
provider.disconnect();
}
}
main().catch(console.error);
Deploying contracts is trivial as well, as the bytecode is imported by web3x-codegen and included as part of the contract class.
The following code deploys an exact replica of the DAI contract on mainnet, only now you can mint your own funds.