Skip to content

airalab/robonomics-js

Repository files navigation

Robonomics-js

NPM Version Build Status

Simple Javascript library to work with Robonomcis on Ethereum data

Install

Installation is performed via npm. To install, run:

> npm install robonomics-js --save

Or with yarn. To install, run:

> yarn add robonomics-js

Dependencies

Use

Create and return an instance of Robonomics.

const options = {...};
const robonomics = new Robonomics(options);

options

This is a properties object:

options.web3

That's an instance of web3.js

// metamask
const options = {
  web3: new Web3(window.ethereum),
  ...
};

// infura
const options = {
  web3: new Web3(
    new Web3.providers.WebsocketProvider(
      "wss://mainnet.infura.io/ws/v3/0b2f2a5026264b57b6d698b480332e89"
    )
  ),
  ...
};

options.messageProvider

That's an instance of MessageProviderIpfs, which uses the node of js-ipfs with pubsub option enabled.

const ipfs = new Ipfs({
  repo: 'robonomics-example',
  relay: {
    enabled: true,
    hop: {
      enabled: true
    }
  },
  EXPERIMENTAL: {
    pubsub: true
  },
  config: {
    Addresses: {
      Swarm: [
        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
        '/dns4/1.wsstar.aira.life/tcp/443/wss/p2p-websocket-star/',
        '/dns4/2.wsstar.aira.life/tcp/443/wss/p2p-websocket-star/',
        '/dns4/3.wsstar.aira.life/tcp/443/wss/p2p-websocket-star/'
      ]
    },
    Bootstrap: [
      '/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
      '/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
      '/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
      '/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
      '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
      '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
      '/dns4/1.pubsub.aira.life/tcp/443/wss/ipfs/QmdfQmbmXt6sqjZyowxPUsmvBsgSGQjm4VXrV7WGy62dv8',
      '/dns4/2.pubsub.aira.life/tcp/443/wss/ipfs/QmPTFt7GJ2MfDuVYwJJTULr6EnsQtGVp8ahYn9NSyoxmd9',
      '/dns4/3.pubsub.aira.life/tcp/443/wss/ipfs/QmWZSKTEQQ985mnNzMqhGCrwQ1aTA6sxVsorsycQz9cQrw'
    ]
  }
})

const options = {
  messageProvider: new MessageProviderIpfs(ipfs),
  ...
};

options.account

This is an object of account's properties, the account to sing messages with. Pass either an account's address (it should be unlocked in prior), or teh account's private key (account's address will be derived out of the key).

isSignPrefix is responsible for adding a prefix to the message. Defaults to true.

const options = {
  account: {
    address: '0x0000000000000000000000000000000000000000',
    privateKey: '0x0000000000000000000000000000000000000000000000000000',
    isSignPrefix: true
  },
  ...
};

options.ens

This is a properties object of the ens contract. The parameter is optional. If necessary, set address of the contract (if using other than mainnet network). Suffix suffix for names, defaults to eth, for any sidechain it's sid. Version version of Robonomics network, defaults to the latest one.

const options = {
  ens: {
    address: '0x314159265dD8dbb310642f98f50C066173C1259b',
    suffix: 'eth',
    version: 5
  },
  ...
};

options.lighthouse

ENS lighthouse name, optional, defaults to airalab.lighthouse.5.robonomics.eth. It's possible to set the short name only: airalab.

const options = {
  lighthouse: 'airalab.lighthouse.5.robonomics.eth',
  ...
};

Wait for components initialization before use.

const options = {...};
const robonomics = new Robonomics(options);
robonomics.ready().then(() => {
  console.log('Robonomics instance ready')
})

API

Messages

Demand

Message format.

const demand = {
  // Required
  model: "QmSt69qQqGka1qwRRHbdmAWk4nCbsV1mqJwd8cWbEyhf1M", // model as an IPFS CID to a rosbag file
  objective: "QmSt69qQqGka1qwRRHbdmAWk4nCbsV1mqJwd8cWbEyhf2M", // task as an IPFS CID to a rosbag file
  token: robonomics.xrt.address, // payment token address
  cost: 1, // cost
  deadline: 9999999, // deadline block number

  // Optional
  lighthouse: "0x0000000000000000000000000000000000000000", // lighthouse address, defaults to the one set at initialization
  validator: "0x0000000000000000000000000000000000000000", // validator address in case proof of work needed
  validatorFee: 0, // validator fee
  nonce: 1 // nonce
};

robonomics.sendDemand

Sign and send the demand. Returns liability.

