Skip to content

Commit 68694db

Browse files
authored
Merge pull request #437 from greymass/swap
Fixed pricing info + updated test config
2 parents 472d7f3 + 4fd6a04 commit 68694db

File tree

8 files changed

+69
-30
lines changed

8 files changed

+69
-30
lines changed

configs/.env.jungle4.vaulta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PUBLIC_FEATURE_ROBO=true
4242
PUBLIC_FEATURE_STAKERESOURCE=true
4343
PUBLIC_FEATURE_STAKING=true
4444
PUBLIC_FEATURE_TIMESERIES=false
45-
PUBLIC_FEATURE_UNICOVE_CONTRACT_API=unicove2.gm
45+
PUBLIC_FEATURE_UNICOVE_CONTRACT_API=unicove3.gm
4646

4747
BACKEND_API_CHAIN="https://jungle4.greymass.com"
4848
BACKEND_API_HISTORY="https://jungle4.greymass.com"

src/lib/components/chart/chart-container.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
let { pair, data, ...props }: Props = $props();
1717
1818
const range: ExtendedSelectOption[] = [
19-
{ label: '1D', value: 1 },
20-
{ label: '1W', value: 7 },
21-
{ label: '1M', value: 30 }
19+
{ label: '1d', value: 1 },
20+
{ label: '1w', value: 7 },
21+
{ label: '1mo', value: 30 }
2222
// { label: '1Y', value: 365 } // We're currently only getting data for the last 30 days
2323
];
2424

