Adapters

What are Adapters?

Adapters represent the colonyJS solution for the many different ways to access the Ethereum blockchain provided by various JavaScript libraries. Adapters provide a wrapper for such libraries that enforces a standard and predictable API for use with Colony's contracts.

Initializing an adapter

First, make sure you understand how to use Loaders.

Adapters require the following parameters:

{
  loader, // Instance of a Loader
  provider, // Interface to Ethereum, e.g. for sending/receiving transactions
  wallet, // Interface to an Ethereum wallet, e.g. for signing transactions
}

An example of initializing an adapter might look like this:

import EthersAdapter from '@colony/colony-js-adapter-ethers';
import { EtherscanLoader } from '@colony/colony-js-contract-loader-http';
import { providers, Wallet } from 'ethers';

// Create a provider, wallet and loader
const provider = providers.getDefaultProvider();
const privateKey = '0x...';
const wallet = new Wallet(privateKey);
const loader = new EtherscanLoader();

// Create the adapter
const adapter = new EthersAdapter({
    loader,
    provider,
    wallet,
});

Using an adapter

Adapters can be used directly:

const contractDefinition = await adapter.load({ contractName: 'Token' });

But we will probably be using them to create an instance of ColonyNetworkClient:

import ColonyNetworkClient from '@colony/colony-js-client';

(async () => {

  const networkClient = new ColonyNetworkClient({ adapter });
  await networkClient.init(); // This will use the adapter to find and load the necessary contract

})();

Officially supported adapters