Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix net price for powerup and stake #211

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
16 changes: 12 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,24 @@
import {ChainFeatures} from '~/config'
import {activeBlockchain} from '~/store'

import {powerupPrice, rexPrice, stakingPrice} from '~/pages/resources/resources'
import {
cpuPowerupPrice,
netPowerupPrice,
cpuRexPrice,
netRexPrice,
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 rexPrice = resource === 'cpu' ? cpuRexPrice : netRexPrice

const {PowerUp, REX, Staking} = ChainFeatures

Expand Down Expand Up @@ -156,9 +166,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)}
dafuga marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div class="pair">
{$token} per
Expand Down
93 changes: 77 additions & 16 deletions src/pages/resources/resources.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 { BNPrecision } from '@greymass/eosio-resources'

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that we don't want to spend too much time on this project, but we could probably use a proper type here instead of any.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResourcesOptions it not exported. Otherwise, need to change greymass/eosio-resources,Can this be ignored?

options.sampleAccount = chain.resourceSampleAccount
}
Expand Down Expand Up @@ -96,7 +97,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,18 +111,45 @@ export const powerupPrice = derived(
}
)

export const stakingPrice = derived(
// price per kb
export const netPowerupPrice = derived(
[sampleUsage, statePowerUp, info],
([$sampleUsage, $statePowerUp, $info]) => {
if ($sampleUsage && $statePowerUp) {
return Asset.from(
$statePowerUp.net.price_per_kb($sampleUsage, 1, $info),
'4,EOS'
)
ttwishing marked this conversation as resolved.
Show resolved Hide resolved
}
return Asset.from(0, '4,EOS')
}
ttwishing marked this conversation as resolved.
Show resolved Hide resolved
)

//price per ms
export const cpuStakingPrice = derived(
[activeBlockchain, msToRent, sampleUsage],
([$activeBlockchain, $msToRent, $sampleUsage]) => {
if ($msToRent && $sampleUsage) {
const {account} = $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)
let price = (cpu_weight / cpu_limit) * $msToRent
return Asset.fromUnits(price * 1000, $activeBlockchain.coreTokenSymbol)
}
return Asset.from(0, $activeBlockchain.coreTokenSymbol)
}
)

// price per kb for staking
export const netStakingPrice = derived(
[activeBlockchain, sampleUsage],
([$activeBlockchain, $sampleUsage]) => {
if ($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 Expand Up @@ -152,7 +181,7 @@ 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) {
Expand All @@ -162,6 +191,38 @@ export const rexPrice = derived(
}
)

// 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`)
}
ttwishing marked this conversation as resolved.
Show resolved Hide resolved
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
Loading