Skip to content

Commit

Permalink
fix rex price for net rent
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Sep 19, 2024
1 parent 7ad533a commit 70ac369
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/pages/resources/components/forms/rex.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -24,6 +24,7 @@
export let resource = 'cpu'
const unit = resource === 'cpu' ? 'ms' : 'kb'
const rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice
let amount: Writable<string> = writable('')
let error: string | undefined
Expand All @@ -42,6 +43,10 @@
}
)
const disabled: Readable<boolean> = 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) {
Expand Down Expand Up @@ -142,7 +147,7 @@
<FormBalance token={$systemToken} balance={systemTokenBalance} />
{/if}
<InputErrorMessage errorMessage={error} />
<Button fluid size="large" formValidation on:action={rex}
<Button fluid disabled={$disabled} size="large" formValidation on:action={rex}
>Rent {Number($amount)} {unit} for {$cost}</Button
>
</Form>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/resources/components/state/prices.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import {
cpuPowerupPrice,
netPowerupPrice,
rexPrice,
cpuRexPrice,
netRexPrice,
cpuStakingPrice,
netStakingPrice,
} from '~/pages/resources/resources'
Expand All @@ -21,6 +22,7 @@
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
Expand Down
48 changes: 40 additions & 8 deletions src/pages/resources/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 { BNPrecision } from '@greymass/eosio-resources'

import { getClient } from '../../api-client'
import { ChainConfig, ChainFeatures, resourceFeatures } from '~/config'
Expand Down Expand Up @@ -112,12 +113,11 @@ export const cpuPowerupPrice = 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)
[sampleUsage, statePowerUp, info],
([$sampleUsage, $statePowerUp, $info]) => {
if ($sampleUsage && $statePowerUp) {
return Asset.from(
$statePowerUp.net.price_per_kb($sampleUsage, $msToRent, $info),
$statePowerUp.net.price_per_kb($sampleUsage, 1, $info),
'4,EOS'
)
}
Expand Down Expand Up @@ -181,17 +181,49 @@ export const stateREX: Readable<REXState | undefined> = derived(
)

// The price of CPU in the REX system
export const rexPrice = derived(
export const cpuRexPrice = derived(
[msToRent, sampleUsage, stateREX],
([$msToRent, $sampleUsage, $stateREX]) => {
if ($msToRent && $sampleUsage && $stateREX) {
const price = $stateREX.price_per($sampleUsage);
return Asset.from(price * $msToRent, '4,EOS')
return Asset.from($stateREX.price_per($sampleUsage, $msToRent * 30000), '4,EOS')
}
return Asset.from(0, '4,EOS')
}
)

// The price of Net in the REX system
export const netRexPrice = derived(
[sampleUsage, stateREX],
([$sampleUsage, $stateREX]) => {
if ($sampleUsage && $stateREX) {
const price = calculateNetRexPrice($stateREX, $sampleUsage, 30000);
let precision = 4;
if (price > 0 && price < 0.0001) {
precision = Number(price.toExponential().split('-')[1])
}

return Asset.from(price, `${precision},EOS`)
}
return Asset.from(0, '4,EOS')
}
)

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<RAMState | undefined> = derived(
[activeBlockchain],
Expand Down

0 comments on commit 70ac369

Please sign in to comment.