Skip to content

Commit 0442a83

Browse files
committed
fix net price for powerup and stake
1 parent 3506310 commit 0442a83

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/pages/resources/components/forms/powerup.svelte

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import {activeBlockchain, activeSession, currentAccount} from '~/store'
1010
import {systemToken} from '~/stores/tokens'
1111
import {systemTokenBalance} from '~/stores/balances'
12-
import {powerupPrice, sampleUsage, statePowerUp} from '~/pages/resources/resources'
12+
import {
13+
cpuPowerupPrice,
14+
netPowerupPrice,
15+
sampleUsage,
16+
statePowerUp,
17+
} from '~/pages/resources/resources'
1318
1419
import type {FormTransaction} from '~/ui-types'
1520
import Button from '~/components/elements/button.svelte'
@@ -23,6 +28,7 @@
2328
2429
export let resource: string = 'cpu'
2530
const unit = resource === 'cpu' ? 'ms' : 'kb'
31+
const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice
2632
2733
let amount: Writable<string> = writable('')
2834
let error: string | undefined
@@ -33,7 +39,6 @@
3339
[activeBlockchain, amount, powerupPrice],
3440
([$activeBlockchain, $amount, $powerupPrice]) => {
3541
if ($activeBlockchain && $powerupPrice) {
36-
3742
return Asset.from(
3843
Number($powerupPrice.value) * Number($amount),
3944
$activeBlockchain.coreTokenSymbol

src/pages/resources/components/state/prices.svelte

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
import {ChainFeatures} from '~/config'
66
import {activeBlockchain} from '~/store'
77
8-
import {powerupPrice, rexPrice, stakingPrice} from '~/pages/resources/resources'
8+
import {
9+
cpuPowerupPrice,
10+
netPowerupPrice,
11+
rexPrice,
12+
cpuStakingPrice,
13+
netStakingPrice,
14+
} from '~/pages/resources/resources'
915
1016
import Button from '~/components/elements/button.svelte'
1117
import Segment from '~/components/elements/segment.svelte'
1218
import SegmentGroup from '~/components/elements/segment/group.svelte'
1319
1420
export let resource = 'cpu'
1521
const unit = resource === 'cpu' ? 'ms' : 'kb'
22+
const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice
23+
const stakingPrice = resource === 'cpu' ? cpuStakingPrice : netStakingPrice
1624
1725
const {PowerUp, REX, Staking} = ChainFeatures
1826
@@ -156,9 +164,7 @@
156164
<div class="offer">
157165
<div class="service">Staking</div>
158166
<div class="price">
159-
{(Number($stakingPrice.value) * 1000).toFixed(
160-
$stakingPrice.symbol.precision
161-
)}
167+
{$stakingPrice.value.toFixed($stakingPrice.symbol.precision)}
162168
</div>
163169
<div class="pair">
164170
{$token} per

src/pages/resources/resources.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export const msToRent: Readable<number> = derived(activeBlockchain, ($activeBloc
9696
return 1
9797
})
9898

99-
export const powerupPrice = derived(
99+
//price per ms
100+
export const cpuPowerupPrice = derived(
100101
[msToRent, sampleUsage, statePowerUp, info],
101102
([$msToRent, $sampleUsage, $statePowerUp, $info]) => {
102103
if ($msToRent && $sampleUsage && $statePowerUp) {
@@ -109,7 +110,20 @@ export const powerupPrice = derived(
109110
}
110111
)
111112

112-
export const stakingPrice = derived(
113+
// price per kb
114+
export const netPowerupPrice = derived(
115+
[msToRent, sampleUsage, statePowerUp, info],
116+
([$msToRent, $sampleUsage, $statePowerUp, $info]) => {
117+
if ($msToRent && $sampleUsage && $statePowerUp) {
118+
const price = $statePowerUp.net.price_per_kb($sampleUsage, $msToRent, $info)
119+
return Asset.from(price,'4,EOS')
120+
}
121+
return Asset.from(0, '4,EOS')
122+
}
123+
)
124+
125+
//price per ms
126+
export const cpuStakingPrice = derived(
113127
[activeBlockchain, msToRent, sampleUsage],
114128
([$activeBlockchain, $msToRent, $sampleUsage]) => {
115129
if ($msToRent && $sampleUsage) {
@@ -120,7 +134,22 @@ export const stakingPrice = derived(
120134
if ($activeBlockchain.resourceSampleMilliseconds) {
121135
price *= $activeBlockchain.resourceSampleMilliseconds
122136
}
123-
return Asset.fromUnits(price, $activeBlockchain.coreTokenSymbol)
137+
return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol)
138+
}
139+
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
140+
}
141+
)
142+
143+
// price per kb
144+
export const netStakingPrice = derived(
145+
[activeBlockchain, msToRent, sampleUsage],
146+
([$activeBlockchain, $msToRent, $sampleUsage]) => {
147+
if ($msToRent && $sampleUsage) {
148+
const {account} = $sampleUsage
149+
const net_weight = Number(account.total_resources.net_weight.units)
150+
const net_limit = Number(account.net_limit.max.value)
151+
let price = net_weight / net_limit
152+
return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol)
124153
}
125154
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
126155
}

0 commit comments

Comments
 (0)