Skip to content

Commit de8940f

Browse files
committed
feat: refactor action retrieval and balance calculation for improved efficiency
1 parent c15c493 commit de8940f

File tree

1 file changed

+66
-111
lines changed

1 file changed

+66
-111
lines changed

projects/1dex/index.js

Lines changed: 66 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,135 +10,90 @@ const symbolToCoingeckoId = {
1010
'XSAT': 'exsat-network',
1111
};
1212

13-
async function getContractActionsWithPagination(contract, actionName) {
14-
try {
15-
let allActions = [];
16-
let pos = -1;
17-
const pageSize = 100;
18-
let hasMore = true;
19-
let oldestActionSeq = null;
13+
async function getContractActions(contract, actionNames) {
14+
const allActions = [];
15+
const pageSize = 100;
16+
let pos = -1;
17+
let hasMore = true;
18+
19+
while (hasMore) {
20+
try {
21+
const response = await post(`${endpoint}/history/get_actions`, {
22+
account_name: contract,
23+
pos: pos,
24+
offset: -pageSize
25+
});
26+
27+
const actions = response.actions || [];
28+
if (actions.length === 0) break;
29+
30+
const filteredActions = actions.filter(action =>
31+
Array.isArray(actionNames)
32+
? actionNames.includes(action.action_trace.act.name)
33+
: action.action_trace.act.name === actionNames
34+
);
35+
36+
allActions.push(...filteredActions);
37+
38+
const lastAction = actions[actions.length - 1];
39+
pos = lastAction.account_action_seq - pageSize + 1;
40+
hasMore = actions.length === pageSize;
41+
42+
} catch (error) {
43+
console.error('Pagination error:', error.message);
44+
break;
45+
}
46+
}
47+
48+
return allActions;
49+
}
50+
51+
function calculateBalancesFromActions(actions) {
52+
const balances = {};
53+
54+
for (const action of actions) {
55+
const { name, data } = action.action_trace.act;
56+
const { quantity, contract } = data;
57+
const [amount, symbol] = quantity.split(' ');
2058

21-
while (hasMore) {
22-
try {
23-
const response = await post(`${endpoint}/history/get_actions`, {
24-
account_name: contract,
25-
pos: pos,
26-
offset: -pageSize
27-
});
28-
29-
if (!response.actions || response.actions.length === 0) {
30-
hasMore = false;
31-
break;
32-
}
33-
34-
const filteredActions = response.actions.filter(action => {
35-
if (Array.isArray(actionName)) {
36-
return actionName.includes(action.action_trace.act.name);
37-
}
38-
return action.action_trace.act.name === actionName;
39-
});
40-
41-
if (filteredActions.length > 0) {
42-
allActions = [...allActions, ...filteredActions];
43-
}
44-
45-
if (response.actions.length > 0) {
46-
const lastAction = response.actions[response.actions.length - 1];
47-
oldestActionSeq = lastAction.account_action_seq;
48-
pos = oldestActionSeq - pageSize + 1;
49-
50-
if (response.actions.length < pageSize) {
51-
hasMore = false;
52-
}
53-
} else {
54-
hasMore = false;
55-
}
56-
57-
} catch (error) {
58-
hasMore = false;
59-
}
59+
if (!balances[symbol]) {
60+
balances[symbol] = {
61+
amount: 0,
62+
contract,
63+
symbol
64+
};
6065
}
61-
return allActions;
62-
} catch (error) {
63-
console.error('Error during pagination:', error);
64-
return [];
66+
67+
const parsedAmount = parseFloat(amount);
68+
balances[symbol].amount += name === 'logdeposit1' ? parsedAmount : -parsedAmount;
6569
}
70+
71+
return balances;
6672
}
6773

6874
function convertToDefiLlamaFormat(balances) {
6975
const result = {};
7076

71-
Object.entries(balances).forEach(([symbol, data]) => {
72-
if (data.amount > 0) {
73-
const coingeckoId = symbolToCoingeckoId[symbol];
74-
if (coingeckoId) {
75-
result[coingeckoId] = data.amount;
76-
}
77+
for (const [symbol, data] of Object.entries(balances)) {
78+
if (data.amount > 0 && symbolToCoingeckoId[symbol]) {
79+
result[symbolToCoingeckoId[symbol]] = data.amount;
7780
}
78-
});
81+
}
7982

8083
return result;
8184
}
8285

83-
84-
async function calculateTVLFromActions(contract) {
86+
async function eos() {
8587
try {
86-
const actions = await getContractActionsWithPagination(contract, ["logdeposit1", "logwithdraw1"]);
87-
88-
const balances = {};
89-
90-
actions.forEach(action => {
91-
const { action_trace } = action;
92-
const { act } = action_trace;
93-
const { name, data } = act;
94-
95-
if (name === 'logdeposit1') {
96-
const { quantity, contract } = data;
97-
const [amount, symbol] = quantity.split(' ');
98-
99-
if (!balances[symbol]) {
100-
balances[symbol] = {
101-
amount: 0,
102-
contract: contract,
103-
symbol: symbol
104-
};
105-
}
106-
107-
balances[symbol].amount += parseFloat(amount);
108-
}
109-
else if (name === 'logwithdraw1') {
110-
const { quantity, contract } = data;
111-
const [amount, symbol] = quantity.split(' ');
112-
113-
if (!balances[symbol]) {
114-
balances[symbol] = {
115-
amount: 0,
116-
contract: contract,
117-
symbol: symbol
118-
};
119-
}
120-
121-
balances[symbol].amount -= parseFloat(amount);
122-
}
123-
});
124-
125-
return balances;
88+
const actions = await getContractActions("portal.1dex", ["logdeposit1", "logwithdraw1"]);
89+
const balances = calculateBalancesFromActions(actions);
90+
return convertToDefiLlamaFormat(balances);
12691
} catch (error) {
127-
console.error('Error calculating TVL from actions:', error);
128-
throw error;
92+
console.error('Error fetching 1DEX TVL:', error);
93+
return {};
12994
}
13095
}
13196

132-
// 1DEX
133-
// https://1dex.com
134-
async function eos() {
135-
const actionBasedBalances = await calculateTVLFromActions("portal.1dex", 50);
136-
137-
const accountTvl = convertToDefiLlamaFormat(actionBasedBalances)
138-
139-
return accountTvl;
140-
}
141-
14297
module.exports = {
14398
methodology: `1DEX TVL is calculated by tracking deposit and withdrawal actions (logdeposit1 and logwithdraw1) from the portal.1dex contract.`,
14499
eos: {

0 commit comments

Comments
 (0)