Skip to content

Commit 588d986

Browse files
committed
feat: get asset rate added
1 parent 4621476 commit 588d986

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/services/tonapi/tonapi.service.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { address } from '@ton/ton';
22
import { Services } from '../../core/services';
3-
import { Balance, CustomPayload, IJettonsRate, WalletAssets, WalletInfo } from '../../types/swap';
3+
import {
4+
Balance,
5+
CustomPayload,
6+
IJettonsRate,
7+
Prices,
8+
WalletAssets,
9+
WalletInfo,
10+
} from '../../types/swap';
411

512
export class TonApi extends Services {
613
public async getJettonData(walletAddr: string, jettonAddress: string) {
@@ -77,4 +84,25 @@ export class TonApi extends Services {
7784
}, new Map<string, Balance>());
7885
return newBalances;
7986
}
87+
88+
/**
89+
* getAssetsRates
90+
*/
91+
public async getAssetsRates(assetsAddresses: string[]) {
92+
const addresses = assetsAddresses.join(',');
93+
const { rates } = await this.client.request.send<IJettonsRate>({
94+
baseURL: 'https://tonapi.io/v2',
95+
url: `/rates?tokens=${
96+
addresses.length > 0 ? addresses : ''
97+
},EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c&currencies=usd`,
98+
});
99+
100+
const ratesMap = assetsAddresses.reduce((map, item) => {
101+
const userFriendlyAddr = address(item).toString();
102+
map.set(userFriendlyAddr, rates[userFriendlyAddr].prices);
103+
return map;
104+
}, new Map<string, Prices>());
105+
106+
return ratesMap;
107+
}
80108
}

src/services/tonapi/tonapi.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ test('it should get user assets in type of map', async () => {
3030
);
3131
});
3232

33+
test('it should get asset rate by any address', async () => {
34+
const rateRaw = await client.tonapi.getAssetsRates([
35+
'0:0000000000000000000000000000000000000000000000000000000000000000',
36+
]);
37+
expect(
38+
rateRaw.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD,
39+
).not.toBeUndefined();
40+
expect(rateRaw.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD).toBeNumber();
41+
const rateEq = await client.tonapi.getAssetsRates([
42+
'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c',
43+
]);
44+
expect(rateEq.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD).not.toBeUndefined();
45+
expect(rateEq.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD).toBeNumber();
46+
const rateUq = await client.tonapi.getAssetsRates([
47+
'UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ',
48+
]);
49+
expect(rateUq.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD).not.toBeUndefined();
50+
expect(rateUq.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.USD).toBeNumber();
51+
});
52+
3353
// test('it should get custom payload for Hamster Kombat Token', async () => {
3454
// const customPayload = await client.tonapi.getCustomPayload(userWallet, hmstrJetton);
3555
// expect(customPayload.custom_payload).not.toBeUndefined();

src/types/swap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface IJettonsRate {
3131
}
3232

3333
export interface Rates {
34-
[key: string]: Price;
34+
[address: string]: Price;
3535
}
3636
export interface Price {
3737
prices: Prices;
@@ -41,6 +41,7 @@ export interface Price {
4141
}
4242

4343
export interface Prices {
44+
USD: number;
4445
[key: string]: number;
4546
}
4647

0 commit comments

Comments
 (0)