Skip to content

Commit

Permalink
Update to ethers v6.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
therealjmj committed Jun 1, 2023
1 parent 45b1822 commit 7c63eb8
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 586 deletions.
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
"test": "npm run compile-test && mocha 'src/**/__tests__/*.test.ts'"
},
"peerDependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/providers": "^5.7.1"
"ethers": "^6.4.0"
},
"devDependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/providers": "^5.7.1",
"ethers": "^6.4.0",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
Expand All @@ -45,9 +43,5 @@
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.2",
"typescript": "^4.8.3"
},
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/transactions": "^5.7.0"
}
}
59 changes: 16 additions & 43 deletions src/models/response-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from '@ethersproject/bignumber';
/// <reference types="../types/global" />
import { PreparedTransactionRequest } from 'ethers';
import { MerkletreeScanStatus } from './merkletree-scan';
import { FeesSerialized } from './network-config';

Expand All @@ -24,45 +25,21 @@ export type TransactionGasDetails =

export type TransactionGasDetailsType0 = {
evmGasType: EVMGasType.Type0;
gasEstimate: BigNumber;
gasPrice: BigNumber;
gasEstimate: bigint;
gasPrice: bigint;
};

export type TransactionGasDetailsType1 = {
evmGasType: EVMGasType.Type1;
gasEstimate: BigNumber;
gasPrice: BigNumber;
gasEstimate: bigint;
gasPrice: bigint;
};

export type TransactionGasDetailsType2 = {
evmGasType: EVMGasType.Type2;
gasEstimate: BigNumber;
maxFeePerGas: BigNumber;
maxPriorityFeePerGas: BigNumber;
};

export type TransactionGasDetailsSerialized =
| TransactionGasDetailsSerializedType0
| TransactionGasDetailsSerializedType1
| TransactionGasDetailsSerializedType2;

export type TransactionGasDetailsSerializedType0 = {
evmGasType: EVMGasType.Type0;
gasEstimateString: string;
gasPriceString: string;
};

export type TransactionGasDetailsSerializedType1 = {
evmGasType: EVMGasType.Type1;
gasEstimateString: string;
gasPriceString: string;
};

export type TransactionGasDetailsSerializedType2 = {
evmGasType: EVMGasType.Type2;
gasEstimateString: string;
maxFeePerGasString: string;
maxPriorityFeePerGasString: string;
gasEstimate: bigint;
maxFeePerGas: bigint;
maxPriorityFeePerGas: bigint;
};

