From 44dbe8090a6e9df535a699f451e5fb4adb915108 Mon Sep 17 00:00:00 2001 From: kurt Date: Thu, 3 Oct 2024 00:31:01 +0800 Subject: [PATCH 1/2] fix net price for powerup and stake (#211) * fix net price for powerup and stake * fix rex price for net rent * fix eos symbol to core token symbol * fix REXInfo import error * format resources.ts --- src/pages/earn/step/bootstrap.svelte | 2 +- .../resources/components/forms/powerup.svelte | 8 +- .../resources/components/forms/rex.svelte | 9 +- .../resources/components/state/prices.svelte | 16 ++- src/pages/resources/resources.ts | 109 +++++++++++++++--- 5 files changed, 119 insertions(+), 25 deletions(-) diff --git a/src/pages/earn/step/bootstrap.svelte b/src/pages/earn/step/bootstrap.svelte index 80f4d290..8e41e32b 100644 --- a/src/pages/earn/step/bootstrap.svelte +++ b/src/pages/earn/step/bootstrap.svelte @@ -7,7 +7,7 @@ import Form from '~/components/elements/form.svelte' import InputAsset from '~/components/elements/input/asset.svelte' import InputLabel from '~/components/elements/input/label.svelte' - import {REXInfo} from '../types' + import type {REXInfo} from '../types' export let amount: string export let availableTokens: Asset diff --git a/src/pages/resources/components/forms/powerup.svelte b/src/pages/resources/components/forms/powerup.svelte index 34bd2913..b34633d5 100644 --- a/src/pages/resources/components/forms/powerup.svelte +++ b/src/pages/resources/components/forms/powerup.svelte @@ -9,7 +9,12 @@ import {activeBlockchain, activeSession, currentAccount} from '~/store' import {systemToken} from '~/stores/tokens' import {systemTokenBalance} from '~/stores/balances' - import {powerupPrice, sampleUsage, statePowerUp} from '~/pages/resources/resources' + import { + cpuPowerupPrice, + netPowerupPrice, + sampleUsage, + statePowerUp, + } from '~/pages/resources/resources' import type {FormTransaction} from '~/ui-types' import Button from '~/components/elements/button.svelte' @@ -23,6 +28,7 @@ export let resource: string = 'cpu' const unit = resource === 'cpu' ? 'ms' : 'kb' + const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice let amount: Writable = writable('') let error: string | undefined diff --git a/src/pages/resources/components/forms/rex.svelte b/src/pages/resources/components/forms/rex.svelte index ccecb0e1..5605c922 100644 --- a/src/pages/resources/components/forms/rex.svelte +++ b/src/pages/resources/components/forms/rex.svelte @@ -10,7 +10,7 @@ import {activeBlockchain, activeSession, currentAccount} from '~/store' import {systemToken} from '~/stores/tokens' import {systemTokenBalance} from '~/stores/balances' - import {rexPrice} from '~/pages/resources/resources' + import {cpuRexPrice, netRexPrice} from '~/pages/resources/resources' import type {FormTransaction} from '~/ui-types' import Button from '~/components/elements/button.svelte' @@ -24,6 +24,7 @@ export let resource = 'cpu' const unit = resource === 'cpu' ? 'ms' : 'kb' + const rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice let amount: Writable = writable('') let error: string | undefined @@ -42,6 +43,10 @@ } ) + const disabled: Readable = derived(cost, ($cost) => { + return $cost ? $cost.value <= 0 : true + }) + // Create a derived store of the field we expect to be modified export const field = derived([currentAccount], ([$currentAccount]) => { if ($currentAccount && $currentAccount.self_delegated_bandwidth) { @@ -142,7 +147,7 @@ {/if} - diff --git a/src/pages/resources/components/state/prices.svelte b/src/pages/resources/components/state/prices.svelte index fd517a10..ec8fbbab 100644 --- a/src/pages/resources/components/state/prices.svelte +++ b/src/pages/resources/components/state/prices.svelte @@ -5,7 +5,14 @@ import {ChainFeatures} from '~/config' import {activeBlockchain} from '~/store' - import {powerupPrice, rexPrice, stakingPrice} from '~/pages/resources/resources' + import { + cpuPowerupPrice, + netPowerupPrice, + cpuRexPrice, + netRexPrice, + cpuStakingPrice, + netStakingPrice, + } from '~/pages/resources/resources' import Button from '~/components/elements/button.svelte' import Segment from '~/components/elements/segment.svelte' @@ -13,6 +20,9 @@ export let resource = 'cpu' const unit = resource === 'cpu' ? 'ms' : 'kb' + const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice + const stakingPrice = resource === 'cpu' ? cpuStakingPrice : netStakingPrice + const rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice const {PowerUp, REX, Staking} = ChainFeatures @@ -156,9 +166,7 @@
Staking
- {(Number($stakingPrice.value) * 1000).toFixed( - $stakingPrice.symbol.precision - )} + {$stakingPrice.value.toFixed($stakingPrice.symbol.precision)}
{$token} per diff --git a/src/pages/resources/resources.ts b/src/pages/resources/resources.ts index 56b8a935..54b9414e 100644 --- a/src/pages/resources/resources.ts +++ b/src/pages/resources/resources.ts @@ -1,6 +1,14 @@ import {derived, Readable} from 'svelte/store' import {API, Asset} from '@greymass/eosio' -import {Resources, SampleUsage, PowerUpState, RAMState, REXState} from '@greymass/eosio-resources' +import { + Resources, + SampleUsage, + PowerUpState, + RAMState, + REXState, + BNPrecision, +} from '@greymass/eosio-resources' + import {activeBlockchain} from '~/store' import {getClient} from '../../api-client' @@ -96,31 +104,59 @@ export const msToRent: Readable = derived(activeBlockchain, ($activeBloc return 1 }) -export const powerupPrice = derived( - [msToRent, sampleUsage, statePowerUp, info], - ([$msToRent, $sampleUsage, $statePowerUp, $info]) => { +//price per ms +export const cpuPowerupPrice = derived( + [activeBlockchain, msToRent, sampleUsage, statePowerUp, info], + ([$activeBlockchain, $msToRent, $sampleUsage, $statePowerUp, $info]) => { if ($msToRent && $sampleUsage && $statePowerUp) { return Asset.from( $statePowerUp.cpu.price_per_ms($sampleUsage, $msToRent, $info), - '4,EOS' + $activeBlockchain.coreTokenSymbol + ) + } + return Asset.from(0, $activeBlockchain.coreTokenSymbol) + } +) + +// price per kb +export const netPowerupPrice = derived( + [activeBlockchain, sampleUsage, statePowerUp, info], + ([$activeBlockchain, $sampleUsage, $statePowerUp, $info]) => { + if ($sampleUsage && $statePowerUp) { + return Asset.from( + $statePowerUp.net.price_per_kb($sampleUsage, 1, $info), + $activeBlockchain.coreTokenSymbol ) } - return Asset.from(0, '4,EOS') + return Asset.from(0, $activeBlockchain.coreTokenSymbol) } ) -export const stakingPrice = derived( +//price per ms +export const cpuStakingPrice = derived( [activeBlockchain, msToRent, sampleUsage], ([$activeBlockchain, $msToRent, $sampleUsage]) => { if ($msToRent && $sampleUsage) { const {account} = $sampleUsage const cpu_weight = Number(account.total_resources.cpu_weight.units) const cpu_limit = Number(account.cpu_limit.max.value) - let price = cpu_weight / cpu_limit - if ($activeBlockchain.resourceSampleMilliseconds) { - price *= $activeBlockchain.resourceSampleMilliseconds - } - return Asset.fromUnits(price, $activeBlockchain.coreTokenSymbol) + let price = (cpu_weight / cpu_limit) * $msToRent + return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol) + } + return Asset.from(0, $activeBlockchain.coreTokenSymbol) + } +) + +// price per kb for staking +export const netStakingPrice = derived( + [activeBlockchain, sampleUsage], + ([$activeBlockchain, $sampleUsage]) => { + if ($sampleUsage) { + const {account} = $sampleUsage + const net_weight = Number(account.total_resources.net_weight.units) + const net_limit = Number(account.net_limit.max.value) + let price = net_weight / net_limit + return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol) } return Asset.from(0, $activeBlockchain.coreTokenSymbol) } @@ -152,16 +188,55 @@ export const stateREX: Readable = derived( ) // The price of CPU in the REX system -export const rexPrice = derived( - [msToRent, sampleUsage, stateREX], - ([$msToRent, $sampleUsage, $stateREX]) => { +export const cpuRexPrice = derived( + [activeBlockchain, msToRent, sampleUsage, stateREX], + ([$activeBlockchain, $msToRent, $sampleUsage, $stateREX]) => { if ($msToRent && $sampleUsage && $stateREX) { - return Asset.from($stateREX.price_per($sampleUsage, $msToRent * 30000), '4,EOS') + const price = $stateREX.price_per($sampleUsage, $msToRent * 30000) + const coreTokenSymbol = $activeBlockchain.coreTokenSymbol + return compatPriceWithPrecision(price, coreTokenSymbol) + } + return Asset.from(0, $activeBlockchain.coreTokenSymbol) + } +) + +// The price of Net in the REX system +export const netRexPrice = derived( + [activeBlockchain, sampleUsage, stateREX], + ([$activeBlockchain, $sampleUsage, $stateREX]) => { + if ($sampleUsage && $stateREX) { + const price = calculateNetRexPrice($stateREX, $sampleUsage, 30000) + const coreTokenSymbol = $activeBlockchain.coreTokenSymbol + return compatPriceWithPrecision(price, coreTokenSymbol) } - return Asset.from(0, '4,EOS') + return Asset.from(0, $activeBlockchain.coreTokenSymbol) } ) +function compatPriceWithPrecision(price: number, coreTokenSymbol: Asset.Symbol) { + let precision = coreTokenSymbol.precision + if (price > 0 && price < 1 / Math.pow(10, precision)) { + precision = Number(price.toExponential().split('-')[1]) + } + return Asset.from(price, `${precision},${coreTokenSymbol.name}`) +} + +function calculateNetRexPrice(stateRex: REXState, sample: SampleUsage, unit = 1000): number { + // Sample token units + const tokens = Asset.fromUnits(10000, stateRex.symbol) + + // Spending 1 EOS (10000 units) on REX gives this many tokens + const bancor = Number(tokens.units) / (stateRex.total_rent.value / stateRex.total_unlent.value) + // The ratio of the number of tokens received vs the sampled values + const unitPrice = bancor * (Number(sample.net) / BNPrecision) + // The token units spent per unit + const perunit = Number(tokens.units) / unitPrice + // Multiply the per unit cost by the units requested + const cost = perunit * unit + // Converting to an Asset + return cost / Math.pow(10, stateRex.precision) +} + // The state of the REX system export const stateRAM: Readable = derived( [activeBlockchain], From 93cb2ad541bf0e6570f8f6f927c8452b699e5c69 Mon Sep 17 00:00:00 2001 From: Vance Date: Thu, 3 Oct 2024 01:24:35 +0800 Subject: [PATCH 2/2] staked value fix (#212) * earn: add rexfund into staked value * Update src/pages/earn/index.svelte Co-authored-by: Daniel Fugere * Update src/pages/earn/index.svelte Co-authored-by: Daniel Fugere * Update src/pages/earn/index.svelte Co-authored-by: Daniel Fugere * Update src/pages/earn/index.svelte Co-authored-by: Daniel Fugere * Update src/pages/earn/index.svelte Co-authored-by: Daniel Fugere * Syntax fixes * browser list updates * Revert "browser list updates" This reverts commit c5ac2d17641b56e9e7313a2eb14c3db9814349bd. --------- Co-authored-by: Aaron Cox Co-authored-by: Daniel Fugere --- src/pages/earn/index.svelte | 59 +++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/pages/earn/index.svelte b/src/pages/earn/index.svelte index e3397b0c..c84bc957 100644 --- a/src/pages/earn/index.svelte +++ b/src/pages/earn/index.svelte @@ -121,9 +121,33 @@ return Asset.fromUnits(result, $systemToken!.symbol) } + const rexEOSBalance: Writable = writable(Asset.from(0, $systemToken!.symbol)) + onMount(async () => { + const client = getClient($activeBlockchain.chainId) + const unsubscribe = currentAccount.subscribe(async (account) => { + if (!$systemToken) return + 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)) + } else { + rexEOSBalance.set(Asset.from(0, $systemToken.symbol)) + } + }) + return () => { + unsubscribe() + } + }) + const rexInfo: Readable = derived( - [currentAccount, stateREX, systemToken], - ([$currentAccount, $stateREX, $systemToken]) => { + [currentAccount, stateREX, systemToken, rexEOSBalance], + ([$currentAccount, $stateREX, $systemToken, $rexEOSBalance]) => { let defaultZero = Asset.from(0, $systemToken!.symbol) let total = defaultZero let savings = defaultZero @@ -136,8 +160,14 @@ const annualReward = 31250000 const totalStaked = Number($stateREX.total_lendable.value) apy = ((annualReward / totalStaked) * 100).toFixed(2) - if ($currentAccount && $currentAccount.rex_info) { + if ($currentAccount && $systemToken && $currentAccount.rex_info) { total = convertRexToEos($currentAccount.rex_info.rex_balance.value) + if ($rexEOSBalance.value > 0) { + total = Asset.fromUnits( + total.units.adding($rexEOSBalance.units), + $systemToken.symbol + ) + } const claimableBuckets = $currentAccount.rex_info.rex_maturities.filter( (maturity) => +new Date(maturity.first!.toString()) < +now @@ -190,29 +220,6 @@ return result }) - const rexEOSBalance: Writable = 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)) - } else { - rexEOSBalance.set(Asset.from(0, $systemToken!.symbol)) - } - }) - return () => { - unsubscribe() - } - }) - const initialStep: Step = Step.Bootstrap const step: Writable = writable(initialStep, () => { const unsubscribeStep = defaultStep.subscribe((s) => {