Skip to content

Commit

Permalink
Merge pull request #286 from dappradar/ton-integration
Browse files Browse the repository at this point in the history
Ton chain and 3 dapps integration
  • Loading branch information
mantasfam authored Jul 3, 2024
2 parents 8adb82d + 65c8ccb commit 3de90fe
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BASE_NODE_URL=https://base.llamarpc.com
LINEA_NODE_URL=https://rpc.linea.build
BTTC_NODE_URL=https://rpc.bittorrentchain.io
XAI_NODE_URL=
TONCENTER_API_KEY=
##======================== LOGSTASH ========================
LOGSTASH_PORT=
LOGSTASH_HOST=
Expand Down
1 change: 1 addition & 0 deletions src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ nodeUrls['WAX_NODE_URL'] = process.env['WAX_NODE_URL'];
nodeUrls['APTOS_NODE_URL'] = process.env['APTOS_NODE_URL'];
nodeUrls['BTTC_NODE_URL'] = process.env['BTTC_NODE_URL'];
nodeUrls['XAI_NODE_URL'] = process.env['XAI_NODE_URL'];
nodeUrls['TON_NODE_URL'] = process.env['TON_NODE_URL'];

export { config, nodeUrls };
5 changes: 4 additions & 1 deletion src/factory/factory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export class FactoryService {
if (req.block === undefined) {
throw new RpcException('Block is undefined');
}
if (this.web3ProviderService.checkNodeUrl(req?.chain)) {
if (
this.web3ProviderService.checkNodeUrl(req?.chain) &&
req.chain !== 'ton'
) {
throw new RpcException('Node URL is not provided');
}

Expand Down
27 changes: 27 additions & 0 deletions src/factory/providers/ton/dedust/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import axios from 'axios';
import formatter from '../../../../util/formatter';
import BigNumber from 'bignumber.js';

const POOLS_URI = 'https://api.dedust.io/v1/pools';

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

const balances = {};
const pools = (await axios.get(POOLS_URI)).data;

pools.forEach((pool) => {
balances[pool.left_token_root] = BigNumber(
balances[pool.left_token_root] || 0,
).plus(pool.left_token_reserve);
balances[pool.right_token_root] = BigNumber(
balances[pool.right_token_root] || 0,
).plus(pool.right_token_reserve);
});

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

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

const POOLS_URI = 'https://app.ston.fi/rpc';

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

const balances = {};
const pools = (
await axios.post(POOLS_URI, {
jsonrpc: '2.0',
id: 2,
method: 'pool.list',
params: {},
})
).data.result.pools;

pools.forEach((pool) => {
balances[pool.token0_address] = BigNumber(
balances[pool.token0_address] || 0,
).plus(pool.reserve0);
balances[pool.token1_address] = BigNumber(
balances[pool.token1_address] || 0,
).plus(pool.reserve1);
});

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

export { tvl };
20 changes: 20 additions & 0 deletions src/factory/providers/ton/tonstakers/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 = 'EQCkWxfyhAkim3g2DjKQQg8T5P4g-Q1-K_jErGcDJZ4i-vqR';
const METHOD = 'get_pool_full_data';
const TON_ADDRESS = 'EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c';

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 };
2 changes: 1 addition & 1 deletion src/util/basicUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import data from './data';
import { log } from './logger/logger';
import { config } from '../app.config';
import Redis from './redis';
const DEFAULT_DELAY = 20;
const DEFAULT_DELAY = 0;

function basicUtil() {
function getPath(chain, provider) {
Expand Down
4 changes: 4 additions & 0 deletions src/util/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ const data = {
prefix: 'apt_',
delay: 4600,
},
ton: {
prefix: 'ton_',
delay: 0,
},
},
FILTERS: {
MarketCapInFiat: 'marketCapInFiat',
Expand Down
33 changes: 33 additions & 0 deletions src/web3Provider/ton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import axios from 'axios';

@Injectable()
export class Ton {
async call(address, method, params = []) {
const requestBody = {
address,
method,
stack: params,
};

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

const { exit_code, stack } = response;
if (exit_code !== 0) {
throw new Error('Expected a zero exit code, but got ' + exit_code);
}
stack.forEach((i, idx) => {
if (i[0] === 'num') {
stack[idx] = parseInt(i[1], 16);
}
});

return stack;
}
}
2 changes: 2 additions & 0 deletions src/web3Provider/web3Provider.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Near } from './near';
import { Tezos } from './tezos';
import { Wax } from './wax';
import { Aptos } from './aptos';
import { Ton } from './ton';

@Module({
exports: [Web3ProviderService],
Expand All @@ -21,6 +22,7 @@ import { Aptos } from './aptos';
Tezos,
Wax,
Aptos,
Ton,
],
})
export class Web3ProviderModule {}
7 changes: 6 additions & 1 deletion src/web3Provider/web3Provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Tezos } from './tezos';
import Web3 from 'web3';
import { Wax } from './wax';
import { Aptos } from './aptos';
import { Ton } from './ton';

const webSocketConfig = {
timeout: 60000,
Expand Down Expand Up @@ -38,6 +39,7 @@ export class Web3ProviderService {
private readonly tezos: Tezos,
private readonly wax: Wax,
private readonly aptos: Aptos,
private readonly ton: Ton,
) {}

async getWeb3(chain = 'ethereum') {
Expand All @@ -56,7 +58,6 @@ export class Web3ProviderService {
nodeUrls[`ETHEREUM_NODE_URL`]);

let web3;
console.log(nodeUrls[`${chain.toUpperCase()}_NODE_URL`]);
switch (chain) {
case 'everscale': {
web3 = { eth: this.everscale };
Expand Down Expand Up @@ -92,6 +93,10 @@ export class Web3ProviderService {
await web3.eth.onModuleInit();
break;
}
case 'ton': {
web3 = { eth: this.ton };
break;
}
default: {
if (node_url.startsWith('ws')) {
web3 = new Web3(
Expand Down

0 comments on commit 3de90fe

Please sign in to comment.