-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[emojicoin] Add fees #2140
base: master
Are you sure you want to change the base?
[emojicoin] Add fees #2140
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import {SimpleAdapter} from "../adapters/types"; | ||
import {CHAIN} from "../helpers/chains"; | ||
import {GraphQLClient} from "graphql-request"; | ||
import {view} from "../helpers/aptops"; | ||
import {getPrices} from "../utils/prices"; | ||
|
||
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 prices = await getPrices(["coingecko:aptos"], timestamp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid pulling token prices and change code to something like this |
||
const apt_price = prices["coingecko:aptos"].price; | ||
|
||
function parseFees(fees: bigint) { | ||
return parseInt((BigInt(fees) / BigInt(10 ** 8)).toString(10)) * apt_price | ||
} | ||
|
||
const totalFees = parseFees(BigInt(todayRegistry.cumulative_integrator_fees.value)); | ||
const dailyFees = parseFees(BigInt(todayRegistry.cumulative_integrator_fees.value) - BigInt(yesterdayRegistry.cumulative_integrator_fees.value)); | ||
|
||
return { | ||
totalFees: `${totalFees}`, | ||
dailyFees: `${dailyFees}`, | ||
// TODO: revenue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The revenue can just be the integrator fees (what you're already using for total/daily) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so fees = revenue? |
||
timestamp, | ||
}; | ||
}; | ||
|
||
const adapter: SimpleAdapter = { | ||
adapter: { | ||
[CHAIN.APTOS]: { | ||
fetch, | ||
start: '2023-04-03', | ||
}, | ||
}, | ||
}; | ||
|
||
export default adapter; |
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'; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change this to adapter version 2, then you get start and endTimestamp as part of input