Skip to content

Commit 3a6a22a

Browse files
committed
feat: simplify action retrieval and error handling in getContractActions
1 parent 50ad64e commit 3a6a22a

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

projects/1dex/index.js

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,26 @@ async function getContractActions(contract, actionNames) {
1616
let hasMore = true;
1717

1818
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-
const actions = response.actions || [];
27-
if (actions.length === 0) break;
28-
29-
const filteredActions = actions.filter(action =>
30-
Array.isArray(actionNames)
31-
? actionNames.includes(action.action_trace.act.name)
32-
: action.action_trace.act.name === actionNames
33-
);
34-
35-
allActions.push(...filteredActions);
19+
const response = await post(`${endpoint}/history/get_actions`, {
20+
account_name: contract,
21+
pos: pos,
22+
offset: -pageSize
23+
});
24+
25+
const actions = response.actions || [];
26+
if (actions.length === 0) break;
27+
28+
const filteredActions = actions.filter(action =>
29+
Array.isArray(actionNames)
30+
? actionNames.includes(action.action_trace.act.name)
31+
: action.action_trace.act.name === actionNames
32+
);
33+
34+
allActions.push(...filteredActions);
3635

37-
const lastAction = actions[actions.length - 1];
38-
pos = lastAction.account_action_seq - pageSize + 1;
39-
hasMore = actions.length === pageSize;
40-
41-
} catch (error) {
42-
console.error('Pagination error:', error.message);
43-
break;
44-
}
36+
const lastAction = actions[actions.length - 1];
37+
pos = lastAction.account_action_seq - pageSize + 1;
38+
hasMore = actions.length === pageSize;
4539
}
4640

4741
return allActions;
@@ -64,7 +58,9 @@ function calculateBalancesFromActions(actions) {
6458
}
6559

6660
const parsedAmount = parseFloat(amount);
67-
balances[symbol].amount += name === 'logdeposit1' ? parsedAmount : -parsedAmount;
61+
if (name === 'logdeposit1' || name === 'logwithdraw1') {
62+
balances[symbol].amount += name === 'logdeposit1' ? parsedAmount : -parsedAmount;
63+
}
6864
}
6965

7066
return balances;
@@ -83,14 +79,9 @@ function convertToDefiLlamaFormat(balances) {
8379
}
8480

8581
async function eos() {
86-
try {
87-
const actions = await getContractActions("portal.1dex", ["logdeposit1", "logwithdraw1"]);
88-
const balances = calculateBalancesFromActions(actions);
89-
return convertToDefiLlamaFormat(balances);
90-
} catch (error) {
91-
console.error('Error fetching 1DEX TVL:', error);
92-
return {};
93-
}
82+
const actions = await getContractActions("portal.1dex", ["logdeposit1", "logwithdraw1"]);
83+
const balances = calculateBalancesFromActions(actions);
84+
return convertToDefiLlamaFormat(balances);
9485
}
9586

9687
module.exports = {

0 commit comments

Comments
 (0)