tempo.ts 0.8.1 → 0.8.2

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.
@@ -1810,6 +1810,62 @@ export type Decorator<
1810
1810
  startSync: (
1811
1811
  parameters: rewardActions.startSync.Parameters<chain, account>,
1812
1812
  ) => Promise<rewardActions.startSync.ReturnValue>
1813
+ /**
1814
+ * Watches for reward recipient set events.
1815
+ *
1816
+ * @example
1817
+ * ```ts
1818
+ * import { createClient, http } from 'viem'
1819
+ * import { tempo } from 'tempo.ts/chains'
1820
+ * import { tempoActions } from 'tempo.ts/viem'
1821
+ *
1822
+ * const client = createClient({
1823
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
1824
+ * transport: http(),
1825
+ * }).extend(tempoActions())
1826
+ *
1827
+ * const unwatch = client.reward.watchRewardRecipientSet({
1828
+ * token: '0x20c0000000000000000000000000000000000001',
1829
+ * onRewardRecipientSet: (args, log) => {
1830
+ * console.log('Reward recipient set:', args)
1831
+ * },
1832
+ * })
1833
+ * ```
1834
+ *
1835
+ * @param parameters - Parameters.
1836
+ * @returns A function to unsubscribe from the event.
1837
+ */
1838
+ watchRewardRecipientSet: (
1839
+ parameters: rewardActions.watchRewardRecipientSet.Parameters,
1840
+ ) => () => void
1841
+ /**
1842
+ * Watches for reward scheduled events.
1843
+ *
1844
+ * @example
1845
+ * ```ts
1846
+ * import { createClient, http } from 'viem'
1847
+ * import { tempo } from 'tempo.ts/chains'
1848
+ * import { tempoActions } from 'tempo.ts/viem'
1849
+ *
1850
+ * const client = createClient({
1851
+ * chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
1852
+ * transport: http(),
1853
+ * }).extend(tempoActions())
1854
+ *
1855
+ * const unwatch = client.reward.watchRewardScheduled({
1856
+ * token: '0x20c0000000000000000000000000000000000001',
1857
+ * onRewardScheduled: (args, log) => {
1858
+ * console.log('Reward scheduled:', args)
1859
+ * },
1860
+ * })
1861
+ * ```
1862
+ *
1863
+ * @param parameters - Parameters.
1864
+ * @returns A function to unsubscribe from the event.
1865
+ */
1866
+ watchRewardScheduled: (
1867
+ parameters: rewardActions.watchRewardScheduled.Parameters,
1868
+ ) => () => void
1813
1869
  }
