Skip to content

Commit

Permalink
Merge branch 'dev' into ram-polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga authored Oct 1, 2024
2 parents 570f107 + dd58de2 commit df00c19
Show file tree
Hide file tree
Showing 21 changed files with 430 additions and 108 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Code Checks

on:
push:

jobs:
formatting:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: oven-sh/setup-bun@v2
- run: bun install
- run: bun lint
types:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: oven-sh/setup-bun@v2
- run: bun install
- run: bunx tsc --noemit

Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"@wharfkit/wallet-plugin-anchor": "^1.4.0",
"@wharfkit/wallet-plugin-metamask": "1.0.0-rc8",
"@wharfkit/wallet-plugin-privatekey": "^1.1.0",
"@wharfkit/wallet-plugin-scatter": "^1.5.1",
"@wharfkit/wallet-plugin-tokenpocket": "^1.5.1",
"@wharfkit/wallet-plugin-wombat": "^1.5.1",
"@wharfkit/web-renderer": "^1.4.1",
"big.js": "^6.2.1",
"chart.js": "^4.4.3",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/navigation/sidemenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
const destinations = $derived.by(() => {
const features = [];
if (network.config.features.staking) {
if (network.supports('staking')) {
features.push({ href: `/${network}/staking`, text: 'Staking' });
}
if (network.config.features.rammarket) {
if (network.supports('rammarket')) {
features.push({ href: `/${network}/ram`, text: 'RAM' });
}
Expand Down
9 changes: 2 additions & 7 deletions src/lib/state/client/account.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as SystemContract from '$lib/wharf/contracts/system';
import { type DataSources } from '$lib/types';
import { chainMapper } from '$lib/wharf/chains';
import { NetworkState } from '$lib/state/network.svelte';
import { calculateValue } from '$lib/utils';

const defaultDataSources = {
get_account: undefined,
Expand Down Expand Up @@ -107,12 +108,6 @@ export class AccountState {
}
}

export function calculateValue(balance: Asset, currency: Asset): Asset {
return Asset.from(
`${(currency.value * balance.value).toFixed(currency.symbol.precision)} ${currency.symbol.code}`
);
}

export interface AccountValue {
delegated: Asset;
liquid: Asset;
Expand Down Expand Up @@ -194,7 +189,7 @@ export function getBalance(network: NetworkState, sources: DataSources): Balance

// Add any staked (REX) tokens to total balance based on current value
if (sources.rex) {
if (network.config.features.rex && network.rexstate) {
if (network.supports('rex') && network.rexstate) {
const rex = network.rexToToken(sources.rex.rex_balance);
// const rex = convertRexToToken(sources.rex.rex_balance, network.rexstate);
staked.units.add(rex.units);
Expand Down
14 changes: 11 additions & 3 deletions src/lib/state/client/wharf.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import WebRenderer from '@wharfkit/web-renderer';
import { WalletPluginAnchor } from '@wharfkit/wallet-plugin-anchor';
import { WalletPluginMetaMask } from '@wharfkit/wallet-plugin-metamask';
import { WalletPluginPrivateKey } from '@wharfkit/wallet-plugin-privatekey';
import { WalletPluginWombat } from '@wharfkit/wallet-plugin-wombat';
import { WalletPluginScatter } from '@wharfkit/wallet-plugin-scatter';
import { WalletPluginTokenPocket } from '@wharfkit/wallet-plugin-tokenpocket';

import { AccountCreationPluginMetamask } from '@wharfkit/account-creation-plugin-metamask';
import { AccountCreationPluginGreymass } from '@wharfkit/account-creation-plugin-greymass';

Expand All @@ -33,9 +37,13 @@ import {

import { chainMapper } from '$lib/wharf/chains';

const metamaskWalletPlugin = new WalletPluginMetaMask();

const walletPlugins: WalletPlugin[] = [new WalletPluginAnchor(), metamaskWalletPlugin];
const walletPlugins: WalletPlugin[] = [
new WalletPluginAnchor(),
new WalletPluginMetaMask(),
new WalletPluginScatter(),
new WalletPluginTokenPocket(),
new WalletPluginWombat()
];

const accountCreationPlugins: AccountCreationPlugin[] = [
new AccountCreationPluginMetamask(),
Expand Down
28 changes: 6 additions & 22 deletions src/lib/state/network.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import {
chainConfigs,
chainMapper,
type ChainConfig,
type DefaultContracts
type DefaultContracts,
type FeatureType
} from '$lib/wharf/chains';

import { calculateValue } from './client/account.svelte';
import { tokens } from '../../routes/[network]/api/tokens/tokens';
import { calculateValue } from '$lib/utils';

export class NetworkState {
public chain: ChainDefinition;
Expand Down Expand Up @@ -53,25 +54,6 @@ export class NetworkState {
}
return undefined;
});
public stakingprice: Asset | undefined = $derived.by(() => {
if (this.sampledUsage && this.chain.systemToken) {
const { account } = this.sampledUsage;
return Asset.fromUnits(
Number(account.cpu_weight) / Number(account.cpu_limit.max),
this.chain.systemToken.symbol
);
}
return undefined;
});
public powerupprice: Asset | undefined = $derived.by(() => {
if (this.sampledUsage && this.powerupstate && this.chain.systemToken) {
return Asset.from(
this.powerupstate.cpu.price_per_ms(this.sampledUsage, 1),
this.chain.systemToken.symbol
);
}
return undefined;
});
public tokenprice = $derived.by(() => {
return this.tokenstate ? Asset.fromUnits(this.tokenstate.median, '4,USD') : undefined;
});
Expand All @@ -98,7 +80,7 @@ export class NetworkState {
system: new SystemContract({ client: this.client })
};

if (this.config.features.delphioracle) {
if (this.supports('delphioracle')) {
this.contracts.delphioracle = new DelphiOracleContract({ client: this.client });
}
}
Expand Down Expand Up @@ -148,6 +130,8 @@ export class NetworkState {
this.loaded = true;
}

supports = (feature: FeatureType): boolean => this.config.features[feature];

tokenToRex = (token: AssetType) => {
if (!this.rexstate) {
throw new Error('REX state not initialized');
Expand Down
8 changes: 7 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { ABI } from '@wharfkit/antelope';
import { Asset, type ABI } from '@wharfkit/antelope';
import yaml from 'yaml';

export function calculateValue(balance: Asset, currency: Asset): Asset {
return Asset.from(
`${(currency.value * balance.value).toFixed(currency.symbol.precision)} ${currency.symbol.code}`
);
}

export function getCacheHeaders(ttl: number, irreversible: boolean = false) {
// Maintain a ttl cache by default
let browser = `public, max-age=${ttl}, s-max-age=${ttl}`;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/(send)/send/state.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { calculateValue } from '$lib/state/client/account.svelte';
import { calculateValue } from '$lib/utils';
import { Asset, Name, Serializer } from '@wharfkit/antelope';
import { TokenBalance, TokenIdentifier, TokenMeta } from '@wharfkit/common';

Expand Down
4 changes: 2 additions & 2 deletions src/routes/[network]/(account)/ram/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
<p>Loading current RAM price...</p>
{/if}

{#if data.network.config.features.timeseries}
{#if data.historicalPrices && data.historicalPrices.length > 0}
{#if data.network.supports('timeseries') && data.historicalPrices?.length > 0}
{#if data.historicalPrices.length > 0}
<Pageheader title="Historical RAM Prices" />
<RamPriceHistory data={data.historicalPrices} />
{:else}
Expand Down
26 changes: 12 additions & 14 deletions src/routes/[network]/(account)/resources/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import Code from '$lib/components/code.svelte';
import PageHeader from '$lib/components/pageheader.svelte';
import Grid from '$lib/components/layout/grid.svelte';
import Stack from '$lib/components/layout/stack.svelte';
Expand All @@ -11,7 +10,7 @@
import { getContext } from 'svelte';
import { ResourceType } from './types';
import { NetworkConfig, ResourceState } from './state.svelte';
import { ResourceState } from './state.svelte';
const { data } = $props();
Expand All @@ -21,11 +20,6 @@
const cpuState = $state(new ResourceState(ResourceType.CPU));
const netState = $state(new ResourceState(ResourceType.NET));
const networkConfig = $state(new NetworkConfig());
$effect(() => {
networkConfig.setConfig(context.network?.config);
});
$effect(() => {
ramState.setResource(context.account?.ram);
cpuState.setResource(context.account?.cpu);
Expand All @@ -39,26 +33,30 @@
<Stack class="mt-10">
<Grid itemWidth="270px">
<ResourceWrapper resourceState={ramState}>
{#if networkConfig.hasBuyRAM}
{#if context.network?.supports('rammarket')}
<div class="flex flex-col">
<Button class="text-blue-400" variant="pill" href="/{network}/ram/buy">BUY</Button>
<Button class="text-blue-400" variant="pill" href="/{network}/ram/sell">SELL</Button>
<Button class="text-blue-400" variant="pill" href="/{network}/ram/buy/tokens">BUY</Button>
<Button class="text-blue-400" variant="pill" href="/{network}/ram/sell/tokens"
>SELL</Button
>
</div>
{/if}
</ResourceWrapper>
<ResourceWrapper resourceState={cpuState}>
{#if networkConfig.hasREX || networkConfig.hasPowerUp}
{#if context.network?.supports('rentrex') || context.network?.supports('powerup')}
<Button class="text-blue-400" variant="pill" href="/{network}/resources/cpu">RENT</Button>
{:else if networkConfig.hasStaking}
{/if}
{#if context.network?.supports('stakeresource')}
<Button class="text-blue-400" variant="pill" href="/{network}/resources/cpu/stake"
>STAKE</Button
>
{/if}
</ResourceWrapper>
<ResourceWrapper resourceState={netState}>
{#if networkConfig.hasREX || networkConfig.hasPowerUp}
{#if context.network?.supports('rentrex') || context.network?.supports('powerup')}
<Button class="text-blue-400" variant="pill" href="/{network}/resources/net">RENT</Button>
{:else if networkConfig.hasStaking}
{/if}
{#if context.network?.supports('stakeresource')}
<Button class="text-blue-400" variant="pill" href="/{network}/resources/net/stake"
>STAKE</Button
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import Stack from '$lib/components/layout/stack.svelte';
import Transaction from '$lib/components/transaction.svelte';
import { Checksum256 } from '@wharfkit/antelope';
import { Asset, Checksum256 } from '@wharfkit/antelope';
import { RentState } from './state.svelte';
import { RentType, ResourceType } from '../../types';
import type { UnicoveContext } from '$lib/state/client.svelte';
import type { NetworkState } from '$lib/state/network.svelte';
import { getContext } from 'svelte';
import Error from '../../../../../+error.svelte';
const context = getContext<UnicoveContext>('state');
interface Props {
Expand All @@ -28,21 +29,32 @@
rentState.payer = context.account.name;
rentState.receiver = context.account.name;
}
if (context.network.powerupstate && context.network.sampledUsage) {
if (
context.network.powerupstate &&
context.network.sampledUsage &&
context.network.chain.systemToken
) {
if (resourceType === ResourceType.CPU) {
rentState.frac = context.network.powerupstate.cpu.frac_by_ms(
context.network.sampledUsage,
Number(rentState.amount)
);
rentState.pricePerUnit = Asset.from(
context.network.powerupstate?.cpu.price_per_ms(context.network.sampledUsage, 1),
context.network.chain.systemToken?.symbol
);
} else {
rentState.frac = context.network.powerupstate.net.frac_by_kb(
context.network.sampledUsage,
Number(rentState.amount)
);
rentState.pricePerUnit = Asset.from(
context.network.powerupstate?.net.price_per_kb(context.network.sampledUsage, 1),
context.network.chain.systemToken?.symbol
);
}
}
rentState.balance = context.account.balance ? context.account.balance.liquid : undefined;
rentState.pricePerUnit = context.network.powerupprice;
} else {
rentState.reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,38 @@
let quantityInput: AssetInput | undefined = $state();
$effect(() => {
if (context.account && context.network) {
if (
context.account &&
context.network &&
context.network.sampledUsage &&
context.network.chain.systemToken
) {
if (context.account.name) {
rentState.payer = context.account.name;
rentState.receiver = context.account.name;
}
rentState.balance = context.account.balance ? context.account.balance.liquid : undefined;
rentState.pricePerUnit = context.network.stakingprice;
switch (resourceType) {
case ResourceType.CPU: {
rentState.pricePerUnit = Asset.fromUnits(
context.network.sampledUsage.account.cpu_weight.dividing(
context.network.sampledUsage.account.cpu_limit.max
),
context.network.chain.systemToken.symbol
);
break;
}
case ResourceType.NET: {
rentState.pricePerUnit = Asset.fromUnits(
context.network.sampledUsage.account.net_weight.dividing(
context.network.sampledUsage.account.net_limit.max
),
context.network.chain.systemToken.symbol
);
break;
}
}
} else {
rentState.reset();
}
Expand Down
Loading

0 comments on commit df00c19

Please sign in to comment.