-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from dappradar/wax_integration
Wax integration
- Loading branch information
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters