1
1
// TODO remove these disables when moving off the mock APIs
2
2
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-non-null-assertion */
3
3
4
+ import {
5
+ getAmountByTargetAndState ,
6
+ getCurrentEpoch ,
7
+ PositionState ,
8
+ PythStakingClient ,
9
+ type StakeAccountPositions ,
10
+ } from "@pythnetwork/staking-sdk" ;
4
11
import type { AnchorWallet } from "@solana/wallet-adapter-react" ;
5
- import type { Connection } from "@solana/web3.js" ;
6
-
7
- export type StakeAccount = {
8
- publicKey : `0x${string } `;
9
- } ;
12
+ import { PublicKey , type Connection } from "@solana/web3.js" ;
10
13
11
14
export type Context = {
12
15
connection : Connection ;
13
16
wallet : AnchorWallet ;
14
- stakeAccount : StakeAccount ;
17
+ stakeAccount : StakeAccountPositions ;
15
18
} ;
16
19
17
20
type Data = {
@@ -23,10 +26,12 @@ type Data = {
23
26
date : Date ;
24
27
}
25
28
| undefined ;
26
- expiringRewards : {
27
- amount : bigint ;
28
- expiry : Date ;
29
- } ;
29
+ expiringRewards :
30
+ | {
31
+ amount : bigint ;
32
+ expiry : Date ;
33
+ }
34
+ | undefined ;
30
35
locked : bigint ;
31
36
unlockSchedule : {
32
37
date : Date ;
@@ -41,7 +46,7 @@ type Data = {
41
46
} ;
42
47
integrityStakingPublishers : {
43
48
name : string ;
44
- publicKey : `0x${ string } ` ;
49
+ publicKey : string ;
45
50
isSelf : boolean ;
46
51
selfStake : bigint ;
47
52
poolCapacity : bigint ;
@@ -140,96 +145,177 @@ type AccountHistory = {
140
145
} [ ] ;
141
146
142
147
export const getStakeAccounts = async (
143
- _connection : Connection ,
144
- _wallet : AnchorWallet ,
145
- ) : Promise < StakeAccount [ ] > => {
146
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
147
- return MOCK_STAKE_ACCOUNTS ;
148
+ connection : Connection ,
149
+ wallet : AnchorWallet ,
150
+ ) : Promise < StakeAccountPositions [ ] > => {
151
+ const pythStakingClient = new PythStakingClient ( { connection , wallet } ) ;
152
+ return pythStakingClient . getAllStakeAccountPositions ( wallet . publicKey ) ;
148
153
} ;
149
154
150
155
export const loadData = async ( context : Context ) : Promise < Data > => {
151
156
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
152
157
// While using mocks we need to clone the MOCK_DATA object every time
153
158
// `loadData` is called so that swr treats the response as changed and
154
159
// triggers a rerender.
155
- return { ...MOCK_DATA [ context . stakeAccount . publicKey ] ! } ;
160
+
161
+ const pythStakingClient = new PythStakingClient ( {
162
+ connection : context . connection ,
163
+ wallet : context . wallet ,
164
+ } ) ;
165
+ const stakeAccountPositions = context . stakeAccount ;
166
+
167
+ const [ stakeAccountCustody , publishers , ownerAtaAccount , currentEpoch ] =
168
+ await Promise . all ( [
169
+ pythStakingClient . getStakeAccountCustody ( stakeAccountPositions . address ) ,
170
+ pythStakingClient . getPublishers ( ) ,
171
+ pythStakingClient . getOwnerPythAtaAccount ( ) ,
172
+ getCurrentEpoch ( context . connection ) ,
173
+ ] ) ;
174
+
175
+ const unlockSchedule = await pythStakingClient . getUnlockSchedule ( {
176
+ stakeAccountPositions : stakeAccountPositions . address ,
177
+ } ) ;
178
+
179
+ const filterGovernancePositions = ( positionState : PositionState ) =>
180
+ getAmountByTargetAndState ( {
181
+ stakeAccountPositions,
182
+ targetWithParameters : { voting : { } } ,
183
+ positionState,
184
+ epoch : currentEpoch ,
185
+ } ) ;
186
+ const filterOISPositions = (
187
+ publisher : PublicKey ,
188
+ positionState : PositionState ,
189
+ ) =>
190
+ getAmountByTargetAndState ( {
191
+ stakeAccountPositions,
192
+ targetWithParameters : { integrityPool : { publisher } } ,
193
+ positionState,
194
+ epoch : currentEpoch ,
195
+ } ) ;
196
+
197
+ return {
198
+ lastSlash : undefined , // TODO
199
+ availableRewards : 0n , // TODO
200
+ expiringRewards : undefined , // TODO
201
+ total : stakeAccountCustody . amount ,
202
+ governance : {
203
+ warmup : filterGovernancePositions ( PositionState . LOCKING ) ,
204
+ staked : filterGovernancePositions ( PositionState . LOCKED ) ,
205
+ cooldown : filterGovernancePositions ( PositionState . PREUNLOCKING ) ,
206
+ cooldown2 : filterGovernancePositions ( PositionState . UNLOCKED ) ,
207
+ } ,
208
+ unlockSchedule,
209
+ locked : unlockSchedule . reduce ( ( sum , { amount } ) => sum + amount , 0n ) ,
210
+ walletAmount : ownerAtaAccount . amount ,
211
+ integrityStakingPublishers : publishers . map ( ( { pubkey : publisher } ) => ( {
212
+ apyHistory : [ ] , // TODO
213
+ isSelf : false , // TODO
214
+ name : publisher . toString ( ) ,
215
+ numFeeds : 0 , // TODO
216
+ poolCapacity : 100n , // TODO
217
+ poolUtilization : 0n , // TODO
218
+ publicKey : publisher . toString ( ) ,
219
+ qualityRanking : 0 , // TODO
220
+ selfStake : 0n , // TODO
221
+ positions : {
222
+ warmup : filterOISPositions ( publisher , PositionState . LOCKING ) ,
223
+ staked : filterOISPositions ( publisher , PositionState . LOCKED ) ,
224
+ cooldown : filterOISPositions ( publisher , PositionState . PREUNLOCKING ) ,
225
+ cooldown2 : filterOISPositions ( publisher , PositionState . UNLOCKED ) ,
226
+ } ,
227
+ } ) ) ,
228
+ } ;
156
229
} ;
157
230
158
231
export const loadAccountHistory = async (
159
- context : Context ,
232
+ _context : Context ,
160
233
) : Promise < AccountHistory > => {
161
234
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
162
- return [ ... MOCK_HISTORY [ context . stakeAccount . publicKey ] ! ] ;
235
+ return MOCK_HISTORY [ "0x000000" ] ! ;
163
236
} ;
164
237
165
238
export const deposit = async (
166
239
context : Context ,
167
240
amount : bigint ,
168
241
) : Promise < void > => {
169
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
170
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . total += amount ;
171
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . walletAmount -= amount ;
242
+ const pythStakingClient = new PythStakingClient ( {
243
+ connection : context . connection ,
244
+ wallet : context . wallet ,
245
+ } ) ;
246
+ await pythStakingClient . depositTokensToStakeAccountCustody (
247
+ context . stakeAccount . address ,
248
+ amount ,
249
+ ) ;
172
250
} ;
173
251
174
252
export const withdraw = async (
175
253
context : Context ,
176
254
amount : bigint ,
177
255
) : Promise < void > => {
178
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
179
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . total -= amount ;
180
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . walletAmount += amount ;
256
+ const pythStakingClient = new PythStakingClient ( {
257
+ connection : context . connection ,
258
+ wallet : context . wallet ,
259
+ } ) ;
260
+ await pythStakingClient . withdrawTokensFromStakeAccountCustody (
261
+ context . stakeAccount . address ,
262
+ amount ,
263
+ ) ;
181
264
} ;
182
265
183
266
export const claim = async ( context : Context ) : Promise < void > => {
184
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
185
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . total +=
186
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . availableRewards ;
187
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . availableRewards = 0n ;
188
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . expiringRewards . amount = 0n ;
267
+ const pythStakingClient = new PythStakingClient ( {
268
+ connection : context . connection ,
269
+ wallet : context . wallet ,
270
+ } ) ;
271
+ await pythStakingClient . advanceDelegationRecord ( {
272
+ stakeAccountPositions : context . stakeAccount . address ,
273
+ } ) ;
189
274
} ;
190
275
191
276
export const stakeGovernance = async (
192
277
context : Context ,
193
278
amount : bigint ,
194
279
) : Promise < void > => {
195
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
196
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . governance . warmup += amount ;
280
+ const pythStakingClient = new PythStakingClient ( {
281
+ connection : context . connection ,
282
+ wallet : context . wallet ,
283
+ } ) ;
284
+ await pythStakingClient . stakeToGovernance (
285
+ context . stakeAccount . address ,
286
+ amount ,
287
+ ) ;
197
288
} ;
198
289
199
290
export const cancelWarmupGovernance = async (
200
- context : Context ,
201
- amount : bigint ,
291
+ _context : Context ,
292
+ _amount : bigint ,
202
293
) : Promise < void > => {
203
294
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
204
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . governance . warmup -= amount ;
205
295
} ;
206
296
207
297
export const unstakeGovernance = async (
208
- context : Context ,
209
- amount : bigint ,
298
+ _context : Context ,
299
+ _amount : bigint ,
210
300
) : Promise < void > => {
211
301
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
212
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . governance . staked -= amount ;
213
- MOCK_DATA [ context . stakeAccount . publicKey ] ! . governance . cooldown += amount ;
214
302
} ;
215
303
216
304
export const delegateIntegrityStaking = async (
217
305
context : Context ,
218
306
publisherKey : string ,
219
307
amount : bigint ,
220
308
) : Promise < void > => {
221
- await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
222
- const publisher = MOCK_DATA [
223
- context . stakeAccount . publicKey
224
- ] ! . integrityStakingPublishers . find (
225
- ( publisher ) => publisher . publicKey === publisherKey ,
226
- ) ;
227
- if ( publisher ) {
228
- publisher . positions ||= { } ;
229
- publisher . positions . warmup = ( publisher . positions . warmup ?? 0n ) + amount ;
230
- } else {
231
- throw new Error ( `Invalid publisher key: "${ publisherKey } "` ) ;
232
- }
309
+ const pythStakingClient = new PythStakingClient ( {
310
+ connection : context . connection ,
311
+ wallet : context . wallet ,
312
+ } ) ;
313
+
314
+ await pythStakingClient . stakeToPublisher ( {
315
+ stakeAccountPositions : context . stakeAccount . address ,
316
+ publisher : new PublicKey ( publisherKey ) ,
317
+ amount,
318
+ } ) ;
233
319
} ;
234
320
235
321
export const cancelWarmupIntegrityStaking = async (
@@ -239,7 +325,7 @@ export const cancelWarmupIntegrityStaking = async (
239
325
) : Promise < void > => {
240
326
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
241
327
const publisher = MOCK_DATA [
242
- context . stakeAccount . publicKey
328
+ context . stakeAccount . address . toString ( )
243
329
] ! . integrityStakingPublishers . find (
244
330
( publisher ) => publisher . publicKey === publisherKey ,
245
331
) ;
@@ -259,7 +345,7 @@ export const unstakeIntegrityStaking = async (
259
345
) : Promise < void > => {
260
346
await new Promise ( ( resolve ) => setTimeout ( resolve , MOCK_DELAY ) ) ;
261
347
const publisher = MOCK_DATA [
262
- context . stakeAccount . publicKey
348
+ context . stakeAccount . address . toString ( )
263
349
] ! . integrityStakingPublishers . find (
264
350
( publisher ) => publisher . publicKey === publisherKey ,
265
351
) ;
@@ -308,11 +394,6 @@ export const getNextFullEpoch = (): Date => {
308
394
309
395
const MOCK_DELAY = 500 ;
310
396
311
- const MOCK_STAKE_ACCOUNTS : StakeAccount [ ] = [
312
- { publicKey : "0x000000" } ,
313
- { publicKey : "0x111111" } ,
314
- ] ;
315
-
316
397
const mkMockData = ( isDouro : boolean ) : Data => ( {
317
398
total : 15_000_000n ,
318
399
availableRewards : 156_000n ,
@@ -417,10 +498,7 @@ const mkMockData = (isDouro: boolean): Data => ({
417
498
] ,
418
499
} ) ;
419
500
420
- const MOCK_DATA : Record <
421
- ( typeof MOCK_STAKE_ACCOUNTS ) [ number ] [ "publicKey" ] ,
422
- Data
423
- > = {
501
+ const MOCK_DATA : Record < string , Data > = {
424
502
"0x000000" : mkMockData ( true ) ,
425
503
"0x111111" : mkMockData ( false ) ,
426
504
} ;
@@ -464,10 +542,7 @@ const mkMockHistory = (): AccountHistory => [
464
542
} ,
465
543
] ;
466
544
467
- const MOCK_HISTORY : Record <
468
- ( typeof MOCK_STAKE_ACCOUNTS ) [ number ] [ "publicKey" ] ,
469
- AccountHistory
470
- > = {
545
+ const MOCK_HISTORY : Record < string , AccountHistory > = {
471
546
"0x000000" : mkMockHistory ( ) ,
472
547
"0x111111" : mkMockHistory ( ) ,
473
548
} ;
0 commit comments