mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 12:26:19 +00:00
Setup config for paid RPC methods and funding amounts with upstream ETH server (#423)
* Add config option for paid RPC methods * Change rpcProviderNitroNode to nitro * Add config option for funding amounts * Handle review changes * Refactor config and add a method to setup provider with payments * Upgrade package versions --------- Co-authored-by: Shreerang Kale <shreerangkale@gmail.com>
This commit is contained in:
parent
e203154ac7
commit
ae60bbe1ce
@ -2,7 +2,7 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"command": {
|
||||
|
2
packages/cache/package.json
vendored
2
packages/cache/package.json
vendored
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/cache",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "Generic object cache",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/cli",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"scripts": {
|
||||
@ -12,13 +12,13 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.7.1",
|
||||
"@cerc-io/cache": "^0.2.61",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.61",
|
||||
"@cerc-io/cache": "^0.2.62",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.62",
|
||||
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
|
||||
"@cerc-io/nitro-node": "^0.1.11",
|
||||
"@cerc-io/peer": "^0.2.61",
|
||||
"@cerc-io/rpc-eth-client": "^0.2.61",
|
||||
"@cerc-io/util": "^0.2.61",
|
||||
"@cerc-io/peer": "^0.2.62",
|
||||
"@cerc-io/rpc-eth-client": "^0.2.62",
|
||||
"@cerc-io/util": "^0.2.62",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@graphql-tools/utils": "^9.1.1",
|
||||
"@ipld/dag-cbor": "^8.0.0",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/codegen",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "Code generator",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
@ -20,7 +20,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@cerc-io/util": "^0.2.61",
|
||||
"@cerc-io/util": "^0.2.62",
|
||||
"@graphql-tools/load-files": "^6.5.2",
|
||||
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
||||
"@solidity-parser/parser": "^0.13.2",
|
||||
|
@ -41,12 +41,12 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.61",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.61",
|
||||
"@cerc-io/solidity-mapper": "^0.2.61",
|
||||
"@cerc-io/util": "^0.2.61",
|
||||
"@cerc-io/cli": "^0.2.62",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.62",
|
||||
"@cerc-io/solidity-mapper": "^0.2.62",
|
||||
"@cerc-io/util": "^0.2.62",
|
||||
{{#if (subgraphPath)}}
|
||||
"@cerc-io/graph-node": "^0.2.61",
|
||||
"@cerc-io/graph-node": "^0.2.62",
|
||||
{{/if}}
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@cerc-io/graph-node",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"devDependencies": {
|
||||
"@cerc-io/solidity-mapper": "^0.2.61",
|
||||
"@cerc-io/solidity-mapper": "^0.2.62",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@graphprotocol/graph-ts": "^0.22.0",
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||
@ -51,9 +51,9 @@
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
||||
"@cerc-io/cache": "^0.2.61",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.61",
|
||||
"@cerc-io/util": "^0.2.61",
|
||||
"@cerc-io/cache": "^0.2.62",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.62",
|
||||
"@cerc-io/util": "^0.2.62",
|
||||
"@types/json-diff": "^0.5.2",
|
||||
"@types/yargs": "^17.0.0",
|
||||
"bn.js": "^4.11.9",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/ipld-eth-client",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "IPLD ETH Client",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
@ -20,7 +20,7 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.7.1",
|
||||
"@cerc-io/cache": "^0.2.61",
|
||||
"@cerc-io/cache": "^0.2.62",
|
||||
"cross-fetch": "^3.1.4",
|
||||
"debug": "^4.3.1",
|
||||
"ethers": "^5.4.4",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/peer",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "libp2p module",
|
||||
"main": "dist/index.js",
|
||||
"exports": "./dist/index.js",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/rpc-eth-client",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "RPC ETH Client",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
@ -19,9 +19,9 @@
|
||||
},
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@cerc-io/cache": "^0.2.61",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.61",
|
||||
"@cerc-io/util": "^0.2.61",
|
||||
"@cerc-io/cache": "^0.2.62",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.62",
|
||||
"@cerc-io/util": "^0.2.62",
|
||||
"chai": "^4.3.4",
|
||||
"ethers": "^5.4.4",
|
||||
"left-pad": "^1.3.0",
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@cerc-io/solidity-mapper",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"devDependencies": {
|
||||
"@cerc-io/ipld-eth-client": "^0.2.61",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.62",
|
||||
"@ethersproject/abi": "^5.3.0",
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/test",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"private": true,
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/tracing-client",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"description": "ETH VM tracing client",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
|
@ -1,15 +1,17 @@
|
||||
{
|
||||
"name": "@cerc-io/util",
|
||||
"version": "0.2.61",
|
||||
"version": "0.2.62",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"dependencies": {
|
||||
"@apollo/utils.keyvaluecache": "^1.0.1",
|
||||
"@cerc-io/nitro-node": "^0.1.11",
|
||||
"@cerc-io/peer": "^0.2.61",
|
||||
"@cerc-io/solidity-mapper": "^0.2.61",
|
||||
"@cerc-io/peer": "^0.2.62",
|
||||
"@cerc-io/solidity-mapper": "^0.2.62",
|
||||
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
|
||||
"@ethersproject/properties": "^5.7.0",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@ethersproject/web": "^5.7.1",
|
||||
"@graphql-tools/schema": "^9.0.10",
|
||||
"@graphql-tools/utils": "^9.1.1",
|
||||
"@ipld/dag-cbor": "^6.0.12",
|
||||
@ -49,7 +51,7 @@
|
||||
"yargs": "^17.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cerc-io/cache": "^0.2.61",
|
||||
"@cerc-io/cache": "^0.2.62",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||
"@types/bunyan": "^1.8.8",
|
||||
"@types/express": "^4.17.14",
|
||||
|
@ -224,9 +224,19 @@ export interface ServerConfig {
|
||||
rpcSupportsBlockHashParam: boolean;
|
||||
}
|
||||
|
||||
export interface FundingAmountsConfig {
|
||||
directFund: string;
|
||||
virtualFund: string;
|
||||
}
|
||||
export interface NitroPeerConfig {
|
||||
address: string;
|
||||
multiAddr: string;
|
||||
fundingAmounts: FundingAmountsConfig;
|
||||
}
|
||||
|
||||
export interface EthServerPaymentsConfig {
|
||||
nitro: NitroPeerConfig;
|
||||
paidRPCMethods: string[];
|
||||
amount: string;
|
||||
}
|
||||
|
||||
@ -237,7 +247,7 @@ export interface UpstreamConfig {
|
||||
rpcProviderEndpoint: string;
|
||||
rpcProviderMutationEndpoint: string;
|
||||
rpcClient: boolean;
|
||||
rpcProviderNitroNode: NitroPeerConfig;
|
||||
payments: EthServerPaymentsConfig;
|
||||
}
|
||||
traceProviderEndpoint: string;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
import debug from 'debug';
|
||||
import assert from 'assert';
|
||||
import { providers } from 'ethers';
|
||||
import { LRUCache } from 'lru-cache';
|
||||
import { FieldNode } from 'graphql';
|
||||
import { ApolloServerPlugin, GraphQLResponse, GraphQLRequestContext } from 'apollo-server-plugin-base';
|
||||
@ -13,6 +14,8 @@ import Channel from '@cerc-io/ts-channel';
|
||||
import type { ReadWriteChannel } from '@cerc-io/ts-channel';
|
||||
import type { Voucher } from '@cerc-io/nitro-node';
|
||||
import { utils as nitroUtils, ChannelStatus, Destination } from '@cerc-io/nitro-node';
|
||||
import { deepCopy } from '@ethersproject/properties';
|
||||
import { fetchJson } from '@ethersproject/web';
|
||||
|
||||
import { BaseRatesConfig, NitroPeerConfig, PaymentsConfig } from './config';
|
||||
|
||||
@ -70,8 +73,6 @@ export class PaymentsManager {
|
||||
private stopSubscriptionLoop: ReadWriteChannel<void>;
|
||||
private paymentListeners: ReadWriteChannel<string>[] = [];
|
||||
|
||||
private upstreamNodePaymentChannel?: string;
|
||||
|
||||
constructor (nitro: nitroUtils.Nitro, config: PaymentsConfig, baseRatesConfig: BaseRatesConfig) {
|
||||
this.nitro = nitro;
|
||||
this.config = config;
|
||||
@ -240,43 +241,37 @@ export class PaymentsManager {
|
||||
}
|
||||
}
|
||||
|
||||
async setupUpstreamPaymentChannel (rpcProviderNitroNode: NitroPeerConfig): Promise<void> {
|
||||
log(`Adding upstream Nitro node: ${rpcProviderNitroNode.address}`);
|
||||
await this.nitro.addPeerByMultiaddr(rpcProviderNitroNode.address, rpcProviderNitroNode.multiAddr);
|
||||
async setupPaymentChannel (nodeConfig: NitroPeerConfig): Promise<string> {
|
||||
log(`Adding Nitro node: ${nodeConfig.address}`);
|
||||
await this.nitro.addPeerByMultiaddr(nodeConfig.address, nodeConfig.multiAddr);
|
||||
|
||||
// Create a payment channel with upstream Nitro node
|
||||
// Create a payment channel with the given Nitro node
|
||||
// if it doesn't already exist
|
||||
const existingPaymentChannel = await this.getPaymentChannelWithPeer(rpcProviderNitroNode.address);
|
||||
const existingPaymentChannel = await this.getPaymentChannelWithPeer(nodeConfig.address);
|
||||
if (existingPaymentChannel) {
|
||||
this.upstreamNodePaymentChannel = existingPaymentChannel;
|
||||
log(`Using existing payment channel ${existingPaymentChannel} with upstream Nitro node`);
|
||||
|
||||
return;
|
||||
log(`Using existing payment channel ${existingPaymentChannel} with Nitro node ${nodeConfig.address}`);
|
||||
return existingPaymentChannel;
|
||||
}
|
||||
|
||||
await this.nitro.directFund(
|
||||
rpcProviderNitroNode.address,
|
||||
// TODO: Configure amount
|
||||
1_000_000_000_000
|
||||
nodeConfig.address,
|
||||
Number(nodeConfig.fundingAmounts?.directFund || 0)
|
||||
);
|
||||
|
||||
this.upstreamNodePaymentChannel = await this.nitro.virtualFund(
|
||||
rpcProviderNitroNode.address,
|
||||
// TODO: Configure amount
|
||||
1_000_000_000
|
||||
return this.nitro.virtualFund(
|
||||
nodeConfig.address,
|
||||
Number(nodeConfig.fundingAmounts?.virtualFund || 0)
|
||||
);
|
||||
|
||||
// TODO: Handle closures
|
||||
}
|
||||
|
||||
async sendUpstreamPayment (amount: string): Promise<{
|
||||
async sendPayment (destChannelId: string, amount: string): Promise<{
|
||||
channelId: string,
|
||||
amount: string,
|
||||
signature: string
|
||||
}> {
|
||||
assert(this.upstreamNodePaymentChannel);
|
||||
|
||||
const dest = new Destination(this.upstreamNodePaymentChannel);
|
||||
const dest = new Destination(destChannelId);
|
||||
const voucher = await this.nitro.node.createVoucher(dest, BigInt(amount ?? 0));
|
||||
assert(voucher.amount);
|
||||
|
||||
@ -431,3 +426,90 @@ export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerP
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Helper method to modify a given JsonRpcProvider to make payment for required methods
|
||||
// and attach the voucher details in reqeust URL
|
||||
export const setupProviderWithPayments = (
|
||||
provider: providers.JsonRpcProvider,
|
||||
paymentsManager: PaymentsManager,
|
||||
paymentChannelId: string,
|
||||
paidRPCMethods: string[],
|
||||
paymentAmount: string
|
||||
): void => {
|
||||
// https://github.com/ethers-io/ethers.js/blob/v5.7.2/packages/providers/src.ts/json-rpc-provider.ts#L502
|
||||
provider.send = async (method: string, params: Array<any>): Promise<any> => {
|
||||
log(`Making RPC call: ${method}`);
|
||||
|
||||
const request = {
|
||||
method: method,
|
||||
params: params,
|
||||
id: (provider._nextId++),
|
||||
jsonrpc: '2.0'
|
||||
};
|
||||
|
||||
provider.emit('debug', {
|
||||
action: 'request',
|
||||
request: deepCopy(request),
|
||||
provider: provider
|
||||
});
|
||||
|
||||
// We can expand this in the future to any call, but for now these
|
||||
// are the biggest wins and do not require any serializing parameters.
|
||||
const cache = (['eth_chainId', 'eth_blockNumber'].indexOf(method) >= 0);
|
||||
// @ts-expect-error copied code
|
||||
if (cache && provider._cache[method]) {
|
||||
return provider._cache[method];
|
||||
}
|
||||
|
||||
// Send a payment to upstream Nitro node and add details to the request URL
|
||||
let updatedURL = `${provider.connection.url}?method=${method}`;
|
||||
if (paidRPCMethods.includes(method)) {
|
||||
const voucher = await paymentsManager.sendPayment(paymentChannelId, paymentAmount);
|
||||
updatedURL = `${updatedURL}&channelId=${voucher.channelId}&amount=${voucher.amount}&signature=${voucher.signature}`;
|
||||
}
|
||||
|
||||
const result = fetchJson({ ...provider.connection, url: updatedURL }, JSON.stringify(request), getResult).then((result) => {
|
||||
provider.emit('debug', {
|
||||
action: 'response',
|
||||
request: request,
|
||||
response: result,
|
||||
provider: provider
|
||||
});
|
||||
|
||||
return result;
|
||||
}, (error) => {
|
||||
provider.emit('debug', {
|
||||
action: 'response',
|
||||
error: error,
|
||||
request: request,
|
||||
provider: provider
|
||||
});
|
||||
|
||||
throw error;
|
||||
});
|
||||
|
||||
// Cache the fetch, but clear it on the next event loop
|
||||
if (cache) {
|
||||
provider._cache[method] = result;
|
||||
setTimeout(() => {
|
||||
// @ts-expect-error copied code
|
||||
provider._cache[method] = null;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
// https://github.com/ethers-io/ethers.js/blob/v5.7.2/packages/providers/src.ts/json-rpc-provider.ts#L139
|
||||
function getResult (payload: { error?: { code?: number, data?: any, message?: string }, result?: any }): any {
|
||||
if (payload.error) {
|
||||
// @TODO: not any
|
||||
const error: any = new Error(payload.error.message);
|
||||
error.code = payload.error.code;
|
||||
error.data = payload.error.data;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return payload.result;
|
||||
}
|
||||
|
@ -1451,7 +1451,7 @@
|
||||
"@ethersproject/properties" "^5.6.0"
|
||||
"@ethersproject/strings" "^5.6.1"
|
||||
|
||||
"@ethersproject/web@5.7.1", "@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0":
|
||||
"@ethersproject/web@5.7.1", "@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0", "@ethersproject/web@^5.7.1":
|
||||
version "5.7.1"
|
||||
resolved "https://registry.npmjs.org/@ethersproject/web/-/web-5.7.1.tgz"
|
||||
integrity sha512-Gueu8lSvyjBWL4cYsWsjh6MtMwM0+H4HvqFPZfB6dV8ctbP9zFAO73VG1cMWae0FLPCtz0peKPpZY8/ugJJX2w==
|
||||
|
Loading…
Reference in New Issue
Block a user