Skip to content

Commit

Permalink
add sushi-aptos
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Mar 2, 2024
1 parent b82cbd0 commit f2ec21c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
96 changes: 96 additions & 0 deletions dexs/sushi-aptos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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";
const plimit = require('p-limit');
const limits = plimit(1);

interface ISwapEventData {
type: string;
amount_x_in: string;
amount_x_out: string;
amount_y_in: string;
amount_y_out: string;
user: string;
}
const getToken = (i: string) => i.split('<')[1].replace('>', '').split(', ');
const account = '0x31a6675cbe84365bf2b0cbce617ece6c47023ef70826533bde5203d32171dc3c';

const fetchVolume: FetchV2 = async (options: FetchOptions): Promise<FetchResultV2> => {
const fromTimestamp = options.fromTimestamp
const toTimestamp = options.toTimestamp;
const resources = await getResources(account);
const pools = resources.filter(e => e.type?.includes('swap::PairEventHolder'))
.map((e: any) => {
const [token0, token1] = getToken(e.type);
return {
type: e.type,
token0,
token1,
swap_events: {
counter: e.data.swap.counter,
creation_num: e.data.swap.guid.id.creation_num,
},
timestamp: e.data.timestamp,
counter: Number(e.data.swap.counter),
}
}).sort((a, b) => b.counter - a.counter)
const logs_swap: ISwapEventData[] = (await Promise.all(pools.map(async pool => limits(() => getSwapEvent(pool, fromTimestamp, toTimestamp))))).flat()
const dailyVolume = options.createBalances();
logs_swap.map((e: ISwapEventData) => {
const [token0, token1] = getToken(e.type);
dailyVolume.add(token0, e.amount_x_out)
dailyVolume.add(token1, e.amount_y_out)
})
return { dailyVolume: dailyVolume }
}

const getSwapEvent = async (pool: any, fromTimestamp: number, toTimestamp: number): Promise<ISwapEventData[]> => {
const limit = 100;
const swap_events: ISwapEventData[] = [];
let start = Math.max(pool.swap_events.counter - limit, 0);
while (true) {
if (start < 0) break;
const getEventByCreation = `${APTOS_PRC}/v1/accounts/${account}/events/${pool.swap_events.creation_num}?start=${start}&limit=${limit}`;
try {
const event: any[] = await httpGet(getEventByCreation);
const listSequence: number[] = event.map(e => Number(e.sequence_number));
const lastMin = Math.min(...listSequence);
if (!isFinite(lastMin)) break;
const lastVision = event.find(e => Number(e.sequence_number) === lastMin)?.version;
const urlBlock = `${APTOS_PRC}/v1/blocks/by_version/${lastVision}`;
const block = await httpGet(urlBlock);
const lastTimestamp = toUnixTime(block.block_timestamp);
const lastTimestampNumber = lastTimestamp;
if (lastTimestampNumber >= fromTimestamp && lastTimestampNumber <= toTimestamp) {
swap_events.push(...event);
}
if (lastTimestampNumber < fromTimestamp) {
break;
}
if (start === 0) break;
start = Math.max(lastMin - (limit + 1), 0);
} catch (e: any) {
break;
}
}
return swap_events.map((e: any) => ({
...e,
type: e.type,
...e.data,
}));
}

const toUnixTime = (timestamp: string) => Number((Number(timestamp) / 1e6).toString().split('.')[0])

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.APTOS]: {
fetch: fetchVolume,
start: 1708992000,
}
},
version: 2,
}

export default adapter;
22 changes: 22 additions & 0 deletions helpers/aptops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { httpGet } 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
}

export {
getResources
}

0 comments on commit f2ec21c

Please sign in to comment.