From 1c805a486424816e5c1b1ac96d37de4489d2727e Mon Sep 17 00:00:00 2001 From: Mantas S Date: Sun, 7 Jul 2024 10:51:27 +0300 Subject: [PATCH 1/3] evaa --- src/factory/providers/ton/evaa/index.ts | 15 +++++++++ src/web3Provider/ton.ts | 43 ++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/factory/providers/ton/evaa/index.ts 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/web3Provider/ton.ts b/src/web3Provider/ton.ts index 468d0ca..d911923 100644 --- a/src/web3Provider/ton.ts +++ b/src/web3Provider/ton.ts @@ -1,6 +1,14 @@ import { Injectable } from '@nestjs/common'; import axios from 'axios'; +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'; + @Injectable() export class Ton { async call(address, method, params = []) { @@ -11,11 +19,11 @@ export class Ton { }; const response = await axios - .post('https://toncenter.com/api/v2/runGetMethod', requestBody, { - headers: { - 'X-API-Key': process.env.TONCENTER_API_KEY, - }, - }) + .post( + `${ton_center_api_url}/v2/runGetMethod`, + requestBody, + ton_center_api_headers, + ) .then((response) => response.data.result); const { exit_code, stack } = response; @@ -30,4 +38,29 @@ 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 axios + .get( + `${ton_center_api_url}/v2/packAddress?address=${balance.jetton.address}`, + ton_center_api_headers, + ) + .then((response) => response.data.result); + balances[packedAddress] = balance.balance; + } + + response = await axios + .get(`${ton_center_api_url}/v3/account?address=${address}`) + .then((response) => response.data); + balances['ton'] = response.balance; + + return balances; + } } From 71a04b1cc3566ea3c8003cc8140475a061216e6b Mon Sep 17 00:00:00 2001 From: Mantas S Date: Sun, 7 Jul 2024 13:02:19 +0300 Subject: [PATCH 2/3] 3 more integrations --- src/factory/providers/ton/bemo/index.ts | 2 +- src/factory/providers/ton/hipo/index.ts | 2 +- src/factory/providers/ton/stakee/index.ts | 20 +++++++++ src/factory/providers/ton/stormtrade/index.ts | 25 +++++++++++ src/factory/providers/ton/tonstakers/index.ts | 2 +- src/web3Provider/ton.ts | 44 +++++++++++++------ 6 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 src/factory/providers/ton/stakee/index.ts create mode 100644 src/factory/providers/ton/stormtrade/index.ts 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/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..3ffc715 --- /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', // jUSDT vault + 'EQDpJnZP89Jyxz3euDaXXFUhwCWtaOeRmiUJTi3jGYgF8fnj', // TON-M vault + 'EQAz6ehNfL7_8NI7OVh1Qg46HsuC4kFpK-icfqK9J3Frd6CJ', // USDT vault + 'EQBwfRtqEf3ZzhkeGsmXiC7hzTh1C5zZZzLgDH5VL8gENQ2A', // Notcoin pre-market vault +]; + +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 d911923..1ab3ec4 100644 --- a/src/web3Provider/ton.ts +++ b/src/web3Provider/ton.ts @@ -1,5 +1,6 @@ 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 = { @@ -9,6 +10,10 @@ const ton_center_api_headers = { }; const ton_api_url = 'https://tonapi.io'; +const limiter = new Bottleneck({ + minTime: 1000, // 1 request per second +}); + @Injectable() export class Ton { async call(address, method, params = []) { @@ -18,11 +23,13 @@ export class Ton { stack: params, }; - const response = await axios - .post( - `${ton_center_api_url}/v2/runGetMethod`, - requestBody, - ton_center_api_headers, + const response = await limiter + .schedule(() => + axios.post( + `${ton_center_api_url}/v2/runGetMethod`, + requestBody, + ton_center_api_headers, + ), ) .then((response) => response.data.result); @@ -45,22 +52,33 @@ export class Ton { 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 axios - .get( - `${ton_center_api_url}/v2/packAddress?address=${balance.jetton.address}`, - ton_center_api_headers, + 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); - balances[packedAddress] = balance.balance; + const customSafePackedAddress = this.customUrlSafeEncode(packedAddress); + balances[customSafePackedAddress] = balance.balance; } - response = await axios - .get(`${ton_center_api_url}/v3/account?address=${address}`) + 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, '-'); + } } From 8824bcd25d70906b52241bcbcead64236f25d268 Mon Sep 17 00:00:00 2001 From: Mantas S Date: Sun, 7 Jul 2024 13:03:20 +0300 Subject: [PATCH 3/3] Update index.ts --- src/factory/providers/ton/stormtrade/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/factory/providers/ton/stormtrade/index.ts b/src/factory/providers/ton/stormtrade/index.ts index 3ffc715..6cdbf91 100644 --- a/src/factory/providers/ton/stormtrade/index.ts +++ b/src/factory/providers/ton/stormtrade/index.ts @@ -2,10 +2,10 @@ import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl'; import formatter from '../../../../util/formatter'; const CONTRACT_ADDRESSES = [ - 'EQDynReiCeK8xlKRbYArpp4jyzZuF6-tYfhFM0O5ulOs5H0L', // jUSDT vault - 'EQDpJnZP89Jyxz3euDaXXFUhwCWtaOeRmiUJTi3jGYgF8fnj', // TON-M vault - 'EQAz6ehNfL7_8NI7OVh1Qg46HsuC4kFpK-icfqK9J3Frd6CJ', // USDT vault - 'EQBwfRtqEf3ZzhkeGsmXiC7hzTh1C5zZZzLgDH5VL8gENQ2A', // Notcoin pre-market vault + 'EQDynReiCeK8xlKRbYArpp4jyzZuF6-tYfhFM0O5ulOs5H0L', + 'EQDpJnZP89Jyxz3euDaXXFUhwCWtaOeRmiUJTi3jGYgF8fnj', + 'EQAz6ehNfL7_8NI7OVh1Qg46HsuC4kFpK-icfqK9J3Frd6CJ', + 'EQBwfRtqEf3ZzhkeGsmXiC7hzTh1C5zZZzLgDH5VL8gENQ2A', ]; async function tvl(params: ITvlParams): Promise> {