Skip to content

Commit

Permalink
chore(request-node): eip-1559 for TheGraphDataAccess (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux authored Feb 7, 2022
1 parent 8d81be3 commit c621373
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/ethereum-storage/src/gas-price-definer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XDaiFixedProvider from './gas-price-providers/xdai-fixed-provider';
* Determines the gas price to use depending on the used network
* Polls gas price API providers if necessary
*/
export default class GasPriceDefiner {
export class GasPriceDefiner {
private defaultProviders = [
new EtherchainProvider(),
new EthGasStationProvider(),
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-storage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { EthereumStorage } from './ethereum-storage';
export { GasPriceDefiner } from './gas-price-definer';
export { IpfsStorage } from './ipfs-storage';
2 changes: 1 addition & 1 deletion packages/ethereum-storage/src/smart-contract-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Bluebird from 'bluebird';
import * as config from './config';
import EthereumBlocks from './ethereum-blocks';
import EthereumUtils from './ethereum-utils';
import GasPriceDefiner from './gas-price-definer';
import { GasPriceDefiner } from './gas-price-definer';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const web3Eth = require('web3-eth');
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-storage/test/gas-price-definer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StorageTypes } from '@requestnetwork/types';
import EthereumUtils from '../src/ethereum-utils';

import * as config from '../src/config';
import GasPriceDefiner from '../src/gas-price-definer';
import { GasPriceDefiner } from '../src/gas-price-definer';

import { BigNumber } from 'ethers';

Expand Down
1 change: 1 addition & 0 deletions packages/request-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"chalk": "4.1.0",
"cors": "2.8.5",
"dotenv": "8.2.0",
"eip1559-fee-suggestions-ethers": "1.3.3",
"ethers": "5.5.2",
"express": "4.17.1",
"graphql": "15.5.0",
Expand Down
51 changes: 43 additions & 8 deletions packages/request-node/src/thegraph/TheGraphStorage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { EventEmitter } from 'events';
import { utils, Signer, ContractReceipt } from 'ethers';
import { BigNumber, ContractReceipt, PayableOverrides, providers, Signer, utils } from 'ethers';
import TypedEmitter from 'typed-emitter';
import Utils from '@requestnetwork/utils';
import { LogTypes, StorageTypes } from '@requestnetwork/types';
import { requestHashSubmitterArtifact } from '@requestnetwork/smart-contracts';
import { RequestOpenHashSubmitter } from '@requestnetwork/smart-contracts/types';
import { suggestFees } from 'eip1559-fee-suggestions-ethers';
import { GasPriceDefiner } from '@requestnetwork/ethereum-storage';
import assert from 'assert';

type TheGraphStorageProps = {
network: string;
Expand All @@ -19,15 +22,22 @@ export type TheGraphStorageEventEmitter = TypedEmitter<{
}>;

export class TheGraphStorage {
private logger: LogTypes.ILogger;
private ipfsStorage: StorageTypes.IIpfsStorage;
private hashSubmitter: RequestOpenHashSubmitter;
private network: string;
private readonly logger: LogTypes.ILogger;
private readonly ipfsStorage: StorageTypes.IIpfsStorage;
private readonly hashSubmitter: RequestOpenHashSubmitter;
private readonly network: string;
private readonly provider: providers.JsonRpcProvider;
private enableEip1559 = true;

constructor({ network, signer, ipfsStorage, logger }: TheGraphStorageProps) {
this.logger = logger || new Utils.SimpleLogger();
this.ipfsStorage = ipfsStorage;
this.network = network;
assert(
signer.provider instanceof providers.JsonRpcProvider,
'TheGraphStorage provider must be a JsonRpcProvider',
);
this.provider = signer.provider;
this.hashSubmitter = requestHashSubmitterArtifact.connect(
network,
signer,
Expand All @@ -36,17 +46,42 @@ export class TheGraphStorage {

async initialize(): Promise<void> {
await this.ipfsStorage.initialize();
try {
await this.provider.send('eth_feeHistory', []);
} catch (e) {
this.logger.warn(
'This RPC provider does not support the "eth_feeHistory" method: switching to legacy gas price',
);
this.enableEip1559 = false;
}
this.logger.debug('TheGraph storage initialized');
}

async append(content: string): Promise<StorageTypes.IAppendResult> {
const { ipfsHash, ipfsSize } = await this.ipfsStorage.ipfsAdd(content);

const fee = await this.hashSubmitter.getFeesAmount(ipfsSize);
const overrides: PayableOverrides = { value: fee };
if (this.enableEip1559) {
const suggestedFee = await suggestFees(
this.hashSubmitter.provider as providers.JsonRpcProvider,
);
overrides.maxFeePerGas = BigNumber.from(suggestedFee.baseFeeSuggestion);
overrides.maxPriorityFeePerGas = BigNumber.from(
suggestedFee.maxPriorityFeeSuggestions.urgent,
);
} else {
// retro-compatibility for networks where the eth_feeHistory RPC method is not available (pre EIP-1559)
const gasPriceDefiner = new GasPriceDefiner();
overrides.gasPrice = await gasPriceDefiner.getGasPrice(
StorageTypes.GasPriceType.FAST,
this.network,
);
}
const tx = await this.hashSubmitter.submitHash(
ipfsHash,
utils.hexZeroPad(utils.hexlify(ipfsSize), 32),
{ value: fee },
overrides,
);

const eventEmitter = new EventEmitter() as TheGraphStorageEventEmitter;
Expand All @@ -66,13 +101,13 @@ export class TheGraphStorage {

void tx
.wait()
.then((receipt) => {
.then((receipt: providers.TransactionReceipt) => {
this.logger.debug(
`TX ${receipt.transactionHash} confirmed at block ${receipt.blockNumber}`,
);
eventEmitter.emit('confirmed', receipt);
})
.catch((e) => eventEmitter.emit('error', e));
.catch((e: Error) => eventEmitter.emit('error', e));

return Object.assign(eventEmitter, result);
}
Expand Down
91 changes: 91 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,13 @@
dependencies:
"@ethersproject/logger" "^5.5.0"

"@ethersproject/networks@5.5.2":
version "5.5.2"
resolved "https://registry.yarnpkg.com/@ethersproject/networks/-/networks-5.5.2.tgz#784c8b1283cd2a931114ab428dae1bd00c07630b"
integrity sha512-NEqPxbGBfy6O3x4ZTISb90SjEDkWYDUbEeIFhJly0F7sZjoQMnj5KYzMSkMkLKZ+1fGpx00EDpHQCy6PrDupkQ==
dependencies:
"@ethersproject/logger" "^5.5.0"

"@ethersproject/networks@^5.4.0":
version "5.4.0"
resolved "https://registry.yarnpkg.com/@ethersproject/networks/-/networks-5.4.0.tgz#71eecd3ef3755118b42c1a5d2a44a7e07202e10a"
Expand Down Expand Up @@ -2805,6 +2812,31 @@
bech32 "1.1.4"
ws "7.4.6"

"@ethersproject/providers@5.5.3", "@ethersproject/providers@^5.5.0":
version "5.5.3"
resolved "https://registry.yarnpkg.com/@ethersproject/providers/-/providers-5.5.3.tgz#56c2b070542ac44eb5de2ed3cf6784acd60a3130"
integrity sha512-ZHXxXXXWHuwCQKrgdpIkbzMNJMvs+9YWemanwp1fA7XZEv7QlilseysPvQe0D7Q7DlkJX/w/bGA1MdgK2TbGvA==
dependencies:
"@ethersproject/abstract-provider" "^5.5.0"
"@ethersproject/abstract-signer" "^5.5.0"
"@ethersproject/address" "^5.5.0"
"@ethersproject/basex" "^5.5.0"
"@ethersproject/bignumber" "^5.5.0"
"@ethersproject/bytes" "^5.5.0"
"@ethersproject/constants" "^5.5.0"
"@ethersproject/hash" "^5.5.0"
"@ethersproject/logger" "^5.5.0"
"@ethersproject/networks" "^5.5.0"
"@ethersproject/properties" "^5.5.0"
"@ethersproject/random" "^5.5.0"
"@ethersproject/rlp" "^5.5.0"
"@ethersproject/sha2" "^5.5.0"
"@ethersproject/strings" "^5.5.0"
"@ethersproject/transactions" "^5.5.0"
"@ethersproject/web" "^5.5.0"
bech32 "1.1.4"
ws "7.4.6"

"@ethersproject/random@5.4.0", "@ethersproject/random@^5.4.0":
version "5.4.0"
resolved "https://registry.yarnpkg.com/@ethersproject/random/-/random-5.4.0.tgz#9cdde60e160d024be39cc16f8de3b9ce39191e16"
Expand All @@ -2821,6 +2853,14 @@
"@ethersproject/bytes" "^5.5.0"
"@ethersproject/logger" "^5.5.0"

"@ethersproject/random@5.5.1":
version "5.5.1"
resolved "https://registry.yarnpkg.com/@ethersproject/random/-/random-5.5.1.tgz#7cdf38ea93dc0b1ed1d8e480ccdaf3535c555415"
integrity sha512-YaU2dQ7DuhL5Au7KbcQLHxcRHfgyNgvFV4sQOo0HrtW3Zkrc9ctWNz8wXQ4uCSfSDsqX2vcjhroxU5RQRV0nqA==
dependencies:
"@ethersproject/bytes" "^5.5.0"
"@ethersproject/logger" "^5.5.0"

"@ethersproject/rlp@5.4.0", "@ethersproject/rlp@^5.4.0":
version "5.4.0"
resolved "https://registry.yarnpkg.com/@ethersproject/rlp/-/rlp-5.4.0.tgz#de61afda5ff979454e76d3b3310a6c32ad060931"
Expand Down Expand Up @@ -10839,6 +10879,16 @@ ee-first@1.1.1:
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

eip1559-fee-suggestions-ethers@1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/eip1559-fee-suggestions-ethers/-/eip1559-fee-suggestions-ethers-1.3.3.tgz#cf21b6ae4734b697c8f79e556d91aeda3e464d60"
integrity sha512-W/5SQICRpDL0XvOdcX5oVWfaE2sow2u9il8I6Qyd9fkL9XY1knypywTcoBTdU9ULrPoP3phn/RnEKGq7Y8qkLw==
dependencies:
"@ethersproject/providers" "^5.5.0"
bignumber.js "^9.0.1"
ethers "^5.4.2"
moving-averages "^4.0.6"

electron-to-chromium@^1.3.47:
version "1.3.778"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.778.tgz#bf01048736c95b78f2988e88005e0ebb385942a4"
Expand Down Expand Up @@ -12126,6 +12176,42 @@ ethers@^5.0.0, ethers@^5.0.1, ethers@^5.0.2:
"@ethersproject/web" "5.4.0"
"@ethersproject/wordlists" "5.4.0"

ethers@^5.4.2:
version "5.5.4"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.5.4.tgz#e1155b73376a2f5da448e4a33351b57a885f4352"
integrity sha512-N9IAXsF8iKhgHIC6pquzRgPBJEzc9auw3JoRkaKe+y4Wl/LFBtDDunNe7YmdomontECAcC5APaAgWZBiu1kirw==
dependencies:
"@ethersproject/abi" "5.5.0"
"@ethersproject/abstract-provider" "5.5.1"
"@ethersproject/abstract-signer" "5.5.0"
"@ethersproject/address" "5.5.0"
"@ethersproject/base64" "5.5.0"
"@ethersproject/basex" "5.5.0"
"@ethersproject/bignumber" "5.5.0"
"@ethersproject/bytes" "5.5.0"
"@ethersproject/constants" "5.5.0"
"@ethersproject/contracts" "5.5.0"
"@ethersproject/hash" "5.5.0"
"@ethersproject/hdnode" "5.5.0"
"@ethersproject/json-wallets" "5.5.0"
"@ethersproject/keccak256" "5.5.0"
"@ethersproject/logger" "5.5.0"
"@ethersproject/networks" "5.5.2"
"@ethersproject/pbkdf2" "5.5.0"
"@ethersproject/properties" "5.5.0"
"@ethersproject/providers" "5.5.3"
"@ethersproject/random" "5.5.1"
"@ethersproject/rlp" "5.5.0"
"@ethersproject/sha2" "5.5.0"
"@ethersproject/signing-key" "5.5.0"
"@ethersproject/solidity" "5.5.0"
"@ethersproject/strings" "5.5.0"
"@ethersproject/transactions" "5.5.0"
"@ethersproject/units" "5.5.0"
"@ethersproject/wallet" "5.5.0"
"@ethersproject/web" "5.5.1"
"@ethersproject/wordlists" "5.5.0"

ethjs-abi@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/ethjs-abi/-/ethjs-abi-0.2.1.tgz#e0a7a93a7e81163a94477bad56ede524ab6de533"
Expand Down Expand Up @@ -18066,6 +18152,11 @@ move-concurrently@^1.0.1:
rimraf "^2.5.4"
run-queue "^1.0.3"

moving-averages@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/moving-averages/-/moving-averages-4.0.6.tgz#4978d4d9f68aef8f2b5fa028b1ec316da0cc8c95"
integrity sha512-Jv+mH0emTFP40Q5ONsBqTfIO9NuUyK9zuW4pWbOzWJm8jEqpLBtAH2CnE2MFIuH/G9f9nDugmnDVUJaHx9jckw==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit c621373

Please sign in to comment.