88 defaultQuantity ,
99 getStakableBalance ,
1010 getAPR ,
11- getUnstakableBalance
11+ getUnstakableBalance ,
12+ getUnstakingBalances ,
13+ type UnstakingRecord
1214} from '$lib/utils/staking' ;
1315import { Types as REXTypes } from '$lib/types/rex' ;
1416import { PlaceholderAuth } from '@wharfkit/session' ;
@@ -31,6 +33,9 @@ export class StakeManager {
3133 public error : string = $state ( '' ) ;
3234 public txid : string = $state ( '' ) ;
3335
36+ public unstaking : Array < UnstakingRecord > = $derived (
37+ getUnstakingBalances ( this . network , this . account )
38+ ) ;
3439 public staked : Asset = $derived ( getUnstakableBalance ( this . network , this . account ) ) ;
3540 public stakable : Asset = $derived ( getStakableBalance ( this . network , this . account ) ) ;
3641 public apr : string = $derived (
@@ -114,8 +119,28 @@ export class StakeManager {
114119 } )
115120 } ) ;
116121
122+ const actions = [ deposit , buyrex ] ;
123+
124+ const maturedRex = this . account . rex ? Number ( this . account . rex . matured_rex ) / 10000 : 0 ;
125+ const claimableBalance =
126+ maturedRex + this . unstaking . reduce ( ( acc , x ) => acc + parseFloat ( x . rex . toString ( ) ) , 0 ) ;
127+
128+ if ( claimableBalance > 0 && claimableBalance < 10000 ) {
129+ actions . unshift (
130+ Action . from ( {
131+ account : this . network . contracts . system . account ,
132+ name : 'mvtosavings' ,
133+ authorization : [ PlaceholderAuth ] ,
134+ data : REXTypes . mvtosavings . from ( {
135+ owner : this . account . name ,
136+ rex : `${ claimableBalance . toFixed ( 4 ) } REX`
137+ } )
138+ } )
139+ ) ;
140+ }
141+
117142 const result = await this . wharf . transact ( {
118- actions : [ deposit , buyrex ]
143+ actions
119144 } ) ;
120145
121146 this . txid = String ( result ?. response ?. transaction_id ) ;
0 commit comments