Skip to content

Commit

Permalink
[emojicoin] Add fees
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Nov 27, 2024
1 parent 1d91aed commit f9e4ed3
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 14 deletions.
119 changes: 119 additions & 0 deletions fees/emojicoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {SimpleAdapter} from "../adapters/types";
import {CHAIN} from "../helpers/chains";
import {GraphQLClient} from "graphql-request";
import {view} from "../helpers/aptops";

const VERSION_GROUPING = BigInt(1000000)

// If I can get this timestampQuery to work... everything will work seamlessly
async function timestampToVersion(timestamp: number, start_version: bigint = BigInt(1962588495), end_version: bigint = BigInt(1962588495) + VERSION_GROUPING): Promise<string> {
// 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")
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}
) {
timestamp
version
}
}`;

async function findClosestTransaction(timestamp: number, start_version: bigint, end_version: bigint): Promise<{
version: string
}[]> {
let date = new Date(timestamp * 1000).toISOString()

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

return results.block_metadata_transactions as { version: string }[]
}


async function registryView(version?: bigint): Promise<{
cumulative_chat_messages: { value: string },
cumulative_integrator_fees: { value: string },
cumulative_quote_volume: { value: string },
cumulative_swaps: { value: string },
fully_diluted_value: { value: string },
last_bump_time: string,
market_cap: { value: string },
n_markets: string,
nonce: { value: string },
registry_address: string,
total_quote_locked: { value: string },
total_value_locked: { value: string }
}> {
const [result] = await view<[{
cumulative_chat_messages: { value: string },
cumulative_integrator_fees: { value: string },
cumulative_quote_volume: { value: string },
cumulative_swaps: { value: string },
fully_diluted_value: { value: string },
last_bump_time: string,
market_cap: { value: string },
n_markets: string,
nonce: { value: string },
registry_address: string,
total_quote_locked: { value: string },
total_value_locked: { value: string }
}]>(
"0xface729284ae5729100b3a9ad7f7cc025ea09739cd6e7252aff0beb53619cafe::emojicoin_dot_fun::registry_view",
[],
[],
version);
return result
}

const ONE_DAY: number = (24 * 60 * 60 * 1000);

const fetch = async (timestamp: number) => {
// Find the timestamp
const date = new Date(timestamp * 1000)
const closestToDate = await timestampToVersion(timestamp)
const previousDayTimestamp = new Date(date.getTime() - ONE_DAY).getTime() / 1000
const closestToPreviousDate = await timestampToVersion(previousDayTimestamp)

const yesterdayRegistry = await registryView(BigInt(closestToPreviousDate))
const todayRegistry = await registryView(BigInt(closestToDate))

const totalFees = BigInt(todayRegistry.cumulative_integrator_fees.value);
const dailyFees = BigInt(totalFees) - BigInt(yesterdayRegistry.cumulative_integrator_fees.value);

return {
totalFees: `${totalFees}`,
dailyFees: `${dailyFees}`,
// TODO: revenue
timestamp,
};
};

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.APTOS]: {
fetch,
start: '2023-04-03',
},
},
};

export default adapter;
35 changes: 21 additions & 14 deletions helpers/aptops.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { httpGet } from "../utils/fetchURL";
import {httpGet, httpPost} from "../utils/fetchURL";

export const APTOS_PRC = 'https://aptos-mainnet.pontem.network';

const getResources = async (account: string): Promise<any[]> => {
const data: any = []
let lastData: any;
let cursor
do {
let url = `${APTOS_PRC}/v1/accounts/${account}/resources?limit=9999`
if (cursor) url += '&start=' + cursor
const res = await httpGet(url, undefined, { withMetadata: true })
lastData = res.data
data.push(...lastData)
cursor = res.headers['x-aptos-cursor']
} while (lastData.length === 9999)
return data
const data: any = []
let lastData: any;
let cursor
do {
let url = `${APTOS_PRC}/v1/accounts/${account}/resources?limit=9999`
if (cursor) url += '&start=' + cursor
const res = await httpGet(url, undefined, {withMetadata: true})
lastData = res.data
data.push(...lastData)
cursor = res.headers['x-aptos-cursor']
} while (lastData.length === 9999)
return data
}

async function view<T extends any[]>(functionStr: string, type_arguments: string[] = [], args: (string | boolean | number)[] = [], ledgerVersion?: bigint | number): Promise<T> {
let path = `https://fullnode.mainnet.aptoslabs.com/v1/view`
if (ledgerVersion !== undefined) path += `?ledger_version=${ledgerVersion.toString()}`
return (await httpPost(path, {"function": functionStr, "type_arguments": type_arguments, arguments: args})) as T
}

export {
getResources
getResources,
view
}

0 comments on commit f9e4ed3

Please sign in to comment.