Skip to content

Commit

Permalink
fix net price for powerup and stake
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 2, 2024
1 parent 3506310 commit b3140ac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/pages/resources/components/forms/powerup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -23,6 +28,7 @@
export let resource: string = 'cpu'
const unit = resource === 'cpu' ? 'ms' : 'kb'
const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice
let amount: Writable<string> = writable('')
let error: string | undefined
Expand Down
14 changes: 10 additions & 4 deletions src/pages/resources/components/state/prices.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
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'
import SegmentGroup from '~/components/elements/segment/group.svelte'
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
Expand Down Expand Up @@ -156,9 +164,7 @@
<div class="offer">
<div class="service">Staking</div>
<div class="price">
{(Number($stakingPrice.value) * 1000).toFixed(
$stakingPrice.symbol.precision
)}
{$stakingPrice.value.toFixed($stakingPrice.symbol.precision)}
</div>
<div class="pair">
{$token} per
Expand Down
38 changes: 35 additions & 3 deletions src/pages/resources/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export const msToRent: Readable<number> = 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) {
Expand All @@ -109,7 +110,23 @@ 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) {
Expand All @@ -120,7 +137,22 @@ export const stakingPrice = derived(
if ($activeBlockchain.resourceSampleMilliseconds) {
price *= $activeBlockchain.resourceSampleMilliseconds
}
return Asset.fromUnits(price, $activeBlockchain.coreTokenSymbol)
return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol)
}
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
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)
}
Expand Down

0 comments on commit b3140ac

Please sign in to comment.