Skip to content

Commit

Permalink
Merge pull request #1850 from GainsNetwork-org/master
Browse files Browse the repository at this point in the history
[GainsNetwork | gTrade] Add v9.2 events
  • Loading branch information
dtmkeng committed Sep 2, 2024
2 parents b192db9 + 02af643 commit 44ab4e5
Showing 1 changed file with 61 additions and 25 deletions.
86 changes: 61 additions & 25 deletions dexs/gains-network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,71 @@ const topic0_market_ex = [
"0xca42b0e44cd853d207b87e8f8914eaefef9c9463a8c77ca33754aa62f6904f00", // v7
V8_MARKET_TOPIC0, // v8
];
const topic0_partials = [
"0xf09a9c949c4bd4cbe75b424bea11c683c3ae55e7cdb8321c3ec37e01af72c8d5", // PositionSizeIncreaseExecuted
"0xe74b50af866d7f8e3577bc959bf73a2690841f0abce22ab0cfb1b1c84122a7d7", // PositionSizeDecreaseExecuted
];

const precisionException: { [a: string | number]: number } = {
"0x2ac6749d0affd42c8d61ef25e433f92e375a1aef": 1e6,
"0x4542256c583bcad66a19a525b57203773a6485bf": 1e6,
3: 1e6, // v8 USDC
};

const diamonds = {
[CHAIN.POLYGON]: "0x209a9a01980377916851af2ca075c2b170452018",
[CHAIN.ARBITRUM]: "0xff162c694eaa571f685030649814282ea457f169",
};

const contract_addresses: IAddresses = {
[CHAIN.POLYGON]: [
"0x82e59334da8c667797009bbe82473b55c7a6b311", // DAI TradingCallbacks
"0x0bbed2eac3237ba128643670b7cf3be475933755", // ETH TradingCallbacks
"0x2ac6749d0affd42c8d61ef25e433f92e375a1aef", // USDC TradingCallbacks
"0x209a9a01980377916851af2ca075c2b170452018", // v8 Diamond
diamonds[CHAIN.POLYGON], // v8 Diamond
],
[CHAIN.ARBITRUM]: [
"0x298a695906e16aea0a184a2815a76ead1a0b7522", // DAI TradingCallbacks
"0x62a9f50c92a57c719ff741133caa55c7a81ce019", // ETH TradingCallbacks
"0x4542256c583bcad66a19a525b57203773a6485bf", // USDC TradingCallbacks
"0xff162c694eaa571f685030649814282ea457f169", // v8 Diamond
diamonds[CHAIN.ARBITRUM], // v8 Diamond
],
};

const fetch: any = async (timestamp: number, _, { getLogs, createBalances, chain }): Promise<FetchResultVolume> => {
const limitLogs: ILog[] = (
(await Promise.all(
topic0_limit_ex.map(async (topic0) =>
getLogs({
targets: contract_addresses[chain],
topics: [topic0],
})
)
)) as ILog[][]
).flat();

const marketLogs: ILog[] = (
(await Promise.all(
topic0_market_ex.map(async (topic0) =>
getLogs({
targets: contract_addresses[chain],
topics: [topic0],
})
)
)) as ILog[][]
).flat();
const fetch: any = async (timestamp: number, _, { getLogs, chain }): Promise<FetchResultVolume> => {
const [limitLogs, marketLogs, partialsLogs] = (
await Promise.all([
// Limit Executed logs
(await Promise.all(
topic0_limit_ex.map(async (topic0) =>
getLogs({
targets: contract_addresses[chain],
topics: [topic0],
})
)
)) as ILog[][],

// Market Executed logs
(await Promise.all(
topic0_market_ex.map(async (topic0) =>
getLogs({
targets: contract_addresses[chain],
topics: [topic0],
})
)
)) as ILog[][],

// Partial Increase/Decrease logs
(await Promise.all(
topic0_partials.map(async (topic0) =>
getLogs({
targets: [diamonds[chain]],
topics: [topic0],
})
)
)) as ILog[][],
])
).map((logs: ILog[][]) => logs.flat());

const limit_volume = limitLogs
.map((e: ILog) => {
Expand Down Expand Up @@ -111,7 +132,22 @@ const fetch: any = async (timestamp: number, _, { getLogs, createBalances, chain
})
.reduce((a: number, b: number) => a + b, 0);

const dailyVolume = limit_volume + market_volume;
const partials_volume = partialsLogs
.map((e: ILog) => {
const data = e.data.replace("0x", "");
const cancelReason = Number("0x" + data.slice(128, 192));

if (cancelReason > 0) return 0;

const collateralPrecision = precisionException[Number(e.topics[1])] ?? 1e18;
const collateralPriceUsd = Number("0x" + data.slice(384, 448)) / 1e8;
const positionSizeDelta = Number("0x" + data.slice(576, 640)) / collateralPrecision;

return positionSizeDelta * collateralPriceUsd;
})
.reduce((a: number, b: number) => a + b, 0);

const dailyVolume = limit_volume + market_volume + partials_volume;

return { dailyVolume, timestamp };
};
Expand Down

0 comments on commit 44ab4e5

Please sign in to comment.