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
5 changes: 5 additions & 0 deletions .changeset/tidy-wolves-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

`viem/tempo`: Supported calling token `.call` builders without a Client (restores the pre-`2.54` call signature). When the Client is omitted, `token` must be a TIP20 token id or contract address, and formatted amounts require explicit `decimals`.
10 changes: 7 additions & 3 deletions src/clients/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,13 @@ export function bindActionDecorators(
if (Object.hasOwn(action, key)) {
const helper = action[key]
wrapped[key] = (args: any = {}) => {
if (helper.length >= 2) return helper(client, args)
if (helper.length === 0) return helper()
return helper(args)
// Single-parameter helpers are client-less (e.g. `dex.buy.call(args)`).
// Everything else is called with `(client, args)`: two-parameter
// helpers, rest-parameter helpers that accept `(client, args)` or
// `(args)` (`length === 0`, e.g. `token.transfer.call`), and
// zero-argument helpers, which ignore the extra arguments.
if (helper.length === 1) return helper(args)
return helper(client, args)
}
}
for (const key of ['extractEvent', 'extractEvents'] as const)
Expand Down
48 changes: 48 additions & 0 deletions src/tempo/actions/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,54 @@ const tokenlessClient = getClient({
tokens: undefined,
})

describe('call (without client)', () => {
test('default: builds the same call as the client form', () => {
const args = {
amount: parseUnits('1.25', 6),
to: account2.address,
token: addresses.alphaUsd,
} as const
expect(actions.token.transfer.call(args)).toEqual(
actions.token.transfer.call(client, args),
)

expect(
actions.token.approve.call({
amount: 100n,
spender: account2.address,
token: TokenId.fromAddress(addresses.alphaUsd),
}).to,
).toBe(addresses.alphaUsd)

expect(
actions.token.getBalance.call({
account: account2.address,
token: addresses.alphaUsd,
}).args,
).toEqual([account2.address])
})

test('amount: formatted amounts require explicit decimals', () => {
expect(
actions.token.mint.call({
amount: { decimals: 6, formatted: '1.25' },
to: account2.address,
token: addresses.alphaUsd,
}).args,
).toEqual([account2.address, parseUnits('1.25', 6)])

expect(() =>
actions.token.mint.call({
amount: { formatted: '1.25' },
to: account2.address,
token: addresses.alphaUsd,
}),
).toThrowErrorMatchingInlineSnapshot(
`[Error: Token decimals are required. Pass \`amount.decimals\` or select a declared token.]`,
)
})
})

describe('approve', () => {
test('decimals: requires decimals for an undeclared formatted amount', async () => {
await expect(
Expand Down
Loading
Loading