Skip to content

Commit e5de896

Browse files
committed
feat: added getWalletAssets function to tonapi service
1 parent 0ef6843 commit e5de896

File tree

3 files changed

+128
-13
lines changed

3 files changed

+128
-13
lines changed

src/services/tonapi/tonapi.service.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { address } from '@ton/ton';
12
import { Services } from '../../core/services';
2-
import { Balance, CustomPayload } from '../../types/swap';
3+
import { Balance, CustomPayload, IJettonsRate, WalletAssets, WalletInfo } from '../../types/swap';
34

45
export class TonApi extends Services {
56
public async getJettonData(walletAddr: string, jettonAddress: string) {
@@ -21,4 +22,59 @@ export class TonApi extends Services {
2122

2223
return data;
2324
}
25+
26+
/**
27+
* getWalletAssets
28+
*/
29+
public async getWalletAssets(
30+
walletAddress: string,
31+
currencies: string[] = ['usd'],
32+
custom_payload: boolean = true,
33+
) {
34+
const { balances } = await this.client.request.send<WalletAssets>({
35+
baseURL: 'https://tonapi.io/v2',
36+
maxBodyLength: Infinity,
37+
url: `/accounts/${walletAddress}/jettons?currencies=${currencies.join(',')}${
38+
custom_payload ? '&supported_extensions=custom_payload' : ''
39+
}`,
40+
});
41+
42+
const addresses = balances.map((item) => item.jetton.address).join(',');
43+
44+
const { rates } = await this.client.request.send<IJettonsRate>({
45+
baseURL: 'https://tonapi.io/v2',
46+
url: `/rates?tokens=${
47+
addresses.length > 0 ? addresses : ''
48+
},EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c&currencies=usd`,
49+
});
50+
51+
const { balance: tonBalance } = await this.client.request.send<WalletInfo>({
52+
baseURL: 'https://tonapi.io/v2',
53+
url: `/accounts/${walletAddress}`,
54+
});
55+
56+
balances.push({
57+
balance: String(tonBalance),
58+
wallet_address: {
59+
address: walletAddress,
60+
is_scam: false,
61+
is_wallet: true,
62+
},
63+
jetton: {
64+
address: 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c',
65+
symbol: 'TON',
66+
name: 'TON',
67+
image: 'https://asset.ston.fi/img/EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c/4ecd4687e0b5b8ff21a7fbe03f9d281c26a2dc13eac7b7d16048cc693fe0ec39',
68+
decimals: 9,
69+
verification: '',
70+
},
71+
});
72+
const newBalances = balances.reduce((map, item) => {
73+
const userFriendlyAddr = address(item.jetton.address).toString();
74+
item.jetton.prices = rates[userFriendlyAddr].prices;
75+
map.set(userFriendlyAddr, item);
76+
return map;
77+
}, new Map<string, Balance>());
78+
return newBalances;
79+
}
2480
}

src/services/tonapi/tonapi.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ test('it should get fail on random address', async () => {
1616
expect(async () => await client.tonapi.getJettonData(userWallet, 'hmstrJetton')).toThrow();
1717
});
1818

19+
test('it should get user assets in type of map', async () => {
20+
const data = await client.tonapi.getWalletAssets(
21+
'UQAaIQh7jVlOEylI8jM8OI1O-yYGZRqUfJRxuR-K57pskl9I',
22+
);
23+
expect(data instanceof Map).toBeTrue();
24+
expect(
25+
data.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.jetton.name,
26+
).not.toBeUndefined();
27+
28+
expect(data.get('EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c')?.jetton.name).toEqual(
29+
'TON',
30+
);
31+
});
32+
1933
// test('it should get custom payload for Hamster Kombat Token', async () => {
2034
// const customPayload = await client.tonapi.getCustomPayload(userWallet, hmstrJetton);
2135
// expect(customPayload.custom_payload).not.toBeUndefined();

src/types/swap.ts

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
export interface Balance {
22
balance: string;
3+
price?: Price;
34
wallet_address: WalletAddress;
45
jetton: Jetton;
56
extensions?: string[];
6-
}
7-
8-
export interface WalletAddress {
9-
address: string;
10-
is_scam: boolean;
11-
is_wallet: boolean;
7+
lock?: Lock;
128
}
139

1410
export interface Jetton {
@@ -21,15 +17,64 @@ export interface Jetton {
2117
prices?: Prices;
2218
custom_payload_api_uri?: string;
2319
}
24-
export interface Prices {
25-
USD: number;
26-
}
2720

2821
export interface CustomPayload {
2922
custom_payload: string;
3023
state_init: string;
3124
}
32-
export interface CustomPayload {
33-
custom_payload: string;
34-
state_init: string;
25+
26+
export interface WalletAssets {
27+
balances: Balance[];
28+
}
29+
export interface IJettonsRate {
30+
rates: Rates;
31+
}
32+
33+
export interface Rates {
34+
[key: string]: Price;
35+
}
36+
export interface Price {
37+
prices: Prices;
38+
diff_24h: Diff24h;
39+
diff_7d: Diff7d;
40+
diff_30d: Diff30d;
41+
}
42+
43+
export interface Prices {
44+
[key: string]: number;
45+
}
46+
47+
export interface Diff24h {
48+
[key: string]: number;
49+
}
50+
51+
export interface Diff7d {
52+
[key: string]: number;
53+
}
54+
55+
export interface Diff30d {
56+
TON: string;
57+
}
58+
59+
export interface WalletAddress {
60+
address: string;
61+
name?: string;
62+
is_scam: boolean;
63+
icon?: string;
64+
is_wallet: boolean;
65+
}
66+
67+
export interface Lock {
68+
amount: string;
69+
till: number;
70+
}
71+
72+
export interface WalletInfo {
73+
address: string;
74+
balance: number;
75+
last_activity: number;
76+
status: string;
77+
interfaces: string[];
78+
get_methods: string[];
79+
is_wallet: boolean;
3580
}

0 commit comments

Comments
 (0)