-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Added kaching helpers #17093
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
+50
−0
Merged
Added kaching helpers #17093
Changes from 1 commit
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| const sdk = require('@defillama/sdk'); | ||
| const { function_view } = require('../helper/chain/aptos'); | ||
|
|
||
| const MODULE_ADDRESS = "0x7d0edbf4a540fc8421e3dbabf221d291217718859814220684c378e8c69da31d"; | ||
| const MICRO_UNIT = 1_000_000; | ||
|
|
||
| async function getTreasuryDetails() { | ||
| try { | ||
| const res = await function_view({ | ||
| functionStr: `${MODULE_ADDRESS}::lotto_pots::get_treasury_details`, | ||
| type_arguments: [], | ||
| args: [] | ||
| }); | ||
|
|
||
| const vault = res?.vault_balance ?? res?.[0]?.vault_balance ?? res?.[0] ?? "0"; | ||
| return BigInt(vault.toString()); | ||
| } catch (e) { | ||
| sdk.log("Error fetching treasury:", e.message); | ||
| return 0n; | ||
| } | ||
| } | ||
|
|
||
| async function getPotListPaged(start, size) { | ||
| try { | ||
| const res = await function_view({ | ||
| functionStr: `${MODULE_ADDRESS}::lotto_pots::get_pot_list_paged`, | ||
| type_arguments: [], | ||
| args: [String(start), String(size)] // ensure strings | ||
| }); | ||
|
|
||
| const pots = Array.isArray(res) ? res : (res?.[0] || []); | ||
|
|
||
| return pots.map(row => ({ | ||
| status: mapStatus(Number(row.status)), | ||
| prizePool: BigInt(row.prize_pool?.toString() || "0") | ||
| })); | ||
| } catch (e) { | ||
| sdk.log(`Error fetching pots (start=${start}):`, e.message); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| function mapStatus(code) { | ||
| const map = { | ||
| 1: 'ACTIVE', | ||
| 2: 'PAUSED', | ||
| 3: 'DRAWN', | ||
| 4: 'CANCELLED', | ||
| 5: 'COMPLETED', | ||
| 6: 'CANCELLATION_IN_PROGRESS', | ||
| 7: 'WINNER_ANNOUNCEMENT_IN_PROGRESS' | ||
| }; | ||
| return map[code] || 'UNKNOWN'; | ||
| } | ||
|
|
||
| async function getActivePotsTotalBalance() { | ||
| let total = 0n; | ||
| let index = 0; | ||
| const page = 50; | ||
|
|
||
| while (true) { | ||
| const pots = await getPotListPaged(index, page); | ||
| if (pots.length === 0) break; | ||
|
|
||
| for (const pot of pots) { | ||
| if (pot.status === "ACTIVE" || pot.status === "PAUSED") { | ||
| total += pot.prizePool; | ||
| } | ||
| } | ||
|
|
||
| if (pots.length < page) break; | ||
| index += page; | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| // ===== MAIN TVL FUNCTION (returns number, not object) ===== | ||
| async function aptosTVL() { | ||
| try { | ||
| const treasury = await getTreasuryDetails(); | ||
| const activePots = await getActivePotsTotalBalance(); | ||
| const totalMicro = treasury + activePots; | ||
| const totalUSD = Number(totalMicro) / MICRO_UNIT; | ||
|
|
||
| return totalUSD; | ||
deepakpraka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (err) { | ||
| return 0; | ||
deepakpraka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| module.exports = { aptosTVL }; | ||
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,22 @@ | ||
| const { aptosTVL } = require('../helper/kaching-helper.js'); | ||
|
|
||
| async function tvl() { | ||
| try { | ||
| const totalUsdc = await aptosTVL(); | ||
| if (isNaN(totalUsdc) || totalUsdc < 0) return {}; | ||
|
|
||
| return { | ||
| 'aptos:0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC': | ||
| Math.floor(totalUsdc * 1e6) | ||
| }; | ||
| } catch (e) { | ||
| console.error('Error computing TVL:', e); | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| methodology: | ||
| "Kaching! allows users to pay using any supported token across supported chains. Kaching! payment abstraction layer swaps and bridges these tokens into USDC on Aptos, which powers the pot size, winnings, and payouts. TVL reflects the total USDC held in the protocol's smart contracts, including active lottery pot balances and treasury reserves used for prize payouts.", | ||
| aptos: { tvl } | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.