Skip to content

Commit

Permalink
Merge pull request #239 from dappradar/wax_integration
Browse files Browse the repository at this point in the history
Wax integration
  • Loading branch information
Sonmezturk authored Dec 4, 2023
2 parents c50dc90 + 5d283cd commit 9a725fa
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ nodeUrls['ELYSIUM_NODE_URL'] = process.env['ELYSIUM_NODE_URL'];
nodeUrls['BASE_NODE_URL'] = process.env['BASE_NODE_URL'];
nodeUrls['CORE_NODE_URL'] = process.env['CORE_NODE_URL'];
nodeUrls['LINEA_NODE_URL'] = process.env['LINEA_NODE_URL'];
nodeUrls['WAX_NODE_URL'] = process.env['WAX_NODE_URL'];

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

const START_BLOCK = 279871224;
const accounts = ['swap.alcor', 'alcordexmain'];
const tokens = [
['eosio.token', 'wax', 'wax'],
['alien.worlds', 'tlm', 'alien-worlds'],
['token.rfox', 'usd', 'redfox-labs'],
['usdt.alcor', 'usdt', 'usdt-alcor'],
];

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}
const balances = await web3.eth.get_account_tvl(accounts, tokens, 'wax');

formatter.convertBalancesToFixed(balances);
return { balances };
}
export { tvl };
17 changes: 17 additions & 0 deletions src/factory/providers/wax/atomichub/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';

const START_BLOCK = 279871224;
const accounts = ['atomicmarket'];
const tokens = [['eosio.token', 'wax', 'wax']];
async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}
const balances = await web3.eth.get_account_tvl(accounts, tokens, 'wax');

formatter.convertBalancesToFixed(balances);
return { balances };
}
export { tvl };
17 changes: 17 additions & 0 deletions src/factory/providers/wax/defibox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import axios from 'axios';

const START_BLOCK = 279871224;
const END_POINT = 'https://wax.defibox.io/api/';
async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}
const swap = (await axios.get(END_POINT + 'swap/get24HInfo')).data;
const balances = { wax: (Number(swap.data.waxBalance) * 2).toFixed() }; // swap TVL (dual sided 50/50 AMM pool)
formatter.convertBalancesToFixed(balances);
return { balances };
}
export { tvl };
87 changes: 87 additions & 0 deletions src/web3Provider/wax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { nodeUrls } from '../app.config';
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import fetch from 'node-fetch';

const nodeUrl = nodeUrls.WAX_NODE_URL;

@Injectable()
export class Wax {
async getBlockNumber() {
const url = `${nodeUrl}/v1/chain/get_block_info`;
const block = await fetch(url).then((res) => res.json());
return block.block_num;
}

async getBlock(blockNumber) {
const blockInfo = (
await axios.post(`${nodeUrl}/v1/chain/get_block`, {
block_num_or_id: blockNumber,
})
).data;
return { timestamp: blockInfo.timestamp, number: blockInfo.block_num };
}

async getAccountBalances(account_name, chain = 'eos') {
return (
await axios.post(`${nodeUrl}/v1/chain/get_account`, { account_name })
).data;
}

async get_currency_balance(code, account, symbol, chain = 'eos') {
const data = (
await axios.post(`${nodeUrl}/v1/chain/get_currency_balance`, {
code,
account,
symbol,
})
).data;
if (!data.length) return 0;
return (
Number(data[0].split(' ')[0]) * (code == 'eosio.token' ? 1 : 10 ** 4)
);
}

get_precision(symbol) {
if (symbol.includes(',')) return symbol.split(',')[0];
if (symbol == 'WAX') return 8;
return 4;
}

async get_staked(account_name, symbol, chain = 'eos') {
const response = (
await axios.post(`${nodeUrl}/v1/chain/get_account`, {
account_name,
})
).data;
try {
return response.voter_info.staked / 10 ** 4;
} catch (e) {
return 0;
}
}

async get_account_tvl(accounts, tokens, chain = 'eos') {
const balances = {};

for (const account of Array.isArray(accounts) ? accounts : [accounts]) {
for (const [code, symbol, id] of tokens) {
const balance = await this.get_currency_balance(
code,
account,
symbol,
chain,
);
const staked =
code == 'eosio.token'
? await this.get_staked(account, symbol, chain)
: 0;

if (balances[`${code}/${symbol}`])
balances[`${code}/${symbol}`] += balance + staked;
else balances[`${code}/${symbol}`] = balance + staked;
}
}
return balances;
}
}
2 changes: 2 additions & 0 deletions src/web3Provider/web3Provider.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Solana } from './solana';
import { Stacks } from './stacks';
import { Near } from './near';
import { Tezos } from './tezos';
import { Wax } from './wax';

@Module({
exports: [Web3ProviderService],
Expand All @@ -17,6 +18,7 @@ import { Tezos } from './tezos';
Solana,
Stacks,
Tezos,
Wax,
],
})
export class Web3ProviderModule {}
6 changes: 6 additions & 0 deletions src/web3Provider/web3Provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Solana } from './solana';
import { Stacks } from './stacks';
import { Tezos } from './tezos';
import Web3 from 'web3';
import { Wax } from './wax';

const webSocketConfig = {
timeout: 60000,
Expand Down Expand Up @@ -34,6 +35,7 @@ export class Web3ProviderService {
private readonly solana: Solana,
private readonly stacks: Stacks,
private readonly tezos: Tezos,
private readonly wax: Wax,
) {}

async getWeb3(chain = 'ethereum') {
Expand Down Expand Up @@ -66,6 +68,10 @@ export class Web3ProviderService {
web3 = { eth: this.near };
break;
}
case 'wax': {
web3 = { eth: this.wax };
break;
}
case 'solana': {
web3 = { eth: this.solana };
break;
Expand Down

0 comments on commit 9a725fa

Please sign in to comment.