diff --git a/packages/core/src/MPEContract.js b/packages/core/src/MPEContract.js index bf74f92..967784c 100644 --- a/packages/core/src/MPEContract.js +++ b/packages/core/src/MPEContract.js @@ -2,7 +2,7 @@ import MPEAbi from 'singularitynet-platform-contracts/abi/MultiPartyEscrow'; import MPENetworks from 'singularitynet-platform-contracts/networks/MultiPartyEscrow'; import { BigNumber } from 'bignumber.js'; import { map } from 'lodash'; - +import Web3 from 'web3'; import PaymentChannel from './PaymentChannel'; import logger from './utils/logger'; import { toBNString } from './utils/bignumber_helper'; @@ -12,9 +12,10 @@ class MPEContract { * @param {Web3} web3 * @param {number} networkId */ - constructor(web3, networkId) { + constructor(web3, networkId, rpcEndpoint) { this._web3 = web3; this._networkId = networkId; + this.rpcEndpoint = rpcEndpoint; this._contract = new this._web3.eth.Contract(MPEAbi, MPENetworks[networkId].address); } @@ -207,6 +208,11 @@ class MPEContract { */ async getPastOpenChannels(account, service, startingBlockNumber) { const fromBlock = startingBlockNumber || await this._deploymentBlockNumber(); + let contract = this._contract; + if(this.rpcEndpoint) { + const _web3 = new Web3(this.rpcEndpoint); + contract = new _web3.eth.Contract(MPEAbi, MPENetworks[this._networkId].address); + } logger.debug(`Fetching all payment channel open events starting at block: ${fromBlock}`, { tags: ['MPE'] }); const address = await account.getAddress(); @@ -221,7 +227,7 @@ class MPEContract { fromBlock, toBlock: 'latest', }; - const channelsOpened = await this.contract.getPastEvents('ChannelOpen', options); + const channelsOpened = await contract.getPastEvents('ChannelOpen', options); return map(channelsOpened, (channelOpenEvent) => { const { channelId } = channelOpenEvent.returnValues; return new PaymentChannel(channelId, this._web3, account, service, this); diff --git a/packages/core/src/sdk.js b/packages/core/src/sdk.js index 2162c94..17c53da 100644 --- a/packages/core/src/sdk.js +++ b/packages/core/src/sdk.js @@ -30,7 +30,9 @@ class SnetSDK { this._networkId = config.networkId; this._web3 = new Web3(config.web3Provider, null, options); const identity = this._createIdentity(); - this._mpeContract = new MPEContract(this._web3, this._networkId); + // 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); } diff --git a/packages/web/README.md b/packages/web/README.md index 3d54ca3..9ca3425 100644 --- a/packages/web/README.md +++ b/packages/web/README.md @@ -7,22 +7,31 @@ SingularityNET SDK for Browser (Web) ## Getting Started These instructions are for the development and use of the SingularityNET SDK for JavaScript on web platform like browsers. + ### Installation + ```bash npm install snet-sdk-web ``` +**Note:** This SDK requires Node.js version 18 or higher and `react-scripts` version 5.0.1 or higher for optimal functionality and compatibility. + If you are using `create-react-app` then require Node.js polyfills for browser compatibility, To add these polyfills, you can use the `config-overrides.js` file with `react-app-rewired`. This approach allows you to customize the Webpack configuration without ejecting from `create-react-app`. Install **react-app-rewired** into your application + ```bash npm install --save-dev react-app-rewired ``` + Install the necessary polyfill packages + ```bash npm install --save-dev buffer process os-browserify url ``` + Create **config-overrides.js** in the root of your project with the content: + ```javascript const webpack = require("webpack"); @@ -36,7 +45,7 @@ module.exports = function override(config) { config.plugins = (config.plugins || []).concat([ new webpack.ProvidePlugin({ process: "process/browser", - Buffer: ["buffer", "Buffer"] + Buffer: ["buffer", "Buffer"], }), ]); config.ignoreWarnings = [/Failed to parse source map/]; @@ -51,6 +60,7 @@ module.exports = function override(config) { return config; }; ``` + Update your **package.json** scripts to use **react-app-rewired** instead of **react-scripts**. ```json @@ -76,6 +86,24 @@ import config from "./config"; const sdk = new SnetSDK(config); ``` +You can find a sample config below + +```json +{ + "web3Provider": window.web3.currentProvider, + "networkId": "3", + "ipfsEndpoint": "http://ipfs.organization.io:80", + "defaultGasPrice": "4700000", + "defaultGasLimit": "210000", + "rpcEndpoint": "https://ropsten.infura.io/v3/1234567890" +} + +``` + +**Note:** `rpcEndpoint` is optional, you should provide this if you are getting block size limit exceeded error. This is usually happens when you are using any web social auth providers. + +**Debugging Tip:** To view debug logs, enable verbose mode in your browser's developer console. + Now, the instance of the sdk can be used to instantiate clients for SingularityNET services. To interact with those services, the sdk needs to be supplied with the compiled gRPC client libraries. This SDK uses [gRPC-web](https://github.com/improbable-eng/grpc-web) by improbable engineering. To generate the gRPC client libraries, follow the instructions given by the `gRPC-web` package [here](https://github.com/improbable-eng/grpc-web/tree/master/client/grpc-web). @@ -91,8 +119,10 @@ import { } from '' const client = sdk.createServiceClient("", "") ``` + This generates a service client which can be used to make gRPC calls to the desired service. You can then invoke service specific calls as follows + ```javascript client.invoke(., ); ```