-
Notifications
You must be signed in to change notification settings - Fork 0
Snet
James Chong edited this page Mar 21, 2019
·
1 revision
This is the main class of singularity-js. It provides basic operations to access information and run job.
To initialize a Snet
object.
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});
})();
- web3 web3 object for ethereum.
-
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.
- Snet Snet object.
Utility functions.
snet.utils
none
- Utils Utils utility functions. See Utils
Current Account
snet.account
none
- Account Account utility functions. See Account
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', ...]
-
InitOptions (optional) for initialising a model.
- init: default to false. Only id is retrieved.
- OrganizationList Promise<Organization[]> List of organizations found in the blockchain.
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'}
- OrganizationId (string) Organization Id to retrieve.
-
InitOptions (optional)
- init: default to true. get the organization detail.
- Organization Organization Object
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'}
- OrganizationId organization Id.
- ServiceId service Id.
-
InitOptions (optional) for initialising the organization.
- init: default to true.
- Service Service Object
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);
- OrganizationId organization Id.
- ServiceId service Id.
- Method method.
- Request Request object.
- Options (optional) for initialising the organization.
- PromiseEvent runJob Object