Skip to content

Commit

Permalink
Add Web3 RPC endpoint support to MPEContract
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Apr 27, 2024
1 parent 2076bd8 commit f0def70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/MPEContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
32 changes: 31 additions & 1 deletion packages/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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/];
Expand All @@ -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
Expand All @@ -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).
Expand All @@ -91,8 +119,10 @@ import { <Message> } from '<path_to_grpc_message_file>'
const client = sdk.createServiceClient("<org_id>", "<service_id>")

```

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(<ServiceName>.<MethodName>, <InvokeRpcOptions>);
```
Expand Down

0 comments on commit f0def70

Please sign in to comment.