diff --git a/src/consts.ts b/src/consts.ts new file mode 100644 index 0000000..7657716 --- /dev/null +++ b/src/consts.ts @@ -0,0 +1,7 @@ +export const Y_PARITY_EIP_2098 = 27; +export const EIP155_NUMBER = 35; +// NOTE: Black magic number, unsure where it comes from +export const EIGHT = 8; + +// NOTE: Logic stolen from https://github.com/celo-org/celo-monorepo/blob/e7ebc92cb0715dc56c9d7f613dca81e076541cf3/packages/sdk/connect/src/connection.ts#L382-L396 +export const GAS_INFLATION_FACTOR = 1.3; diff --git a/src/lib/CeloProvider.ts b/src/lib/CeloProvider.ts index 6dda604..cd9fbde 100644 --- a/src/lib/CeloProvider.ts +++ b/src/lib/CeloProvider.ts @@ -65,8 +65,7 @@ export class CeloProvider extends providers.JsonRpcProvider { ] as const; const tx = { - // @ts-expect-error - ...this.constructor.hexlifyTransaction( + ...providers.JsonRpcProvider.hexlifyTransaction( params.transaction, extraneous_keys.reduce((acc, [key]) => { acc[key] = true; diff --git a/src/lib/CeloWallet.ts b/src/lib/CeloWallet.ts index 5ebcf04..3b632db 100644 --- a/src/lib/CeloWallet.ts +++ b/src/lib/CeloWallet.ts @@ -122,11 +122,10 @@ export class CeloWallet extends Wallet { * https://github.com/ethers-io/ethers.js/blob/master/packages/abstract-signer/src.ts/index.ts */ async estimateGas( - transaction: utils.Deferrable + transaction: utils.Deferrable ): Promise { this._checkProvider("estimateGas"); - // @ts-expect-error - const tx: CeloTransaction = await utils.resolveProperties(transaction); + const tx = await utils.resolveProperties(transaction); return this.provider.estimateGas(tx).then(adjustForGasInflation); } diff --git a/src/lib/transaction/utils.ts b/src/lib/transaction/utils.ts index d0f8478..7b9d9b9 100644 --- a/src/lib/transaction/utils.ts +++ b/src/lib/transaction/utils.ts @@ -1,5 +1,6 @@ import { BigNumber, BigNumberish } from "ethers"; import { hexlify } from "ethers/lib/utils"; +import { GAS_INFLATION_FACTOR } from "../../consts"; function isEmpty(value: string | BigNumberish | undefined | null) { return ( @@ -11,6 +12,7 @@ function isEmpty(value: string | BigNumberish | undefined | null) { : hexlify(value) === "0x0") ); } + function isPresent(value: string | BigNumberish | undefined | null) { return !isEmpty(value); } @@ -27,6 +29,7 @@ export function isCIP64(tx: any) { !isPresent(tx.gatewayFeeRecipient) ); } + export function isCIP42(tx: any): boolean { return ( isEIP1559(tx) && @@ -55,9 +58,6 @@ export function omit( } export function adjustForGasInflation(gas: BigNumber): BigNumber { - // NOTE: Don't ask me - const GAS_INFLATION_FACTOR = 1.3; - // NOTE: prevent floating point math return gas.mul(Math.floor(GAS_INFLATION_FACTOR * 100)).div(100); } diff --git a/src/lib/transactions.ts b/src/lib/transactions.ts index 90706ad..b22b0b7 100644 --- a/src/lib/transactions.ts +++ b/src/lib/transactions.ts @@ -9,10 +9,7 @@ import { } from "ethers"; import { concatHex, isCIP64, isEIP1559, omit } from "./transaction/utils"; import { accessListify, AccessListish } from "ethers/lib/utils"; - -// NOTE: Black magic -const Y_PARITY_EIP_2098 = 27; -const BULLSHIT_NUMBER = 8; +import { EIGHT, EIP155_NUMBER, Y_PARITY_EIP_2098 } from "../consts"; // From https://github.com/ethers-io/ethers.js/blob/master/packages/bytes/src.ts/index.ts#L33 // Copied because it doesn't seem to be exported from 'ethers' anywhere @@ -290,7 +287,7 @@ export function serializeCeloTransaction( signature.v > 28 ) { // No chainId provided, but the signature is signing with EIP-155; derive chainId - chainId = Math.floor((signature.v - 35) / 2); + chainId = Math.floor((signature.v - EIP155_NUMBER) / 2); } // We have an EIP-155 transaction (chainId was specified and non-zero) @@ -323,7 +320,7 @@ export function serializeCeloTransaction( v = Y_PARITY_EIP_2098 + sig.recoveryParam; if (chainId !== 0) { - v += chainId * 2 + BULLSHIT_NUMBER; + v += chainId * 2 + EIGHT; // If an EIP-155 v (directly or indirectly; maybe _vs) was provided, check it! if (sig.v > Y_PARITY_EIP_2098 + 1 && sig.v !== v) { @@ -430,9 +427,9 @@ export function parseCeloTransaction( let recoveryParam = tx.v; if (!type) { // celo-legacy - tx.chainId = Math.max(0, Math.floor((tx.v - 35) / 2)); + tx.chainId = Math.max(0, Math.floor((tx.v - EIP155_NUMBER) / 2)); recoveryParam = tx.v - Y_PARITY_EIP_2098; - recoveryParam -= tx.chainId * 2 + BULLSHIT_NUMBER; + recoveryParam -= tx.chainId * 2 + EIGHT; } // NOTE: Serialization needs to happen here because chainId may not populated before