Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughAdds Alephium token address mappings to fixBalancesTokens and a new Linx Alephium adapter implementing tvl and borrowed functions that fetch MARKET_CREATED events, derive markets, and aggregate token balances and borrowings via Alephium node API and contract multicalls. Changes
Sequence Diagram(s)sequenceDiagram
participant Consumer as Consumer/API
participant Linx as Linx Module
participant AlephiumAPI as Alephium Node API
participant SmartContracts as Smart Contracts
Consumer->>Linx: tvl(api)
activate Linx
Linx->>AlephiumAPI: getEvents(MARKET_CREATED_EVENT_INDEX) [paginated]
AlephiumAPI-->>Linx: market creation events
Linx->>Linx: derive markets list
loop for each market
Linx->>AlephiumAPI: getTokenBalance(contractId, collateralToken)
AlephiumAPI-->>Linx: collateral balance
Linx->>AlephiumAPI: getTokenBalance(contractId, loanToken)
AlephiumAPI-->>Linx: loan balance
end
Linx->>Consumer: api.add(tokenId, totalAmount)
deactivate Linx
Consumer->>Linx: borrowed(api)
activate Linx
Linx->>AlephiumAPI: getEvents(MARKET_CREATED_EVENT_INDEX)
AlephiumAPI-->>Linx: market creation events
loop for each market
Linx->>SmartContracts: contractMultiCall(borrowAssets)
SmartContracts-->>Linx: borrow amounts per token
end
Linx->>Consumer: api.add(tokenId, totalBorrowedAmount)
deactivate Linx
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The adapter at projects/linx exports TVL: |
|
The adapter at projects/linx exports TVL: |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@projects/linx/index.js`:
- Around line 14-26: The loop in getEvents can hang if the API returns a
nextStart that doesn't advance or returns empty events; after each fetch (in
getEvents) add explicit guards: if response.events is empty break; if
response.nextStart is undefined break; and if response.nextStart <= start break;
optionally also add a safety counter (e.g., maxIterations) to force-break after
N iterations to protect against API bugs. Ensure you reference and update the
local start variable only when nextStart is a valid, strictly greater value.
🧹 Nitpick comments (2)
projects/linx/index.js (2)
54-62: Consider caching token balances per market to avoid duplicate RPC calls.
Right now,getTokensBalancecan be called twice per market (collateral + loan). A per-market cache reduces network overhead.♻️ Example refactor
async function tvl(api) { const markets = await getMarkets(); for (const market of markets) { - const collateral = await getTokenBalance(market.contractId, market.collateralTokenId); - const loanBalance = await getTokenBalance(market.contractId, market.loanTokenId); + const contractAddress = alephium.addressFromContractId(market.contractId); + const [alphBalance, tokensBalance] = await Promise.all([ + alephium.getAlphBalance(contractAddress), + alephium.getTokensBalance(contractAddress), + ]); + const getBalance = (tokenId) => + tokenId === ALPH_TOKEN_ID + ? BigInt(alphBalance.balance) + : BigInt(tokensBalance.find(b => b.tokenId === tokenId)?.balance ?? 0); + const collateral = getBalance(market.collateralTokenId); + const loanBalance = getBalance(market.loanTokenId); api.add(market.collateralTokenId, collateral); api.add(market.loanTokenId, loanBalance); } }
64-76: Batch multicalls for improved node efficiency.
Currently callingcontractMultiCallonce per market. Batching all calls (as shown in alphpad and alphbanx adapters) reduces node requests. The hardcodedgroup: 0andMARKET_METHOD_INDEXare consistent with Alephium adapter patterns and appear correct, but confirm the method index matches the Linx contract ABI if the contract is updated.♻️ Example batching approach
async function borrowed(api) { const markets = await getMarkets(); - for (const market of markets) { - const state = await alephium.contractMultiCall([{ - group: 0, - address: config.linx, - methodIndex: MARKET_METHOD_INDEX, - args: [{ type: "ByteVec", value: market.marketId }] - }]); - const borrowAssets = BigInt(state[0].returns[2].value); - api.add(market.loanTokenId, borrowAssets); - } + const calls = markets.map(market => ({ + group: 0, + address: config.linx, + methodIndex: MARKET_METHOD_INDEX, + args: [{ type: "ByteVec", value: market.marketId }], + })); + const states = await alephium.contractMultiCall(calls); + states.forEach((state, i) => { + const borrowAssets = BigInt(state.returns[2].value); + api.add(markets[i].loanTokenId, borrowAssets); + }); }
| async function getEvents(contractAddress) { | ||
| let events = []; | ||
| let start = 0; | ||
| const limit = 100; | ||
|
|
||
| while (true) { | ||
| const response = await get(`${config.nodeApiHost}/events/contract/${contractAddress}?start=${start}&limit=${limit}`); | ||
| events = events.concat(response.events); | ||
| if (!response.events.length || response.nextStart === undefined) break; | ||
| start = response.nextStart; | ||
| } | ||
| return events; | ||
| } |
There was a problem hiding this comment.
Add a pagination safety guard to prevent infinite loops.
If nextStart fails to advance (API bug or edge case), the loop never terminates. Guard against nextStart <= start and missing events.
🔧 Suggested fix
while (true) {
const response = await get(`${config.nodeApiHost}/events/contract/${contractAddress}?start=${start}&limit=${limit}`);
- events = events.concat(response.events);
- if (!response.events.length || response.nextStart === undefined) break;
- start = response.nextStart;
+ const nextStart = response?.nextStart;
+ const pageEvents = response?.events ?? [];
+ events = events.concat(pageEvents);
+ if (!pageEvents.length || nextStart === undefined || nextStart <= start) break;
+ start = nextStart;
}🤖 Prompt for AI Agents
In `@projects/linx/index.js` around lines 14 - 26, The loop in getEvents can hang
if the API returns a nextStart that doesn't advance or returns empty events;
after each fetch (in getEvents) add explicit guards: if response.events is empty
break; if response.nextStart is undefined break; and if response.nextStart <=
start break; optionally also add a safety counter (e.g., maxIterations) to
force-break after N iterations to protect against API bugs. Ensure you reference
and update the local start variable only when nextStart is a valid, strictly
greater value.
|
The adapter at projects/linx exports TVL: |
|
The adapter at projects/linx exports TVL: |
May resolve #17790
totalSupplyAssetsvalue from market state function call (i.e.totalSupplyAssets-totalBorrowAssets)fixBalancesTokensinhelper/tokenMapping.jsSummary by CodeRabbit