Skip to content

iExecBlockchainComputing/iexec-server-js-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iexec-server-js-client

Build Status npm version license

JS client lib to interact with iExec server REST API

Ressources

Examples

Below are examples showcasing the use of the library in the most common worklow:

1. Create iExec client

iExec server URL:

  • tesnet (ropsten / rinkeby / kovan): https://testxw.iex.ec:443
  • mainnet: https://mainxw.iex.ec:443
const createIEXECClient = require('iexec-server-js-client');
const iexec = createIEXECClient({ server: 'https://testxw.iex.ec:443' });

2. Auth

Authenticate before hitting iExec API:

iexec.auth(web3.currentProvider, accountAddress).then(({ jwtoken, cookie }) => {
  console.log(jwtoken); // this is given by auth.iex.ec server
  console.log(cookie); // this is given by iExec server
  // hit iExec server API
  iexec.getAppByName(deployTxHash).then(console.log); // print app description from deploy txHash
  iexec.getWorkByExternalID(submitTxHash).then(console.log); // print work description from submit txHash
});

If you already have your JWT token, no need to do full auth (avoid wallet signing):

iexec.getCookieByJWT('my_jwt_token').then(cookie => {
  // hit iExec server API
  iexec.getByUID(workUID).then(console.log); // print work description
});

3. Submit a work

Call the dapp smart contract "iexecSubmit" method to submit a work (for reference only, not part of this repo library):

const oracleJSON = require('iexec-oracle-contract/build/contracts/IexecOracle.json');
const work = '{"cmdline":"10"}'

const oracleContract = web3.eth
  .contract(oracleJSON.abi)
  .at(oracleJSON.networks[chainID].address);
const callbackPrice = await oracleContract.callbackPrice();

const dappContract = web3.eth
  .contract(dappSubmitABI)
  .at(dappAddress);

// this is the work submit
const txHash = await dappContract.iexecSubmit(work, {
  value: callbackPrice[0].toNumber(),
});

4. Wait for work result

After submitting a work through Ethereum, use the transaction hash (txHash) to wait for the work result:

iexec
  .waitForWorkResult(oracleContract.getWork, txHash)
  .then(workResultURI => iexec.createDownloadURI(workResultURI))
  .then(console.log); // let user open this URL in the browser to download the work result