Skip to content
James Chong edited this page Mar 21, 2019 · 1 revision

Snet

This is the main class of singularity-js. It provides basic operations to access information and run job.

To initialize a Snet object.

Snet.init

Init supports two approaches. Private Key or Wallet.

  • Private Key approach. Initialize the Snet object with 2 fields, address and privateKey.
  • Wallet approach. Inject the ethereum object provided by plugin such as metamask.

To initialize an instance:

import {Snet} from 'singularity-js';
import Web3 from 'web3';

const web3Provider = 'wss://ropsten.infura.io/ws';
const web3 = new Web3(new Web3.providers.WebsocketProvider(web3Provider));

const ADDRESS = "ADDRESS_HERE";
const PRIVATE_KEY = "PRIVATE_KEY";

// for browser support
const ethereum = window.ethereum;

(async () => {
    // for node application
    const snet = await Snet.init(web3, {address: ADDRESS, privateKey: PRIVATE_KEY});

    // for browser application
    const snet = await Snet.init(web3, {ethereum: ethereum});
})();

Parameters

  1. web3 web3 object for ethereum.
  2. InitOptions InitOptions.
    • address: the address of the account used. Not required if browser approach is used.
    • privateKey: private key of the account used. Not required if browser approach is used.
    • ethereum: Ethereum object injected by wallet plugin such as metamask. Not required if private key approach is used.

Returns

  • Snet Snet object.

utils

Utility functions.

snet.utils

Parameters

none

Returns

  • Utils Utils utility functions. See Utils

account

Current Account

snet.account

Parameters

none

Returns

  • Account Account utility functions. See Account

listOrganizations

const organizations = snet.listOrganizations();
console.log(organizations.map(o => o.data));
// [ { id: 'snet' },
//   { id: 'SingularityLab' },
//   { id: 'DappTestOrganization' },
//   { id: 'DappTesOrganization' },
//   { id: 'holy-moly' }...]

const organizations = snet.listOrganizations({init:true});
console.log(organizations.map(o => o.data));
// [ { id: 'snet',
//     name: 'snet',
//     owner: '0xFF2a327ed1Ca40CE93F116C5d6646b56991c0ddE',
//     members:
//      [ '0x4b4546ce47089925E5792E0a6d085BfB876cE621',... ],
//     services: undefined },
//   { id: 'SingularityLab',
//     name: 'SingularityLab',
//     owner: '0x4acA1c99a999E87A7E22F71556003a2d434bf398', ...]
Parameters
  1. InitOptions (optional) for initialising a model.
    • init: default to false. Only id is retrieved.
Returns
  • OrganizationList Promise<Organization[]> List of organizations found in the blockchain.

getOrganization

const organizationDetail = snet.getOrganization('snet');
console.log(organizationDetail.data);
// { id: 'snet',
//   name: 'snet',
//   owner: '0xFF2a327ed1Ca40CE93F116C5d6646b56991c0ddE',
//   members:
//      [ '0x4b4546ce47089925E5792E0a6d085BfB876cE621',... ]}

const organization = snet.getOrganization('snet', {init:false});
console.log(organization.data);
// { id: 'snet'}
Parameters
  1. OrganizationId (string) Organization Id to retrieve.
  2. InitOptions (optional)
    • init: default to true. get the organization detail.
Returns

getService

const service = snet.getService('snet', 'example-service');
// { id: 'example-service',
//   organizationId: 'snet',
//   metadata:
//    { version: 1,
//      display_name: 'SingularityNET Example Service',
//      encoding: 'proto',
//      service_type: 'grpc', ... } },
//   tags: [ 'Service', 'Example', 'Arithmetic' ] }

const service = snet.getService('snet', 'example-service', {init:false});
// {id: 'example-service', organizationId: 'snet'}
Parameters
  1. OrganizationId organization Id.
  2. ServiceId service Id.
  3. InitOptions (optional) for initialising the organization.
    • init: default to true.
Returns

runJob

For running a job. Refer to service.runJob for more detail.

 const jobPromise = snet.runJob('snet', 'example-service', 'add', {a:3, b:4}, {autohandle_channel: true});

jobPromise.on('selected_channel', channel => console.log(channel.data));

jobPromise.then(console.log);
Parameters
  1. OrganizationId organization Id.
  2. ServiceId service Id.
  3. Method method.
  4. Request Request object.
  5. Options (optional) for initialising the organization.
Returns