robonomics.sendDemand(demand).then(liability => {
  console.log(liability.address);
});

robonomics.onDemand

Listens to demand messages for a passed model. If set model to null, listens to all the demands on a lighthouse.

robonomics.onDemand(model, message => {
  console.log(message);
});

Offer

Message format.

const offer = {
  // Required
  model: "QmSt69qQqGka1qwRRHbdmAWk4nCbsV1mqJwd8cWbEyhf1M", // model as an IPFS CID to a rosbag file
  objective: "QmSt69qQqGka1qwRRHbdmAWk4nCbsV1mqJwd8cWbEyhf2M", // task as an IPFS CID to a rosbag file
  token: robonomics.xrt.address, // payment token address
  cost: 1, // cost
  deadline: 9999999, // deadline block number

  // Optional
  lighthouse: "0x0000000000000000000000000000000000000000", // lighthouse address, defaults to the one set at initialization
  lighthouseFee: 0, // lighthouse fee
  validator: "0x0000000000000000000000000000000000000000", // validator address in case proof of work needed
  nonce: 1 // nonce
};

robonomics.sendOffer

Sign and send the offer. Returns liability.

robonomics.sendOffer(offer).then(liability => {
  console.log(liability.address);
});

robonomics.onOffer

Listens to offer messages for a passed model. If set model to null, listens to all the offers on a lighthouse.

robonomics.onOffer(model, message => {
  console.log(message);
});

Result

Message format.

const result = {
  // Required
  liability: "0x0000000000000000000000000000000000000000", // liability contract address
  success: true, // job success flag
  result: "QmWXk8D1Fh5XFJvBodcWbwgyw9htjc6FJg8qi1YYEoPnrg" // Result as an IPFS CID to a rosbag file
};

robonomics.sendResult

Sign and send offer message.

robonomics.sendResult(result).then(() => {
  console.log("ok");
});

robonomics.onResult

Listens to result messages in the network. Result is not supposed as valid. Valid result is to be obtained from the liability.

robonomics.onResult(result => {
  console.log(result);
});

Contracts

Liability

liability.getInfo

Returns an object of contract properties.

liability.getInfo().then(data => {
  console.log(data);
  /*
  {
    model,
    objective,
    result,
    token,
    cost,
    lighthouseFee,
    validatorFee,
    demandHash,
    offerHash,
    promisor,
    promisee,
    lighthouse,
    validator,
    isSuccess,
    isFinalized
  }
  */
});

liability.onResult

Waits for the contract to complete, returns result.

liability.onResult().then(result => {
  console.log(result);
});

Lighthouse

robonomics.lighthouse.getInfo

Returns an object of contract properties.

robonomics.lighthouse.getInfo().then(data => {
  console.log(data);
  /*
  {
    minimalStake,
    timeoutInBlocks,
    keepAliveBlock,
    marker,
    quota
  }
  */
});

robonomics.lighthouse.getProviders

Returns a list of provides working on a lighthouse.

robonomics.lighthouse.getProviders().then(list => {
  console.log(list);
});
Create a new lighthouse
const minimalFreeze = 1000 // Wn
const timeout = 25 // blocks
const name = 'mylighthouse' // lighthouse name
robonomics.factory.methods.createLighthouse(minimalFreeze, timeout, name).send({ from: robonomics.account.address })
    .then((tx) => console.log(tx))robonomics.factory.onLighthouse((lighthouse) => {
    console.log(lighthouse.name)
})
Become a lighthouse provider

First, XRT tokens approve needed:

const name = "mylighthouse"; // lighthouse name
const stake = 1000; // Wn
robonomics.lighthouse.methods
  .refill(stake)
  .send({ from: robonomics.account.address })
  .then(tx => console.log(tx));

Token

robonomics.xrt.getInfo

Returns an object of contract properties.

robonomics.xrt.getInfo().then(data => {
  console.log(data);
  /*
  {
    name,
    totalSupply,
    decimals,
    symbol
  }
  */
});
Check balance
robonomics.xrt.methods
  .balanceOf(robonomics.account.address)
  .call()
  .then(balance => console.log(balance));
Check number of approved tokens for the factory by its address
robonomics.xrt.methods
  .allowance(robonomics.account.address, robonomics.factory.address)
  .call()
  .then(allowance => console.log(allowance));
Approve tokens for the lighthouse address
robonomics.xrt.methods
  .approve(robonomics.lighthouse.address, 100)
  .send({
    from: robonomics.account.address
  })
  .then(tx => console.log(tx));

Links

Edit Robonomics Vue Template