1814
1870
  token: {
1815
1871
  /**
@@ -3078,6 +3134,10 @@ export function decorator() {
3078
3134
  rewardActions.setRecipientSync(client, parameters),
3079
3135
  start: (parameters) => rewardActions.start(client, parameters),
3080
3136
  startSync: (parameters) => rewardActions.startSync(client, parameters),
3137
+ watchRewardRecipientSet: (parameters) =>
3138
+ rewardActions.watchRewardRecipientSet(client, parameters),
3139
+ watchRewardScheduled: (parameters) =>
3140
+ rewardActions.watchRewardScheduled(client, parameters),
3081
3141
  },
3082
3142
  token: {
3083
3143
  approve: (parameters) => tokenActions.approve(client, parameters),
@@ -216,9 +216,6 @@ export async function serialize(
216
216
  | OneOf<SignatureEnvelope.SignatureEnvelope | viem_Signature>
217
217
  | undefined,
218
218
  ) {
219
- // Convert EIP-1559 transactions to AA transactions.
220
- if (transaction.type === 'eip1559') (transaction as any).type = 'aa'
221
-
222
219
  // If the transaction is not a Tempo transaction, route to Viem serializer.
223
220
  if (!isTempo(transaction)) {
224
221
  if (signature && 'type' in signature && signature.type !== 'secp256k1')
@@ -145,3 +145,72 @@ describe('setRecipientSync', () => {
145
145
  expect(recipient).toBe(account.address)
146
146
  })
147
147
  })
148
+
149
+ describe('watchRewardScheduled', () => {
150
+ test('default', async () => {
151
+ const { token } = await setupToken()
152
+
153
+ const account = getAccount(config)
154
+
155
+ // Setup rewards
156
+ await actions.setRecipientSync(config, {
157
+ recipient: account.address!,
158
+ token,
159
+ })
160
+
161
+ const rewardAmount = parseUnits('100', 6)
162
+ await tokenActions.mintSync(config, {
163
+ amount: rewardAmount,
164
+ to: account.address!,
165
+ token,
166
+ })
167
+
168
+ const events: any[] = []
169
+ const unwatch = actions.watchRewardScheduled(config, {
170
+ token,
171
+ onRewardScheduled: (args) => {
172
+ events.push(args)
173
+ },
174
+ })
175
+
176
+ await actions.startSync(config, {
177
+ amount: rewardAmount,
178
+ token,
179
+ })
180
+
181
+ await new Promise((resolve) => setTimeout(resolve, 500))
182
+
183
+ expect(events.length).toBeGreaterThan(0)
184
+ expect(events[0]?.amount).toBe(rewardAmount)
185
+ expect(events[0]?.funder).toBe(account.address)
186
+ unwatch()
187
+ })
188
+ })
189
+
190
+ describe('watchRewardRecipientSet', () => {
191
+ test('default', async () => {
192
+ const { token } = await setupToken()
193
+
194
+ const account = getAccount(config)
195
+
196
+ const events: any[] = []
197
+ const unwatch = actions.watchRewardRecipientSet(config, {
198
+ token,
199
+ onRewardRecipientSet: (args) => {
200
+ events.push(args)
201
+ },
202
+ })
203
+
204
+ await actions.setRecipientSync(config, {
205
+ recipient: account.address!,
206
+ token,
207
+ })
208
+
209
+ await new Promise((resolve) => setTimeout(resolve, 500))
210
+
211
+ expect(events.length).toBeGreaterThan(0)
212
+ expect(events[0]?.holder).toBe(account.address)
213
+ expect(events[0]?.recipient).toBe(account.address)
214
+ unwatch()
215
+ })
216
+ })
@@ -524,3 +524,87 @@ export declare namespace startSync {
524
524
 
525
525
  export type ErrorType = viem_Actions.startSync.ErrorType
526
526
  }
527
+
528
+ /**
529
+ * Watches for reward scheduled events.
530
+ *
531
+ * @example
532
+ * ```ts
533
+ * import { createConfig, http } from '@wagmi/core'
534
+ * import { tempo } from 'tempo.ts/chains'
535
+ * import { Actions } from 'tempo.ts/wagmi'
536
+ *
537
+ * const config = createConfig({
538
+ * chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
539
+ * transports: {
540
+ * [tempo.id]: http(),
541
+ * },
542
+ * })
543
+ *
544
+ * const unwatch = Actions.reward.watchRewardScheduled(config, {
545
+ * token: '0x20c0000000000000000000000000000000000001',
546
+ * onRewardScheduled: (args, log) => {
547
+ * console.log('Reward scheduled:', args)
548
+ * },
549
+ * })
550
+ * ```
551
+ *
552
+ * @param config - Config.
553
+ * @param parameters - Parameters.
554
+ * @returns A function to unsubscribe from the event.
555
+ */
556
+ export function watchRewardScheduled<config extends Config>(
557
+ config: config,
558
+ parameters: watchRewardScheduled.Parameters<config>,
559
+ ) {
560
+ const { chainId, ...rest } = parameters
561
+ const client = config.getClient({ chainId })
562
+ return viem_Actions.watchRewardScheduled(client, rest)
563
+ }
564
+
565
+ export declare namespace watchRewardScheduled {
566
+ export type Parameters<config extends Config> = ChainIdParameter<config> &
567
+ viem_Actions.watchRewardScheduled.Parameters
568
+ }
569
+
570
+ /**
571
+ * Watches for reward recipient set events.
572
+ *
573
+ * @example
574
+ * ```ts
575
+ * import { createConfig, http } from '@wagmi/core'
576
+ * import { tempo } from 'tempo.ts/chains'
577
+ * import { Actions } from 'tempo.ts/wagmi'
578
+ *
579
+ * const config = createConfig({
580
+ * chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
581
+ * transports: {
582
+ * [tempo.id]: http(),
583
+ * },
584
+ * })
585
+ *
586
+ * const unwatch = Actions.reward.watchRewardRecipientSet(config, {
587
+ * token: '0x20c0000000000000000000000000000000000001',
588
+ * onRewardRecipientSet: (args, log) => {
589
+ * console.log('Reward recipient set:', args)
590
+ * },
591
+ * })
592
+ * ```
593
+ *
594
+ * @param config - Config.
595
+ * @param parameters - Parameters.
596
+ * @returns A function to unsubscribe from the event.
597
+ */
598
+ export function watchRewardRecipientSet<config extends Config>(
599
+ config: config,
600
+ parameters: watchRewardRecipientSet.Parameters<config>,
601
+ ) {
602
+ const { chainId, ...rest } = parameters
603
+ const client = config.getClient({ chainId })
604
+ return viem_Actions.watchRewardRecipientSet(client, rest)
605
+ }
606
+
607
+ export declare namespace watchRewardRecipientSet {
608
+ export type Parameters<config extends Config> = ChainIdParameter<config> &
609
+ viem_Actions.watchRewardRecipientSet.Parameters
610
+ }
@@ -1,9 +1,11 @@
1
1
  import { getAccount } from '@wagmi/core'
2
2
  import type { Address } from 'viem'
3
+ import { parseUnits } from 'viem'
3
4
  import { describe, expect, test, vi } from 'vitest'
4
5
  import { useConnect } from 'wagmi'
5
6
  import { config, renderHook, setupToken } from '../../../test/wagmi/config.js'
6
7
  import * as hooks from './reward.js'
8
+ import * as tokenHooks from './token.js'
7
9
 
8
10
  describe('useGetTotalPerSecond', () => {
9
11
  test('default', async () => {
@@ -148,3 +150,100 @@ describe('useSetRecipientSync', () => {
148
150
  )
149
151
  })
150
152
  })
153
+
154
+ describe('useWatchRewardScheduled', () => {
155
+ test('default', async () => {
156
+ const { result: connectResult } = await renderHook(() => ({
157
+ connect: useConnect(),
158
+ grantRolesSync: tokenHooks.useGrantRolesSync(),
159
+ mintSync: tokenHooks.useMintSync(),
160
+ setRecipientSync: hooks.useSetRecipientSync(),
161
+ startSync: hooks.useStartSync(),
162
+ }))
163
+
164
+ await connectResult.current.connect.connectAsync({
165
+ connector: config.connectors[0]!,
166
+ })
167
+
168
+ const { token: tokenAddr } = await setupToken()
169
+
170
+ const account = getAccount(config).address
171
+
172
+ await connectResult.current.grantRolesSync.mutateAsync({
173
+ token: tokenAddr,
174
+ roles: ['issuer'],
175
+ to: account!,
176
+ })
177
+
178
+ const rewardAmount = parseUnits('100', 6)
179
+ await connectResult.current.mintSync.mutateAsync({
180
+ token: tokenAddr,
181
+ to: account!,
182
+ amount: rewardAmount,
183
+ })
184
+
185
+ await connectResult.current.setRecipientSync.mutateAsync({
186
+ token: tokenAddr,
187
+ recipient: account!,
188
+ })
189
+
190
+ const events: any[] = []
191
+ await renderHook(() =>
192
+ hooks.useWatchRewardScheduled({
193
+ token: tokenAddr,
194
+ onRewardScheduled(args) {
195
+ events.push(args)
196
+ },
197
+ }),
198
+ )
199
+
200
+ await connectResult.current.startSync.mutateAsync({
201
+ token: tokenAddr,
202
+ amount: rewardAmount,
203
+ })
204
+
205
+ await vi.waitUntil(() => events.length >= 1)
206
+
207
+ expect(events.length).toBeGreaterThanOrEqual(1)
208
+ expect(events[0]?.amount).toBe(rewardAmount)
209
+ expect(events[0]?.funder).toBe(account)
210
+ })
211
+ })
212
+
213
+ describe('useWatchRewardRecipientSet', () => {
214
+ test('default', async () => {
215
+ const { result: connectResult } = await renderHook(() => ({
216
+ connect: useConnect(),
217
+ setRecipientSync: hooks.useSetRecipientSync(),
218
+ }))
219
+
220
+ await connectResult.current.connect.connectAsync({
221
+ connector: config.connectors[0]!,
222
+ })
223
+
224
+ const { token: tokenAddr } = await setupToken()
225
+
226
+ const account = getAccount(config).address
227
+
228
+ const events: any[] = []
229
+ await renderHook(() =>
230
+ hooks.useWatchRewardRecipientSet({
231
+ token: tokenAddr,
232
+ onRewardRecipientSet(args) {
233
+ events.push(args)
234
+ },
235
+ }),
236
+ )
237
+
238
+ await connectResult.current.setRecipientSync.mutateAsync({
239
+ token: tokenAddr,
240
+ recipient: account!,
241
+ })
242
+
243
+ await vi.waitUntil(() => events.length >= 1)
244
+
245
+ expect(events.length).toBeGreaterThanOrEqual(1)
246
+ expect(events[0]?.holder).toBe(account)
247
+ expect(events[0]?.recipient).toBe(account)
248
+ })
249
+ })
@@ -1,6 +1,7 @@
1
1
  import type { DefaultError } from '@tanstack/query-core'
2
2
  import type { UseMutationResult } from '@tanstack/react-query'
3
3
  import type { Config, ResolvedRegister } from '@wagmi/core'
4
+ import { useEffect } from 'react'
4
5
  import { useChainId, useConfig } from 'wagmi'
5
6
  import type { ConfigParameter, QueryParameter } from 'wagmi/internal'
6
7
  import {
@@ -9,7 +10,7 @@ import {
9
10
  useMutation,
10
11
  useQuery,
11
12
  } from 'wagmi/query'
12
- import type { ExactPartial } from '../../internal/types.js'
13
+ import type { ExactPartial, UnionCompute } from '../../internal/types.js'
13
14
  import * as Actions from '../Actions/reward.js'
14
15
 
15
16
  /**
@@ -542,3 +543,103 @@ export declare namespace useStartSync {
542
543
  context
543
544
  >
544
545
  }
546
+
547
+ /**
548
+ * Hook for watching reward scheduled events.
549
+ *
550
+ * @example
551
+ * ```tsx
552
+ * import { Hooks } from 'tempo.ts/wagmi'
553
+ *
554
+ * function App() {
555
+ * Hooks.reward.useWatchRewardScheduled({
556
+ * token: '0x20c0000000000000000000000000000000000001',
557
+ * onRewardScheduled(args) {
558
+ * console.log('Reward scheduled:', args)
559
+ * },
560
+ * })
561
+ *
562
+ * return <div>Watching for reward scheduled events...</div>
563
+ * }
564
+ * ```
565
+ *
566
+ * @param parameters - Parameters.
567
+ */
568
+ export function useWatchRewardScheduled<
569
+ config extends Config = ResolvedRegister['config'],
570
+ >(parameters: useWatchRewardScheduled.Parameters<config> = {}) {
571
+ const { enabled = true, onRewardScheduled, token, ...rest } = parameters
572
+
573
+ const config = useConfig({ config: parameters.config })
574
+ const configChainId = useChainId({ config })
575
+ const chainId = parameters.chainId ?? configChainId
576
+
577
+ useEffect(() => {
578
+ if (!enabled) return
579
+ if (!onRewardScheduled) return
580
+ if (!token) return
581
+ return Actions.watchRewardScheduled(config, {
582
+ ...rest,
583
+ chainId,
584
+ onRewardScheduled,
585
+ token,
586
+ })
587
+ }, [config, enabled, onRewardScheduled, rest, chainId, token])
588
+ }
589
+
590
+ export declare namespace useWatchRewardScheduled {
591
+ type Parameters<config extends Config = Config> = UnionCompute<
592
+ ExactPartial<Actions.watchRewardScheduled.Parameters<config>> &
593
+ ConfigParameter<config> & { enabled?: boolean | undefined }
594
+ >
595
+ }
596
+
597
+ /**
598
+ * Hook for watching reward recipient set events.
599
+ *
600
+ * @example
601
+ * ```tsx
602
+ * import { Hooks } from 'tempo.ts/wagmi'
603
+ *
604
+ * function App() {
605
+ * Hooks.reward.useWatchRewardRecipientSet({
606
+ * token: '0x20c0000000000000000000000000000000000001',
607
+ * onRewardRecipientSet(args) {
608
+ * console.log('Reward recipient set:', args)
609
+ * },
610
+ * })
611
+ *
612
+ * return <div>Watching for reward recipient set events...</div>
613
+ * }
614
+ * ```
615
+ *
616
+ * @param parameters - Parameters.
617
+ */
618
+ export function useWatchRewardRecipientSet<
619
+ config extends Config = ResolvedRegister['config'],
620
+ >(parameters: useWatchRewardRecipientSet.Parameters<config> = {}) {
621
+ const { enabled = true, onRewardRecipientSet, token, ...rest } = parameters
622
+
623
+ const config = useConfig({ config: parameters.config })
624
+ const configChainId = useChainId({ config })
625
+ const chainId = parameters.chainId ?? configChainId
626
+
627
+ useEffect(() => {
628
+ if (!enabled) return
629
+ if (!onRewardRecipientSet) return
630
+ if (!token) return
631
+ return Actions.watchRewardRecipientSet(config, {
632
+ ...rest,
633
+ chainId,
634
+ onRewardRecipientSet,
635
+ token,
636
+ })
637
+ }, [config, enabled, onRewardRecipientSet, rest, chainId, token])
638
+ }
639
+
640
+ export declare namespace useWatchRewardRecipientSet {
641
+ type Parameters<config extends Config = Config> = UnionCompute<
642
+ ExactPartial<Actions.watchRewardRecipientSet.Parameters<config>> &
643
+ ConfigParameter<config> & { enabled?: boolean | undefined }
644
+ >
645
+ }