tempo.ts 0.8.0 → 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.
- package/CHANGELOG.md +14 -0
- package/dist/viem/Actions/reward.d.ts +164 -1
- package/dist/viem/Actions/reward.d.ts.map +1 -1
- package/dist/viem/Actions/reward.js +135 -1
- package/dist/viem/Actions/reward.js.map +1 -1
- package/dist/viem/Decorator.d.ts +126 -0
- package/dist/viem/Decorator.d.ts.map +1 -1
- package/dist/viem/Decorator.js +5 -0
- package/dist/viem/Decorator.js.map +1 -1
- package/dist/viem/Transaction.d.ts.map +1 -1
- package/dist/viem/Transaction.js +0 -3
- package/dist/viem/Transaction.js.map +1 -1
- package/dist/wagmi/Actions/reward.d.ts +108 -0
- package/dist/wagmi/Actions/reward.d.ts.map +1 -1
- package/dist/wagmi/Actions/reward.js +115 -0
- package/dist/wagmi/Actions/reward.js.map +1 -1
- package/dist/wagmi/Hooks/reward.d.ts +87 -1
- package/dist/wagmi/Hooks/reward.d.ts.map +1 -1
- package/dist/wagmi/Hooks/reward.js +122 -0
- package/dist/wagmi/Hooks/reward.js.map +1 -1
- package/package.json +1 -1
- package/src/viem/Actions/reward.test.ts +167 -0
- package/src/viem/Actions/reward.ts +233 -1
- package/src/viem/Decorator.ts +144 -0
- package/src/viem/Transaction.ts +0 -3
- package/src/wagmi/Actions/reward.test.ts +105 -0
- package/src/wagmi/Actions/reward.ts +175 -0
- package/src/wagmi/Hooks/reward.test.ts +171 -0
- package/src/wagmi/Hooks/reward.ts +170 -1
package/src/viem/Decorator.ts
CHANGED
|
@@ -1591,6 +1591,60 @@ export type Decorator<
|
|
|
1591
1591
|
) => () => void
|
|
1592
1592
|
}
|
|
1593
1593
|
reward: {
|
|
1594
|
+
/**
|
|
1595
|
+
* Claims accumulated rewards for a recipient.
|
|
1596
|
+
*
|
|
1597
|
+
* @example
|
|
1598
|
+
* ```ts
|
|
1599
|
+
* import { createClient, http } from 'viem'
|
|
1600
|
+
* import { privateKeyToAccount } from 'viem/accounts'
|
|
1601
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
1602
|
+
* import { tempoActions } from 'tempo.ts/viem'
|
|
1603
|
+
*
|
|
1604
|
+
* const client = createClient({
|
|
1605
|
+
* account: privateKeyToAccount('0x...'),
|
|
1606
|
+
* chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
|
|
1607
|
+
* transport: http(),
|
|
1608
|
+
* }).extend(tempoActions())
|
|
1609
|
+
*
|
|
1610
|
+
* const hash = await client.reward.claim({
|
|
1611
|
+
* token: '0x20c0000000000000000000000000000000000001',
|
|
1612
|
+
* })
|
|
1613
|
+
* ```
|
|
1614
|
+
*
|
|
1615
|
+
* @param parameters - Parameters.
|
|
1616
|
+
* @returns The transaction hash.
|
|
1617
|
+
*/
|
|
1618
|
+
claim: (
|
|
1619
|
+
parameters: rewardActions.claim.Parameters<chain, account>,
|
|
1620
|
+
) => Promise<rewardActions.claim.ReturnValue>
|
|
1621
|
+
/**
|
|
1622
|
+
* Claims accumulated rewards for a recipient and waits for confirmation.
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```ts
|
|
1626
|
+
* import { createClient, http } from 'viem'
|
|
1627
|
+
* import { privateKeyToAccount } from 'viem/accounts'
|
|
1628
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
1629
|
+
* import { tempoActions } from 'tempo.ts/viem'
|
|
1630
|
+
*
|
|
1631
|
+
* const client = createClient({
|
|
1632
|
+
* account: privateKeyToAccount('0x...'),
|
|
1633
|
+
* chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
|
|
1634
|
+
* transport: http(),
|
|
1635
|
+
* }).extend(tempoActions())
|
|
1636
|
+
*
|
|
1637
|
+
* const result = await client.reward.claimSync({
|
|
1638
|
+
* token: '0x20c0000000000000000000000000000000000001',
|
|
1639
|
+
* })
|
|
1640
|
+
* ```
|
|
1641
|
+
*
|
|
1642
|
+
* @param parameters - Parameters.
|
|
1643
|
+
* @returns The amount claimed and transaction receipt.
|
|
1644
|
+
*/
|
|
1645
|
+
claimSync: (
|
|
1646
|
+
parameters: rewardActions.claimSync.Parameters<chain, account>,
|
|
1647
|
+
) => Promise<rewardActions.claimSync.ReturnValue>
|
|
1594
1648
|
/**
|
|
1595
1649
|
* Gets the total reward per second rate for all active streams.
|
|
1596
1650
|
*
|
|
@@ -1616,6 +1670,32 @@ export type Decorator<
|
|
|
1616
1670
|
getTotalPerSecond: (
|
|
1617
1671
|
parameters: rewardActions.getTotalPerSecond.Parameters,
|
|
1618
1672
|
) => Promise<rewardActions.getTotalPerSecond.ReturnValue>
|
|
1673
|
+
/**
|
|
1674
|
+
* Gets the reward information for a specific account.
|
|
1675
|
+
*
|
|
1676
|
+
* @example
|
|
1677
|
+
* ```ts
|
|
1678
|
+
* import { createClient, http } from 'viem'
|
|
1679
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
1680
|
+
* import { tempoActions } from 'tempo.ts/viem'
|
|
1681
|
+
*
|
|
1682
|
+
* const client = createClient({
|
|
1683
|
+
* chain: tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })
|
|
1684
|
+
* transport: http(),
|
|
1685
|
+
* }).extend(tempoActions())
|
|
1686
|
+
*
|
|
1687
|
+
* const info = await client.reward.getUserRewardInfo({
|
|
1688
|
+
* token: '0x20c0000000000000000000000000000000000001',
|
|
1689
|
+
* account: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
|
1690
|
+
* })
|
|
1691
|
+
* ```
|
|
1692
|
+
*
|
|
1693
|
+
* @param parameters - Parameters.
|
|
1694
|
+
* @returns The user's reward information (recipient, rewardPerToken, rewardBalance).
|
|
1695
|
+
*/
|
|
1696
|
+
getUserRewardInfo: (
|
|
1697
|
+
parameters: rewardActions.getUserRewardInfo.Parameters,
|
|
1698
|
+
) => Promise<rewardActions.getUserRewardInfo.ReturnValue>
|
|
1619
1699
|
/**
|
|
1620
1700
|
* Sets or changes the reward recipient for a token holder.
|
|
1621
1701
|
*
|
|
@@ -1730,6 +1810,62 @@ export type Decorator<
|
|
|
1730
1810
|
startSync: (
|
|
1731
1811
|
parameters: rewardActions.startSync.Parameters<chain, account>,
|
|
1732
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
|
|
1733
1869
|
}
|
|
1734
1870
|
token: {
|
|
1735
1871
|
/**
|
|
@@ -2986,14 +3122,22 @@ export function decorator() {
|
|
|
2986
3122
|
policyActions.watchBlacklistUpdated(client, parameters),
|
|
2987
3123
|
},
|
|
2988
3124
|
reward: {
|
|
3125
|
+
claim: (parameters) => rewardActions.claim(client, parameters),
|
|
3126
|
+
claimSync: (parameters) => rewardActions.claimSync(client, parameters),
|
|
2989
3127
|
getTotalPerSecond: (parameters) =>
|
|
2990
3128
|
rewardActions.getTotalPerSecond(client, parameters),
|
|
3129
|
+
getUserRewardInfo: (parameters) =>
|
|
3130
|
+
rewardActions.getUserRewardInfo(client, parameters),
|
|
2991
3131
|
setRecipient: (parameters) =>
|
|
2992
3132
|
rewardActions.setRecipient(client, parameters),
|
|
2993
3133
|
setRecipientSync: (parameters) =>
|
|
2994
3134
|
rewardActions.setRecipientSync(client, parameters),
|
|
2995
3135
|
start: (parameters) => rewardActions.start(client, parameters),
|
|
2996
3136
|
startSync: (parameters) => rewardActions.startSync(client, parameters),
|
|
3137
|
+
watchRewardRecipientSet: (parameters) =>
|
|
3138
|
+
rewardActions.watchRewardRecipientSet(client, parameters),
|
|
3139
|
+
watchRewardScheduled: (parameters) =>
|
|
3140
|
+
rewardActions.watchRewardScheduled(client, parameters),
|
|
2997
3141
|
},
|
|
2998
3142
|
token: {
|
|
2999
3143
|
approve: (parameters) => tokenActions.approve(client, parameters),
|
package/src/viem/Transaction.ts
CHANGED
|
@@ -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')
|
|
@@ -89,6 +89,42 @@ describe('getTotalPerSecond', () => {
|
|
|
89
89
|
})
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
+
describe('getUserRewardInfo', () => {
|
|
93
|
+
test('default', async () => {
|
|
94
|
+
const { token } = await setupToken()
|
|
95
|
+
|
|
96
|
+
const account = getAccount(config)
|
|
97
|
+
|
|
98
|
+
const info = await actions.getUserRewardInfo(config, {
|
|
99
|
+
token,
|
|
100
|
+
account: account.address!,
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
expect(info.rewardRecipient).toBeDefined()
|
|
104
|
+
expect(info.rewardPerToken).toBeDefined()
|
|
105
|
+
expect(info.rewardBalance).toBeDefined()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
describe('queryOptions', () => {
|
|
109
|
+
test('default', async () => {
|
|
110
|
+
const { token } = await setupToken()
|
|
111
|
+
|
|
112
|
+
const account = getAccount(config)
|
|
113
|
+
|
|
114
|
+
const options = actions.getUserRewardInfo.queryOptions(config, {
|
|
115
|
+
token,
|
|
116
|
+
account: account.address!,
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
const info = await queryClient.fetchQuery(options)
|
|
120
|
+
|
|
121
|
+
expect(info.rewardRecipient).toBeDefined()
|
|
122
|
+
expect(info.rewardPerToken).toBeDefined()
|
|
123
|
+
expect(info.rewardBalance).toBeDefined()
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
92
128
|
describe('setRecipientSync', () => {
|
|
93
129
|
test('default', async () => {
|
|
94
130
|
const { token } = await setupToken()
|
|
@@ -109,3 +145,72 @@ describe('setRecipientSync', () => {
|
|
|
109
145
|
expect(recipient).toBe(account.address)
|
|
110
146
|
})
|
|
111
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
|
+
})
|
|
@@ -205,6 +205,97 @@ export namespace getTotalPerSecond {
|
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
/**
|
|
209
|
+
* Gets the reward information for a specific account.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* import { createConfig, http } from '@wagmi/core'
|
|
214
|
+
* import { tempo } from 'tempo.ts/chains'
|
|
215
|
+
* import { Actions } from 'tempo.ts/wagmi'
|
|
216
|
+
*
|
|
217
|
+
* const config = createConfig({
|
|
218
|
+
* chains: [tempo({ feeToken: '0x20c0000000000000000000000000000000000001' })],
|
|
219
|
+
* transports: {
|
|
220
|
+
* [tempo.id]: http(),
|
|
221
|
+
* },
|
|
222
|
+
* })
|
|
223
|
+
*
|
|
224
|
+
* const info = await Actions.reward.getUserRewardInfo(config, {
|
|
225
|
+
* token: '0x20c0000000000000000000000000000000000001',
|
|
226
|
+
* account: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
|
|
227
|
+
* })
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @param config - Config.
|
|
231
|
+
* @param parameters - Parameters.
|
|
232
|
+
* @returns The user's reward information (recipient, rewardPerToken, rewardBalance).
|
|
233
|
+
*/
|
|
234
|
+
export function getUserRewardInfo<config extends Config>(
|
|
235
|
+
config: config,
|
|
236
|
+
parameters: getUserRewardInfo.Parameters<config>,
|
|
237
|
+
) {
|
|
238
|
+
const { chainId, ...rest } = parameters
|
|
239
|
+
const client = config.getClient({ chainId })
|
|
240
|
+
return viem_Actions.getUserRewardInfo(client, rest)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export namespace getUserRewardInfo {
|
|
244
|
+
export type Parameters<config extends Config> = ChainIdParameter<config> &
|
|
245
|
+
viem_Actions.getUserRewardInfo.Parameters
|
|
246
|
+
|
|
247
|
+
export type ReturnValue = viem_Actions.getUserRewardInfo.ReturnValue
|
|
248
|
+
|
|
249
|
+
export function queryKey<config extends Config>(
|
|
250
|
+
parameters: Parameters<config>,
|
|
251
|
+
) {
|
|
252
|
+
return ['getUserRewardInfo', parameters] as const
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export type QueryKey<config extends Config> = ReturnType<
|
|
256
|
+
typeof queryKey<config>
|
|
257
|
+
>
|
|
258
|
+
|
|
259
|
+
export function queryOptions<config extends Config, selectData = ReturnValue>(
|
|
260
|
+
config: Config,
|
|
261
|
+
parameters: queryOptions.Parameters<config, selectData>,
|
|
262
|
+
): queryOptions.ReturnValue<config, selectData> {
|
|
263
|
+
const { query, ...rest } = parameters
|
|
264
|
+
return {
|
|
265
|
+
...query,
|
|
266
|
+
queryKey: queryKey(rest),
|
|
267
|
+
async queryFn({ queryKey }) {
|
|
268
|
+
const [, parameters] = queryKey
|
|
269
|
+
return await getUserRewardInfo(config, parameters)
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export declare namespace queryOptions {
|
|
275
|
+
export type Parameters<
|
|
276
|
+
config extends Config,
|
|
277
|
+
selectData = getUserRewardInfo.ReturnValue,
|
|
278
|
+
> = getUserRewardInfo.Parameters<config> & {
|
|
279
|
+
query?:
|
|
280
|
+
| Omit<ReturnValue<config, selectData>, 'queryKey' | 'queryFn'>
|
|
281
|
+
| undefined
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export type ReturnValue<
|
|
285
|
+
config extends Config,
|
|
286
|
+
selectData = getUserRewardInfo.ReturnValue,
|
|
287
|
+
> = RequiredBy<
|
|
288
|
+
Query.QueryOptions<
|
|
289
|
+
getUserRewardInfo.ReturnValue,
|
|
290
|
+
Query.DefaultError,
|
|
291
|
+
selectData,
|
|
292
|
+
getUserRewardInfo.QueryKey<config>
|
|
293
|
+
>,
|
|
294
|
+
'queryKey' | 'queryFn'
|
|
295
|
+
>
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
208
299
|
/**
|
|
209
300
|
* Sets or changes the reward recipient for a token holder.
|
|
210
301
|
*
|
|
@@ -433,3 +524,87 @@ export declare namespace startSync {
|
|
|
433
524
|
|
|
434
525
|
export type ErrorType = viem_Actions.startSync.ErrorType
|
|
435
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,8 +1,11 @@
|
|
|
1
|
+
import { getAccount } from '@wagmi/core'
|
|
1
2
|
import type { Address } from 'viem'
|
|
3
|
+
import { parseUnits } from 'viem'
|
|
2
4
|
import { describe, expect, test, vi } from 'vitest'
|
|
3
5
|
import { useConnect } from 'wagmi'
|
|
4
6
|
import { config, renderHook, setupToken } from '../../../test/wagmi/config.js'
|
|
5
7
|
import * as hooks from './reward.js'
|
|
8
|
+
import * as tokenHooks from './token.js'
|
|
6
9
|
|
|
7
10
|
describe('useGetTotalPerSecond', () => {
|
|
8
11
|
test('default', async () => {
|
|
@@ -53,6 +56,77 @@ describe('useGetTotalPerSecond', () => {
|
|
|
53
56
|
})
|
|
54
57
|
})
|
|
55
58
|
|
|
59
|
+
describe('useUserRewardInfo', () => {
|
|
60
|
+
test('default', async () => {
|
|
61
|
+
const { token } = await setupToken()
|
|
62
|
+
|
|
63
|
+
const account = getAccount(config).address
|
|
64
|
+
|
|
65
|
+
const { result } = await renderHook(() =>
|
|
66
|
+
hooks.useUserRewardInfo({
|
|
67
|
+
token,
|
|
68
|
+
account,
|
|
69
|
+
}),
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy())
|
|
73
|
+
|
|
74
|
+
expect(result.current.data?.rewardRecipient).toBeDefined()
|
|
75
|
+
expect(result.current.data?.rewardPerToken).toBeDefined()
|
|
76
|
+
expect(result.current.data?.rewardBalance).toBeDefined()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('reactivity: account and token parameters', async () => {
|
|
80
|
+
const { result, rerender } = await renderHook(
|
|
81
|
+
(props) =>
|
|
82
|
+
hooks.useUserRewardInfo({
|
|
83
|
+
token: props?.token,
|
|
84
|
+
account: props?.account,
|
|
85
|
+
}),
|
|
86
|
+
{
|
|
87
|
+
initialProps: {
|
|
88
|
+
token: undefined as Address | undefined,
|
|
89
|
+
account: undefined as Address | undefined,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
await vi.waitFor(() => result.current.fetchStatus === 'idle')
|
|
95
|
+
|
|
96
|
+
// Should be disabled when both token and account are undefined
|
|
97
|
+
expect(result.current.data).toBeUndefined()
|
|
98
|
+
expect(result.current.isPending).toBe(true)
|
|
99
|
+
expect(result.current.isEnabled).toBe(false)
|
|
100
|
+
|
|
101
|
+
// Setup token (this also connects the account)
|
|
102
|
+
const { token } = await setupToken()
|
|
103
|
+
|
|
104
|
+
// Set token only (account still undefined)
|
|
105
|
+
rerender({ token, account: undefined })
|
|
106
|
+
|
|
107
|
+
await vi.waitFor(() => result.current.fetchStatus === 'idle')
|
|
108
|
+
|
|
109
|
+
// Should still be disabled when account is undefined
|
|
110
|
+
expect(result.current.data).toBeUndefined()
|
|
111
|
+
expect(result.current.isPending).toBe(true)
|
|
112
|
+
expect(result.current.isEnabled).toBe(false)
|
|
113
|
+
|
|
114
|
+
// Get account from config (already connected by setupToken)
|
|
115
|
+
const account = getAccount(config).address
|
|
116
|
+
|
|
117
|
+
// Set both token and account
|
|
118
|
+
rerender({ token, account })
|
|
119
|
+
|
|
120
|
+
await vi.waitFor(() => expect(result.current.isSuccess).toBeTruthy())
|
|
121
|
+
|
|
122
|
+
// Should now be enabled and have data
|
|
123
|
+
expect(result.current.isEnabled).toBe(true)
|
|
124
|
+
expect(result.current.data?.rewardRecipient).toBeDefined()
|
|
125
|
+
expect(result.current.data?.rewardPerToken).toBeDefined()
|
|
126
|
+
expect(result.current.data?.rewardBalance).toBeDefined()
|
|
127
|
+
})
|
|
128
|
+
})
|
|
129
|
+
|
|
56
130
|
describe('useSetRecipientSync', () => {
|
|
57
131
|
test('default', async () => {
|
|
58
132
|
const { result } = await renderHook(() => ({
|
|
@@ -76,3 +150,100 @@ describe('useSetRecipientSync', () => {
|
|
|
76
150
|
)
|
|
77
151
|
})
|
|
78
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
|
+
})
|