@@ -6,12 +6,13 @@ import { WalletPluginAnchor } from "@wharfkit/wallet-plugin-anchor"
6
6
import { toast } from 'svelte-sonner'
7
7
import { showConfetti } from "$lib/index" ;
8
8
9
+ export let loadingFromChain :Writable < boolean > = writable ( false ) ;
9
10
export let account :Writable < string | null > = writable ( null ) ;
10
11
export let eosBalance :Writable < number > = writable ( 0 ) ;
11
12
export let rexBalance :Writable < number > = writable ( 0 ) ;
13
+ export let rexfund :Writable < any > = writable ( null ) ;
12
14
export let rexpool :Writable < any > = writable ( null ) ;
13
15
export let rawRexBalance :Writable < any > = writable ( null ) ;
14
- export let apr = writable ( 0.49 ) ;
15
16
16
17
const chains = [
17
18
Chains . EOS ,
@@ -94,6 +95,7 @@ export default class WharfService {
94
95
account . set ( null )
95
96
eosBalance . set ( 0 )
96
97
rexBalance . set ( 0 )
98
+ rexfund . set ( null )
97
99
unstakingBalances . set ( [ ] )
98
100
rawRexBalance . set ( null )
99
101
}
@@ -108,6 +110,8 @@ export default class WharfService {
108
110
const _rexpool = await WharfService . getRexPool ( ) ;
109
111
if ( _rexpool ) rexpool . set ( _rexpool ) ;
110
112
113
+ await WharfService . getRexFund ( ) ;
114
+
111
115
const _unstakingBalances = await WharfService . getUnstakingBalances ( ) ;
112
116
if ( _unstakingBalances ) {
113
117
unstakingBalances . set ( _unstakingBalances ) ;
@@ -188,6 +192,30 @@ export default class WharfService {
188
192
} ) ;
189
193
}
190
194
195
+ static async getRexFund ( ) {
196
+ if ( ! WharfService . session ) return ;
197
+
198
+
199
+ return WharfService . session ?. client . v1 . chain . get_table_rows ( {
200
+ code : "eosio" ,
201
+ scope : "eosio" ,
202
+ lower_bound : WharfService . session . actor ,
203
+ upper_bound : WharfService . session . actor ,
204
+ table : "rexfund" ,
205
+ json : true ,
206
+ } ) . then ( ( res :any ) => {
207
+ const row = res . rows . find ( ( row :any ) => row . owner === WharfService . session ?. actor . toString ( ) ) ;
208
+ if ( ! row ) return null ;
209
+
210
+ rexfund . set ( parseFloat ( row . balance . split ( ' ' ) [ 0 ] ) ) ;
211
+ return true ;
212
+ } ) . catch ( err => {
213
+ console . error ( err )
214
+ toast . error ( 'There was a problem getting your REX fund balance. Check the console for more information about what happened.' )
215
+ return null ;
216
+ } ) ;
217
+ }
218
+
191
219
static async getRexPool ( ) {
192
220
if ( ! WharfService . session && ! WharfService . client ) return ;
193
221
@@ -319,41 +347,55 @@ export default class WharfService {
319
347
} ) ;
320
348
}
321
349
350
+ static withdrawableBalance ( _claimable :number | null = null ) {
351
+ const claimable = _claimable ? _claimable : WharfService . claimableBalance ( ) ;
352
+ const withdrawable = parseFloat ( get ( rexfund ) ?? 0 ) ;
353
+ const matured = parseFloat ( WharfService . convertRexToEos ( claimable ) . toString ( ) ) ;
354
+ return parseFloat ( ( withdrawable + matured ) . toString ( ) ) . toFixed ( 4 ) ;
355
+ }
356
+
322
357
static claimableBalance ( ) {
323
- return WharfService . claimableBalances ( ) . reduce ( ( acc , x ) => acc + parseFloat ( x . rex . toString ( ) ) , 0 ) ;
358
+ const maturedRex = get ( rawRexBalance ) ? get ( rawRexBalance ) . matured_rex / 10000 : 0 ;
359
+ return maturedRex + WharfService . claimableBalances ( ) . reduce ( ( acc , x ) => acc + parseFloat ( x . rex . toString ( ) ) , 0 ) ;
324
360
}
325
361
326
362
static async claim ( ) {
327
363
const claimable = WharfService . claimableBalance ( ) ;
364
+ const totalEos = WharfService . withdrawableBalance ( claimable ) ;
328
365
329
- if ( claimable == = 0 ) {
366
+ if ( parseFloat ( totalEos ) < = 0 ) {
330
367
toast . error ( 'You have no rewards to claim.' )
331
368
return null ;
332
369
}
333
370
334
- return WharfService . session ?. transact ( {
335
- actions : [
336
- {
337
- account : "eosio" ,
338
- name : "sellrex" ,
339
- authorization : [ WharfService . session ?. permissionLevel ] ,
340
- data : {
341
- from : WharfService . session ?. actor ,
342
- rex : `${ claimable . toFixed ( 4 ) } REX`
343
- } ,
344
- } ,
345
- {
346
- account : "eosio" ,
347
- name : "withdraw" ,
348
- authorization : [ WharfService . session ?. permissionLevel ] ,
349
- data : {
350
- owner : WharfService . session ?. actor ,
351
- amount : `${ WharfService . convertRexToEos ( claimable ) . toFixed ( 4 ) } EOS`
352
- }
371
+ const actions :any [ ] = [
372
+ {
373
+ account : "eosio" ,
374
+ name : "withdraw" ,
375
+ authorization : [ WharfService . session ?. permissionLevel ] ,
376
+ data : {
377
+ owner : WharfService . session ?. actor ,
378
+ amount : `${ totalEos } EOS`
353
379
}
354
- ]
380
+ }
381
+ ]
382
+
383
+ if ( claimable > 0 ) {
384
+ actions . unshift ( {
385
+ account : "eosio" ,
386
+ name : "sellrex" ,
387
+ authorization : [ WharfService . session ?. permissionLevel ] ,
388
+ data : {
389
+ from : WharfService . session ?. actor ,
390
+ rex : `${ claimable . toFixed ( 4 ) } REX`
391
+ } ,
392
+ } )
393
+ }
394
+
395
+ return WharfService . session ?. transact ( {
396
+ actions : actions as any
355
397
} ) . then ( x => {
356
- success ( `Successfully claimed ${ claimable } EOS!` ) ;
398
+ success ( `Successfully claimed ${ totalEos } EOS!` ) ;
357
399
return x ;
358
400
} ) . catch ( err => {
359
401
console . error ( err )
0 commit comments