Skip to content

Commit

Permalink
Add claim
Browse files Browse the repository at this point in the history
  • Loading branch information
apporc committed Jul 5, 2024
1 parent d8b5d09 commit 46b92c5
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 55 deletions.
108 changes: 68 additions & 40 deletions src/pages/earn/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import TransactionForm from '~/components/elements/form/transaction.svelte'
import {stateREX} from '~/pages/resources/resources'
import {Step} from '~/pages/earn/types'
import {Step, REXInfo} from '~/pages/earn/types'
import REXBootstrap from '~/pages/earn/step/bootstrap.svelte'
import REXError from '~/pages/earn/step/error.svelte'
import REXOverview from '~/pages/earn/step/overview.svelte'
import REXStake from '~/pages/earn/step/stake.svelte'
import REXUnstake from '~/pages/earn/step/unstake.svelte'
import REXClaim from '~/pages/earn/step/claim.svelte'
import REXConfirm from '~/pages/earn/step/confirm.svelte'
let selectedAmount: string
Expand Down Expand Up @@ -61,53 +62,52 @@
}
)
const rexBalance: Readable<Asset> = derived(
const rexInfo: Readable<REXInfo> = derived(
[currentAccount, stateREX, systemToken],
([$currentAccount, $stateREX, $systemToken]) => {
let value = 0
let defaultZero = Asset.from(0, $systemToken!.symbol)
let total = defaultZero
let savings = defaultZero
let matured = defaultZero
let rexPrice = 0
if ($currentAccount && $currentAccount.rex_info && $stateREX && $stateREX.value) {
console.log(`currentAccount: ${JSON.stringify($currentAccount.rex_info)}`)
if ($stateREX.value === 0.0001) {
value =
($stateREX.total_lendable.value / $stateREX.total_rex.value) *
$currentAccount.rex_info.rex_balance.value
rexPrice = $stateREX.total_lendable.value / $stateREX.total_rex.value
} else {
value = $stateREX.value * $currentAccount.rex_info.rex_balance.value
rexPrice = $stateREX.value
}
}
return Asset.from(value, $systemToken!.symbol)
}
)
const maturedBalance: Readable<Asset> = derived(
[currentAccount, stateREX, systemToken],
([$currentAccount, $stateREX, $systemToken]) => {
let value = 0
if ($currentAccount && $currentAccount.rex_info && $stateREX && $stateREX.value) {
if ($stateREX.value === 0.0001) {
value =
($stateREX.total_lendable.value / $stateREX.total_rex.value) *
Number($currentAccount.rex_info.matured_rex)
} else {
value = $stateREX.value * Number($currentAccount.rex_info.matured_rex)
}
total = Asset.from(
$currentAccount.rex_info.rex_balance.value * rexPrice,
$systemToken!.symbol
)
savings = $currentAccount.rex_info.vote_stake
matured = Asset.from(
Asset.fromUnits(
$currentAccount.rex_info.matured_rex,
$currentAccount.rex_info.rex_balance.symbol
).value * rexPrice,
$systemToken!.symbol
)
}
return Asset.from(value, $systemToken!.symbol)
return {total, savings, matured, price: rexPrice}
}
)
const rexToken: Readable<Token> = derived(
[systemToken, rexBalance],
([$systemToken, $rexBalance]) => {
[systemToken, rexInfo],
([$systemToken, $rexInfo]) => {
let tokenValue = $systemToken!
let newToken = {...tokenValue}
newToken.balance = $rexBalance
newToken.balance = $rexInfo.total
return newToken
}
)
const defaultStep: Readable<Step> = derived(rexBalance, ($rexBalance) => {
const defaultStep: Readable<Step> = derived(rexInfo, ($rexInfo) => {
let result: Step = Step.Bootstrap
if ($rexBalance && $rexBalance.value != 0) {
if ($rexInfo && $rexInfo.total.value != 0) {
result = Step.Overview
}
return result
Expand Down Expand Up @@ -135,6 +135,8 @@
function toStakeConfirm(fromStep: Step) {
if (fromStep === Step.Stake || fromStep === Step.Bootstrap) {
selectedAction = 'Stake'
} else if (fromStep === Step.Claim) {
selectedAction = 'Claim'
} else {
selectedAction = 'Unstake'
}
Expand All @@ -149,6 +151,8 @@
return getStakeAction()
} else if (selectedAction === 'Unstake') {
return getUnstakeAction()
} else if (selectedAction === 'Claim') {
return getClaimAction()
} else {
throw new Error('Unknown action')
}
Expand Down Expand Up @@ -178,13 +182,26 @@
}
function getUnstakeAction() {
let rexNumber =
Number(selectedAmount) / ($stateREX!.total_lendable.value / $stateREX!.total_rex.value)
let rexNumber = Number(selectedAmount) / $rexInfo.price
let rexAsset = Asset.from(rexNumber, $currentAccount!.rex_info!.rex_balance.symbol)
console.log(`rexAsset: ${JSON.stringify(rexAsset)}`)
return [
{
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'mvfrsavings',
data: REXWithdraw.from({
owner: $activeSession!.auth.actor,
amount: rexAsset,
}),
},
]
}
function getClaimAction() {
let rexNumber = Number(selectedAmount) / $rexInfo.price
let rexAsset = Asset.from(rexNumber, $currentAccount!.rex_info!.rex_balance.symbol)
let finalAmount = Asset.from(
rexAsset.value * ($stateREX!.total_lendable.value / $stateREX!.total_rex.value),
$systemToken!.symbol
)
let finalAmount = Asset.from(rexAsset.value * $rexInfo.price, $systemToken!.symbol)
return [
{
authorization: [$activeSession!.auth],
Expand Down Expand Up @@ -266,26 +283,37 @@
{:else if $step === Step.Overview}
<REXOverview
availableTokens={$availableSystemTokens}
maturedBalance={$maturedBalance}
rexBalance={$rexBalance}
rexInfo={$rexInfo}
toStake={() => switchStep(Step.Stake)}
toUnstake={() => switchStep(Step.Unstake)}
toClaim={() => switchStep(Step.Claim)}
/>
{:else if $step === Step.Stake}
<REXStake
bind:amount={selectedAmount}
rexBalance={$rexBalance}
rexInfo={$rexInfo}
token={$systemToken}
availableTokens={$availableSystemTokens}
nextStep={() => toStakeConfirm(Step.Stake)}
handleBack={() => switchStep($defaultStep)}
/>
{:else if $step === Step.Unstake}
<REXUnstake
bind:amount={selectedAmount}
rexBalance={$rexBalance}
rexInfo={$rexInfo}
token={$rexToken}
availableTokens={$maturedBalance}
availableTokens={$rexInfo.savings}
nextStep={() => toStakeConfirm(Step.Unstake)}
handleBack={() => switchStep($defaultStep)}
/>
{:else if $step === Step.Claim}
<REXClaim
bind:amount={selectedAmount}
rexInfo={$rexInfo}
token={$rexToken}
availableTokens={$rexInfo.matured}
nextStep={() => toStakeConfirm(Step.Claim)}
handleBack={() => switchStep($defaultStep)}
/>
{:else if $step === Step.Confirm}
<REXConfirm
Expand Down
190 changes: 190 additions & 0 deletions src/pages/earn/step/claim.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<script lang="ts">
import {Asset} from 'anchor-link'
import type {Token} from '~/stores/tokens'
import type {InputResponse} from '~/ui-types'
import Form from '~/components/elements/form.svelte'
import ProgressBar from '~/pages/earn/components/progress.svelte'
import Button from '~/components/elements/button.svelte'
import InputAsset from '~/components/elements/input/asset.svelte'
import InputLabel from '~/components/elements/input/label.svelte'
import TokenSelector from '~/components/elements/input/token/selector.svelte'
import type {REXInfo} from '~/pages/earn/types'
export let amount: string
export let token: Token | undefined
export let rexInfo: REXInfo
export let availableTokens: Asset
export let nextStep: () => void
export let handleBack: () => void
let selectedToken: Token
let tokenOptions: Token[] = []
if (token) {
let newToken = {...token, balance: availableTokens}
selectedToken = newToken
tokenOptions = [newToken]
}
let amountValid = false
function maxBalance() {
if (availableTokens) {
amount = String(availableTokens.value)
amountValid = true
}
}
function onConfirm() {
nextStep()
}
function onAmountChanged(event: CustomEvent<InputResponse>) {
try {
let newAmount = Asset.from(Number(event.detail.value), availableTokens.symbol).value
amount = String(newAmount)
} catch (error) {
console.log('failed to apply amount change', error)
}
}
</script>

<style type="scss">
.container {
border: 1px solid var(--divider-grey);
border-radius: 20px;
padding: 26px;
}
.top-section {
margin-bottom: 42px;
}
.middle-section {
margin: 0 auto;
max-width: 28rem;
margin-bottom: 31px;
}
.bottom-section {
margin: 0 auto;
max-width: 28rem;
}
.header {
color: var(--black);
font-family: Inter;
font-style: normal;
font-weight: bold;
font-size: 24px;
line-height: 29px;
text-align: center;
letter-spacing: -0.47px;
margin-bottom: 7px;
}
.subheader {
color: var(--dark-grey);
font-family: Inter;
font-style: normal;
font-weight: normal;
font-size: 16px;
line-height: 19px;
text-align: center;
letter-spacing: -0.18px;
}
.token-selector {
margin-bottom: 10px;
}
.label {
color: var(--dark-grey);
font-weight: 600;
font-size: 10px;
line-height: 12px;
letter-spacing: 0.1px;
text-transform: uppercase;
margin-bottom: 12px;
text-align: start;
margin-bottom: 32px;
}
.controls {
margin-top: 31px;
text-align: center;
:global(.button) {
background: none;
color: var(--main-blue);
font-size: 10px;
text-transform: uppercase;
}
}
.actions {
padding: 13px;
cursor: pointer;
font-family: Inter;
font-style: normal;
font-weight: bold;
font-size: 10px;
line-height: 12px;
display: flex;
justify-content: end;
align-items: center;
text-align: center;
letter-spacing: 0.1px;
text-transform: uppercase;
color: var(--main-blue);
user-select: none;
-webkit-user-select: none;
}
</style>

<div class="container">
<div class="top-section">
<div class="header">Claim</div>
<div class="subheader">Remove from your staked balance</div>
<ProgressBar step={1} />
</div>
<div class="middle-section">
<Form on:submit={onConfirm}>
<InputLabel>amount to claim</InputLabel>
<div class="token-selector">
<TokenSelector
defaultToken={selectedToken}
{tokenOptions}
{selectedToken}
onTokenSelect={() => {}}
/>
</div>
<div class="label">
total staked {rexInfo.total}
</div>

<InputAsset
bind:valid={amountValid}
bind:value={amount}
symbol={availableTokens.symbol}
focus
fluid
name="amount"
placeholder={`Enter amount of tokens`}
balance={availableTokens}
on:changed={onAmountChanged}
/>
<div class="actions">
<span on:click={maxBalance}>Entire Balance</span>
</div>
</Form>
</div>
<div class="bottom-section">
<Button
fluid
style="primary"
size="large"
disabled={!amountValid}
formValidation
on:action={onConfirm}
>
Unstake tokens
</Button>

<div class="controls">
<Button style="no-frame" on:action={handleBack}>Cancel</Button>
</div>
</div>
</div>
Loading

0 comments on commit 46b92c5

Please sign in to comment.