API Reference

Creating a Loader

Before contracts can be loaded, a loader object must be configured.

function setupLoader({ provider, defaultSender, defaultGas, defaultGasPrice }: {
    provider: any;
    defaultSender: string?;
    defaultGas: number?;
});

provider

The web3 provider to use when connecting to the blockchain.

defaultSender

The address to send transactions from when no explicit from field is provided. Optional, no default value.

defaultGas

How much gas to allocate for transactions when no explicit gas field is provided. Optional, defaults to 8 million.

Using the Loader

setupLoader returns an object with a key for each contract type (web3 or truffle).

Despite these contract abstractions being different, each specialized loader exposes the same Loader interface:

interface Loader {
    fromArtifact(name: string): any;
    fromABI(abi: object, bytecode?: string): any;
};

function setupLoader(options) : {
    web3: Loader;
    truffle: Loader;
};

fromArtifact

function fromArtifact(name: string, address?: string): any;

Loads a contract abstraction object from artifacts built by the compiler.

Receives the contract’s name and looks for its corresponding .json artifact file in the build/contracts directory.

fromABI

function fromABI(abi: object, bytecode?: string): any;

Loads a contract abstraction object from the provided ABI.

To deploy new contract instances, bytecode must be provided.

The .json artifact files are quite large: use fromABI to minimize how much data your application needs to bundle, or when you only need to interact with existing contracts, but not deploy new ones yourself.

Installing Extra Dependencies

Contract Loader needs to know how to create web3 and Truffle contracts, but it doesn’t force a specific version of either on you.

Instead, it is up to the caller to make sure the required dependencies are available. To load web3 contracts, you may need to install web3-eth-contract, while you may require @truffle/contract for Truffle contracts.