diff --git a/fees/emojicoin.ts b/fees/emojicoin.ts new file mode 100644 index 0000000000..7d1790d5ca --- /dev/null +++ b/fees/emojicoin.ts @@ -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 { + // 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; diff --git a/helpers/aptops.ts b/helpers/aptops.ts index 657ea9a5b3..9d91ce6131 100644 --- a/helpers/aptops.ts +++ b/helpers/aptops.ts @@ -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 => { - 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(functionStr: string, type_arguments: string[] = [], args: (string | boolean | number)[] = [], ledgerVersion?: bigint | number): Promise { + 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 }