Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ttwishing committed Oct 15, 2024
1 parent b43d3bf commit 56d6ba1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 38 deletions.
19 changes: 8 additions & 11 deletions src/lib/state/network.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
APIClient,
Asset,
FetchProvider,
Int128,
Serializer,
type AssetType
} from '@wharfkit/antelope';
import { APIClient, Asset, FetchProvider, Int128, type AssetType } from '@wharfkit/antelope';
import { Chains, ChainDefinition, TokenMeta } from '@wharfkit/common';
import { RAMState, Resources, REXState, PowerUpState, type SampleUsage } from '@wharfkit/resources';
import { chainIdsToIndices } from '@wharfkit/session';
Expand Down Expand Up @@ -44,7 +37,7 @@ export class NetworkState {
public resources?: Resources = $state();
public rexstate?: REXState = $state();
public powerupstate?: PowerUpState = $state();
public sampledUsage?: SampledUsage = $state();
public sampleUsage?: SampleUsage = $state();
public tokenmeta?: TokenMeta[] = $state();
public tokenstate?: DelphiOracleTypes.datapoints = $state();
public tokenprice = $derived.by(() => {
Expand Down Expand Up @@ -114,8 +107,12 @@ export class NetworkState {
}

try {
console.log("#####", json.sampleUsage);
this.sampledUsage = SampledUsage.from(json.sampleUsage);
const sampledUsage = SampledUsage.from(json.sampleUsage);
this.sampleUsage = {
account: sampledUsage.account,
cpu: sampledUsage.cpu,
net: sampledUsage.net
};
} catch (error) {
console.log('SampleUsage Parse', error);
console.log(json);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ export interface LightAPIBalanceResponse {
@Struct.type('sampledusage')
export class SampledUsage extends Struct {
@Struct.field(API.v1.AccountObject) declare account: API.v1.AccountObject;
@Struct.field(UInt128) declare cpu: UInt128;
@Struct.field(UInt128) declare net: UInt128;
@Struct.field(UInt128) declare cpu: UInt128;
@Struct.field(UInt128) declare net: UInt128;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@
}
if (
context.network.powerupstate &&
context.network.sampledUsage &&
context.network.sampleUsage &&
context.network.chain.systemToken
) {
rentState.frac = getPowerupFrac(
resourceType,
context.network.powerupstate,
context.network.sampledUsage,
context.network.sampleUsage,
Number(rentState.amount)
);
rentState.pricePerUnit = getPowerupPrice(
resourceType,
context.network.powerupstate,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
rentState.balance = context.account.balance ? context.account.balance.liquid : undefined;
if (
context.network.rexstate &&
context.network.sampledUsage &&
context.network.sampleUsage &&
context.network.chain.systemToken
) {
rentState.pricePerUnit = getRexPrice(
resourceType,
context.network.rexstate,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Stack from '$lib/components/layout/stack.svelte';
import Transaction from '$lib/components/transaction.svelte';
import { Checksum256, Asset } from '@wharfkit/antelope';
import { Checksum256 } from '@wharfkit/antelope';
import type { TransactResult } from '@wharfkit/session';
import { RentState } from './state.svelte';
Expand All @@ -24,7 +24,7 @@
if (
context.account &&
context.network &&
context.network.sampledUsage &&
context.network.sampleUsage &&
context.network.chain.systemToken
) {
if (context.account.name) {
Expand All @@ -34,7 +34,7 @@
rentState.balance = context.account.balance ? context.account.balance.liquid : undefined;
rentState.pricePerUnit = getStakingPrice(
resourceType,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
);
} else {
Expand Down Expand Up @@ -128,9 +128,9 @@
</p>
{/if}
{#if rentState.pricePerUnit}
<p>
Price:{rentState.pricePerUnit}
</p>
<p>
Price:{rentState.pricePerUnit}
</p>
{/if}

{#if rentState.error}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@
let stakingPrice: Asset | undefined = $state();
$effect(() => {
if (context.network && context.network.sampledUsage && context.network.chain.systemToken) {
if (context.network && context.network.sampleUsage && context.network.chain.systemToken) {
powerupPrice = context.network.powerupstate
? getPowerupPrice(
resource,
context.network.powerupstate,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
)
: undefined;
rexPrice = context.network.rexstate
? getRexPrice(
resource,
context.network.rexstate,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
)
: undefined;
stakingPrice = getStakingPrice(
resource,
context.network.sampledUsage,
context.network.sampleUsage,
context.network.chain.systemToken.symbol
);
} else {
Expand Down
17 changes: 7 additions & 10 deletions src/routes/[network]/(account)/resources/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ResourceType } from './types';
import { PowerUpState } from '@wharfkit/resources';
import type { REXState } from '@wharfkit/resources';
import type { REXState, SampleUsage } 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 @@ -51,7 +48,7 @@ export const getUnit = (resourceType: ResourceType) => {
export const getPowerupPrice = (
resourceType: ResourceType,
powerupstate: PowerUpState,
sampleUsage: SampledUsage,
sampleUsage: SampleUsage,
systemTokenSymbol: Asset.Symbol
) => {
switch (resourceType) {
Expand All @@ -69,17 +66,17 @@ export const getPowerupPrice = (
export const getRexPrice = (
resourceType: ResourceType,
rexState: REXState,
sampledUsage: SampledUsage,
sampledUsage: SampleUsage,
systemTokenSymbol: Asset.Symbol
) => {
switch (resourceType) {
case ResourceType.NET:
const netPrice = rexState.net_price_per_kb(sampledUsage, 30);
console.log("netPrice = ", netPrice);
console.log('netPrice = ', netPrice);
return compatPriceWithPrecision(netPrice, systemTokenSymbol);
case ResourceType.CPU:
const cpuPrice = rexState.cpu_price_per_ms(sampledUsage, 30);
console.log("cpuPrice = ", cpuPrice);
console.log('cpuPrice = ', cpuPrice);
return compatPriceWithPrecision(cpuPrice, systemTokenSymbol);
default:
throw new Error(`unsupport resource type: ${resourceType}`);
Expand All @@ -96,7 +93,7 @@ function compatPriceWithPrecision(price: number, coreTokenSymbol: Asset.Symbol)

export const getStakingPrice = (
resourceType: ResourceType,
sampledUsage: SampledUsage,
sampledUsage: SampleUsage,
systemTokenSymbol: Asset.Symbol
) => {
const { account } = sampledUsage;
Expand All @@ -115,7 +112,7 @@ export const getStakingPrice = (
export const getPowerupFrac = (
resourceType: ResourceType,
powerupstate: PowerUpState,
sampledUsage: SampledUsage,
sampledUsage: SampleUsage,
amount: number
) => {
switch (resourceType) {
Expand Down

0 comments on commit 56d6ba1

Please sign in to comment.