tempo.ts 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/README.md +6 -2
  3. package/dist/ox/Transaction.js +1 -1
  4. package/dist/ox/Transaction.js.map +1 -1
  5. package/dist/server/Handler.d.ts +346 -0
  6. package/dist/server/Handler.d.ts.map +1 -0
  7. package/dist/server/Handler.js +441 -0
  8. package/dist/server/Handler.js.map +1 -0
  9. package/dist/server/Kv.d.ts +16 -0
  10. package/dist/server/Kv.d.ts.map +1 -0
  11. package/dist/server/Kv.js +25 -0
  12. package/dist/server/Kv.js.map +1 -0
  13. package/dist/server/index.d.ts +3 -0
  14. package/dist/server/index.d.ts.map +1 -0
  15. package/dist/server/index.js +3 -0
  16. package/dist/server/index.js.map +1 -0
  17. package/dist/server/internal/requestListener.d.ts +124 -0
  18. package/dist/server/internal/requestListener.d.ts.map +1 -0
  19. package/dist/server/internal/requestListener.js +174 -0
  20. package/dist/server/internal/requestListener.js.map +1 -0
  21. package/dist/viem/Actions/account.d.ts +40 -0
  22. package/dist/viem/Actions/account.d.ts.map +1 -0
  23. package/dist/viem/Actions/account.js +87 -0
  24. package/dist/viem/Actions/account.js.map +1 -0
  25. package/dist/viem/Actions/amm.d.ts +51 -1245
  26. package/dist/viem/Actions/amm.d.ts.map +1 -1
  27. package/dist/viem/Actions/amm.js +15 -478
  28. package/dist/viem/Actions/amm.js.map +1 -1
  29. package/dist/viem/Actions/index.d.ts +1 -0
  30. package/dist/viem/Actions/index.d.ts.map +1 -1
  31. package/dist/viem/Actions/index.js +1 -0
  32. package/dist/viem/Actions/index.js.map +1 -1
  33. package/dist/viem/Actions/reward.d.ts +0 -1067
  34. package/dist/viem/Actions/reward.d.ts.map +1 -1
  35. package/dist/viem/Actions/reward.js +4 -212
  36. package/dist/viem/Actions/reward.js.map +1 -1
  37. package/dist/viem/Decorator.d.ts +28 -263
  38. package/dist/viem/Decorator.d.ts.map +1 -1
  39. package/dist/viem/Decorator.js +2 -10
  40. package/dist/viem/Decorator.js.map +1 -1
  41. package/dist/viem/Storage.d.ts +23 -0
  42. package/dist/viem/Storage.d.ts.map +1 -0
  43. package/dist/viem/Storage.js +47 -0
  44. package/dist/viem/Storage.js.map +1 -0
  45. package/dist/viem/Transport.d.ts +10 -1
  46. package/dist/viem/Transport.d.ts.map +1 -1
  47. package/dist/viem/Transport.js +22 -3
  48. package/dist/viem/Transport.js.map +1 -1
  49. package/dist/viem/internal/utils.d.ts +6 -0
  50. package/dist/viem/internal/utils.d.ts.map +1 -1
  51. package/dist/viem/internal/utils.js +24 -0
  52. package/dist/viem/internal/utils.js.map +1 -1
  53. package/dist/wagmi/Actions/amm.d.ts +0 -225
  54. package/dist/wagmi/Actions/amm.d.ts.map +1 -1
  55. package/dist/wagmi/Actions/amm.js +0 -248
  56. package/dist/wagmi/Actions/amm.js.map +1 -1
  57. package/dist/wagmi/Actions/reward.d.ts +0 -110
  58. package/dist/wagmi/Actions/reward.d.ts.map +1 -1
  59. package/dist/wagmi/Actions/reward.js +0 -121
  60. package/dist/wagmi/Actions/reward.js.map +1 -1
  61. package/dist/wagmi/Connector.d.ts +6 -17
  62. package/dist/wagmi/Connector.d.ts.map +1 -1
  63. package/dist/wagmi/Connector.js +17 -43
  64. package/dist/wagmi/Connector.js.map +1 -1
  65. package/dist/wagmi/Hooks/amm.d.ts +0 -236
  66. package/dist/wagmi/Hooks/amm.d.ts.map +1 -1
  67. package/dist/wagmi/Hooks/amm.js +0 -285
  68. package/dist/wagmi/Hooks/amm.js.map +1 -1
  69. package/dist/wagmi/Hooks/reward.d.ts +0 -88
  70. package/dist/wagmi/Hooks/reward.d.ts.map +1 -1
  71. package/dist/wagmi/Hooks/reward.js +0 -103
  72. package/dist/wagmi/Hooks/reward.js.map +1 -1
  73. package/dist/wagmi/KeyManager.d.ts +57 -0
  74. package/dist/wagmi/KeyManager.d.ts.map +1 -0
  75. package/dist/wagmi/KeyManager.js +101 -0
  76. package/dist/wagmi/KeyManager.js.map +1 -0
  77. package/dist/wagmi/index.d.ts +1 -0
  78. package/dist/wagmi/index.d.ts.map +1 -1
  79. package/dist/wagmi/index.js +1 -0
  80. package/dist/wagmi/index.js.map +1 -1
  81. package/package.json +8 -2
  82. package/src/ox/Transaction.ts +1 -1
  83. package/src/ox/e2e.test.ts +7 -0
  84. package/src/server/Handler.test.ts +566 -0
  85. package/src/server/Handler.ts +577 -0
  86. package/src/server/Kv.ts +40 -0
  87. package/src/server/index.ts +2 -0
  88. package/src/server/internal/requestListener.ts +285 -0
  89. package/src/viem/Actions/account.test.ts +414 -0
  90. package/src/viem/Actions/account.ts +108 -0
  91. package/src/viem/Actions/amm.test.ts +10 -284
  92. package/src/viem/Actions/amm.ts +88 -768
  93. package/src/viem/Actions/index.ts +1 -0
  94. package/src/viem/Actions/reward.test.ts +4 -212
  95. package/src/viem/Actions/reward.ts +4 -291
  96. package/src/viem/Decorator.test.ts +1 -0
  97. package/src/viem/Decorator.ts +32 -294
  98. package/src/viem/Storage.ts +88 -0
  99. package/src/viem/Transport.ts +40 -2
  100. package/src/viem/e2e.test.ts +106 -3
  101. package/src/viem/internal/utils.ts +21 -0
  102. package/src/wagmi/Actions/amm.test.ts +7 -85
  103. package/src/wagmi/Actions/amm.ts +0 -346
  104. package/src/wagmi/Actions/reward.test.ts +0 -99
  105. package/src/wagmi/Actions/reward.ts +0 -203
  106. package/src/wagmi/Connector.test.ts +4 -1
  107. package/src/wagmi/Connector.ts +24 -58
  108. package/src/wagmi/Hooks/amm.test.ts +8 -200
  109. package/src/wagmi/Hooks/amm.ts +0 -443
  110. package/src/wagmi/Hooks/reward.test.ts +1 -142
  111. package/src/wagmi/Hooks/reward.ts +0 -196
  112. package/src/wagmi/KeyManager.ts +159 -0
  113. package/src/wagmi/index.ts +1 -0
