Skip to content

Commit 5766cae

Browse files
committed
fix rex price for net rent
1 parent 3740f3d commit 5766cae

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import {activeBlockchain, activeSession, currentAccount} from '~/store'
1111
import {systemToken} from '~/stores/tokens'
1212
import {systemTokenBalance} from '~/stores/balances'
13-
import {rexPrice} from '~/pages/resources/resources'
13+
import {cpuRexPrice, netRexPrice} from '~/pages/resources/resources'
1414
1515
import type {FormTransaction} from '~/ui-types'
1616
import Button from '~/components/elements/button.svelte'
@@ -24,6 +24,7 @@
2424
2525
export let resource = 'cpu'
2626
const unit = resource === 'cpu' ? 'ms' : 'kb'
27+
const rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice
2728
2829
let amount: Writable<string> = writable('')
2930
let error: string | undefined
@@ -42,6 +43,10 @@
4243
}
4344
)
4445
46+
const disabled: Readable<boolean> = derived(cost, ($cost) => {
47+
return $cost ? $cost.value <= 0 : true
48+
})
49+
4550
// Create a derived store of the field we expect to be modified
4651
export const field = derived([currentAccount], ([$currentAccount]) => {
4752
if ($currentAccount && $currentAccount.self_delegated_bandwidth) {
@@ -142,7 +147,7 @@
142147
<FormBalance token={$systemToken} balance={systemTokenBalance} />
143148
{/if}
144149
<InputErrorMessage errorMessage={error} />
145-
<Button fluid size="large" formValidation on:action={rex}
150+
<Button fluid disabled={$disabled} size="large" formValidation on:action={rex}
146151
>Rent {Number($amount)} {unit} for {$cost}</Button
147152
>
148153
</Form>

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import {
99
cpuPowerupPrice,
1010
netPowerupPrice,
11-
rexPrice,
11+
cpuRexPrice,
12+
netRexPrice,
1213
cpuStakingPrice,
1314
netStakingPrice,
1415
} from '~/pages/resources/resources'
@@ -21,6 +22,7 @@
2122
const unit = resource === 'cpu' ? 'ms' : 'kb'
2223
const powerupPrice = resource === 'cpu' ? cpuPowerupPrice : netPowerupPrice
2324
const stakingPrice = resource === 'cpu' ? cpuStakingPrice : netStakingPrice
25+
const rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice
2426
2527
const {PowerUp, REX, Staking} = ChainFeatures
2628

src/pages/resources/resources.ts

+43-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { derived, Readable } from 'svelte/store'
22
import { Int64, API, Asset } from '@greymass/eosio'
33
import { Resources, SampleUsage, PowerUpState, RAMState, REXState } from '@greymass/eosio-resources'
44
import { activeBlockchain } from '~/store'
5+
import { BNPrecision } from '@greymass/eosio-resources'
56

67
import { getClient } from '../../api-client'
78
import { ChainConfig, ChainFeatures, resourceFeatures } from '~/config'
@@ -112,12 +113,11 @@ export const cpuPowerupPrice = derived(
112113

113114
// price per kb
114115
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)
116+
[sampleUsage, statePowerUp, info],
117+
([$sampleUsage, $statePowerUp, $info]) => {
118+
if ($sampleUsage && $statePowerUp) {
119119
return Asset.from(
120-
$statePowerUp.net.price_per_kb($sampleUsage, $msToRent, $info),
120+
$statePowerUp.net.price_per_kb($sampleUsage, 1, $info),
121121
'4,EOS'
122122
)
123123
}
@@ -133,21 +133,18 @@ export const cpuStakingPrice = derived(
133133
const { account } = $sampleUsage
134134
const cpu_weight = Number(account.total_resources.cpu_weight.units)
135135
const cpu_limit = Number(account.cpu_limit.max.value)
136-
let price = cpu_weight / cpu_limit
137-
if ($activeBlockchain.resourceSampleMilliseconds) {
138-
price *= $activeBlockchain.resourceSampleMilliseconds
139-
}
136+
let price = (cpu_weight / cpu_limit) * $msToRent
140137
return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol)
141138
}
142139
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
143140
}
144141
)
145142

146-
// price per kb
143+
// price per kb for staking
147144
export const netStakingPrice = derived(
148-
[activeBlockchain, msToRent, sampleUsage],
149-
([$activeBlockchain, $msToRent, $sampleUsage]) => {
150-
if ($msToRent && $sampleUsage) {
145+
[activeBlockchain, sampleUsage],
146+
([$activeBlockchain, $sampleUsage]) => {
147+
if ($sampleUsage) {
151148
const { account } = $sampleUsage
152149
const net_weight = Number(account.total_resources.net_weight.units)
153150
const net_limit = Number(account.net_limit.max.value)
@@ -184,7 +181,7 @@ export const stateREX: Readable<REXState | undefined> = derived(
184181
)
185182

186183
// The price of CPU in the REX system
187-
export const rexPrice = derived(
184+
export const cpuRexPrice = derived(
188185
[msToRent, sampleUsage, stateREX],
189186
([$msToRent, $sampleUsage, $stateREX]) => {
190187
if ($msToRent && $sampleUsage && $stateREX) {
@@ -194,6 +191,38 @@ export const rexPrice = derived(
194191
}
195192
)
196193

194+
// The price of Net in the REX system
195+
export const netRexPrice = derived(
196+
[sampleUsage, stateREX],
197+
([$sampleUsage, $stateREX]) => {
198+
if ($sampleUsage && $stateREX) {
199+
const price = calculateNetRexPrice($stateREX, $sampleUsage, 30000);
200+
let precision = 4;
201+
if (price > 0 && price < 0.0001) {
202+
precision = Number(price.toExponential().split('-')[1])
203+
}
204+
return Asset.from(price, `${precision},EOS`)
205+
}
206+
return Asset.from(0, '4,EOS')
207+
}
208+
)
209+
210+
function calculateNetRexPrice(stateRex: REXState, sample: SampleUsage, unit = 1000): number {
211+
// Sample token units
212+
const tokens = Asset.fromUnits(10000, stateRex.symbol)
213+
214+
// Spending 1 EOS (10000 units) on REX gives this many tokens
215+
const bancor = Number(tokens.units) / (stateRex.total_rent.value / stateRex.total_unlent.value)
216+
// The ratio of the number of tokens received vs the sampled values
217+
const unitPrice = bancor * (Number(sample.net) / BNPrecision)
218+
// The token units spent per unit
219+
const perunit = Number(tokens.units) / unitPrice
220+
// Multiply the per unit cost by the units requested
221+
const cost = perunit * unit
222+
// Converting to an Asset
223+
return cost / Math.pow(10, stateRex.precision)
224+
}
225+
197226
// The state of the REX system
198227
export const stateRAM: Readable<RAMState | undefined> = derived(
199228
[activeBlockchain],

0 commit comments

Comments
 (0)