Skip to content

Commit

Permalink
Merge pull request #302 from greymass/homepage
Browse files Browse the repository at this point in the history
Homepage
  • Loading branch information
aaroncox authored Dec 13, 2024
2 parents cb284ed + dbaa25f commit 7fceff4
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 377 deletions.
23 changes: 22 additions & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ function isNetwork(value: string) {
return isNetworkShortName(value);
}

const redirects: Record<string, string> = {
'/earn': '/staking',
'/resources/ram/buy': '/ram/buy',
'/resources/ram/sell': '/ram/sell'
};

function getManualRedirectPath(pathMore: string[]): string {
const pathname = '/' + pathMore.join('/');
return redirects[pathname];
}

function isManualRedirectPath(pathMore: string[]): boolean {
const pathname = '/' + pathMore.join('/');
return pathname in redirects;
}

export async function redirectHandle({ event, resolve }: HandleParams): Promise<Response> {
const { pathname, search } = new URL(event.request.url);

Expand Down Expand Up @@ -79,10 +95,15 @@ export async function redirectHandle({ event, resolve }: HandleParams): Promise<
url += `/${network}`;
}
if (pathMore.length > 0) {
url += `/${pathMore.join('/')}`;
if (isManualRedirectPath(pathMore)) {
url += getManualRedirectPath(pathMore);
} else {
url += `/${pathMore.join('/')}`;
}
}

if (pathname !== url) {
console.log('redirecting to', url + search);
return new Response(undefined, {
headers: { Location: url + search },
status: 301
Expand Down
23 changes: 21 additions & 2 deletions src/lib/components/elements/asset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface AssetProps extends HTMLAttributes<HTMLSpanElement> {
value?: Asset;
variant?: 'value' | 'full';
variant?: 'value' | 'full' | 'short';
fallback?: number;
}
Expand All @@ -34,6 +34,23 @@
minimumFractionDigits: context.settings.data.advancedMode ? asset?.symbol.precision : undefined
};
function abbreviatedValue(): string {
const suffixes = ['', 'K', 'M', 'B', 'T'];
let suffixIndex = 0;
if (!asset) {
return '';
}
let value = asset.value;
while (value >= 1000 && suffixIndex < suffixes.length - 1) {
value /= 1000;
suffixIndex++;
}
return `${value.toFixed(2)}${suffixes[suffixIndex]} ${asset.symbol.name}`;
}
function formatAssetValue() {
// Use Big.js to accurately convert the string into a usable number
// Some precision may be lost in extreme circumstances
Expand All @@ -52,7 +69,9 @@
<span class={cn('text-right tabular-nums', props.class)} {...props}>{string}</span>
{/snippet}

{#if isCurrency && variant === 'value'}
{#if variant === 'short'}
{@render span(abbreviatedValue())}
{:else if isCurrency && variant === 'value'}
{@render span(formatCurrencyValue())}
{:else if isCurrency && variant === 'full'}
{@render span(`${formatCurrencyValue()} ${asset?.symbol.name}`)}
Expand Down
1 change: 0 additions & 1 deletion src/lib/state/client/account.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Int128,
Int64,
Name,
UInt128,
UInt64,
type NameType
} from '@wharfkit/antelope';
Expand Down
26 changes: 25 additions & 1 deletion src/lib/state/network.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ export class NetworkState {
public contracts: DefaultContracts;

public abis?: ABICache = $state();
public globalstate?: SystemTypes.eosio_global_state = $state();
public marketcap: Asset = $derived.by(() => {
if (this.supply && this.tokenprice) {
return calculateValue(this.supply, this.tokenprice);
}
return Asset.fromUnits(0, '4,USD');
});
public ramstate?: RAMState = $state();
public resources?: Resources = $state();
public rexstate?: REXState = $state();
public globalstate?: SystemTypes.eosio_global_state = $state();
public powerupstate?: PowerUpState = $state();
public sampledUsage?: SampledUsage = $state();
public supply?: Asset = $state();
public tokenmeta?: TokenMeta[] = $state();
public tokenstate?: DelphiOracleTypes.datapoints = $state();
public tokenprice = $derived.by(() => {
Expand Down Expand Up @@ -138,6 +145,21 @@ export class NetworkState {
console.log(json);
}

try {
const index = String(this.chain.systemToken?.symbol.name);
const supply = Asset.from(json.supply[index].supply);
if (json.lockedsupply && json.lockedsupply.length) {
for (const balance of json.lockedsupply) {
const locked = Asset.from(balance);
supply.units.subtract(locked.units);
}
}
this.supply = supply;
} catch (error) {
console.log('Supply Parse', error);
console.log(json);
}

this.loaded = true;
}

Expand Down Expand Up @@ -175,10 +197,12 @@ export class NetworkState {
return {
chain: this.chain,
last_update: this.last_update,
marketcap: this.marketcap,
ramstate: this.ramstate,
ramprice: this.ramprice,
rexstate: this.rexstate,
sampleAccount: this.resources?.sampleAccount,
supply: this.supply,
tokenprice: this.tokenprice,
tokenstate: this.tokenstate
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ export function getUnstakableBalance(
export function getAPR(network: NetworkState): string {
const annualReward = 31250000;
const totalStaked = Number(network.rexstate!.total_lendable.value);
return ((annualReward / totalStaked) * 100).toFixed(2);
return ((annualReward / totalStaked) * 100).toFixed(1);
}
4 changes: 3 additions & 1 deletion src/lib/wharf/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Asset } from '@wharfkit/antelope';
import type { Asset, NameType } from '@wharfkit/antelope';
import type { ChainIndices } from '@wharfkit/common';

import { Contract as DelphiOracleContract } from '$lib/wharf/contracts/delphioracle';
Expand All @@ -16,6 +16,7 @@ export interface DefaultContracts {
export interface ChainConfig {
name: ChainShortName;
features: Record<FeatureType, boolean>;
lockedsupply?: NameType[]; // Accounts where tokens exist but are not in circulation
symbol: Asset.SymbolType;
timeseries_api?: string;
}
Expand Down Expand Up @@ -61,6 +62,7 @@ export const chainConfigs: Record<string, ChainConfig> = {
stakeresource: false,
staking: true
},
lockedsupply: ['eosio'],
symbol: '4,EOS'
},
// Jungle4
Expand Down
Loading

0 comments on commit 7fceff4

Please sign in to comment.