-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add KGEN Adapter to Chain Revenue Dashboard #3412
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
Open
Akhilleshgoswami
wants to merge
2
commits into
DefiLlama:master
Choose a base branch
from
kgen-protocol:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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,158 @@ | ||
import { | ||
FetchOptions, | ||
FetchResultV2, | ||
FetchV2, | ||
SimpleAdapter, | ||
} from "../../adapters/types"; | ||
import { CHAIN } from "../../helpers/chains"; | ||
import { APTOS_PRC, getResources } from "../../helpers/aptops"; | ||
import { httpGet } from "../../utils/fetchURL"; | ||
|
||
interface DepositFungible { | ||
amount: string; | ||
token: string; | ||
sender: string; | ||
type: string; | ||
} | ||
|
||
const KGEN_APTOS_ON_CHAIN_REVENUE_CONTRACT = | ||
"0x5a96fab415f43721a44c5a761ecfcccc3dae9c21f34313f0e594b49d8d4564f4"; | ||
|
||
const toUnixTime = (timestamp: string): number => | ||
Math.floor(Number(timestamp) / 1e6); | ||
|
||
/** | ||
* Fetches and aggregates volume for the given time window. | ||
*/ | ||
const fetchVolume: FetchV2 = async ({ | ||
fromTimestamp, | ||
toTimestamp, | ||
createBalances, | ||
}: FetchOptions): Promise<FetchResultV2> => { | ||
try { | ||
// Fetch all account resources | ||
const resources = await getResources(KGEN_APTOS_ON_CHAIN_REVENUE_CONTRACT); | ||
|
||
// Filter relevant resources | ||
const relevantResources = resources.filter((resource) => | ||
resource.type.includes("RevenueContractV2::RevenueEventHolder"), | ||
); | ||
|
||
// If no relevant resources, return empty volume | ||
if (relevantResources.length === 0) { | ||
return { | ||
dailyVolume: createBalances(), | ||
dailyRevenue: createBalances(), | ||
dailfyFees: createBalances(), | ||
}; | ||
} | ||
|
||
// Fetch events from all relevant resources concurrently | ||
const eventsArrays = await Promise.all( | ||
relevantResources.map((resource) => | ||
getEventData(resource, fromTimestamp, toTimestamp), | ||
), | ||
); | ||
// Flatten events array | ||
const allEvents = eventsArrays.flat(); | ||
|
||
// Create balances object and aggregate amounts per token | ||
const dailyVolume = createBalances(); | ||
for (const event of allEvents) { | ||
dailyVolume.add(event.token, event.amount); | ||
} | ||
return { | ||
dailyVolume, | ||
dailyRevenue: dailyVolume, | ||
dailyFees: 0, | ||
}; | ||
} catch (error) { | ||
console.error("Error in fetchVolume:", error); | ||
return { | ||
dailyVolume: createBalances(), | ||
dailyRevenue: createBalances(), | ||
dailyFees: createBalances(), | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* Fetch event data from a resource within a time window. | ||
*/ | ||
const getEventData = async ( | ||
resource: any, | ||
fromTimestamp: number, | ||
toTimestamp: number, | ||
): Promise<DepositFungible[]> => { | ||
try { | ||
const eventData: DepositFungible[] = []; | ||
const limit = 100; | ||
|
||
// Defensive check for nested properties | ||
const depositFungible = resource?.data?.deposit_fungible; | ||
if (!depositFungible) return []; | ||
|
||
const counter = depositFungible.counter ?? 0; | ||
const start = Math.max(counter - limit, 0); | ||
|
||
const creationNum = depositFungible.guid?.id?.creation_num; | ||
if (!creationNum) return []; | ||
|
||
const eventUrl = `${APTOS_PRC}/v1/accounts/${KGEN_APTOS_ON_CHAIN_REVENUE_CONTRACT}/events/${creationNum}?start=${start}&limit=${limit}`; | ||
|
||
const events: any[] = await httpGet(eventUrl); | ||
if (!events.length) return []; | ||
|
||
// Find earliest event by sequence_number | ||
const earliest = events.reduce((min, e) => | ||
Number(e.sequence_number) < Number(min.sequence_number) ? e : min, | ||
); | ||
|
||
// Fetch block info by version | ||
const blockInfo = await httpGet( | ||
`${APTOS_PRC}/v1/blocks/by_version/${earliest.version}`, | ||
); | ||
|
||
const blockTimestamp = blockInfo?.block_timestamp; | ||
if (!blockTimestamp) return []; | ||
|
||
const eventTimestamp = toUnixTime(blockTimestamp); | ||
|
||
// Check if event timestamp is within range | ||
if (eventTimestamp >= fromTimestamp && eventTimestamp <= toTimestamp) { | ||
for (const e of events) { | ||
eventData.push({ | ||
amount: e.data.amount, | ||
token: e.data.token, | ||
sender: e.data.sender, | ||
type: e.type, | ||
}); | ||
} | ||
} | ||
|
||
return eventData; | ||
} catch (err) { | ||
console.error("Error in getEventData:", err); | ||
return []; | ||
} | ||
}; | ||
|
||
const adapter: SimpleAdapter = { | ||
adapter: { | ||
[CHAIN.APTOS]: { | ||
fetch: fetchVolume, | ||
start: "2025-06-02", | ||
meta: { | ||
methodology: { | ||
dailyVolume: | ||
"Volume is calculated by summing the token volume of all USDC token deposits on the protocol that day.", | ||
dailyRevenue: | ||
Akhilleshgoswami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Revenue is calculated by summing the token volume of all USDC token deposits on the protocol that day.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
version: 2, | ||
}; | ||
|
||
export default adapter; |
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.