export enum ChainType {
Expand Down Expand Up @@ -96,28 +73,28 @@ export type RailgunWalletInfo = {
railgunAddress: string;
};

export type RailgunWalletAddressDataSerialized = {
masterPublicKey: string;
viewingPublicKey: string;
export type RailgunWalletAddressData = {
masterPublicKey: bigint;
viewingPublicKey: bigint;
};

export type RailgunTxidFromNullifiersResponse = {
txid?: string;
};

export type RailgunPopulateTransactionResponse = {
serializedTransaction: string;
transaction: PreparedTransactionRequest;
nullifiers?: string[];
};

export type RailgunTransactionGasEstimateResponse = {
gasEstimateString: string;
gasEstimate: bigint;
relayerFeeCommitment?: CommitmentSummary;
};

export type RailgunERC20Amount = {
tokenAddress: string;
amountString: string;
amount: bigint;
};

export type RailgunERC20AmountRecipient = RailgunERC20Amount & {
Expand All @@ -136,7 +113,7 @@ export type RailgunNFTAmount = {
nftAddress: string;
nftTokenType: NFTTokenType;
tokenSubID: string;
amountString: string;
amount: bigint;
};

export type RailgunNFTAmountRecipient = RailgunNFTAmount & {
Expand Down Expand Up @@ -212,10 +189,6 @@ export type TransactionHistoryItem = {
category: TransactionHistoryItemCategory;
};

export type TransactionHistorySerializedResponse = {
items: TransactionHistoryItem[];
};

type Ciphertext = {
iv: string;
tag: string;
Expand Down
51 changes: 10 additions & 41 deletions src/utils/__tests__/gas.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { BigNumber } from '@ethersproject/bignumber';
import {
calculateGasLimit,
calculateMaximumGas,
deserializeTransactionGasDetails,
serializeTransactionGasDetails,
} from '../gas';
import { decimalStringToHexString, decimalToHexString } from '../format';
import {
EVMGasType,
TransactionGasDetails,
TransactionGasDetailsSerialized,
} from '../../models/response-types';
import { calculateGasLimit, calculateMaximumGas } from '../gas';
import { EVMGasType, TransactionGasDetails } from '../../models/response-types';

chai.use(chaiAsPromised);
const { expect } = chai;

describe('gas', () => {
it('Should calculate gas limit correctly', () => {
const gasEstimate = BigNumber.from('100000');
const gasLimitString = calculateGasLimit(gasEstimate).toHexString();
expect(gasLimitString).to.equal(decimalStringToHexString('120000'));
const gasEstimate = BigInt('100000');
const gasLimit = calculateGasLimit(gasEstimate);
expect(Number(gasLimit)).to.equal(120000);
});

it('Should calculate maximum gas correctly', () => {
const gasDetails: TransactionGasDetails = {
evmGasType: EVMGasType.Type2,
gasEstimate: BigNumber.from('100000'),
maxFeePerGas: BigNumber.from('20000'),
maxPriorityFeePerGas: BigNumber.from('500'),
gasEstimate: BigInt('100000'),
maxFeePerGas: BigInt('20000'),
maxPriorityFeePerGas: BigInt('500'),
};
const gasLimitString = calculateMaximumGas(gasDetails).toHexString();
expect(gasLimitString).to.equal(decimalStringToHexString('2400000000'));
});

it('Should serialize tx gas details', () => {
const gasDetails: TransactionGasDetails = {
evmGasType: EVMGasType.Type2,
gasEstimate: BigNumber.from('10'),
maxFeePerGas: BigNumber.from('20'),
maxPriorityFeePerGas: BigNumber.from('1'),
};
const serialized = serializeTransactionGasDetails(gasDetails);
const expectedSerialized: TransactionGasDetailsSerialized = {
evmGasType: EVMGasType.Type2,
gasEstimateString: decimalToHexString(10),
maxFeePerGasString: decimalToHexString(20),
maxPriorityFeePerGasString: decimalToHexString(1),
};
expect(serialized).to.deep.equal(expectedSerialized);
expect(deserializeTransactionGasDetails(serialized)).to.deep.equal(
gasDetails,
);
const gasLimit = calculateMaximumGas(gasDetails);
expect(gasLimit.toString()).to.equal('2400000000');
});
});
2 changes: 1 addition & 1 deletion src/utils/available-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonRpcProvider } from '@ethersproject/providers';
import { JsonRpcProvider } from 'ethers';
import { ProviderJson } from './fallback-provider';
import { getUpperBoundMedian } from './median';
import { promiseTimeout } from './promises';
Expand Down
41 changes: 3 additions & 38 deletions src/utils/fallback-provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
FallbackProvider,
StaticJsonRpcProvider,
WebSocketProvider,
} from '@ethersproject/providers';
import { FallbackProvider, WebSocketProvider } from 'ethers';
import { PollingJsonRpcProvider } from './polling-json-rpc-provider';

export type FallbackProviderJsonConfig = {
chainId: number;
Expand All @@ -15,47 +12,15 @@ export type ProviderJson = {
provider: string;
};

type ProviderDebug = {
action: string;
rid: number;
backend: {
weight: number;
start: number;
duration: number;
result?: string[];
error?: typeof Error;
provider?: StaticJsonRpcProvider;
};
request: { method: string; params: { filter?: object } };
provider: FallbackProvider;
};

export const createFallbackProviderFromJsonConfig = (
config: FallbackProviderJsonConfig,
debugMessage?: (msg: string) => void,
): FallbackProvider => {
try {
const providers = config.providers.map(json => {
const isWebsocket = json.provider.startsWith('wss');
const provider = isWebsocket
? new WebSocketProvider(json.provider, Number(config.chainId))
: new StaticJsonRpcProvider(json.provider, Number(config.chainId));

if (debugMessage) {
provider.on('debug', (debug: ProviderDebug) => {
if (debug.backend && debug.backend.error) {
debugMessage(`Provider error: ${debug.backend.error}`);
debugMessage(
`Provider connection: ${
debug.backend.provider
? debug.backend.provider.connection
: undefined
}`,
);
}
});
}

: new PollingJsonRpcProvider(json.provider, Number(config.chainId));
return {
...json,
provider,
Expand Down
10 changes: 2 additions & 8 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { BigNumber } from '@ethersproject/bignumber';

export const decimalStringToHexString = (dec: string) => {
return BigNumber.from(dec).toHexString();
};

export const decimalToHexString = (dec: number) => {
return BigNumber.from(dec).toHexString();
export const decimalToHexString = (dec: string | number) => {
return BigInt(dec).toString(16);
};
78 changes: 6 additions & 72 deletions src/utils/gas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/// <reference types="../types/global" />
import { BigNumber } from '@ethersproject/bignumber';
import { NetworkName, NETWORK_CONFIG } from '../models/network-config';
import {
EVMGasType,
TransactionGasDetails,
TransactionGasDetailsSerialized,
} from '../models/response-types';
import { EVMGasType, TransactionGasDetails } from '../models/response-types';

export const getEVMGasTypeForTransaction = (
networkName: NetworkName,
Expand All @@ -22,9 +16,9 @@ export const getEVMGasTypeForTransaction = (
return defaultEVMGasType;
};

export const calculateGasLimit = (gasEstimate: BigNumber): BigNumber => {
export const calculateGasLimit = (gasEstimate: bigint): bigint => {
// Gas Limit: Add 20% to gas estimate.
return gasEstimate.mul(12000).div(10000);
return (gasEstimate * 12000n) / 10000n;
};

export const calculateGasPrice = (gasDetails: TransactionGasDetails) => {
Expand All @@ -44,73 +38,13 @@ export const calculateTotalGas = (
) => {
const gasPrice = calculateGasPrice(transactionGasDetails);
const { gasEstimate } = transactionGasDetails;
return gasEstimate.mul(gasPrice);
return gasEstimate * gasPrice;
};

export const calculateMaximumGas = (
transactionGasDetails: TransactionGasDetails,
): BigNumber => {
): bigint => {
const gasPrice = calculateGasPrice(transactionGasDetails);
const { gasEstimate } = transactionGasDetails;
return calculateGasLimit(gasEstimate).mul(gasPrice);
};

export const serializeTransactionGasDetails = (
gasDetails: TransactionGasDetails,
): TransactionGasDetailsSerialized => {
switch (gasDetails.evmGasType) {
case EVMGasType.Type0:
case EVMGasType.Type1: {
const { evmGasType, gasEstimate, gasPrice } = gasDetails;
return {
evmGasType,
gasEstimateString: gasEstimate.toHexString(),
gasPriceString: gasPrice.toHexString(),
};
}
case EVMGasType.Type2: {
const { evmGasType, gasEstimate, maxFeePerGas, maxPriorityFeePerGas } =
gasDetails;
return {
evmGasType,
gasEstimateString: gasEstimate.toHexString(),
maxFeePerGasString: maxFeePerGas.toHexString(),
maxPriorityFeePerGasString: maxPriorityFeePerGas.toHexString(),
};
}
}
};

export const deserializeTransactionGasDetails = (
gasDetailsSerialized: Optional<TransactionGasDetailsSerialized>,
): Optional<TransactionGasDetails> => {
if (!gasDetailsSerialized) {
return undefined;
}
switch (gasDetailsSerialized.evmGasType) {
case EVMGasType.Type0:
case EVMGasType.Type1: {
const { evmGasType, gasEstimateString, gasPriceString } =
gasDetailsSerialized;
return {
evmGasType,
gasEstimate: BigNumber.from(gasEstimateString),
gasPrice: BigNumber.from(gasPriceString),
};
}
case EVMGasType.Type2: {
const {
evmGasType,
gasEstimateString,
maxFeePerGasString,
maxPriorityFeePerGasString,
} = gasDetailsSerialized;
return {
evmGasType,
gasEstimate: BigNumber.from(gasEstimateString),
maxFeePerGas: BigNumber.from(maxFeePerGasString),
maxPriorityFeePerGas: BigNumber.from(maxPriorityFeePerGasString),
};
}
}
return calculateGasLimit(gasEstimate) * gasPrice;
};
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export * from './format';
export * from './gas';
export * from './network';
export * from './promises';
export * from './serializer';
export * from './versions';
Loading

0 comments on commit 7c63eb8

Please sign in to comment.