From 44bf7ce67756e04f0160829b3e04e52e3c194b8e Mon Sep 17 00:00:00 2001 From: kurt Date: Fri, 13 Sep 2024 16:35:00 +0800 Subject: [PATCH] fix net price for powerup and stake --- .../resources/components/forms/powerup.svelte | 8 ++- .../resources/components/state/prices.svelte | 14 ++-- src/pages/resources/resources.ts | 67 ++++++++++++++----- 3 files changed, 67 insertions(+), 22 deletions(-) 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/state/prices.svelte b/src/pages/resources/components/state/prices.svelte index fd517a10..909a2597 100644 --- a/src/pages/resources/components/state/prices.svelte +++ b/src/pages/resources/components/state/prices.svelte @@ -5,7 +5,13 @@ import {ChainFeatures} from '~/config' import {activeBlockchain} from '~/store' - import {powerupPrice, rexPrice, stakingPrice} from '~/pages/resources/resources' + import { + cpuPowerupPrice, + netPowerupPrice, + rexPrice, + cpuStakingPrice, + netStakingPrice, + } from '~/pages/resources/resources' import Button from '~/components/elements/button.svelte' import Segment from '~/components/elements/segment.svelte' @@ -13,6 +19,8 @@ export let resource = 'cpu' const unit = resource === 'cpu' ? 'ms' : 'kb' + const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice + const stakingPrice = resource === 'cpu' ? cpuStakingPrice : netStakingPrice const {PowerUp, REX, Staking} = ChainFeatures @@ -156,9 +164,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..ce8be7cb 100644 --- a/src/pages/resources/resources.ts +++ b/src/pages/resources/resources.ts @@ -1,14 +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 {activeBlockchain} from '~/store' +import { derived, Readable } from 'svelte/store' +import { Int64, API, Asset } from '@greymass/eosio' +import { Resources, SampleUsage, PowerUpState, RAMState, REXState } from '@greymass/eosio-resources' +import { activeBlockchain } from '~/store' -import {getClient} from '../../api-client' -import {ChainConfig, ChainFeatures, resourceFeatures} from '~/config' +import { getClient } from '../../api-client' +import { ChainConfig, ChainFeatures, resourceFeatures } from '~/config' const getResourceClient = (chain: ChainConfig) => { const api = getClient(chain) - const options: any = {api} + const options: any = { api } if (chain.resourceSampleAccount) { options.sampleAccount = chain.resourceSampleAccount } @@ -96,7 +96,8 @@ export const msToRent: Readable = derived(activeBlockchain, ($activeBloc return 1 }) -export const powerupPrice = derived( +//price per ms +export const cpuPowerupPrice = derived( [msToRent, sampleUsage, statePowerUp, info], ([$msToRent, $sampleUsage, $statePowerUp, $info]) => { if ($msToRent && $sampleUsage && $statePowerUp) { @@ -109,23 +110,55 @@ export const powerupPrice = derived( } ) -export const stakingPrice = derived( +// price per kb +export const netPowerupPrice = derived( + [msToRent, sampleUsage, statePowerUp, info], + ([$msToRent, $sampleUsage, $statePowerUp, $info]) => { + if ($msToRent && $sampleUsage && $statePowerUp) { + const price = $statePowerUp.net.price_per_kb($sampleUsage, $msToRent, $info) + return Asset.from( + $statePowerUp.net.price_per_kb($sampleUsage, $msToRent, $info), + '4,EOS' + ) + } + return Asset.from(0, '4,EOS') + } +) + +//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) + const { account } = $sampleUsage + return getStakingPrice(account.total_resources.cpu_weight, account.cpu_limit.max, $activeBlockchain); } return Asset.from(0, $activeBlockchain.coreTokenSymbol) } ) +// price per kb +export const netStakingPrice = derived( + [activeBlockchain, msToRent, sampleUsage], + ([$activeBlockchain, $msToRent, $sampleUsage]) => { + if ($msToRent && $sampleUsage) { + const { account } = $sampleUsage + return getStakingPrice(account.total_resources.net_weight, account.net_limit.max, $activeBlockchain); + } + return Asset.from(0, $activeBlockchain.coreTokenSymbol) + } +) + +function getStakingPrice(weight: Asset, max: Int64, chain: ChainConfig) { + const resouce_weight = Number(weight.units) + const resource_limit = Number(max.value) + let price = resouce_weight / resource_limit + if (chain.resourceSampleMilliseconds) { + price *= chain.resourceSampleMilliseconds + } + return Asset.fromUnits(price * 1000, chain.coreTokenSymbol) +} + export const getREXState = async (set: (v: any) => void, chain: ChainConfig) => getResourceClient(chain) .v1.rex.get_state()