Skip to content

Commit

Permalink
fix rex price for net and cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 15, 2024
1 parent 9a6ab79 commit b43d3bf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@wharfkit/antelope": "^1.0.11",
"@wharfkit/common": "^1.4.0",
"@wharfkit/contract": "^1.1.5",
"@wharfkit/resources": "^1.2.3",
"@wharfkit/resources": "^1.3.0",
"@wharfkit/roborovski": "^1.0.0",
"@wharfkit/session": "^1.4.0",
"@wharfkit/transact-plugin-resource-provider": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
let stakingPrice: Asset | undefined = $state();
$effect(() => {
if (
context.network &&
context.network.sampledUsage &&
context.network.chain.systemToken
) {
powerupPrice = context.network.powerupstate ? getPowerupPrice(
resource,
context.network.powerupstate,
context.network.sampledUsage,
context.network.chain.systemToken.symbol
) : undefined;
rexPrice = context.network.rexstate ? getRexPrice(
resource,
context.network.rexstate,
context.network.sampledUsage,
context.network.chain.systemToken.symbol
): undefined;
if (context.network && context.network.sampledUsage && context.network.chain.systemToken) {
powerupPrice = context.network.powerupstate
? getPowerupPrice(
resource,
context.network.powerupstate,
context.network.sampledUsage,
context.network.chain.systemToken.symbol
)
: undefined;
rexPrice = context.network.rexstate
? getRexPrice(
resource,
context.network.rexstate,
context.network.sampledUsage,
context.network.chain.systemToken.symbol
)
: undefined;
stakingPrice = getStakingPrice(
resource,
context.network.sampledUsage,
Expand Down Expand Up @@ -71,7 +71,7 @@
<div>Power up</div>
<div>
{#if powerupPrice}
{powerupPrice.value.toFixed(powerupPrice.symbol.precision)}
{powerupPrice.quantity}
{/if}
</div>
<div>
Expand All @@ -88,7 +88,7 @@
<div>REX</div>
<div>
{#if rexPrice}
{rexPrice.value.toFixed(rexPrice.symbol.precision)}
{rexPrice.quantity}
{/if}
</div>
<div>
Expand All @@ -105,7 +105,7 @@
<div>Staking</div>
<div>
{#if stakingPrice}
{stakingPrice.value.toFixed(stakingPrice.symbol.precision)}
{stakingPrice.quantity}
{/if}
</div>
<div>
Expand Down
1 change: 0 additions & 1 deletion src/routes/[network]/(account)/resources/cpu/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PageHeader title="Network Resources" />
<Stack class="mt-10">
<State resource={ResourceType.CPU} />

<Prices
resource={ResourceType.CPU}
powerupLink="/{network}/resources/cpu/powerup"
Expand Down
1 change: 0 additions & 1 deletion src/routes/[network]/(account)/resources/net/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<PageHeader title="Network Resources" />
<Stack class="mt-10">
<State resource={ResourceType.NET} />

<Prices
resource={ResourceType.NET}
powerupLink="/{network}/resources/net/powerup"
Expand Down
72 changes: 39 additions & 33 deletions src/routes/[network]/(account)/resources/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { PowerUpState } from '@wharfkit/resources';
import type { REXState } from '@wharfkit/resources';
import { Asset } from '@wharfkit/antelope';
import { SampledUsage } from '$lib/types';
import { UInt128 } from '@wharfkit/antelope';
import BN from 'bn.js';

export const calSize = (available: number) => {
let size = 0;
Expand Down Expand Up @@ -46,78 +48,82 @@ export const getUnit = (resourceType: ResourceType) => {
}
};

export const getPowerupPrice = (resourceType: ResourceType,
export const getPowerupPrice = (
resourceType: ResourceType,
powerupstate: PowerUpState,
sampleUsage: SampledUsage,
systemTokenSymbol: Asset.Symbol) => {
systemTokenSymbol: Asset.Symbol
) => {
switch (resourceType) {
case ResourceType.NET:
const netPrice = powerupstate.net.price_per_kb(sampleUsage, 1)
const netPrice = powerupstate.net.price_per_kb(sampleUsage, 1);
return Asset.from(netPrice, systemTokenSymbol);
case ResourceType.CPU:
const cpuPrice = powerupstate.cpu.price_per_ms(sampleUsage, 1)
const cpuPrice = powerupstate.cpu.price_per_ms(sampleUsage, 1);
return Asset.from(cpuPrice, systemTokenSymbol);
default:
throw new Error(`unsupport resource type: ${resourceType}`);
}
};

export const getRexPrice = (resourceType: ResourceType,
export const getRexPrice = (
resourceType: ResourceType,
rexState: REXState,
sampledUsage: SampledUsage,
systemTokenSymbol: Asset.Symbol) => {
systemTokenSymbol: Asset.Symbol
) => {
switch (resourceType) {
case ResourceType.NET:
const netPrice = rexState.price_per(sampledUsage, 30000);
return Asset.from(netPrice, systemTokenSymbol);
const netPrice = rexState.net_price_per_kb(sampledUsage, 30);
console.log("netPrice = ", netPrice);
return compatPriceWithPrecision(netPrice, systemTokenSymbol);
case ResourceType.CPU:
const cpuPrice = rexState.price_per(sampledUsage, 30000);
return Asset.from(cpuPrice, systemTokenSymbol);
const cpuPrice = rexState.cpu_price_per_ms(sampledUsage, 30);
console.log("cpuPrice = ", cpuPrice);
return compatPriceWithPrecision(cpuPrice, systemTokenSymbol);
default:
throw new Error(`unsupport resource type: ${resourceType}`);
}
};

export const getStakingPrice = (resourceType: ResourceType,
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}`);
}

export const getStakingPrice = (
resourceType: ResourceType,
sampledUsage: SampledUsage,
systemTokenSymbol: Asset.Symbol) => {
systemTokenSymbol: Asset.Symbol
) => {
const { account } = sampledUsage;
switch (resourceType) {
case ResourceType.NET:
const pricePerKb = account.net_weight
.multiplying(1000)
.dividing(
account.net_limit.max
);
const pricePerKb = account.net_weight.multiplying(1000).dividing(account.net_limit.max);
return Asset.fromUnits(pricePerKb, systemTokenSymbol);
case ResourceType.CPU:
const pricePerMs = account.cpu_weight
.multiplying(1000)
.dividing(
account.cpu_limit.max
);
const pricePerMs = account.cpu_weight.multiplying(1000).dividing(account.cpu_limit.max);
return Asset.fromUnits(pricePerMs, systemTokenSymbol);
default:
throw new Error(`unsupport resource type: ${resourceType}`);
}
};

export const getPowerupFrac = (resourceType: ResourceType,
export const getPowerupFrac = (
resourceType: ResourceType,
powerupstate: PowerUpState,
sampledUsage: SampledUsage,
amount: number) => {
amount: number
) => {
switch (resourceType) {
case ResourceType.NET:
return powerupstate.net.frac_by_kb(
sampledUsage,
amount
);
return powerupstate.net.frac_by_kb(sampledUsage, amount);
case ResourceType.CPU:
return powerupstate.cpu.frac_by_ms(
sampledUsage,
amount
);
return powerupstate.cpu.frac_by_ms(sampledUsage, amount);
default:
throw new Error(`unsupport resource type: ${resourceType}`);
}
};
};

0 comments on commit b43d3bf

Please sign in to comment.