-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Linx refactor #17928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Linx refactor #17928
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,93 @@ | ||
| const alephium = require('../helper/chain/alephium'); | ||
| const { get } = require('../helper/http'); | ||
|
|
||
| const config = { | ||
| linx: "vQcfta4Mm32L7Xsb7tYF2rrR76JWxjNv3oia8GPK6x71", | ||
| nodeApiHost: "https://node.mainnet.alephium.org", | ||
| } | ||
|
|
||
| const ALPH_TOKEN_ID = '0000000000000000000000000000000000000000000000000000000000000000'; | ||
|
|
||
| const MARKET_CREATED_EVENT_INDEX = 4; | ||
| const MARKET_METHOD_INDEX = 4; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| async function getMarkets() { | ||
| const events = await getEvents(config.linx); | ||
| let markets = events.filter(e => e.eventIndex === MARKET_CREATED_EVENT_INDEX).map(event => { | ||
| const marketId = event.fields[0].value; | ||
| const contractId = event.fields[1].value; | ||
| const loanTokenId = event.fields[2].value; | ||
| const collateralTokenId = event.fields[3].value; | ||
|
|
||
| return { marketId, contractId, loanTokenId, collateralTokenId }; | ||
| }); | ||
|
|
||
| markets = Array.from(new Map(markets.map(m => [m.marketId, m])).values()); | ||
| return markets; | ||
| } | ||
|
|
||
| async function getTokenBalance(marketContractId, tokenId) { | ||
| const contractAddress = alephium.addressFromContractId(marketContractId); | ||
| if (tokenId === ALPH_TOKEN_ID) { | ||
| return BigInt((await alephium.getAlphBalance(contractAddress)).balance); | ||
| } else { | ||
| const tokensBalance = await alephium.getTokensBalance(contractAddress); | ||
| const tokenBalance = tokensBalance.find(b => b.tokenId === tokenId); | ||
| return BigInt(tokenBalance?.balance ?? 0); | ||
| } | ||
| } | ||
|
|
||
| async function tvl(api) { | ||
| const markets = await getMarkets(); | ||
| const tokenAmounts = new Map(); | ||
| for (const market of markets) { | ||
| const collateral = await getTokenBalance(market.contractId, market.collateralTokenId); | ||
| const loanBalance = await getTokenBalance(market.contractId, market.loanTokenId); | ||
| tokenAmounts.set(market.collateralTokenId, (tokenAmounts.get(market.collateralTokenId) ?? BigInt(0)) + collateral); | ||
RohanNero marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tokenAmounts.set(market.loanTokenId, (tokenAmounts.get(market.loanTokenId) ?? BigInt(0)) + loanBalance); | ||
| } | ||
|
|
||
| for (const [tokenId, amount] of tokenAmounts.entries()) { | ||
| api.add(tokenId, amount); | ||
| } | ||
| } | ||
|
|
||
| async function borrowed(api) { | ||
| const markets = await getMarkets(); | ||
| const tokenAmounts = new Map(); | ||
| 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); | ||
| tokenAmounts.set(market.loanTokenId, (tokenAmounts.get(market.loanTokenId) ?? BigInt(0)) + borrowAssets); | ||
RohanNero marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| for (const [tokenId, amount] of tokenAmounts.entries()) { | ||
| api.add(tokenId, amount); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| alephium: { | ||
| tvl, | ||
| borrowed | ||
| } | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a pagination safety guard to prevent infinite loops.
If
nextStartfails to advance (API bug or edge case), the loop never terminates. Guard againstnextStart <= startand missingevents.🔧 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