Skip to content

Commit

Permalink
Merge pull request #288 from dappradar/ton-integration
Browse files Browse the repository at this point in the history
Ton integration
  • Loading branch information
mantasfam authored Jul 7, 2024
2 parents d331717 + 8824bcd commit a177eff
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/factory/providers/ton/bemo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESS = 'EQDNhy-nxYFgUqzfUzImBEP67JqsyMIcyk2S5_RwNNEYku0k';
const METHOD = 'get_full_data';
const TON_ADDRESS = 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c';
const TON_ADDRESS = 'ton';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
Expand Down
15 changes: 15 additions & 0 deletions src/factory/providers/ton/evaa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESS = 'EQC8rUZqR_pWV1BylWUlPNBzyiTYVoBEmQkMIQDZXICfnuRr';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

const balances = await web3.eth.getAccountBalances(CONTRACT_ADDRESS);

formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
2 changes: 1 addition & 1 deletion src/factory/providers/ton/hipo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESS = 'EQCLyZHP4Xe8fpchQz76O-_RmUhaVc_9BAoGyJrwJrcbz2eZ';
const METHOD = 'get_treasury_state';
const TON_ADDRESS = 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c';
const TON_ADDRESS = 'ton';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
Expand Down
20 changes: 20 additions & 0 deletions src/factory/providers/ton/stakee/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESS = 'EQD2_4d91M4TVbEBVyBF8J1UwpMJc361LKVCz6bBlffMW05o';
const METHOD = 'get_pool_full_data';
const TON_ADDRESS = 'ton';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

const balances = {};
const response = await web3.eth.call(CONTRACT_ADDRESS, METHOD);

balances[TON_ADDRESS] = response[2];

formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
25 changes: 25 additions & 0 deletions src/factory/providers/ton/stormtrade/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESSES = [
'EQDynReiCeK8xlKRbYArpp4jyzZuF6-tYfhFM0O5ulOs5H0L',
'EQDpJnZP89Jyxz3euDaXXFUhwCWtaOeRmiUJTi3jGYgF8fnj',
'EQAz6ehNfL7_8NI7OVh1Qg46HsuC4kFpK-icfqK9J3Frd6CJ',
'EQBwfRtqEf3ZzhkeGsmXiC7hzTh1C5zZZzLgDH5VL8gENQ2A',
];

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

let balances;

for (const contractAdress of CONTRACT_ADDRESSES) {
const accountBalances = await web3.eth.getAccountBalances(contractAdress);
balances = formatter.sum([balances, accountBalances]);
}

formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
2 changes: 1 addition & 1 deletion src/factory/providers/ton/tonstakers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import formatter from '../../../../util/formatter';

const CONTRACT_ADDRESS = 'EQCkWxfyhAkim3g2DjKQQg8T5P4g-Q1-K_jErGcDJZ4i-vqR';
const METHOD = 'get_pool_full_data';
const TON_ADDRESS = 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c';
const TON_ADDRESS = 'ton';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
Expand Down
63 changes: 57 additions & 6 deletions src/web3Provider/ton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import Bottleneck from 'bottleneck';

const ton_center_api_url = 'https://toncenter.com/api';
const ton_center_api_headers = {
headers: {
'X-API-Key': process.env.TONCENTER_API_KEY,
},
};
const ton_api_url = 'https://tonapi.io';

const limiter = new Bottleneck({
minTime: 1000, // 1 request per second
});

@Injectable()
export class Ton {
Expand All @@ -10,12 +23,14 @@ export class Ton {
stack: params,
};

const response = await axios
.post('https://toncenter.com/api/v2/runGetMethod', requestBody, {
headers: {
'X-API-Key': process.env.TONCENTER_API_KEY,
},
})
const response = await limiter
.schedule(() =>
axios.post(
`${ton_center_api_url}/v2/runGetMethod`,
requestBody,
ton_center_api_headers,
),
)
.then((response) => response.data.result);

const { exit_code, stack } = response;
Expand All @@ -30,4 +45,40 @@ export class Ton {

return stack;
}

async getAccountBalances(address) {
const balances = {};

let response = await axios
.get(`${ton_api_url}/v2/accounts/${address}/jettons?currencies=usd`)
.then((response) => response.data);
for (const balance of response.balances) {
const packedAddress = await limiter
.schedule(() =>
axios.get(
`${ton_center_api_url}/v2/packAddress?address=${balance.jetton.address}`,
ton_center_api_headers,
),
)
.then((response) => response.data.result);
const customSafePackedAddress = this.customUrlSafeEncode(packedAddress);
balances[customSafePackedAddress] = balance.balance;
}

response = await limiter
.schedule(() =>
axios.get(
`${ton_center_api_url}/v3/account?address=${address}`,
ton_center_api_headers,
),
)
.then((response) => response.data);
balances['ton'] = response.balance;

return balances;
}

customUrlSafeEncode(address) {
return address.replace(/\//g, '_').replace(/\+/g, '-');
}
}

0 comments on commit a177eff

Please sign in to comment.