|
| 1 | +import type { Account } from 'viem' |
| 2 | +import { createClient } from 'viem' |
| 3 | +import { Actions, Addresses } from 'viem/tempo' |
| 4 | +import { describe, expect, test } from 'vp/test' |
| 5 | +import { nodeEnv } from '~test/config.js' |
| 6 | +import { accounts, asset, chain, fundAccount, http } from '~test/tempo/viem.js' |
| 7 | + |
| 8 | +import { resolveFeeToken } from './fee-token.js' |
| 9 | + |
| 10 | +const isLocalnet = nodeEnv === 'localnet' |
| 11 | + |
| 12 | +function clientFor(account: Account) { |
| 13 | + return createClient({ |
| 14 | + account, |
| 15 | + chain, |
| 16 | + transport: http(), |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +function expectAddress(actual: string | undefined, expected: string) { |
| 21 | + expect(actual?.toLowerCase()).toBe(expected.toLowerCase()) |
| 22 | +} |
| 23 | + |
| 24 | +describe.runIf(isLocalnet)('resolveFeeToken', () => { |
| 25 | + test('uses the funded account fee preference first', async () => { |
| 26 | + const account = accounts[11] |
| 27 | + const client = clientFor(account) |
| 28 | + await fundAccount({ address: account.address, token: asset }) |
| 29 | + await Actions.fee.setUserTokenSync(client, { |
| 30 | + feeToken: asset, |
| 31 | + token: asset, |
| 32 | + } as never) |
| 33 | + |
| 34 | + const feeToken = await resolveFeeToken({ |
| 35 | + account: account.address, |
| 36 | + candidateTokens: [Addresses.pathUsd], |
| 37 | + client, |
| 38 | + }) |
| 39 | + |
| 40 | + expectAddress(feeToken, asset) |
| 41 | + }) |
| 42 | + |
| 43 | + test('falls through to the first funded candidate token', async () => { |
| 44 | + const account = accounts[12] |
| 45 | + const client = clientFor(account) |
| 46 | + await fundAccount({ address: account.address, token: asset }) |
| 47 | + |
| 48 | + const feeToken = await resolveFeeToken({ |
| 49 | + account: account.address, |
| 50 | + candidateTokens: [Addresses.pathUsd, asset], |
| 51 | + client, |
| 52 | + }) |
| 53 | + |
| 54 | + expectAddress(feeToken, asset) |
| 55 | + }) |
| 56 | + |
| 57 | + test('falls through from an unfunded account fee preference', async () => { |
| 58 | + const account = accounts[13] |
| 59 | + const client = clientFor(account) |
| 60 | + await fundAccount({ address: account.address, token: asset }) |
| 61 | + await Actions.fee.setUserTokenSync(client, { |
| 62 | + feeToken: asset, |
| 63 | + token: Addresses.pathUsd, |
| 64 | + } as never) |
| 65 | + |
| 66 | + const feeToken = await resolveFeeToken({ |
| 67 | + account: account.address, |
| 68 | + candidateTokens: [asset], |
| 69 | + client, |
| 70 | + }) |
| 71 | + |
| 72 | + expectAddress(feeToken, asset) |
| 73 | + }) |
| 74 | + |
| 75 | + test('uses a funded chain fee token when configured', async () => { |
| 76 | + const account = accounts[14] |
| 77 | + const client = createClient({ |
| 78 | + account, |
| 79 | + chain: { ...chain, feeToken: asset }, |
| 80 | + transport: http(), |
| 81 | + }) |
| 82 | + await fundAccount({ address: account.address, token: asset }) |
| 83 | + |
| 84 | + const feeToken = await resolveFeeToken({ |
| 85 | + account: account.address, |
| 86 | + candidateTokens: [Addresses.pathUsd], |
| 87 | + client, |
| 88 | + }) |
| 89 | + |
| 90 | + expectAddress(feeToken, asset) |
| 91 | + }) |
| 92 | + |
| 93 | + test('falls through from an unfunded chain fee token', async () => { |
| 94 | + const account = accounts[15] |
| 95 | + const client = createClient({ |
| 96 | + account, |
| 97 | + chain: { ...chain, feeToken: Addresses.pathUsd }, |
| 98 | + transport: http(), |
| 99 | + }) |
| 100 | + await fundAccount({ address: account.address, token: asset }) |
| 101 | + |
| 102 | + const feeToken = await resolveFeeToken({ |
| 103 | + account: account.address, |
| 104 | + candidateTokens: [asset], |
| 105 | + client, |
| 106 | + }) |
| 107 | + |
| 108 | + expectAddress(feeToken, asset) |
| 109 | + }) |
| 110 | + |
| 111 | + test('falls back to the first known token when none are funded', async () => { |
| 112 | + const account = accounts[16] |
| 113 | + const client = clientFor(account) |
| 114 | + |
| 115 | + const feeToken = await resolveFeeToken({ |
| 116 | + account: account.address, |
| 117 | + candidateTokens: [asset, Addresses.pathUsd], |
| 118 | + client, |
| 119 | + }) |
| 120 | + |
| 121 | + expectAddress(feeToken, asset) |
| 122 | + }) |
| 123 | +}) |
0 commit comments