Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Ethers with @metamask/eth-query #59

Draft
wants to merge 1 commit into
base: narrow-provider-type
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write"
},
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@metamask/eth-json-rpc-provider": "^2.3.0",
"@metamask/eth-query": "^4.0.0",
"@metamask/utils": "^8.2.1",
"async-mutex": "^0.3.1"
},
"devDependencies": {
Expand Down
32 changes: 20 additions & 12 deletions src/NonceTracker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import assert from 'assert';
import { Mutex } from 'async-mutex';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import EthQuery from '@metamask/eth-query';
import { hexToNumber } from '@metamask/utils';

// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const { Web3Provider } = require('@ethersproject/providers');
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const BlockTracker = require('eth-block-tracker');

Expand Down Expand Up @@ -93,7 +93,7 @@ export interface Transaction {
export class NonceTracker {
private blockTracker: typeof BlockTracker;

private web3: typeof Web3Provider;
private ethQuery: EthQuery;

private getPendingTransactions: (address: string) => Transaction[];

Expand All @@ -103,7 +103,7 @@ export class NonceTracker {

constructor(opts: NonceTrackerOptions) {
this.blockTracker = opts.blockTracker;
this.web3 = new Web3Provider(opts.provider);
this.ethQuery = new EthQuery(opts.provider);
this.getPendingTransactions = opts.getPendingTransactions;
this.getConfirmedTransactions = opts.getConfirmedTransactions;
this.lockMap = {};
Expand Down Expand Up @@ -208,14 +208,22 @@ export class NonceTracker {
// we need to make sure our base count
// and pending count are from the same block
const blockNumber: string = await this.blockTracker.getLatestBlock();
const baseCount: number = await this.web3.getTransactionCount(
address,
blockNumber,
);
assert(
Number.isInteger(baseCount),
`nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`,
);
const baseCountAsHex = await new Promise<string>((resolve, reject) => {
this.ethQuery.sendAsync<string[], string>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We could drop ethQuery here and call the provider directly:

Suggested change
this.ethQuery.sendAsync<string[], string>(
this.provider.sendAsync<string[], string>(

{
method: 'eth_getTransactionCount',
params: [address, blockNumber],
},
(...args) => {
if (args[0] === null) {
resolve(args[1]);
} else {
reject(args[0]);
}
},
);
});
const baseCount = hexToNumber(baseCountAsHex);
return {
name: 'network',
nonce: baseCount,
Expand Down
Loading