Skip to content

Commit 40a0215

Browse files
nsjamesnsjames
authored andcommitted
fix for claiming matured rex, faq additions, fix for claiming existing balance
1 parent 79b2c80 commit 40a0215

File tree

6 files changed

+93
-56
lines changed

6 files changed

+93
-56
lines changed

src/lib/components/ClaimRewards.svelte

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script>
2-
import WharfService, {apr, eosBalance, rexBalance, unstakingBalances} from "$lib/wharf";
2+
import WharfService, {eosBalance, rexBalance, unstakingBalances} from "$lib/wharf";
33
import InfoRows from "$lib/components/InfoRows.svelte";
44
import GlassBox from "$lib/components/GlassBox.svelte";
55
import {commaNumber, readableNumber} from "$lib";
66
import LoadingSpinner from "$lib/components/LoadingSpinner.svelte";
77
88
9-
$: claimable = WharfService.convertRexToEos(WharfService.claimableBalance());
9+
$: withdrawable = WharfService.withdrawableBalance();
1010
1111
let loading = false;
1212
const claim = async () => {
@@ -16,27 +16,26 @@
1616
loading = false;
1717
}
1818
19-
const now = +new Date();
2019
$: unstaking = WharfService.pendingClaimableBalances().filter(x => !x.savings).map(x => {
2120
return [
2221
`${parseFloat(parseFloat(WharfService.convertRexToEos(parseFloat(x.rex.toString())).toString()).toFixed(4))} EOS`,
2322
x.date.toDateString()];
2423
}).sort((a, b) => +new Date(a[1]) - +new Date(b[1]));
2524
26-
$: totalUnstaking = $unstakingBalances.filter(x => !x.savings).reduce((acc, x) => parseFloat(parseFloat((acc + parseFloat(x.rex.toString())).toString()).toFixed(4)), 0);
25+
$: totalUnstaking = WharfService.pendingClaimableBalances().filter(x => !x.savings).reduce((acc, x) => parseFloat(parseFloat((acc + parseFloat(x.rex.toString())).toString()).toFixed(4)), 0);
2726
$: totalUnstakingInEos = parseFloat(parseFloat(WharfService.convertRexToEos(totalUnstaking).toString()).toFixed(4));
2827
</script>
2928

3029
<GlassBox class="mt-2 overflow-hidden">
3130