src/lib/components/chart/rampricehistory.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import { getContext } from 'svelte';
33
import { Asset } from '@wharfkit/antelope';
44
import ChartContainer from './chart-container.svelte';
5-
import type { UnicoveContext } from '$lib/state/client.svelte';
5+
import type { MarketContext, UnicoveContext } from '$lib/state/client.svelte';
66
import type { HistoricalPrice } from '$lib/types';
77
import Loading from './loading.svelte';
88
99
const { network } = getContext<UnicoveContext>('state');
10+
const market = getContext<MarketContext>('market');
1011
1112
type Price = { date: string; value: number };
1213
type APIResponse = Price[] | { error: string };
@@ -49,6 +50,10 @@
4950
<Loading {pair} />
5051
{:then ramPrices}
5152
{#if ramPrices.length}
52-
<ChartContainer {pair} data={ramPrices} type="line" />
53+
<ChartContainer
54+
{pair}
55+
data={[{ date: new Date(), value: market.network.ramtoken.price }, ...ramPrices]}
56+
type="line"
57+
/>
5358
{/if}
5459
{/await}

src/lib/components/chart/tokenpricehistory.svelte

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,47 @@
22
import { getContext } from 'svelte';
33
import { Asset } from '@wharfkit/antelope';
44
import ChartContainer from './chart-container.svelte';
5-
import type { UnicoveContext } from '$lib/state/client.svelte';
5+
import type { MarketContext, UnicoveContext } from '$lib/state/client.svelte';
66
import type { HistoricalPrice } from '$lib/types';
77
import Loading from './loading.svelte';
8+
import { Currencies } from '$lib/types/currencies';
9+
import { calculateValue } from '$lib/utils';
810
9-
const { network } = getContext<UnicoveContext>('state');
11+
const context = getContext<UnicoveContext>('state');
12+
const market = getContext<MarketContext>('market');
1013
1114
type Price = { date: string; value: number };
1215
type APIResponse = Price[] | { error: string };
1316
1417
const fetchTokenPrices = async () => {
15-
if (network.supports('timeseries')) {
18+
if (context.network.supports('timeseries')) {
1619
try {
17-
const response = await network.fetch(`/${network}/api/metrics/marketprice/token`);
20+
const response = await context.network.fetch(
21+
`/${context.network}/api/metrics/marketprice/token`
22+
);
1823
const parsedTokenResponse: APIResponse = await response.json();
1924
2025
if (Array.isArray(parsedTokenResponse) && parsedTokenResponse.length) {
21-
return parsedTokenResponse.map(
22-
(price: Price) =>
23-
({
24-
date: new Date(price.date),
25-
value: Asset.from(price.value / 10000, '4,USD')
26-
}) as HistoricalPrice
27-
);
26+
const currency = Currencies[context.settings.data.displayCurrency];
27+
const pair = market.market.getPair(Currencies.USD, currency);
28+
const converted = parsedTokenResponse.map((price: Price): HistoricalPrice => {
29+
let value = Asset.from(price.value / 10000, '4,USD');
30+
if (pair) {
31+
value = calculateValue(value, pair.price);
32+
}
33+
return {
34+
date: new Date(price.date),
35+
value
36+
};
37+
});
38+
const tokenpair = market.market.getPair(context.network.token.id, currency);
39+
if (tokenpair) {
40+
converted.unshift({
41+
date: new Date(),
42+
value: tokenpair.price
43+
});
44+
}
45+
return converted;
2846
} else if ('error' in parsedTokenResponse && parsedTokenResponse.error) {
2947
throw new Error(String(parsedTokenResponse.error));
3048
} else {
@@ -38,8 +56,9 @@
3856
}
3957
};
4058
41-
let systemtoken = Asset.Symbol.from(network.config.systemtoken.symbol);
42-
let pair = $derived(String(systemtoken.name) + '/USD');
59+
let systemtoken = Asset.Symbol.from(context.network.config.systemtoken.symbol);
60+
let currency = $derived(Currencies[context.settings.data.displayCurrency]);
61+
let pair = $derived(String(systemtoken.name) + '/' + currency.symbol.name);
4362
</script>
4463

4564
{#await fetchTokenPrices()}

src/lib/state/value.svelte.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ export class NetworkValueState {
9999
return pair;
100100
});
101101

102+
readonly ramtoken = $derived.by(() => {
103+
const quote = this.systemtoken.base;
104+
const pair = this.states.market.getRAMTokenPair(quote);
105+
if (!pair) {
106+
const ramKb = this.states.network.getRamToken();
107+
return TokenPair.from({
108+
base: ramKb,
109+
quote: this.states.network.token,
110+
price: Asset.fromUnits(0, quote.symbol),
111+
updated: new Date()
112+
});
113+
}
114+
return pair;
115+
});
116+
102117
// Currently hardcoded to use the systemtoken price for the legacytoken
103118
readonly legacytoken = $derived.by(() => {
104119
if (!this.states.network.config.legacytoken) {

src/routes/[network]/api/metrics/marketprice/historic/+server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const GET: RequestHandler = async ({ locals: { network } }) => {
2626
year: await getHistoricPrice(network, systemTokenPair, '1y', Currencies.USD.symbol)
2727
});
2828

29-
const rameos = TokenHistoricPrices.from({
29+
const ramsystemtoken = TokenHistoricPrices.from({
3030
day: await getHistoricPrice(network, systemRamPair, '1d', network.token.symbol),
3131
week: await getHistoricPrice(network, systemRamPair, '1w', network.token.symbol),
3232
month: await getHistoricPrice(network, systemRamPair, '1mo', network.token.symbol),
@@ -35,17 +35,17 @@ export const GET: RequestHandler = async ({ locals: { network } }) => {
3535
});
3636

3737
const ram = TokenHistoricPrices.from({
38-
day: convertRamToUSD(rameos.day, systemtoken.day),
39-
week: convertRamToUSD(rameos.week, systemtoken.week),
40-
month: convertRamToUSD(rameos.month, systemtoken.month),
41-
quarter: convertRamToUSD(rameos.quarter, systemtoken.quarter),
42-
year: convertRamToUSD(rameos.year, systemtoken.year)
38+
day: convertRamToUSD(ramsystemtoken.day, systemtoken.day),
39+
week: convertRamToUSD(ramsystemtoken.week, systemtoken.week),
40+
month: convertRamToUSD(ramsystemtoken.month, systemtoken.month),
41+
quarter: convertRamToUSD(ramsystemtoken.quarter, systemtoken.quarter),
42+
year: convertRamToUSD(ramsystemtoken.year, systemtoken.year)
4343
});
4444

4545
return json(
4646
{
4747
ram,
48-
rameos,
48+
ramsystemtoken,
4949
systemtoken
5050
},
5151
{

src/routes/[network]/api/metrics/marketprice/ram/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const GET: RequestHandler = async ({ fetch, locals: { network } }) => {
88
if (!network.config.endpoints.metrics) {
99
return json([]);
1010
}
11-
const response = await fetch(`${network.config.endpoints.metrics}/marketprice/ram/1h/7d`);
11+
const response = await fetch(`${network.config.endpoints.metrics}/marketprice/ram/1h/1mo`);
1212
if (!response.ok) {
1313
throw new Error(`HTTP error! status: ${response.status}`);
1414
}
@@ -20,7 +20,7 @@ export const GET: RequestHandler = async ({ fetch, locals: { network } }) => {
2020
);
2121

2222
return json(historicalPrices, {
23-
headers: getCacheHeaders(30)
23+
headers: getCacheHeaders(3600)
2424
});
2525
} catch (error) {
2626
console.warn(error);

src/routes/[network]/api/metrics/marketprice/token/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const GET: RequestHandler = async ({ fetch, locals: { network } }) => {
1616
systemtoken = Asset.Symbol.from('4,EOS');
1717
}
1818
const response = await fetch(
19-
`${network.config.endpoints.metrics}/marketprice/${systemtoken.name.toLowerCase()}usd/1h/7d`
19+
`${network.config.endpoints.metrics}/marketprice/${systemtoken.name.toLowerCase()}usd/1h/1mo`
2020
);
2121
if (!response.ok) {
2222
throw new Error(`HTTP error! status: ${response.status}`);
@@ -29,7 +29,7 @@ export const GET: RequestHandler = async ({ fetch, locals: { network } }) => {
2929
);
3030

3131
return json(historicalPrices, {
32-
headers: getCacheHeaders(30)
32+
headers: getCacheHeaders(3600)
3333
});
3434
} catch (error) {
3535
console.warn(error);

0 commit comments

Comments
 (0)