Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add liquid REX balances to claim + rename claim to withdraw #195

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions src/pages/earn/index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import type {Readable, Writable} from 'svelte/store'
import {derived, writable, get} from 'svelte/store'
import {Asset} from 'anchor-link'
import {AnyAction, Asset} from 'anchor-link'

import {currentAccount} from '~/store'
import {activeBlockchain, activeSession} from '~/store'
Expand All @@ -24,6 +24,8 @@
import REXUnstake from '~/pages/earn/step/unstake.svelte'
import REXClaim from '~/pages/earn/step/claim.svelte'
import REXConfirm from '~/pages/earn/step/confirm.svelte'
import {onMount} from 'svelte'
import {getClient} from '~/api-client'

let selectedAmount: string
let selectedAction: string
Expand Down Expand Up @@ -139,6 +141,27 @@
return result
})

const rexEOSBalance: Writable<Asset> = writable(Asset.from(0, $systemToken!.symbol))
onMount(async () => {
const client = getClient($activeBlockchain.chainId)
const unsubscribe = currentAccount.subscribe(async (account) => {
const result = await client.v1.chain.get_table_rows({
code: 'eosio',
scope: 'eosio',
table: 'rexfund',
json: true,
lower_bound: $currentAccount?.account_name,
upper_bound: $currentAccount?.account_name,
})
if (result.rows.length > 0) {
rexEOSBalance.set(Asset.from(result.rows[0].balance, $systemToken!.symbol))
}
})
return () => {
unsubscribe()
}
})

const initialStep: Step = Step.Bootstrap
const step: Writable<Step> = writable(initialStep, () => {
const unsubscribeStep = defaultStep.subscribe((s) => {
Expand Down Expand Up @@ -223,28 +246,33 @@
}

function getClaimAction() {
let rexAmount = convertEosToRex(Number(selectedAmount))
let eosAmount = convertRexToEos(rexAmount.value)
return [
{
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'sellrex',
data: REXSELLREX.from({
from: $activeSession!.auth.actor,
rex: rexAmount,
}),
},
const actions: AnyAction[] = [
{
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'withdraw',
data: REXWithdraw.from({
owner: $activeSession!.auth.actor,
amount: eosAmount,
amount: Asset.from(Number(selectedAmount), $systemToken!.symbol),
}),
},
]

if (Number($rexEOSBalance.value) < Number(selectedAmount)) {
const difference = Number(selectedAmount) - Number($rexEOSBalance.value)
const rexAmount = convertEosToRex(difference)
actions.unshift({
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'sellrex',
data: REXSELLREX.from({
from: $activeSession!.auth.actor,
rex: rexAmount,
}),
})
}

return actions
}

// Create a derived store of the rex field we expect to be modified
Expand All @@ -255,6 +283,13 @@
return Asset.from(0, $systemToken!.symbol)
})

const claimableTokens = derived([rexInfo, rexEOSBalance], ([$rexInfo, $rexEOSBalance]) => {
return Asset.fromUnits(
$rexInfo.matured.units.adding($rexEOSBalance.units),
$rexEOSBalance.symbol
)
})

async function handleConfirm(context: FormTransaction) {
const actions = getActionData()
try {
Expand Down Expand Up @@ -308,6 +343,7 @@
<REXOverview
availableTokens={$availableSystemTokens}
rexInfo={$rexInfo}
rexEOSBalance={$rexEOSBalance}
toStake={() => switchStep(Step.Stake)}
toUnstake={() => switchStep(Step.Unstake)}
toClaim={() => switchStep(Step.Claim)}
Expand Down Expand Up @@ -335,7 +371,7 @@
bind:amount={selectedAmount}
rexInfo={$rexInfo}
token={$rexToken}
availableTokens={$rexInfo.matured}
availableTokens={$claimableTokens}
nextStep={() => toStakeConfirm(Step.Claim)}
handleBack={() => switchStep($defaultStep)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/earn/step/bootstrap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
</div>
</div>
<div class="bottom-section">
<p>The unstaking process takes 21 days before the tokens become available for claim.</p>
<p>The unstaking process takes 21 days before the tokens become available to withdraw.</p>
<br />
<p>* The APY changes based on the total number of EOS tokens staked at any given moment.</p>
</div>
Expand Down
11 changes: 4 additions & 7 deletions src/pages/earn/step/claim.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@

<div class="container">
<div class="top-section">
<div class="header">Claim</div>
<div class="subheader">Remove from your staked balance</div>
<div class="header">Withdraw</div>
<div class="subheader">Withdraw tokens from the staking contract</div>
<ProgressBar step={1} />
</div>
<div class="middle-section">
<Form on:submit={onConfirm}>
<InputLabel>amount to claim</InputLabel>
<InputLabel>amount to withdraw</InputLabel>
<div class="token-selector">
<TokenSelector
defaultToken={selectedToken}
Expand All @@ -151,9 +151,6 @@
onTokenSelect={() => {}}
/>
</div>
<div class="label">
total staked {rexInfo.total}
</div>

<InputAsset
bind:valid={amountValid}
Expand All @@ -180,7 +177,7 @@
formValidation
on:action={onConfirm}
>
Unstake tokens
Withdraw Tokens
</Button>

<div class="controls">
Expand Down
18 changes: 11 additions & 7 deletions src/pages/earn/step/overview.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import type {Asset} from 'anchor-link'
import {Asset} from 'anchor-link'

import Button from '~/components/elements/button.svelte'

import type {REXInfo} from '~/pages/earn/types'

export let availableTokens: Asset
export let rexEOSBalance: Asset
export let rexInfo: REXInfo
export let toStake: () => void
export let toUnstake: () => void
Expand Down Expand Up @@ -147,12 +148,15 @@
</div>
{#if Number(rexInfo.savings.value) > 0}
<div class="unstakable">
{rexInfo.savings} unstakable
Unstakable: {rexInfo.savings}
</div>
{/if}
{#if Number(rexInfo.matured.value) > 0}
{#if Number(rexInfo.matured.value) > 0 || Number(rexEOSBalance.value) > 0}
<div class="claimable">
{rexInfo.matured} claimable
Withdrawable: {Asset.fromUnits(
rexInfo.matured.units.adding(rexEOSBalance.units),
rexEOSBalance.symbol
)}
</div>
{/if}

Expand Down Expand Up @@ -181,17 +185,17 @@
fluid
style="primary"
size="regular"
disabled={rexInfo.matured.value <= 0}
disabled={rexInfo.matured.value <= 0 && rexEOSBalance.value <= 0}
formValidation
on:action={toClaim}
>
Claim
Withdraw
</Button>
</div>
</div>
</div>
<div class="bottom-section">
<p>The unstaking process takes 21 days before the tokens become available for claim.</p>
<p>The unstaking process takes 21 days before the tokens become available to withdraw.</p>
<br />
<p>* The APY changes based on the total number of EOS tokens staked at any given moment.</p>
</div>
Expand Down
Loading