@@ -1,5 +1,4 @@
1
1
  import * as Address from 'ox/Address'
2
- import * as Bytes from 'ox/Bytes'
3
2
  import type * as Hex from 'ox/Hex'
4
3
  import {
5
4
  createClient,
@@ -12,8 +11,10 @@ import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
12
11
  import { ChainNotConfiguredError, createConnector } from 'wagmi'
13
12
  import * as Account from '../viem/Account.js'
14
13
  import type * as tempo_Chain from '../viem/Chain.js'
14
+ import { normalizeValue } from '../viem/internal/utils.js'
15
15
  import { walletNamespaceCompat } from '../viem/Transport.js'
16
16
  import * as WebAuthnP256 from '../viem/WebAuthnP256.js'
17
+ import type * as KeyManager from './KeyManager.js'
17
18
 
18
19
  type Chain = ReturnType<ReturnType<typeof tempo_Chain.define>>
19
20
 
@@ -195,7 +196,7 @@ export declare namespace dangerous_secp256k1 {
195
196
  *
196
197
  * @returns Connector.
197
198
  */
198
- export function webAuthn(options: webAuthn.Parameters = {}) {
199
+ export function webAuthn(options: webAuthn.Parameters) {
199
200
  let account: Account.Account | undefined
200
201
 
201
202
  type Properties = {
@@ -219,7 +220,6 @@ export function webAuthn(options: webAuthn.Parameters = {}) {
219
220
  type StorageItem = {
220
221
  'webAuthn.activeCredential': WebAuthnP256.P256Credential
221
222
  'webAuthn.lastActiveCredential': WebAuthnP256.P256Credential
222
- [key: `webAuthn.${string}.publicKey`]: Hex.Hex
223
223
  }
224
224
 
225
225
  return createConnector<Provider, Properties, StorageItem>((config) => ({
@@ -246,22 +246,23 @@ export function webAuthn(options: webAuthn.Parameters = {}) {
246
246
  typeof parameters.capabilities?.createAccount === 'boolean'
247
247
  ? {}
248
248
  : parameters.capabilities?.createAccount
249
- const challenge = await options.createOptions?.getChallenge?.()
249
+ const createOptions_remote = await options.keyManager.getChallenge?.()
250
250
  credential = await WebAuthnP256.createCredential({
251
251
  ...(options.createOptions ?? {}),
252
- challenge: challenge
253
- ? new Uint8Array(Bytes.fromHex(challenge))
254
- : undefined,
255
252
  label:
256
253
  createOptions.label ??
257
254
  options.createOptions?.label ??
258
255
  `Account ${new Date().toISOString().split('T')[0]}`,
259
- rpId: options.createOptions?.rpId ?? options.rpId,
256
+ rpId:
257
+ createOptions_remote?.rp?.id ??
258
+ options.createOptions?.rpId ??
259
+ options.rpId,
260
+ ...(createOptions_remote ?? {}),
261
+ })
262
+ await options.keyManager.setPublicKey({
263
+ credential: credential.raw,
264
+ publicKey: credential.publicKey,
260
265
  })
261
- config.storage?.setItem(
262
- `webAuthn.${credential.id}.publicKey`,
263
- credential.publicKey,
264
- )
265
266
  } else {
266
267
  // Load credential (log in)
267
268
  credential = (await config.storage?.getItem(
@@ -278,19 +279,11 @@ export function webAuthn(options: webAuthn.Parameters = {}) {
278
279
  // biome-ignore lint/suspicious/noTsIgnore: _
279
280
  // @ts-ignore
280
281
  async getPublicKey(credential) {
281
- {
282
- const publicKey =
283
- await options.getOptions?.getPublicKey?.(credential)
284
- if (publicKey) return publicKey
285
- }
286
-
287
- {
288
- const publicKey = await config.storage?.getItem(
289
- `webAuthn.${credential.id}.publicKey`,
290
- )
291
- if (!publicKey) throw new Error('publicKey not found')
292
- return publicKey as Hex.Hex
293
- }
282
+ const publicKey = await options.keyManager.getPublicKey({
283
+ credential,
284
+ })
285
+ if (!publicKey) throw new Error('publicKey not found.')
286
+ return publicKey
294
287
  },
295
288
  rpId: options.getOptions?.rpId ?? options.rpId,
296
289
  })
@@ -380,49 +373,22 @@ export function webAuthn(options: webAuthn.Parameters = {}) {
380
373
  }))
381
374
  }
382
375
 
383
- export declare namespace webAuthn {
376
+ export namespace webAuthn {
384
377
  export type Parameters = {
385
378
  /** Options for WebAuthn registration. */
386
379
  createOptions?:
387
- | (Pick<
380
+ | Pick<
388
381
  WebAuthnP256.createCredential.Parameters,
389
382
  'createFn' | 'label' | 'rpId' | 'userId' | 'timeout'
390
- > & {
391
- /** Function to fetch a challenge to sign over at registration. */
392
- getChallenge?: (() => Promise<Hex.Hex>) | undefined
393
- })
383
+ >
394
384
  | undefined
395
385
  /** Options for WebAuthn authentication. */
396
386
  getOptions?:
397
- | (Pick<WebAuthnP256.getCredential.Parameters, 'getFn' | 'rpId'> & {
398
- /** Function to fetch the public key for a credential. */
399
- getPublicKey?:
400
- | WebAuthnP256.getCredential.Parameters['getPublicKey']
401
- | undefined
402
- })
387
+ | Pick<WebAuthnP256.getCredential.Parameters, 'getFn' | 'rpId'>
403
388
  | undefined
389
+ /** Public key manager. */
390
+ keyManager: KeyManager.KeyManager
404
391
  /** The RP ID to use for WebAuthn. */
405
392
  rpId?: string | undefined
406
393
  }
407
394
  }
408
-
409
- /**
410
- * Normalizes a value into a structured-clone compatible format.
411
- *
412
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone
413
- */
414
- export function normalizeValue<type>(value: type): type {
415
- if (Array.isArray(value)) return value.map(normalizeValue) as never
416
- if (typeof value === 'function') return undefined as never
417
- if (typeof value !== 'object' || value === null) return value
418
- if (Object.getPrototypeOf(value) !== Object.prototype)
419
- try {
420
- return structuredClone(value)
421
- } catch {
422
- return undefined as never
423
- }
424
-
425
- const normalized: Record<string, unknown> = {}
426
- for (const [k, v] of Object.entries(value)) normalized[k] = normalizeValue(v)
427
- return normalized as never
428
- }
@@ -1,9 +1,8 @@
1
- import { getConnectorClient } from '@wagmi/core'
2
1
  import { type Address, parseUnits } from 'viem'
3
2
  import { describe, expect, test, vi } from 'vitest'
4
3
  import { useConnect } from 'wagmi'
5
4
  import { addresses } from '../../../test/config.js'
6
- import { accounts, setupPoolWithLiquidity } from '../../../test/viem/config.js'
5
+ import { accounts } from '../../../test/viem/config.js'
7
6
  import { config, renderHook } from '../../../test/wagmi/config.js'
8
7
  import * as hooks from './amm.js'
9
8
  import * as tokenHooks from './token.js'
@@ -20,7 +19,7 @@ describe('usePool', () => {
20
19
  )
21
20
 
22
21
  await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy(), {
23
- timeout: 5_000,
22
+ timeout: 10_000,
24
23
  })
25
24
 
26
25
  expect(result.current.data).toMatchInlineSnapshot(`
@@ -151,14 +150,9 @@ describe('useMintSync', () => {
151
150
 
152
151
  // Add liquidity to pool
153
152
  const data = await result.current.mintSync.mutateAsync({
154
- userToken: {
155
- address: token,
156
- amount: parseUnits('100', 6),
157
- },
158
- validatorToken: {
159
- address: addresses.alphaUsd,
160
- amount: parseUnits('100', 6),
161
- },
153
+ userTokenAddress: token,
154
+ validatorTokenAddress: addresses.alphaUsd,
155
+ validatorTokenAmount: parseUnits('100', 6),
162
156
  to: account.address,
163
157
  })
164
158
 
@@ -167,134 +161,10 @@ describe('useMintSync', () => {
167
161
  )
168
162
 
169
163
  expect(data.receipt).toBeDefined()
170
- expect(data.amountUserToken).toBe(parseUnits('100', 6))
171
164
  expect(data.amountValidatorToken).toBe(parseUnits('100', 6))
172
165
  })
173
166
  })
174
167
 
175
- describe('useBurnSync', () => {
176
- test('default', async () => {
177
- const { result } = await renderHook(() => ({
178
- connect: useConnect(),
179
- burnSync: hooks.useBurnSync(),
180
- getLiquidityBalance: hooks.useLiquidityBalance,
181
- }))
182
-
183
- await result.current.connect.connectAsync({
184
- connector: config.connectors[0]!,
185
- })
186
-
187
- const client = await getConnectorClient(config)
188
- const { tokenAddress } = await setupPoolWithLiquidity(client)
189
-
190
- // Get LP balance before burn
191
- const { result: balanceResult } = await renderHook(() =>
192
- result.current.getLiquidityBalance({
193
- userToken: tokenAddress,
194
- validatorToken: addresses.alphaUsd,
195
- address: account.address,
196
- }),
197
- )
198
-
199
- await vi.waitFor(() => expect(balanceResult.current.isSuccess).toBeTruthy())
200
-
201
- const lpBalanceBefore = balanceResult.current.data!
202
-
203
- // Burn half of LP tokens
204
- const data = await result.current.burnSync.mutateAsync({
205
- userToken: tokenAddress,
206
- validatorToken: addresses.alphaUsd,
207
- liquidity: lpBalanceBefore / 2n,
208
- to: account.address,
209
- })
210
-
211
- await vi.waitFor(() =>
212
- expect(result.current.burnSync.isSuccess).toBeTruthy(),
213
- )
214
-
215
- expect(data.receipt).toBeDefined()
216
- expect(data.liquidity).toBe(lpBalanceBefore / 2n)
217
- })
218
- })
219
-
220
- describe('useRebalanceSwapSync', () => {
221
- test('default', async () => {
222
- const { result } = await renderHook(() => ({
223
- connect: useConnect(),
224
- rebalanceSwapSync: hooks.useRebalanceSwapSync(),
225
- }))
226
-
227
- await result.current.connect.connectAsync({
228
- connector: config.connectors[0]!,
229
- })
230
-
231
- const client = await getConnectorClient(config)
232
- const { tokenAddress } = await setupPoolWithLiquidity(client)
233
-
234
- const account2 = accounts[1]
235
-
236
- // Perform rebalance swap
237
- const data = await result.current.rebalanceSwapSync.mutateAsync({
238
- userToken: tokenAddress,
239
- validatorToken: addresses.alphaUsd,
240
- amountOut: parseUnits('10', 6),
241
- to: account2.address,
242
- })
243
-
244
- await vi.waitFor(() =>
245
- expect(result.current.rebalanceSwapSync.isSuccess).toBeTruthy(),
246
- )
247
-
248
- expect(data.receipt).toBeDefined()
249
- expect(data.amountOut).toBe(parseUnits('10', 6))
250
- expect(data.swapper).toBe(account.address)
251
- })
252
- })
253
-
254
- describe('useWatchRebalanceSwap', () => {
255
- test('default', async () => {
256
- const { result: connectResult } = await renderHook(() => ({
257
- connect: useConnect(),
258
- rebalanceSwapSync: hooks.useRebalanceSwapSync(),
259
- }))
260
-
261
- await connectResult.current.connect.connectAsync({
262
- connector: config.connectors[0]!,
263
- })
264
-
265
- const client = await getConnectorClient(config)
266
- const { tokenAddress } = await setupPoolWithLiquidity(client)
267
-
268
- const events: any[] = []
269
- await renderHook(() =>
270
- hooks.useWatchRebalanceSwap({
271
- onRebalanceSwap(args) {
272
- events.push(args)
273
- },
274
- }),
275
- )
276
-
277
- const account2 = accounts[1]
278
-
279
- // Perform rebalance swap
280
- await connectResult.current.rebalanceSwapSync.mutateAsync({
281
- userToken: tokenAddress,
282
- validatorToken: addresses.alphaUsd,
283
- amountOut: parseUnits('10', 6),
284
- to: account2.address,
285
- })
286
-
287
- await vi.waitUntil(() => events.length >= 1)
288
-
289
- expect(events.length).toBeGreaterThanOrEqual(1)
290
- expect(events[0]?.userToken.toLowerCase()).toBe(tokenAddress.toLowerCase())
291
- expect(events[0]?.validatorToken.toLowerCase()).toBe(
292
- addresses.alphaUsd.toLowerCase(),
293
- )
294
- expect(events[0]?.amountOut).toBe(parseUnits('10', 6))
295
- })
296
- })
297
-
298
168
  describe('useWatchMint', () => {
299
169
  test('default', async () => {
300
170
  const { result: connectResult } = await renderHook(() => ({
@@ -341,14 +211,9 @@ describe('useWatchMint', () => {
341
211
 
342
212
  // Add liquidity to pool
343
213
  await connectResult.current.mintSync.mutateAsync({
344
- userToken: {
345
- address: token,
346
- amount: parseUnits('100', 6),
347
- },
348
- validatorToken: {
349
- address: addresses.alphaUsd,
350
- amount: parseUnits('100', 6),
351
- },
214
+ userTokenAddress: token,
215
+ validatorTokenAddress: addresses.alphaUsd,
216
+ validatorTokenAmount: parseUnits('100', 6),
352
217
  to: account.address,
353
218
  })
354
219
 
@@ -359,63 +224,6 @@ describe('useWatchMint', () => {
359
224
  expect(events[0]?.validatorToken.address.toLowerCase()).toBe(
360
225
  addresses.alphaUsd.toLowerCase(),
361
226
  )
362
- expect(events[0]?.userToken.amount).toBe(parseUnits('100', 6))
363
227
  expect(events[0]?.validatorToken.amount).toBe(parseUnits('100', 6))
364
228
  })
365
229
  })
366
-
367
- describe('useWatchBurn', () => {
368
- test('default', async () => {
369
- const { result: connectResult } = await renderHook(() => ({
370
- connect: useConnect(),
371
- burnSync: hooks.useBurnSync(),
372
- getLiquidityBalance: hooks.useLiquidityBalance,
373
- }))
374
-
375
- await connectResult.current.connect.connectAsync({
376
- connector: config.connectors[0]!,
377
- })
378
-
379
- const client = await getConnectorClient(config)
380
- const { tokenAddress } = await setupPoolWithLiquidity(client)
381
-
382
- // Get LP balance
383
- const { result: balanceResult } = await renderHook(() =>
384
- connectResult.current.getLiquidityBalance({
385
- userToken: tokenAddress,
386
- validatorToken: addresses.alphaUsd,
387
- address: account.address,
388
- }),
389
- )
390
-
391
- await vi.waitFor(() => expect(balanceResult.current.isSuccess).toBeTruthy())
392
-
393
- const lpBalance = balanceResult.current.data!
394
-
395
- const events: any[] = []
396
- await renderHook(() =>
397
- hooks.useWatchBurn({
398
- onBurn(args) {
399
- events.push(args)
400
- },
401
- }),
402
- )
403
-
404
- // Burn LP tokens
405
- await connectResult.current.burnSync.mutateAsync({
406
- userToken: tokenAddress,
407
- validatorToken: addresses.alphaUsd,
408
- liquidity: lpBalance / 2n,
409
- to: account.address,
410
- })
411
-
412
- await vi.waitUntil(() => events.length >= 1)
413
-
414
- expect(events.length).toBeGreaterThanOrEqual(1)
415
- expect(events[0]?.userToken.toLowerCase()).toBe(tokenAddress.toLowerCase())
416
- expect(events[0]?.validatorToken.toLowerCase()).toBe(
417
- addresses.alphaUsd.toLowerCase(),
418
- )
419
- expect(events[0]?.liquidity).toBe(lpBalance / 2n)
420
- })
421
- })