Skip to content

Commit

Permalink
format resources.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 2, 2024
1 parent 127efe0 commit 300b843
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/pages/resources/resources.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
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 {derived, Readable} from 'svelte/store'
import {API, Asset} from '@greymass/eosio'
import {
Resources,
SampleUsage,
PowerUpState,
RAMState,
REXState,
BNPrecision,
} from '@greymass/eosio-resources'

import { getClient } from '../../api-client'
import { ChainConfig, ChainFeatures, resourceFeatures } from '~/config'
import {activeBlockchain} from '~/store'

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) {
options.sampleAccount = chain.resourceSampleAccount
}
Expand Down Expand Up @@ -113,7 +120,7 @@ export const cpuPowerupPrice = derived(

// price per kb
export const netPowerupPrice = derived(
[activeBlockchain,sampleUsage, statePowerUp, info],
[activeBlockchain, sampleUsage, statePowerUp, info],
([$activeBlockchain, $sampleUsage, $statePowerUp, $info]) => {
if ($sampleUsage && $statePowerUp) {
return Asset.from(
Expand All @@ -130,7 +137,7 @@ 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) * $msToRent
Expand All @@ -145,7 +152,7 @@ export const netStakingPrice = derived(
[activeBlockchain, sampleUsage],
([$activeBlockchain, $sampleUsage]) => {
if ($sampleUsage) {
const { account } = $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
Expand Down Expand Up @@ -185,7 +192,7 @@ export const cpuRexPrice = derived(
[activeBlockchain, msToRent, sampleUsage, stateREX],
([$activeBlockchain, $msToRent, $sampleUsage, $stateREX]) => {
if ($msToRent && $sampleUsage && $stateREX) {
const price = $stateREX.price_per($sampleUsage, $msToRent * 30000);
const price = $stateREX.price_per($sampleUsage, $msToRent * 30000)
const coreTokenSymbol = $activeBlockchain.coreTokenSymbol
return compatPriceWithPrecision(price, coreTokenSymbol)
}
Expand All @@ -206,9 +213,9 @@ export const netRexPrice = derived(
}
)

function compatPriceWithPrecision(price : number, coreTokenSymbol : Asset.Symbol){
let precision = coreTokenSymbol.precision;
if (price > 0 && price < 1/Math.pow(10, precision)) {
function compatPriceWithPrecision(price: number, coreTokenSymbol: Asset.Symbol) {
let precision = coreTokenSymbol.precision
if (price > 0 && price < 1 / Math.pow(10, precision)) {
precision = Number(price.toExponential().split('-')[1])
}
return Asset.from(price, `${precision},${coreTokenSymbol.name}`)
Expand Down

0 comments on commit 300b843

Please sign in to comment.