Skip to content

Latest commit

 

History

History
256 lines (175 loc) · 7.09 KB

README.md

File metadata and controls

256 lines (175 loc) · 7.09 KB

iexec / Exports

< Back home

iExec SDK Library API

Build Status npm version npm version license

Use the iExec decentralised marketplace for off-chain computing in your dapp.

Content


Install

Install iexec sdk

npm install iexec

Quick start

Front-end integration

import { IExec } from 'iexec';

// connect injected provider
const iexec = new IExec({ ethProvider: window.ethereum });

NB: iexec SDK require some NodeJS modules to work, in the browser your bundler might on might not provides polyfills for these modules. If your bundler does not automatically includes NodeJS polyfills you must add them by yourself.

Webpack

webpack >= 5 no longer provides polyfills for NodeJS, you must include them in your configuration.

Here are the recommanded polyfills for the required NodeJS modudes:

  • crypto: fallback to crypto-browserify
  • stream: fallback to stream-browserify
  • constants: fallback to constants-browserify
  • Buffer: fallback to buffer

Create-react-app

create-react-app >= 5 which relies on webpack >= 5 requires to customize the configuration to include NodeJS polyfills.

Since react-scripts enforce the webpack configuration and ejecting is not an option, you will need to use react-app-rewired to override the configuration.

Here is the steps to follow:

  • Install the following dev-dependencies:
npm install --save-dev react-app-rewired crypto-browserify stream-browserify constants-browserify process
  • Create the following config-overrides.js at the root of your project:
const webpack = require('webpack');

module.exports = function override(config) {
  const fallback = config.resolve.fallback || {};
  Object.assign(fallback, {
    crypto: require.resolve('crypto-browserify'),
    stream: require.resolve('stream-browserify'),
    constants: require.resolve('constants-browserify'),
  });
  config.resolve.fallback = fallback;
  config.plugins = (config.plugins || []).concat([
    new webpack.ProvidePlugin({
      process: 'process/browser',
      Buffer: ['buffer', 'Buffer'],
    }),
  ]);
  return config;
};
  • Use react-app-rewired instead of react-scripts in your package.json scripts:
{
  ...
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  }
}

Back-end integration

const { IExec, utils } = require('iexec');

const { PRIVATE_KEY } = process.env;

const ethProvider = utils.getSignerFromPrivateKey(
  'http://localhost:8545', // blockchain node URL
  PRIVATE_KEY,
);
const iexec = new IExec({
  ethProvider,
});

API

IExecModules

IExec SDK is splitted into IExecModules, each providing a set of methods relatives to a specific field.

Additionaly the IExec module exposes all the following listed modules under the corresponding namespace.

Imports

As your app won't probably use all the features, you may want to import only the modules you need.

Each module is available as an independant package under iexec/MODULE_NAME and is exported in the umbrella package.

example:

  • import from module package
import IExecWalletModule from 'iexec/IExecWalletModule';
  • import from umbrella
import { IExecWalletModule } from 'iexec';

Usage

IExecModules are instancied with an IExecConfig providing the configuration to access to a specific instance of the iExec platform.

Once created, an IExecConfig can be shared with any IExecModule.

example:

  • standard usage
import IExecConfig from 'iexec/IExecConfig';

import IExecWalletModule from 'iexec/IExecWalletModule';
import IExecAccountModule from 'iexec/IExecAccountModule';

// create the config once for the target iExec instance
const config = new IExecConfig({ ethProvider: window.ethereum });

// share it with all the modules
const wallet = IExecWalletModule.fromConfig(config);
const account = IExecAccountModule.fromConfig(config);
  • reuse instancied module configuration
import IExecWalletModule from 'iexec/IExecWalletModule';
// some IExecModule instance
import iexecModule from './my-module';

// IExecModules expose their IExecConfig under config
const wallet = IExecWalletModule.fromConfig(iexecModule.config);
  • quick instanciation (shorter but not recommanded)
import IExecWalletModule from 'iexec/IExecWalletModule';

// the IExecConfig step can be skipped
const wallet = new IExecWalletModule({ ethProvider: window.ethereum });

utils

The utils namespace exposes some utility methods.

example:

import utils from 'iexec/utils';

Or

import { utils } from 'iexec';

errors

The errors namespace exposes the errors thrown by the library, use them if you want specific error handling.

example:

import errors from 'iexec/errors';

Or

import { errors } from 'iexec';

Live demos


< Back home