Skip to content

Commit

Permalink
Prevent rate limiting error in emojicoin.fun TVL function (#12785)
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 authored Dec 19, 2024
1 parent 52f395d commit 3c09f50
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 45 deletions.
38 changes: 26 additions & 12 deletions projects/emojicoin/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
const ADDRESSES = require('../helper/coreAssets.json')
const { function_view, timestampToVersion } = require("../helper/chain/aptos");

// This function will get all markets and the associated TVL in APT
async function getAllMarkets(ledgerVersion) {
// This function will get all markets and the associated TVL in APT
const registry = await function_view({
functionStr: "0xface729284ae5729100b3a9ad7f7cc025ea09739cd6e7252aff0beb53619cafe::emojicoin_dot_fun::registry_view",
args: [],
type_arguments: [],
ledgerVersion,
})
return registry.total_quote_locked.value
// If called with a ledger version too early, an error will be thrown by function_view.
// If that is the case, return 0 as it means the package wasn't deployed yet and the
// TVL is then 0.
try {
const registry = await function_view({
functionStr: "0xface729284ae5729100b3a9ad7f7cc025ea09739cd6e7252aff0beb53619cafe::emojicoin_dot_fun::registry_view",
args: [],
type_arguments: [],
ledgerVersion,
})
return registry.total_quote_locked.value;
} catch {
return 0;
}
}

// Date at which the contract was deployed.
const DEPLOYED_AT_DATE = '2024-11-20';

// Block close to the start date but before it.
const DEPLOYED_AT_BLOCK = 254000000;

async function tvl(api) {
// const version = await timestampToVersion(api.timestamp, 1962588495); // this query is not working
const tvl_amount = await getAllMarkets();
const version = await timestampToVersion(new Date(api.timestamp * 1000), DEPLOYED_AT_BLOCK);
const tvl_amount = await getAllMarkets(version);
api.add(ADDRESSES.aptos.APT, tvl_amount);
}

module.exports = {
timetravel: false,
timetravel: true,
methodology:
"Aggregates TVL in all pools in Emojicoin.fun",
aptos: {
tvl,
},
};
start: DEPLOYED_AT_DATE,
};
79 changes: 46 additions & 33 deletions projects/helper/chain/aptos.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,59 @@ function sumTokensExport(options) {
return async (api) => sumTokens({ ...api, api, ...options })
}

const VERSION_GROUPING = 1000000

// If I can get this timestampQuery to work... everything will work seamlessly
async function timestampToVersion(timestamp, start_version = 1962588495, end_version = 1962588495 + VERSION_GROUPING) {
// eslint-disable-next-line no-constant-condition
while (true) {
let closestTransactions = await findClosestTransaction(timestamp, start_version, end_version)
if (closestTransactions.length < 1) {
start_version += VERSION_GROUPING
end_version += VERSION_GROUPING
} else {
return closestTransactions[0].version
}
const graphQLClient = new GraphQLClient("https://api.mainnet.aptoslabs.com/v1/graphql");

// Query to get the latest block.
const latestBlockQuery = `query LatestBlock {
block_metadata_transactions(order_by: {version: desc}, limit: 1) {
block_height
}
}
}`;

const graphQLClient = new GraphQLClient("https://api.mainnet.aptoslabs.com/v1/graphql")
const timestampQuery = `query TimestampToVersion($timestamp: timestamp, $start_version: bigint, $end_version: bigint) {
block_metadata_transactions(
where: {timestamp: {_gte: $timestamp }, version: {_gte: $start_version, _lte: $end_version}}
limit: 1
order_by: {version: asc}
) {
// Query to get a block.
const blockQuery = `query Block($block: bigint) {
block_metadata_transactions(limit: 1, where: {block_height: {_eq: $block}}) {
timestamp
version
}
}`;
async function findClosestTransaction(timestamp, start_version, end_version) {
let date = new Date(timestamp * 1000).toISOString()

const results = await graphQLClient.request(
timestampQuery,
{
timestamp: date,
start_version,
end_version,
}
)

return results.block_metadata_transactions
// Query to get a block range.
const blockRangeQuery = `query Block($firstBlock: bigint, $limit: Int) {
block_metadata_transactions(limit: $limit, where: {block_height: {_gte: $firstBlock}}, order_by: {block_height: asc}) {
timestamp
version
}
}`;

// Given a timestamp, returns the transaction version that is closest to that timestamp.
const timestampToVersion = async (timestamp, minBlock = 0) => {
let left = minBlock;
let right = await graphQLClient.request(latestBlockQuery).then(r => Number(r.block_metadata_transactions[0].block_height));
let middle;
while (left + 100 < right) {
middle = Math.round((left + right) / 2);
const middleBlock = await graphQLClient.request(blockQuery, { block: middle }).then(r => r.block_metadata_transactions[0]);
const middleBlockDate = new Date(middleBlock.timestamp);
if (middleBlockDate.getTime() === timestamp.getTime()) {
return Number(middleBlock.version);
}
if (timestamp.getTime() < middleBlockDate.getTime()) {
right = middle;
} else {
left = middle + 1;
}
}
const blocks = await graphQLClient.request(
blockRangeQuery,
{ firstBlock: left, limit: right - left }
).then(r => r.block_metadata_transactions);
const mappedBlocks = blocks.map((e) => ({
version: Number(e.version),
delta: Math.abs(timestamp.getTime() - new Date(e.timestamp).getTime())
}));
mappedBlocks.sort((a, b) => a.delta - b.delta);
return mappedBlocks[0].version;
}

module.exports = {
Expand Down

0 comments on commit 3c09f50

Please sign in to comment.