Skip to content

Commit

Permalink
Feature: support eth_gasPrice rpc method in sdk/provider (#199)
Browse files Browse the repository at this point in the history
* support eth_gasPRice in provider/sdk

* wip fixing types

* fix tests and typing
  • Loading branch information
mmv08 authored Aug 3, 2021
1 parent da455a1 commit f9d1a18
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 125 deletions.
6 changes: 6 additions & 0 deletions .changeset/ninety-news-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@gnosis.pm/safe-apps-provider': minor
'@gnosis.pm/safe-apps-sdk': minor
---

Support eth_gasPrice rpc method
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"lerna": "^4.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"eslint": "7.31.0",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"eslint": "7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "27.0.6",
"prettier": "^2.3.2",
"ts-jest": "^27.0.3",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
}
}
3 changes: 3 additions & 0 deletions packages/safe-apps-provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export class SafeAppProvider implements EIP1193Provider {
case 'eth_getLogs':
return this.sdk.eth.getPastLogs([params[0]]);

case 'eth_gasPrice':
return this.sdk.eth.getGasPrice();

default:
throw Error(`"${request.method}" not implemented`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-react-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@types/jest": "^26.0.24",
"@types/react-dom": "^17.0.9",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.8.0",
"eslint-plugin-flowtype": "^5.8.2",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.24.0",
Expand Down
75 changes: 39 additions & 36 deletions packages/safe-apps-sdk/dist/package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
{
"name": "@gnosis.pm/safe-apps-sdk",
"version": "4.0.0",
"description": "SDK developed to integrate third-party apps with Safe-Multisig app.",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"_files": [
"dist/**/*",
"README.md"
],
"keywords": [
"Gnosis",
"sdk",
"apps"
],
"scripts": {
"test": "jest",
"format-dist": "sed -i '' 's/\"files\":/\"_files\":/' dist/package.json",
"build": "yarn rimraf dist && tsc && yarn format-dist",
"lint": "tslint -p tsconfig.json"
},
"author": "Gnosis (https://gnosis.io)",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.3.2",
"rimraf": "^3.0.2"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gnosis/safe-apps-sdk.git"
},
"bugs": {
"url": "https://github.com/gnosis/safe-apps-sdk/issues"
},
"homepage": "https://github.com/gnosis/safe-apps-sdk#readme"
"name": "@gnosis.pm/safe-apps-sdk",
"version": "4.1.0-next.0",
"description": "SDK developed to integrate third-party apps with Safe-Multisig app.",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"_files": [
"dist/**/*",
"README.md"
],
"keywords": [
"Gnosis",
"sdk",
"apps"
],
"scripts": {
"test": "jest",
"format-dist": "sed -i '' 's/\"files\":/\"_files\":/' dist/package.json",
"build": "yarn rimraf dist && tsc && yarn format-dist",
"lint": "tslint -p tsconfig.json"
},
"author": "Gnosis (https://gnosis.io)",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.4.10",
"rimraf": "^3.0.2"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gnosis/safe-apps-sdk.git"
},
"bugs": {
"url": "https://github.com/gnosis/safe-apps-sdk/issues"
},
"homepage": "https://github.com/gnosis/safe-apps-sdk#readme",
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/safe-apps-sdk/dist/src/eth/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare const RPC_CALLS: {
readonly eth_call: "eth_call";
readonly eth_gasPrice: "eth_gasPrice";
readonly eth_getLogs: "eth_getLogs";
readonly eth_getBalance: "eth_getBalance";
readonly eth_getCode: "eth_getCode";
Expand Down
1 change: 1 addition & 0 deletions packages/safe-apps-sdk/dist/src/eth/constants.js

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

2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/dist/src/eth/constants.js.map

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

21 changes: 11 additions & 10 deletions packages/safe-apps-sdk/dist/src/eth/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BlockNumberArg, Communicator, Log, BlockTransactionString, BlockTransactionObject, Web3TransactionObject, TransactionConfig, Web3TransactionReceiptObject, PastLogsOptions } from '../types';
declare class Eth {
call: (params: [TransactionConfig, (string | undefined)?]) => Promise<string>;
getBalance: (params: [string, (string | undefined)?]) => Promise<string>;
getCode: (params: [string, (string | undefined)?]) => Promise<string>;
getStorageAt: (params: [string, number, (string | undefined)?]) => Promise<string>;
getPastLogs: (params: [PastLogsOptions]) => Promise<Log[]>;
getBlockByHash: (params: [string, (boolean | undefined)?]) => Promise<BlockTransactionObject | BlockTransactionString>;
getBlockByNumber: (params: [BlockNumberArg, (boolean | undefined)?]) => Promise<BlockTransactionObject | BlockTransactionString>;
getTransactionByHash: (params: [string]) => Promise<Web3TransactionObject>;
getTransactionReceipt: (params: [string]) => Promise<Web3TransactionReceiptObject>;
getTransactionCount: (params: [string, (string | undefined)?]) => Promise<number>;
call: (params?: [TransactionConfig, (string | undefined)?] | undefined) => Promise<string>;
getBalance: (params?: [string, (string | undefined)?] | undefined) => Promise<string>;
getCode: (params?: [string, (string | undefined)?] | undefined) => Promise<string>;
getStorageAt: (params?: [string, number, (string | undefined)?] | undefined) => Promise<string>;
getPastLogs: (params?: [PastLogsOptions] | undefined) => Promise<Log[]>;
getBlockByHash: (params?: [string, (boolean | undefined)?] | undefined) => Promise<BlockTransactionObject | BlockTransactionString>;
getBlockByNumber: (params?: [BlockNumberArg, (boolean | undefined)?] | undefined) => Promise<BlockTransactionObject | BlockTransactionString>;
getTransactionByHash: (params?: [string] | undefined) => Promise<Web3TransactionObject>;
getTransactionReceipt: (params?: [string] | undefined) => Promise<Web3TransactionReceiptObject>;
getTransactionCount: (params?: [string, (string | undefined)?] | undefined) => Promise<string>;
getGasPrice: (params?: never[] | undefined) => Promise<string>;
private readonly communicator;
constructor(communicator: Communicator);
private buildRequest;
Expand Down
8 changes: 6 additions & 2 deletions packages/safe-apps-sdk/dist/src/eth/index.js

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

2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/dist/src/eth/index.js.map

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

2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/dist/src/types/rpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RPC_CALLS } from '../eth/constants';
export declare type RpcCallNames = keyof typeof RPC_CALLS;
export declare type RPCPayload<P = unknown[]> = {
call: RpcCallNames;
params: P;
params: P | never[];
};
export interface Log {
address: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.3.3",
"@types/node": "^16.4.10",
"rimraf": "^3.0.2"
},
"repository": {
Expand Down
1 change: 1 addition & 0 deletions packages/safe-apps-sdk/src/eth/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const RPC_CALLS = {
eth_call: 'eth_call',
eth_gasPrice: 'eth_gasPrice',
eth_getLogs: 'eth_getLogs',
eth_getBalance: 'eth_getBalance',
eth_getCode: 'eth_getCode',
Expand Down
17 changes: 17 additions & 0 deletions packages/safe-apps-sdk/src/eth/eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,21 @@ describe('Safe Apps SDK Read RPC Requests', () => {
);
});
});

