diff --git a/src/factory/providers/ton/bemo/index.ts b/src/factory/providers/ton/bemo/index.ts index f12ea45..8f84f2e 100644 --- a/src/factory/providers/ton/bemo/index.ts +++ b/src/factory/providers/ton/bemo/index.ts @@ -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> { const { block, chain, provider, web3 } = params; diff --git a/src/factory/providers/ton/evaa/index.ts b/src/factory/providers/ton/evaa/index.ts new file mode 100644 index 0000000..d976998 --- /dev/null +++ b/src/factory/providers/ton/evaa/index.ts @@ -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> { + const { block, chain, provider, web3 } = params; + + const balances = await web3.eth.getAccountBalances(CONTRACT_ADDRESS); + + formatter.convertBalancesToFixed(balances); + return { balances }; +} + +export { tvl }; diff --git a/src/factory/providers/ton/hipo/index.ts b/src/factory/providers/ton/hipo/index.ts index 196e8f2..d51a38b 100644 --- a/src/factory/providers/ton/hipo/index.ts +++ b/src/factory/providers/ton/hipo/index.ts @@ -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> { const { block, chain, provider, web3 } = params; diff --git a/src/factory/providers/ton/stakee/index.ts b/src/factory/providers/ton/stakee/index.ts new file mode 100644 index 0000000..7f6e80d --- /dev/null +++ b/src/factory/providers/ton/stakee/index.ts @@ -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> { + 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 }; diff --git a/src/factory/providers/ton/stormtrade/index.ts b/src/factory/providers/ton/stormtrade/index.ts new file mode 100644 index 0000000..6cdbf91 --- /dev/null +++ b/src/factory/providers/ton/stormtrade/index.ts @@ -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> { + 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 }; diff --git a/src/factory/providers/ton/tonstakers/index.ts b/src/factory/providers/ton/tonstakers/index.ts index cbb6597..c700e48 100644 --- a/src/factory/providers/ton/tonstakers/index.ts +++ b/src/factory/providers/ton/tonstakers/index.ts @@ -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> { const { block, chain, provider, web3 } = params; diff --git a/src/web3Provider/ton.ts b/src/web3Provider/ton.ts index 468d0ca..1ab3ec4 100644 --- a/src/web3Provider/ton.ts +++ b/src/web3Provider/ton.ts @@ -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 { @@ -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; @@ -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, '-'); + } }