From 46b92c53274ac3f3eaa6d0fbafc5e5d93d1d6bc3 Mon Sep 17 00:00:00 2001 From: apporc Date: Fri, 5 Jul 2024 14:41:58 +0800 Subject: [PATCH] Add claim --- src/pages/earn/index.svelte | 108 ++++++++++------ src/pages/earn/step/claim.svelte | 190 ++++++++++++++++++++++++++++ src/pages/earn/step/overview.svelte | 43 +++++-- src/pages/earn/step/stake.svelte | 9 +- src/pages/earn/step/unstake.svelte | 11 +- src/pages/earn/types.ts | 10 ++ 6 files changed, 316 insertions(+), 55 deletions(-) create mode 100644 src/pages/earn/step/claim.svelte diff --git a/src/pages/earn/index.svelte b/src/pages/earn/index.svelte index 927f92ae..16159a6c 100644 --- a/src/pages/earn/index.svelte +++ b/src/pages/earn/index.svelte @@ -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 @@ -61,53 +62,52 @@ } ) - const rexBalance: Readable = derived( + const rexInfo: Readable = 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 = 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 = 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 = derived(rexBalance, ($rexBalance) => { + const defaultStep: Readable = derived(rexInfo, ($rexInfo) => { let result: Step = Step.Bootstrap - if ($rexBalance && $rexBalance.value != 0) { + if ($rexInfo && $rexInfo.total.value != 0) { result = Step.Overview } return result @@ -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' } @@ -149,6 +151,8 @@ return getStakeAction() } else if (selectedAction === 'Unstake') { return getUnstakeAction() + } else if (selectedAction === 'Claim') { + return getClaimAction() } else { throw new Error('Unknown action') } @@ -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], @@ -266,26 +283,37 @@ {:else if $step === Step.Overview} switchStep(Step.Stake)} toUnstake={() => switchStep(Step.Unstake)} + toClaim={() => switchStep(Step.Claim)} /> {:else if $step === Step.Stake} toStakeConfirm(Step.Stake)} + handleBack={() => switchStep($defaultStep)} /> {:else if $step === Step.Unstake} toStakeConfirm(Step.Unstake)} + handleBack={() => switchStep($defaultStep)} + /> + {:else if $step === Step.Claim} + toStakeConfirm(Step.Claim)} + handleBack={() => switchStep($defaultStep)} /> {:else if $step === Step.Confirm} + 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) { + 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) + } + } + + + + +
+
+
Claim
+
Remove from your staked balance
+ +
+
+
+ amount to claim +
+ {}} + /> +
+
+ total staked {rexInfo.total} +
+ + +
+ Entire Balance +
+ +
+
+ + +
+ +
+
+
diff --git a/src/pages/earn/step/overview.svelte b/src/pages/earn/step/overview.svelte index a9a3451f..b2ba43ef 100644 --- a/src/pages/earn/step/overview.svelte +++ b/src/pages/earn/step/overview.svelte @@ -1,13 +1,15 @@