Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Aug 10, 2023
1 parent 951c8da commit ee18f34
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
8 changes: 6 additions & 2 deletions .changeset/sweet-lemons-explain.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
"viem": minor
---

Added `fees` to `chain` config that includes a `getDefaultPriorityFee` for setting a default priority fee for a chain.
Added `fees` to `chain` config that includes a `defaultPriorityFee` for setting a default priority fee (`maxFeePerGas`) for a chain.

```ts
import type { Chain } from 'viem'

export const example = {
// ...
fees: {
getDefaultPriorityFee: () => 1_000_000n, // 0.001 gwei
defaultPriorityFee: 1_000_000n, // 0.001 gwei
// or
async defaultPriorityFee() {
// ... some async behavior to derive the fee.
}
},
// ...
} as const satifies Chain
Expand Down
2 changes: 1 addition & 1 deletion src/chains/optimism/fees.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainFees } from '../../types/chain.js'

export const feesOptimism = {
getDefaultPriorityFee: () => 1_000_000n, // 0.001 gwei
defaultPriorityFee: 1_000_000n, // 0.001 gwei
} as const satisfies ChainFees
14 changes: 8 additions & 6 deletions src/types/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export type ChainContract = {
export type ChainFees<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = {
getDefaultPriorityFee(args: {
block: Prettify<FormattedBlock<{ formatters: formatters }>>
request: PrepareRequestParameters<
Omit<Chain, 'formatters'> & { formatters: formatters }
>
}): Promise<bigint> | bigint
defaultPriorityFee:
| bigint
| ((args: {
block: Prettify<FormattedBlock<{ formatters: formatters }>>
request: PrepareRequestParameters<
Omit<Chain, 'formatters'> & { formatters: formatters }
>
}) => Promise<bigint> | bigint)
}

export type ChainFormatters = {
Expand Down
51 changes: 43 additions & 8 deletions src/utils/transaction/prepareRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe('prepareRequest', () => {
chain: {
...anvilChain,
fees: {
getDefaultPriorityFee: () => parseGwei('69'),
defaultPriorityFee: () => parseGwei('69'),
},
},
transport: http(localHttpUrl),
Expand All @@ -465,7 +465,7 @@ describe('prepareRequest', () => {
chain: {
...anvilChain,
fees: {
getDefaultPriorityFee: async () => parseGwei('69'),
defaultPriorityFee: async () => parseGwei('69'),
},
},
transport: http(localHttpUrl),
Expand All @@ -479,29 +479,32 @@ describe('prepareRequest', () => {
(block.baseFeePerGas! * 120n) / 100n + parseGwei('69'),
)

// chain override
const request_3 = await prepareRequest(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
// client chain (bigint)
const client_3 = createWalletClient({
chain: {
...anvilChain,
fees: {
getDefaultPriorityFee: () => parseGwei('69'),
defaultPriorityFee: parseGwei('69'),
},
},
transport: http(localHttpUrl),
})
const request_3 = await prepareRequest(client_3, {
account: privateKeyToAccount(sourceAccount.privateKey),
to: targetAccount.address,
value: parseEther('1'),
})
expect(request_3.maxFeePerGas).toEqual(
(block.baseFeePerGas! * 120n) / 100n + parseGwei('69'),
)

// chain override (async)
// chain override (bigint)
const request_4 = await prepareRequest(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
chain: {
...anvilChain,
fees: {
getDefaultPriorityFee: async () => parseGwei('69'),
defaultPriorityFee: () => parseGwei('69'),
},
},
to: targetAccount.address,
Expand All @@ -510,6 +513,38 @@ describe('prepareRequest', () => {
expect(request_4.maxFeePerGas).toEqual(
(block.baseFeePerGas! * 120n) / 100n + parseGwei('69'),
)

// chain override (async)
const request_5 = await prepareRequest(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
chain: {
...anvilChain,
fees: {
defaultPriorityFee: async () => parseGwei('69'),
},
},
to: targetAccount.address,
value: parseEther('1'),
})
expect(request_5.maxFeePerGas).toEqual(
(block.baseFeePerGas! * 120n) / 100n + parseGwei('69'),
)

// chain override (bigint)
const request_6 = await prepareRequest(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
chain: {
...anvilChain,
fees: {
defaultPriorityFee: parseGwei('69'),
},
},
to: targetAccount.address,
value: parseEther('1'),
})
expect(request_6.maxFeePerGas).toEqual(
(block.baseFeePerGas! * 120n) / 100n + parseGwei('69'),
)
})

test('no account', async () => {
Expand Down
15 changes: 10 additions & 5 deletions src/utils/transaction/prepareRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ export async function prepareRequest<
typeof block.baseFeePerGas === 'bigint' &&
typeof gasPrice === 'undefined'
) {
const defaultPriorityFee =
(await chain?.fees?.getDefaultPriorityFee({
block,
request: request as PrepareRequestParameters,
})) || 1_500_000_000n // 1.5 gwei
let defaultPriorityFee = 1_500_000_000n // 1.5 gwei
if (typeof chain?.fees?.defaultPriorityFee !== 'undefined') {
defaultPriorityFee =
typeof chain.fees.defaultPriorityFee === 'bigint'
? chain.fees.defaultPriorityFee
: await chain.fees.defaultPriorityFee({
block,
request: request as PrepareRequestParameters,
})
}

// EIP-1559 fees
if (typeof maxFeePerGas === 'undefined') {
Expand Down

0 comments on commit ee18f34

Please sign in to comment.