@zapper-fi/contract-generator
v0.3.2
Published
Generates bindings for smart contracts.
Downloads
13
Readme
contract-generator
Smart contracts code generator.
API usage
import { ContractGenerator, AbiSource } from 'contract-generator'
// We setup our generator with ABI sources.
const generator new ContractGenerator({
abiSources: {
[AbiSource.ETHERSCAN]: { apiKey: 'api-key' },
},
})
// Imports all specified contracts ABIs in the specified directory.
await generator.importAbis({
outputDir: './abis',
abis: [{
source: AbiSource.ETHERSCAN,
name: 'my contract',
address: '0x6D9893fa101CD2b1F8D1A12DE3189ff7b80FdC10',
}],
})
// Generates Typescript bindings for specified ABIs
// in the same directory.
await generator.generateTypes({ dir: './abis' })
// Generates a factory for the specified contracts. A factory
// creates contract objects. It sets the provider (web3) on requested
// contracts and casts them to their types.
await generator.generateFactory({ inputDir: './abis', outputDir: '.' })
Then, you can use the following to interact with the generated contracts.
Contract interaction
import { ContractFactory } from './contract-factory.ts'
import Web from 'web3'
const factory = new ContractFactory(new Web3(...))
const myContract = factory.myContract('0x6D9893fa101CD2b1F8D1A12DE3189ff7b80FdC10')
const response = await myContract.methods.myMethod().call()