Skip to content

Commit

Permalink
refactor: some comments review adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Oct 4, 2023
1 parent b3b9a5c commit c64f904
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions src/lib/CeloProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/lib/CeloWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CeloTransaction>
transaction: utils.Deferrable<CeloTransactionRequest>
): Promise<BigNumber> {
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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/transaction/utils.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -11,6 +12,7 @@ function isEmpty(value: string | BigNumberish | undefined | null) {
: hexlify(value) === "0x0")
);
}

function isPresent(value: string | BigNumberish | undefined | null) {
return !isEmpty(value);
}
Expand All @@ -27,6 +29,7 @@ export function isCIP64(tx: any) {
!isPresent(tx.gatewayFeeRecipient)
);
}

export function isCIP42(tx: any): boolean {
return (
isEIP1559(tx) &&
Expand Down Expand Up @@ -55,9 +58,6 @@ export function omit<T extends Object, K extends (keyof T)[]>(
}

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);
}
13 changes: 5 additions & 8 deletions src/lib/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c64f904

Please sign in to comment.