Skip to content
Merged
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
89 changes: 89 additions & 0 deletions src/test/unit/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,95 @@ describe('Should validate blockchain network connections', () => {
expect(isConfidentialEVM(BigInt(DEVELOPMENT_CHAIN_ID))).to.be.equal(false)
})

// Regression tests for the "Cannot mix BigInt and other types" crash in collectFees.
// On legacy / non-EIP-1559 chains (e.g. Ganache / barge chainId 8996) a block has no
// `baseFeePerGas`, so it comes back as null and `null + priorityFee` used to throw.
describe('calculateGasCost gas-fee math', () => {
const DESTINATION = '0x0000000000000000000000000000000000000001'
const ESTIMATED_GAS = 21000n
// parseUnits('2', 'gwei')
const PRIORITY_FEE = 2_000_000_000n

let originalGetProvider: any
function stubProvider(block: { baseFeePerGas: bigint | null } | null) {
;(blockchain as any).getProvider = () =>
Promise.resolve({
estimateGas: () => Promise.resolve(ESTIMATED_GAS),
getBlock: () => Promise.resolve(block)
})
}
beforeEach(() => {
originalGetProvider = (blockchain as any).getProvider
})
afterEach(() => {
;(blockchain as any).getProvider = originalGetProvider
})

it('returns a bigint when baseFeePerGas is null (non-EIP-1559 chain)', async () => {
stubProvider({ baseFeePerGas: null })
const amount = 1n
const cost = await blockchain.calculateGasCost(DESTINATION, amount)
expect(typeof cost).to.equal('bigint')
// baseFee defaults to 0n, so maxFee === priorityFee
expect(cost).to.equal(amount + ESTIMATED_GAS * PRIORITY_FEE)
})

it('returns a bigint when getBlock returns null', async () => {
stubProvider(null)
const amount = 5n
const cost = await blockchain.calculateGasCost(DESTINATION, amount)
expect(typeof cost).to.equal('bigint')
expect(cost).to.equal(amount + ESTIMATED_GAS * PRIORITY_FEE)
})

it('still includes the base fee on EIP-1559 chains', async () => {
const baseFee = 100n
stubProvider({ baseFeePerGas: baseFee })
const amount = 1n
const cost = await blockchain.calculateGasCost(DESTINATION, amount)
expect(cost).to.equal(amount + ESTIMATED_GAS * (baseFee + PRIORITY_FEE))
})
})

describe('getGasOptions fee-field handling', () => {
let originalSigner: any
function stubFeeData(feeData: any) {
// eslint-disable-next-line require-await
;(blockchain as any).signer = { provider: { getFeeData: async () => feeData } }
}
beforeEach(() => {
originalSigner = (blockchain as any).signer
})
afterEach(() => {
;(blockchain as any).signer = originalSigner
})

it('applies the multiplier on EIP-1559 fee data', async () => {
stubFeeData({ maxPriorityFeePerGas: 100n, maxFeePerGas: 1000n, gasPrice: 7n })
const opts: any = await blockchain.getGasOptions(21000n, 1.5)
expect(opts.gasLimit).to.equal(41000n)
expect(opts.maxPriorityFeePerGas).to.equal(150n)
expect(opts.maxFeePerGas).to.equal(1500n)
})

it('falls back to gasPrice when maxFeePerGas is null (partial EIP-1559 data)', async () => {
// A priority fee without a maxFeePerGas is a degenerate pair; rather than produce an
// invalid (maxFee < priority) tx, getGasOptions falls through to the gasPrice branch.
stubFeeData({ maxPriorityFeePerGas: 100n, maxFeePerGas: null, gasPrice: 7n })
const opts: any = await blockchain.getGasOptions(21000n, 1)
expect(opts.gasPrice).to.equal(7n)
expect(opts.maxFeePerGas).to.equal(undefined)
expect(opts.maxPriorityFeePerGas).to.equal(undefined)
})

it('falls back to gasPrice when maxPriorityFeePerGas is null (legacy chain)', async () => {
stubFeeData({ maxPriorityFeePerGas: null, maxFeePerGas: null, gasPrice: 7n })
const opts: any = await blockchain.getGasOptions(21000n, 1)
expect(opts.gasPrice).to.equal(7n)
expect(opts.maxFeePerGas).to.equal(undefined)
})
})

after(async () => {
await tearDownEnvironment(envOverrides)
})
Expand Down
13 changes: 11 additions & 2 deletions src/utils/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ export class Blockchain {
})

const block = await provider.getBlock('latest')
const baseFee = block.baseFeePerGas
// `baseFeePerGas` is null on legacy / non-EIP-1559 chains (e.g. Ganache / barge
// chainId 8996), and `block` itself can be null in edge cases. Default a missing base
// fee to 0n so the gas-cost math stays all-BigInt instead of throwing
// "Cannot mix BigInt and other types, use explicit conversions".
const baseFee = block?.baseFeePerGas ?? 0n
const priorityFee = parseUnits('2', 'gwei')
const maxFee = baseFee + priorityFee
const gasCost = estimatedGas * maxFee
Expand Down Expand Up @@ -208,7 +212,12 @@ export class Blockchain {
const feeData = await this.signer.provider.getFeeData()
const gasLimit = estGas + 20_000n

if (feeData.maxPriorityFeePerGas) {
// Only take the EIP-1559 path when BOTH fields are present. If a chain reports a
// priority fee but no maxFeePerGas (or vice-versa), fall through to the legacy
// gasPrice branch instead of multiplying a null (which throws "Cannot mix BigInt and
// other types") or flooring maxFee to a 1-wei value below the priority fee (an
// invalid, unsubmittable fee pair). Normal EIP-1559 chains are unaffected.
if (feeData.maxPriorityFeePerGas != null && feeData.maxFeePerGas != null) {
const multiplier = BigInt(Math.round(gasFeeMultiplier * 100))

const priority = (feeData.maxPriorityFeePerGas * multiplier) / 100n
Expand Down
Loading