Skip to content

Commit

Permalink
Merge pull request #154 from singnet/development
Browse files Browse the repository at this point in the history
Changed default link to ipfs
  • Loading branch information
MarinaFedy authored Nov 19, 2024
2 parents 569a070 + 1c1dfe1 commit eeb7460
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ PRIVATE_KEY=
SIGNER_PRIVATE_KEY=
NETWORK_ID=3
PROVIDER_HOST=https://goerli.infura.io/v3/xxxxxx
IPFS_ENDPOINT=http://ipfs.singularitynet.io:80
IPFS_ENDPOINT=https://ipfs.singularitynet.io:443
DEFAULT_GAS_PRICE=4700000
DEFAULT_GAS_LIMIT=210000
158 changes: 90 additions & 68 deletions packages/core/src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,112 @@ import IPFSMetadataProvider from './IPFSMetadataProvider';
import { DefaultPaymentStrategy } from '../../payment_strategies';

const DEFAULT_CONFIG = {
defaultGasLimit: 210000,
defaultGasPrice: 4700000,
ipfsEndpoint: 'http://ipfs.singularitynet.io:80',
defaultGasLimit: 210000,
defaultGasPrice: 4700000,
ipfsEndpoint: 'https://ipfs.singularitynet.io:443',
};

class SnetSDK {
/**
* @param {Config} config
* @param {MetadataProvider} metadataProvider
*/
constructor(config, metadataProvider = undefined) {
this._config = {
...DEFAULT_CONFIG,
...config,
};
const options = {
defaultGas: this._config.defaultGasLimit,
defaultGasPrice: this._config.defaultGasPrice,
};
this._networkId = config.networkId;
this._web3 = new Web3(config.web3Provider, null, options);
const identity = this._createIdentity();
// Some RPCs have a block size limit of 5000, but for the getPastEvents/log function, we need a higher limit. So this parameter will be used in getPastOpenChannels function at packages/core/src/MPEContract.js
const rpcEndpoint = config.rpcEndpoint;
this._mpeContract = new MPEContract(this._web3, this._networkId, rpcEndpoint);
this._account = new Account(this._web3, this._networkId, this._mpeContract, identity);
this._metadataProvider = metadataProvider || new IPFSMetadataProvider(this._web3, this._networkId, this._config.ipfsEndpoint);
}

/**
* @type {Account}
*/
get account() {
return this._account;
}

/**
* @type {Web3}
*/
get web3() {
return this._web3;
}
/**
* @param {Config} config
* @param {MetadataProvider} metadataProvider
*/
constructor(config, metadataProvider = undefined) {
this._config = {
...DEFAULT_CONFIG,
...config,
};
const options = {
defaultGas: this._config.defaultGasLimit,
defaultGasPrice: this._config.defaultGasPrice,
};
this._networkId = config.networkId;
this._web3 = new Web3(config.web3Provider, null, options);
const identity = this._createIdentity();
// Some RPCs have a block size limit of 5000, but for the getPastEvents/log function, we need a higher limit. So this parameter will be used in getPastOpenChannels function at packages/core/src/MPEContract.js
const rpcEndpoint = config.rpcEndpoint;
this._mpeContract = new MPEContract(
this._web3,
this._networkId,
rpcEndpoint
);
this._account = new Account(
this._web3,
this._networkId,
this._mpeContract,
identity
);
this._metadataProvider =
metadataProvider ||
new IPFSMetadataProvider(
this._web3,
this._networkId,
this._config.ipfsEndpoint
);
}

set paymentChannelManagementStrategy(paymentChannelManagementStrategy) {
this._paymentChannelManagementStrategy = paymentChannelManagementStrategy;
}
/**
* @type {Account}
*/
get account() {
return this._account;
}

async _serviceGroup(serviceMetadata, orgId, serviceId, groupName = undefined) {
const group = this._findGroup(serviceMetadata.groups, groupName);
if(!group) {
const errorMessage = `Group[name: ${groupName}] not found for orgId: ${orgId} and serviceId: ${serviceId}`;
logger.error(errorMessage);
throw new Error(errorMessage);
/**
* @type {Web3}
*/
get web3() {
return this._web3;
}

return group;
}
set paymentChannelManagementStrategy(paymentChannelManagementStrategy) {
this._paymentChannelManagementStrategy =
paymentChannelManagementStrategy;
}

_findGroup(groups, groupName) {
if(!groupName) {
return first(groups);
async _serviceGroup(
serviceMetadata,
orgId,
serviceId,
groupName = undefined
) {
const group = this._findGroup(serviceMetadata.groups, groupName);
if (!group) {
const errorMessage = `Group[name: ${groupName}] not found for orgId: ${orgId} and serviceId: ${serviceId}`;
logger.error(errorMessage);
throw new Error(errorMessage);
}

return group;
}

return find(groups, ({ group_name }) => group_name === groupName);
}
_findGroup(groups, groupName) {
if (!groupName) {
return first(groups);
}

_constructStrategy(paymentChannelManagementStrategy, concurrentCalls = 1) {
if (paymentChannelManagementStrategy) {
return paymentChannelManagementStrategy;
return find(groups, ({ group_name }) => group_name === groupName);
}

if (this._paymentChannelManagementStrategy) {
return this._paymentChannelManagementStrategy;
}
_constructStrategy(paymentChannelManagementStrategy, concurrentCalls = 1) {
if (paymentChannelManagementStrategy) {
return paymentChannelManagementStrategy;
}

logger.debug('PaymentChannelManagementStrategy not provided, using DefaultPaymentChannelManagementStrategy');
// return new DefaultPaymentChannelManagementStrategy(this);
return new DefaultPaymentStrategy(concurrentCalls)
}
if (this._paymentChannelManagementStrategy) {
return this._paymentChannelManagementStrategy;
}

logger.debug(
'PaymentChannelManagementStrategy not provided, using DefaultPaymentChannelManagementStrategy'
);
// return new DefaultPaymentChannelManagementStrategy(this);
return new DefaultPaymentStrategy(concurrentCalls);
}

_createIdentity() {
logger.error('_createIdentity must be implemented in the sub classes');
}
_createIdentity() {
logger.error('_createIdentity must be implemented in the sub classes');
}
}

export default SnetSDK;
2 changes: 1 addition & 1 deletion packages/core/src/types/snet_type_defs.jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @property {string} privateKey - The accounts private key for performing token operations.
* @property {string} signerPrivateKey - The accounts private key for signing grpc calls to service.
* @property {number} networkId - Ethereum network ID.
* @property {string} [ipfsEndpoint=http://ipfs.singularitynet.io:80] - A URL for fetching service related metadata.
* @property {string} [ipfsEndpoint=https://ipfs.singularitynet.io:443] - A URL for fetching service related metadata.
* @property {number} [defaultGasPrice=4700000] - The gas price to be used in case of fetching the gas price form the blockchain fails.
* @property {number} [defaultGasLimit=210000] - The gas limit to be used in case of fetching the gas estimate from the blockchain fails.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snet-sdk-web",
"version": "4.0.3",
"version": "4.0.4",
"description": "SingularityNET SDK for Web",
"main": "./dist/index.js",
"files": [
Expand Down

0 comments on commit eeb7460

Please sign in to comment.