Skip to content

Commit 22d025f

Browse files
committed
Automatically append mvtosavings if a dust REX balance exists
1 parent adb3c41 commit 22d025f

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/lib/types/rex.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ export namespace Types {
8787
@Struct.field(Asset)
8888
declare rex: Asset;
8989
}
90+
@Struct.type('mvtosavings')
91+
export class mvtosavings extends Struct {
92+
@Struct.field(Name)
93+
declare owner: Name;
94+
@Struct.field(Asset)
95+
declare rex: Asset;
96+
}
9097
@Struct.type('donatetorex')
9198
export class donatetorex extends Struct {
9299
@Struct.field(Name)

src/routes/[network]/(account)/staking/(staking)/stake/manager.svelte.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
defaultQuantity,
99
getStakableBalance,
1010
getAPR,
11-
getUnstakableBalance
11+
getUnstakableBalance,
12+
getUnstakingBalances,
13+
type UnstakingRecord
1214
} from '$lib/utils/staking';
1315
import { Types as REXTypes } from '$lib/types/rex';
1416
import { 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

Comments
 (0)