|
1 |
| -const { get_account_tvl } = require("../helper/chain/eos"); |
| 1 | +const { post } = require("../helper/http"); |
| 2 | + |
| 3 | +const endpoint = 'https://exsat2.greymass.com/v1'; |
| 4 | + |
| 5 | +const symbolToCoingeckoId = { |
| 6 | + 'EOS': 'eos', |
| 7 | + 'USDT': 'tether', |
| 8 | +}; |
| 9 | + |
| 10 | +async function getContractActionsWithPagination(contract, actionName) { |
| 11 | + try { |
| 12 | + let allActions = []; |
| 13 | + let pos = -1; |
| 14 | + const pageSize = 100; |
| 15 | + let hasMore = true; |
| 16 | + let oldestActionSeq = null; |
| 17 | + |
| 18 | + while (hasMore) { |
| 19 | + try { |
| 20 | + const response = await post(`${endpoint}/history/get_actions`, { |
| 21 | + account_name: contract, |
| 22 | + pos: pos, |
| 23 | + offset: -pageSize |
| 24 | + }); |
| 25 | + |
| 26 | + if (!response.actions || response.actions.length === 0) { |
| 27 | + hasMore = false; |
| 28 | + break; |
| 29 | + } |
| 30 | + |
| 31 | + const filteredActions = response.actions.filter(action => { |
| 32 | + if (Array.isArray(actionName)) { |
| 33 | + return actionName.includes(action.action_trace.act.name); |
| 34 | + } |
| 35 | + return action.action_trace.act.name === actionName; |
| 36 | + }); |
| 37 | + |
| 38 | + if (filteredActions.length > 0) { |
| 39 | + allActions = [...allActions, ...filteredActions]; |
| 40 | + } |
| 41 | + |
| 42 | + if (response.actions.length > 0) { |
| 43 | + const lastAction = response.actions[response.actions.length - 1]; |
| 44 | + oldestActionSeq = lastAction.account_action_seq; |
| 45 | + pos = oldestActionSeq - pageSize + 1; |
| 46 | + |
| 47 | + if (response.actions.length < pageSize) { |
| 48 | + hasMore = false; |
| 49 | + } |
| 50 | + } else { |
| 51 | + hasMore = false; |
| 52 | + } |
| 53 | + |
| 54 | + } catch (error) { |
| 55 | + hasMore = false; |
| 56 | + } |
| 57 | + } |
| 58 | + return allActions; |
| 59 | + } catch (error) { |
| 60 | + console.error('Error during pagination:', error); |
| 61 | + return []; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function convertToDefiLlamaFormat(balances) { |
| 66 | + const result = {}; |
| 67 | + |
| 68 | + Object.entries(balances).forEach(([symbol, data]) => { |
| 69 | + if (data.amount > 0) { |
| 70 | + const coingeckoId = symbolToCoingeckoId[symbol]; |
| 71 | + if (coingeckoId) { |
| 72 | + result[coingeckoId] = data.amount; |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + return result; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +async function calculateTVLFromActions(contract) { |
| 82 | + try { |
| 83 | + const actions = await getContractActionsWithPagination(contract, ["logdeposit1", "logwithdraw1"]); |
| 84 | + |
| 85 | + const balances = {}; |
| 86 | + |
| 87 | + actions.forEach(action => { |
| 88 | + const { action_trace } = action; |
| 89 | + const { act } = action_trace; |
| 90 | + const { name, data } = act; |
| 91 | + |
| 92 | + if (name === 'logdeposit1') { |
| 93 | + const { quantity, contract } = data; |
| 94 | + const [amount, symbol] = quantity.split(' '); |
| 95 | + |
| 96 | + if (!balances[symbol]) { |
| 97 | + balances[symbol] = { |
| 98 | + amount: 0, |
| 99 | + contract: contract, |
| 100 | + symbol: symbol |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + balances[symbol].amount += parseFloat(amount); |
| 105 | + } |
| 106 | + else if (name === 'logwithdraw1') { |
| 107 | + const { quantity, contract } = data; |
| 108 | + const [amount, symbol] = quantity.split(' '); |
| 109 | + |
| 110 | + if (!balances[symbol]) { |
| 111 | + balances[symbol] = { |
| 112 | + amount: 0, |
| 113 | + contract: contract, |
| 114 | + symbol: symbol |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + balances[symbol].amount -= parseFloat(amount); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + return balances; |
| 123 | + } catch (error) { |
| 124 | + console.error('Error calculating TVL from actions:', error); |
| 125 | + throw error; |
| 126 | + } |
| 127 | +} |
2 | 128 |
|
3 | 129 | // 1DEX
|
4 | 130 | // https://1dex.com
|
5 | 131 | async function eos() {
|
6 |
| - const accounts = ["app.1dex", "portal.1dex"]; |
7 |
| - const tokens = [ |
8 |
| - ["eosio.token", "EOS", "eos"], |
9 |
| - ["tethertether", "USDT", "tether"] |
10 |
| - ]; |
11 |
| - return await get_account_tvl(accounts, tokens, "eos"); |
| 132 | + const actionBasedBalances = await calculateTVLFromActions("portal.1dex", 50); |
| 133 | + |
| 134 | + console.log(actionBasedBalances); |
| 135 | + |
| 136 | + |
| 137 | + const accountTvl = convertToDefiLlamaFormat(actionBasedBalances) |
| 138 | + |
| 139 | + return accountTvl; |
12 | 140 | }
|
13 | 141 |
|
14 | 142 | module.exports = {
|
15 |
| - methodology: `1DEX TVL is achieved by querying token balances from swap smart contract.`, |
| 143 | + methodology: `1DEX TVL is calculated by tracking deposit and withdrawal actions (logdeposit1 and logwithdraw1) from the portal.1dex contract.`, |
16 | 144 | eos: {
|
17 |
| - tvl: eos |
| 145 | + tvl: eos, |
18 | 146 | },
|
19 | 147 | }
|
0 commit comments