tempo.ts 0.6.2 → 0.7.1
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.
- package/CHANGELOG.md +134 -0
- package/README.md +6 -2
- package/dist/ox/Transaction.js +1 -1
- package/dist/ox/Transaction.js.map +1 -1
- package/dist/server/Handler.d.ts +346 -0
- package/dist/server/Handler.d.ts.map +1 -0
- package/dist/server/Handler.js +441 -0
- package/dist/server/Handler.js.map +1 -0
- package/dist/server/Kv.d.ts +16 -0
- package/dist/server/Kv.d.ts.map +1 -0
- package/dist/server/Kv.js +25 -0
- package/dist/server/Kv.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/internal/requestListener.d.ts +124 -0
- package/dist/server/internal/requestListener.d.ts.map +1 -0
- package/dist/server/internal/requestListener.js +174 -0
- package/dist/server/internal/requestListener.js.map +1 -0
- package/dist/viem/Actions/amm.d.ts +9 -41
- package/dist/viem/Actions/amm.d.ts.map +1 -1
- package/dist/viem/Actions/amm.js +15 -26
- package/dist/viem/Actions/amm.js.map +1 -1
- package/dist/viem/Actions/reward.d.ts +0 -1067
- package/dist/viem/Actions/reward.d.ts.map +1 -1
- package/dist/viem/Actions/reward.js +4 -212
- package/dist/viem/Actions/reward.js.map +1 -1
- package/dist/viem/Decorator.d.ts +0 -263
- package/dist/viem/Decorator.d.ts.map +1 -1
- package/dist/viem/Decorator.js +0 -10
- package/dist/viem/Decorator.js.map +1 -1
- package/dist/viem/Storage.d.ts +23 -0
- package/dist/viem/Storage.d.ts.map +1 -0
- package/dist/viem/Storage.js +47 -0
- package/dist/viem/Storage.js.map +1 -0
- package/dist/viem/Transport.d.ts +10 -1
- package/dist/viem/Transport.d.ts.map +1 -1
- package/dist/viem/Transport.js +22 -3
- package/dist/viem/Transport.js.map +1 -1
- package/dist/viem/internal/utils.d.ts +6 -0
- package/dist/viem/internal/utils.d.ts.map +1 -1
- package/dist/viem/internal/utils.js +24 -0
- package/dist/viem/internal/utils.js.map +1 -1
- package/dist/wagmi/Actions/reward.d.ts +0 -110
- package/dist/wagmi/Actions/reward.d.ts.map +1 -1
- package/dist/wagmi/Actions/reward.js +0 -121
- package/dist/wagmi/Actions/reward.js.map +1 -1
- package/dist/wagmi/Connector.d.ts +6 -17
- package/dist/wagmi/Connector.d.ts.map +1 -1
- package/dist/wagmi/Connector.js +17 -43
- package/dist/wagmi/Connector.js.map +1 -1
- package/dist/wagmi/Hooks/reward.d.ts +0 -88
- package/dist/wagmi/Hooks/reward.d.ts.map +1 -1
- package/dist/wagmi/Hooks/reward.js +0 -103
- package/dist/wagmi/Hooks/reward.js.map +1 -1
- package/dist/wagmi/KeyManager.d.ts +57 -0
- package/dist/wagmi/KeyManager.d.ts.map +1 -0
- package/dist/wagmi/KeyManager.js +101 -0
- package/dist/wagmi/KeyManager.js.map +1 -0
- package/dist/wagmi/index.d.ts +1 -0
- package/dist/wagmi/index.d.ts.map +1 -1
- package/dist/wagmi/index.js +1 -0
- package/dist/wagmi/index.js.map +1 -1
- package/package.json +8 -2
- package/src/ox/Transaction.ts +1 -1
- package/src/ox/e2e.test.ts +7 -0
- package/src/server/Handler.test.ts +566 -0
- package/src/server/Handler.ts +577 -0
- package/src/server/Kv.ts +40 -0
- package/src/server/index.ts +2 -0
- package/src/server/internal/requestListener.ts +285 -0
- package/src/viem/Actions/amm.test.ts +10 -284
- package/src/viem/Actions/amm.ts +32 -40
- package/src/viem/Actions/reward.test.ts +4 -212
- package/src/viem/Actions/reward.ts +4 -291
- package/src/viem/Decorator.ts +0 -294
- package/src/viem/Storage.ts +88 -0
- package/src/viem/Transport.ts +40 -2
- package/src/viem/e2e.test.ts +106 -3
- package/src/viem/internal/utils.ts +21 -0
- package/src/wagmi/Actions/amm.test.ts +7 -85
- package/src/wagmi/Actions/reward.test.ts +0 -99
- package/src/wagmi/Actions/reward.ts +0 -203
- package/src/wagmi/Connector.test.ts +4 -1
- package/src/wagmi/Connector.ts +24 -58
- package/src/wagmi/Hooks/amm.test.ts +8 -200
- package/src/wagmi/Hooks/reward.test.ts +1 -142
- package/src/wagmi/Hooks/reward.ts +0 -196
- package/src/wagmi/KeyManager.ts +159 -0
- package/src/wagmi/index.ts +1 -0
|
@@ -5,118 +5,6 @@ import type { Account } from 'viem'
|
|
|
5
5
|
import type { RequiredBy, UnionOmit } from '../../internal/types.js'
|
|
6
6
|
import * as viem_Actions from '../../viem/Actions/reward.js'
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Cancels an active reward stream and refunds remaining tokens.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import { createConfig, http } from '@wagmi/core'
|
|
14
|
-
* import { tempo } from 'tempo.ts/chains'
|
|
15
|
-
* import { Actions } from 'tempo.ts/wagmi'
|
|
16
|
-
*
|
|
17
|
-
* const config = createConfig({
|
|
18
|
-
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
19
|
-
* transports: {
|
|
20
|
-
* [tempo.id]: http(),
|
|
21
|
-
* },
|
|
22
|
-
* })
|
|
23
|
-
*
|
|
24
|
-
* const hash = await Actions.reward.cancel(config, {
|
|
25
|
-
* id: 1n,
|
|
26
|
-
* token: '0x20c0000000000000000000000000000000000001',
|
|
27
|
-
* })
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @param config - Config.
|
|
31
|
-
* @param parameters - Parameters.
|
|
32
|
-
* @returns The transaction hash.
|
|
33
|
-
*/
|
|
34
|
-
export async function cancel<config extends Config>(
|
|
35
|
-
config: config,
|
|
36
|
-
parameters: cancel.Parameters<config>,
|
|
37
|
-
): Promise<viem_Actions.cancel.ReturnValue> {
|
|
38
|
-
const { account, chainId, connector } = parameters
|
|
39
|
-
|
|
40
|
-
const client = await getConnectorClient(config, {
|
|
41
|
-
account,
|
|
42
|
-
assertChainId: false,
|
|
43
|
-
chainId,
|
|
44
|
-
connector,
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
return viem_Actions.cancel(client, parameters as never)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export declare namespace cancel {
|
|
51
|
-
export type Parameters<config extends Config = Config> =
|
|
52
|
-
ChainIdParameter<config> &
|
|
53
|
-
ConnectorParameter &
|
|
54
|
-
UnionOmit<
|
|
55
|
-
viem_Actions.cancel.Parameters<config['chains'][number], Account>,
|
|
56
|
-
'chain'
|
|
57
|
-
>
|
|
58
|
-
|
|
59
|
-
export type ReturnValue = viem_Actions.cancel.ReturnValue
|
|
60
|
-
|
|
61
|
-
export type ErrorType = viem_Actions.cancel.ErrorType
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Cancels an active reward stream and waits for confirmation.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* import { createConfig, http } from '@wagmi/core'
|
|
70
|
-
* import { tempo } from 'tempo.ts/chains'
|
|
71
|
-
* import { Actions } from 'tempo.ts/wagmi'
|
|
72
|
-
*
|
|
73
|
-
* const config = createConfig({
|
|
74
|
-
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
75
|
-
* transports: {
|
|
76
|
-
* [tempo.id]: http(),
|
|
77
|
-
* },
|
|
78
|
-
* })
|
|
79
|
-
*
|
|
80
|
-
* const result = await Actions.reward.cancelSync(config, {
|
|
81
|
-
* id: 1n,
|
|
82
|
-
* token: '0x20c0000000000000000000000000000000000001',
|
|
83
|
-
* })
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* @param config - Config.
|
|
87
|
-
* @param parameters - Parameters.
|
|
88
|
-
* @returns The transaction receipt and event data.
|
|
89
|
-
*/
|
|
90
|
-
export async function cancelSync<config extends Config>(
|
|
91
|
-
config: config,
|
|
92
|
-
parameters: cancelSync.Parameters<config>,
|
|
93
|
-
): Promise<viem_Actions.cancelSync.ReturnValue> {
|
|
94
|
-
const { account, chainId, connector } = parameters
|
|
95
|
-
|
|
96
|
-
const client = await getConnectorClient(config, {
|
|
97
|
-
account,
|
|
98
|
-
assertChainId: false,
|
|
99
|
-
chainId,
|
|
100
|
-
connector,
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
return viem_Actions.cancelSync(client, parameters as never)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export declare namespace cancelSync {
|
|
107
|
-
export type Parameters<config extends Config = Config> =
|
|
108
|
-
ChainIdParameter<config> &
|
|
109
|
-
ConnectorParameter &
|
|
110
|
-
UnionOmit<
|
|
111
|
-
viem_Actions.cancelSync.Parameters<config['chains'][number], Account>,
|
|
112
|
-
'chain'
|
|
113
|
-
>
|
|
114
|
-
|
|
115
|
-
export type ReturnValue = viem_Actions.cancelSync.ReturnValue
|
|
116
|
-
|
|
117
|
-
export type ErrorType = viem_Actions.cancelSync.ErrorType
|
|
118
|
-
}
|
|
119
|
-
|
|
120
8
|
/**
|
|
121
9
|
* Claims accumulated rewards for a recipient.
|
|
122
10
|
*
|
|
@@ -227,97 +115,6 @@ export declare namespace claimSync {
|
|
|
227
115
|
export type ErrorType = viem_Actions.claimSync.ErrorType
|
|
228
116
|
}
|
|
229
117
|
|
|
230
|
-
/**
|
|
231
|
-
* Gets a reward stream by its ID.
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* ```ts
|
|
235
|
-
* import { createConfig, http } from '@wagmi/core'
|
|
236
|
-
* import { tempo } from 'tempo.ts/chains'
|
|
237
|
-
* import { Actions } from 'tempo.ts/wagmi'
|
|
238
|
-
*
|
|
239
|
-
* const config = createConfig({
|
|
240
|
-
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
241
|
-
* transports: {
|
|
242
|
-
* [tempo.id]: http(),
|
|
243
|
-
* },
|
|
244
|
-
* })
|
|
245
|
-
*
|
|
246
|
-
* const stream = await Actions.reward.getStream(config, {
|
|
247
|
-
* id: 1n,
|
|
248
|
-
* token: '0x20c0000000000000000000000000000000000001',
|
|
249
|
-
* })
|
|
250
|
-
* ```
|
|
251
|
-
*
|
|
252
|
-
* @param config - Config.
|
|
253
|
-
* @param parameters - Parameters.
|
|
254
|
-
* @returns The reward stream details.
|
|
255
|
-
*/
|
|
256
|
-
export function getStream<config extends Config>(
|
|
257
|
-
config: config,
|
|
258
|
-
parameters: getStream.Parameters<config>,
|
|
259
|
-
) {
|
|
260
|
-
const { chainId, ...rest } = parameters
|
|
261
|
-
const client = config.getClient({ chainId })
|
|
262
|
-
return viem_Actions.getStream(client, rest)
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export namespace getStream {
|
|
266
|
-
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
267
|
-
viem_Actions.getStream.Parameters
|
|
268
|
-
|
|
269
|
-
export type ReturnValue = viem_Actions.getStream.ReturnValue
|
|
270
|
-
|
|
271
|
-
export function queryKey<config extends Config>(
|
|
272
|
-
parameters: Parameters<config>,
|
|
273
|
-
) {
|
|
274
|
-
return ['getStream', parameters] as const
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export type QueryKey<config extends Config> = ReturnType<
|
|
278
|
-
typeof queryKey<config>
|
|
279
|
-
>
|
|
280
|
-
|
|
281
|
-
export function queryOptions<config extends Config, selectData = ReturnValue>(
|
|
282
|
-
config: Config,
|
|
283
|
-
parameters: queryOptions.Parameters<config, selectData>,
|
|
284
|
-
): queryOptions.ReturnValue<config, selectData> {
|
|
285
|
-
const { query, ...rest } = parameters
|
|
286
|
-
return {
|
|
287
|
-
...query,
|
|
288
|
-
queryKey: queryKey(rest),
|
|
289
|
-
async queryFn({ queryKey }) {
|
|
290
|
-
const [, parameters] = queryKey
|
|
291
|
-
return await getStream(config, parameters)
|
|
292
|
-
},
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export declare namespace queryOptions {
|
|
297
|
-
export type Parameters<
|
|
298
|
-
config extends Config,
|
|
299
|
-
selectData = getStream.ReturnValue,
|
|
300
|
-
> = getStream.Parameters<config> & {
|
|
301
|
-
query?:
|
|
302
|
-
| Omit<ReturnValue<config, selectData>, 'queryKey' | 'queryFn'>
|
|
303
|
-
| undefined
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export type ReturnValue<
|
|
307
|
-
config extends Config,
|
|
308
|
-
selectData = getStream.ReturnValue,
|
|
309
|
-
> = RequiredBy<
|
|
310
|
-
Query.QueryOptions<
|
|
311
|
-
getStream.ReturnValue,
|
|
312
|
-
Query.DefaultError,
|
|
313
|
-
selectData,
|
|
314
|
-
getStream.QueryKey<config>
|
|
315
|
-
>,
|
|
316
|
-
'queryKey' | 'queryFn'
|
|
317
|
-
>
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
118
|
/**
|
|
322
119
|
* Gets the total reward per second rate for all active streams.
|
|
323
120
|
*
|
|
@@ -3,6 +3,7 @@ import { cdp } from 'vitest/browser'
|
|
|
3
3
|
import { useAccount, useConnect } from 'wagmi'
|
|
4
4
|
import { renderHook } from '../../test/wagmi/config.js'
|
|
5
5
|
import { webAuthn } from './Connector.js'
|
|
6
|
+
import * as KeyManager from './KeyManager.js'
|
|
6
7
|
|
|
7
8
|
async function setupWebAuthn() {
|
|
8
9
|
const client = cdp()
|
|
@@ -39,7 +40,9 @@ test('connect', async (context) => {
|
|
|
39
40
|
expect(result.current.useAccount.status).toEqual('disconnected')
|
|
40
41
|
|
|
41
42
|
result.current.useConnect.connect({
|
|
42
|
-
connector: webAuthn(
|
|
43
|
+
connector: webAuthn({
|
|
44
|
+
keyManager: KeyManager.localStorage(),
|
|
45
|
+
}),
|
|
43
46
|
capabilities: { createAccount: { label: 'Test Account' } },
|
|
44
47
|
})
|
|
45
48
|
|
package/src/wagmi/Connector.ts
CHANGED
|
@@ -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
|
|
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:
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
|
376
|
+
export namespace webAuthn {
|
|
384
377
|
export type Parameters = {
|
|
385
378
|
/** Options for WebAuthn registration. */
|
|
386
379
|
createOptions?:
|
|
387
|
-
|
|
|
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
|
-
|
|
|
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
|
|
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:
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
345
|
-
|
|
346
|
-
|
|
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
|
-
})
|