describe('gasPrice', () => {
it('Should send a valid message to the interface', () => {
sdkInstance.eth.getGasPrice();

expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
method: Methods.rpcCall,
params: {
call: 'eth_gasPrice',
params: [],
},
}),
'*',
);
});
});
});
14 changes: 10 additions & 4 deletions packages/safe-apps-sdk/src/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Eth {
public getTransactionByHash;
public getTransactionReceipt;
public getTransactionCount;
public getGasPrice;
private readonly communicator: Communicator;

constructor(communicator: Communicator) {
Expand Down Expand Up @@ -80,14 +81,19 @@ class Eth {
this.getTransactionReceipt = this.buildRequest<[string], Web3TransactionReceiptObject>({
call: RPC_CALLS.eth_getTransactionReceipt,
});
this.getTransactionCount = this.buildRequest<[string, string?], number>({
this.getTransactionCount = this.buildRequest<[string, string?], string>({
call: RPC_CALLS.eth_getTransactionCount,
formatters: [null, inputFormatters.defaultBlockParam],
});
this.getGasPrice = this.buildRequest<never[], string>({
call: RPC_CALLS.eth_gasPrice,
});
}

private buildRequest<P extends unknown[], R = unknown>({ call, formatters }: BuildRequestArgs) {
return async (params: P): Promise<R> => {
private buildRequest<P = never[], R = unknown>(args: BuildRequestArgs) {
const { call, formatters } = args;

return async (params?: P): Promise<R> => {
if (formatters && Array.isArray(params)) {
formatters.forEach((formatter: ((...args: unknown[]) => unknown) | null, i) => {
if (formatter) {
Expand All @@ -98,7 +104,7 @@ class Eth {

const payload: RPCPayload<P> = {
call,
params,
params: params || [],
};

const response = await this.communicator.send<Methods.rpcCall, RPCPayload<P>, R>(Methods.rpcCall, payload);
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type RpcCallNames = keyof typeof RPC_CALLS;

export type RPCPayload<P = unknown[]> = {
call: RpcCallNames;
params: P;
params: P | never[];
};

export interface Log {
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-web3-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/node": "^16.3.3",
"@types/node": "^16.4.10",
"@web3-react/core": "6.1.9",
"react": "17.0.2",
"rimraf": "^3.0.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-web3modal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"devDependencies": {
"@gnosis.pm/safe-apps-sdk": "4.1.0-next.0",
"rimraf": "^3.0.2",
"web3modal": "^1.9.3"
"web3modal": "^1.9.4"
},
"peerDependencies": {
"@gnosis.pm/safe-apps-sdk": "4.1.0-next.0",
Expand Down
Loading

0 comments on commit f9d1a18

Please sign in to comment.