32-
{#if claimable > 0}
31+
{#if parseFloat(withdrawable) > 0}
3332
{#if !loading}
3433
<button class="btn" on:click={claim}>
3534
Claim Rewards
3635
</button>
3736

3837
<InfoRows class="mt-2" rows={[
39-
["You can claim", `${commaNumber(claimable)} EOS`, "font-black !text-yellow-300"]
38+
["You can claim", `${commaNumber(withdrawable)} EOS`, "font-black !text-yellow-300"]
4039
]} />
4140
{:else}
4241
<button class="btn btn-disabled" disabled>
@@ -49,14 +48,13 @@
4948
</figure>
5049
{/if}
5150

52-
53-
<section class="flex justify-between relative mt-10">
54-
<section>
55-
<figure class="text-sm">Currently unstaking</figure>
56-
<h1 class="text-3xl font-bold -mt-1">{totalUnstakingInEos} EOS</h1>
57-
</section>
58-
</section>
5951
{#if unstaking.length}
52+
<section class="flex justify-between relative mt-10">
53+
<section>
54+
<figure class="text-sm">Currently unstaking</figure>
55+
<h1 class="text-3xl font-bold -mt-1">{totalUnstakingInEos} EOS</h1>
56+
</section>
57+
</section>
6058
<figure class="text-white text-xs text-opacity-50 mt-5">
6159
Below are your unstaking balances.
6260
</figure>
@@ -66,8 +64,6 @@
6664
...unstaking
6765
]} />
6866
</section>
69-
{:else}
70-
7167
{/if}
7268

7369

src/lib/components/FAQ.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
Once you trigger unstaking, you will then have to wait for 21 days before you can claim your rewards.
3737
`,
3838
},
39+
{
40+
question: 'Why was my matured position automatically sold?',
41+
answer:`
42+
After you unstake and wait the 21 days, your position will automatically be sold when you do any action (like staking or unstaking).
43+
You will still need to claim those rewards, but your will not continue to produce yield on the matured position.
44+
`,
45+
},
3946
{
4047
question: 'Can I immediately unstake and get my EOS back?',
4148
answer:`

src/lib/components/Stake.svelte

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script>
2-
import WharfService, {apr, eosBalance, rexpool} from "$lib/wharf";
2+
import WharfService, {account, eosBalance, rexpool} from "$lib/wharf";
33
import TokenInput from "$lib/components/TokenInput.svelte";
44
import InfoRows from "$lib/components/InfoRows.svelte";
55
import GlassBox from "$lib/components/GlassBox.svelte";
66
import {commaNumber, readableNumber} from "$lib";
77
import LoadingSpinner from "$lib/components/LoadingSpinner.svelte";
88
import InfoBox from "$lib/components/InfoBox.svelte";
9+
import {toast} from "svelte-sonner";
910
1011
let amount = 0;
1112
$: rexReturn = WharfService.convertEosToRex(amount);
@@ -16,6 +17,9 @@
1617
1718
let loading = false;
1819
const buy = async () => {
20+
if(!$account){
21+
return WharfService.login();
22+
}
1923
if (loading) return;
2024
loading = true;
2125
if(await WharfService.buyRex(amount)){
@@ -45,18 +49,6 @@
4549
class="mt-8"
4650
/>
4751

48-
<!-- <section class="flex border rounded border-white border-opacity-10 p-2 mt-4">-->
49-
<!-- <input type="checkbox" class="w-8 h-8" />-->
50-
<!-- <section class="">-->
51-
<!-- <figure class="text-sm font-bold text-white text-opacity-80 ml-2">-->
52-
<!-- Start unstaking immediately-->
53-
<!-- </figure>-->
54-
<!-- <figure class="text-xs text-white text-opacity-50 ml-2">-->
55-
<!-- You can only withdraw your staked tokens 21 days after you start the unstaking process.-->
56-
<!-- </figure>-->
57-
<!-- </section>-->
58-
<!-- </section>-->
59-
6052
{#if !loading}
6153
<button class="btn mt-5" on:click={buy}>
6254
Stake
@@ -68,13 +60,12 @@
6860
{/if}
6961

7062
<InfoRows class="mt-2" rows={[
71-
// ["You will get", `${rexReturn} REX`, "opacity-70"],
7263
["You will stake", `${commaNumber(amount)} EOS`, "opacity-70"],
73-
["Estimated yield", `${commaNumber(amount*$apr)} EOS`, "font-black !text-yellow-300"]
64+
["Estimated yield", `${commaNumber(amount*(apy/100))} EOS`, "font-black !text-yellow-300"]
7465
]} />
7566

7667
<InfoBox class="mt-10">
77-
The APR is an estimate, and may fluctuate based on how many and much others are staking.
68+
The APY is an estimate, and may fluctuate based on how many and much others are staking.
7869
Your 21 day lockup period starts when you unstake your EOS.
7970
<br />
8071
<br />

src/lib/components/Unstake.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import WharfService, {apr, eosBalance, rexBalance} from "$lib/wharf";
2+
import WharfService, {rexBalance} from "$lib/wharf";
33
import TokenInput from "$lib/components/TokenInput.svelte";
44
import InfoRows from "$lib/components/InfoRows.svelte";
55
import GlassBox from "$lib/components/GlassBox.svelte";

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export const readableNumber = (num: number) => {
1212
}
1313

1414
export const commaNumber = (num: any) => {
15+
if(isNaN(num) || num === null || num === undefined) return 0;
1516
return parseFloat(num).toLocaleString();
1617
}

src/lib/wharf.ts

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { WalletPluginAnchor } from "@wharfkit/wallet-plugin-anchor"
66
import { toast } from 'svelte-sonner'
77
import {showConfetti} from "$lib/index";
88

9+
export let loadingFromChain:Writable<boolean> = writable(false);
910
export let account:Writable<string|null> = writable(null);
1011
export let eosBalance:Writable<number> = writable(0);
1112
export let rexBalance:Writable<number> = writable(0);
13+
export let rexfund:Writable<any> = writable(null);
1214
export let rexpool:Writable<any> = writable(null);
1315
export let rawRexBalance:Writable<any> = writable(null);
14-
export let apr = writable(0.49);
1516

1617
const chains = [
1718
Chains.EOS,
@@ -94,6 +95,7 @@ export default class WharfService {
9495
account.set(null)
9596
eosBalance.set(0)
9697
rexBalance.set(0)
98+
rexfund.set(null)
9799
unstakingBalances.set([])
98100
rawRexBalance.set(null)
99101
}
@@ -108,6 +110,8 @@ export default class WharfService {
108110
const _rexpool = await WharfService.getRexPool();
109111
if(_rexpool) rexpool.set(_rexpool);
110112

113+
await WharfService.getRexFund();
114+
111115
const _unstakingBalances = await WharfService.getUnstakingBalances();
112116
if(_unstakingBalances) {
113117
unstakingBalances.set(_unstakingBalances);
@@ -188,6 +192,30 @@ export default class WharfService {
188192
});
189193
}
190194

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+
191219
static async getRexPool(){
192220
if(!WharfService.session && !WharfService.client) return;
193221

@@ -319,41 +347,55 @@ export default class WharfService {
319347
});
320348
}
321349

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+
322357
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);
324360
}
325361

326362
static async claim(){
327363
const claimable = WharfService.claimableBalance();
364+
const totalEos = WharfService.withdrawableBalance(claimable);
328365

329-
if(claimable === 0){
366+
if(parseFloat(totalEos) <= 0){
330367
toast.error('You have no rewards to claim.')
331368
return null;
332369
}
333370

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`
353379
}
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
355397
}).then(x => {
356-
success(`Successfully claimed ${claimable} EOS!`);
398+
success(`Successfully claimed ${totalEos} EOS!`);
357399
return x;
358400
}).catch(err => {
359401
console.error(err)

0 commit comments

Comments
 (0)