Skip to content

Commit

Permalink
Merge pull request #953 from changerio/master
Browse files Browse the repository at this point in the history
Add dexs, fee for Gambit arbitrum
  • Loading branch information
dtmkeng authored Nov 7, 2023
2 parents 62932df + cafc271 commit aa47748
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 46 deletions.
77 changes: 50 additions & 27 deletions dexs/gambit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { Chain } from "@defillama/sdk/build/general";
const topic0_limit_ex = '0x165b0f8d6347f7ebe92729625b03ace41aeea8fd7ebf640f89f2593ab0db63d1';
const topic0_market_ex = '0x2739a12dffae5d66bd9e126a286078ed771840f2288f0afa5709ce38c3330997';

const USDC_DECIMAL = 6;
const USDC_DECIMAL = 6
const LEVERAGE_DECIMAL = 18

type IAddress = {
[s: string | Chain]: string
[s: string | Chain]: string[]
}

interface ILog {
Expand All @@ -19,8 +20,9 @@ interface ILog {
topics: string[];
}

const contract_address: IAddress = {
[CHAIN.ERA]: '0xE95a6FCC476Dc306749c2Ac62fB4637c27ac578d',
const CONTRACT_ADDRESS: IAddress = {
[CHAIN.ERA]: ['0xE95a6FCC476Dc306749c2Ac62fB4637c27ac578d', "0x6cf71FaeA3771D56e72c72501e7172e79116E2A3", '0x50853A14cD14CC6A891BF034A204A15d294AF056'],
[CHAIN.ARBITRUM]: ['0x8d85f4615ea5F2Ea8D91C196aaD4C04D8416865C'],
}

const fetch = (chain: Chain) => {
Expand All @@ -30,37 +32,56 @@ const fetch = (chain: Chain) => {

const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const toBlock = (await getBlock(toTimestamp, chain, {}));
const contractAddressList = CONTRACT_ADDRESS[chain];
try {
const logs_limit_ex: ILog[] = (await sdk.api.util.getLogs({
target: contract_address[chain],
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: chain,
topics: [topic0_limit_ex]
})).output as ILog[];
const logs_limit_ex: ILog[] = (
await Promise.all(
contractAddressList.map(async (address) => {
return sdk.api.util.getLogs({
target: address,
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: chain,
topics: [topic0_limit_ex],
});
})
)
).flatMap((response) => (response as any).output) as ILog[];

const logs_market_ex: ILog[] = (await sdk.api.util.getLogs({
target: contract_address[chain],
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: chain,
topics: [topic0_market_ex]
})).output as ILog[];
const logs_market_ex: ILog[] = (
await Promise.all(
contractAddressList.map(async (address) => {
return sdk.api.util.getLogs({
target: address,
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: chain,
topics: [topic0_market_ex],
});
})
)
).flatMap((response) => (response as any).output) as ILog[];

const limit_volume = logs_limit_ex.map((e: ILog) => {
const data = e.data.replace('0x', '');
const leverage = Number('0x' + data.slice(512, 576));
let leverage = Number('0x' + data.slice(448, 512));
if (leverage > 1000) {
leverage = leverage / 10 ** LEVERAGE_DECIMAL
}
const positionSizeUsdc = Number('0x' + data.slice(896, 960)) / 10 ** USDC_DECIMAL;
return (leverage * positionSizeUsdc)
}).reduce((a: number, b: number) => a + b, 0);

const market_volume = logs_market_ex.map((e: ILog) => {
const data = e.data.replace('0x', '');
const leverage = Number('0x' + data.slice(448, 512));
let leverage = Number('0x' + data.slice(448, 512));
if (leverage > 1000) {
leverage = leverage / 10 ** LEVERAGE_DECIMAL
}
const positionSizeUsdc = Number('0x' + data.slice(832, 896)) / 10 ** USDC_DECIMAL;
return (leverage * positionSizeUsdc)
}).reduce((a: number, b: number) => a + b, 0);
Expand All @@ -77,14 +98,16 @@ const fetch = (chain: Chain) => {
}
}


const adapter: SimpleAdapter = {
adapter: {
[CHAIN.ERA]: {
fetch: fetch(CHAIN.ERA),
start: async () => 1684324400,
start: async () => 1690848000, // 2023/08/01 00:00:00
},
[CHAIN.ARBITRUM]: {
fetch: fetch(CHAIN.ARBITRUM),
start: async () => 1698883200, // 2023/11/02 00:00:00
}
}
};

export default adapter;
47 changes: 28 additions & 19 deletions fees/gambit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,46 @@ interface ITx {
data: string;
}

const FEE_ADDRESS_ERA = "0xE95a6FCC476Dc306749c2Ac62fB4637c27ac578d";
const FEE_ADDRESS = {
[CHAIN.ERA]: FEE_ADDRESS_ERA
[CHAIN.ERA]: ["0xE95a6FCC476Dc306749c2Ac62fB4637c27ac578d", "0x6cf71FaeA3771D56e72c72501e7172e79116E2A3", "0x50853A14cD14CC6A891BF034A204A15d294AF056"],
[CHAIN.ARBITRUM]: ["0x8d85f4615ea5F2Ea8D91C196aaD4C04D8416865C"],
};

const BIG_TEN = new BigNumber('10');

const USDC_DECIMAL = 6

const fetch = (address: string, chain: Chain) => {
const fetch = (addressList: string[], chain: Chain) => {
return async (timestamp: number): Promise<FetchResultFees> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp)
const yesterdaysTimestamp = getTimestampAtStartOfNextDayUTC(timestamp)

const todaysBlock = (await getBlock(todaysTimestamp, chain, {}));
const yesterdaysBlock = (await getBlock(yesterdaysTimestamp, chain, {}));
const [devFeeCall, ssFeeCall, referralFeeCall, nftBotFeeCall]: any = await Promise.all(
event.map((e: IEvent) => sdk.api.util.getLogs({
target: address,
topic: e.name,
toBlock: yesterdaysBlock,
fromBlock: todaysBlock,
keys: [],
chain: chain,
topics: [e.topic]
})));

const [devFeeCall, ssFeeCall, referralFeeCall, nftBotFeeCall]: any = (await Promise.all(
event.map((e: IEvent) => {
return Promise.all(
addressList.map(async (address) => {
return sdk.api.util.getLogs({
target: address,
topic: e.name,
toBlock: yesterdaysBlock,
fromBlock: todaysBlock,
keys: [],
chain: chain,
topics: [e.topic],
});
})
);
})
)).flat();
const devFeeValume = devFeeCall.output.map((p: ITx) => new BigNumber(p.data)).reduce((a: BigNumber, c: BigNumber) => a.plus(c), new BigNumber('0'));
const ssFeeVol = ssFeeCall.output.map((p: ITx) => new BigNumber(p.data)).reduce((a: BigNumber, c: BigNumber) => a.plus(c), new BigNumber('0'));
const referralFeeVol = referralFeeCall.output.map((p: ITx) => new BigNumber(p.data)).reduce((a: BigNumber, c: BigNumber) => a.plus(c), new BigNumber('0'));
const usdcVaultFeeVol = nftBotFeeCall.output.map((p: ITx) => new BigNumber(p.data)).reduce((a: BigNumber, c: BigNumber) => a.plus(c), new BigNumber('0'));

const prices = await getPrices(['coingecko:usdc'], todaysTimestamp);
const usdcPrice = prices['coingecko:usdc']?.price || 1;

const dailyRevenue = devFeeValume.plus(ssFeeVol).times(usdcPrice).div(BIG_TEN.pow(USDC_DECIMAL)).toString();
const dailyFees = devFeeValume.plus(ssFeeVol).plus(referralFeeVol).plus(usdcVaultFeeVol).times(usdcPrice).div(BIG_TEN.pow(USDC_DECIMAL)).toString();
return {
Expand All @@ -85,9 +90,13 @@ const adapter: Adapter = {
adapter: {
[CHAIN.ERA]: {
fetch: fetch(FEE_ADDRESS[CHAIN.ERA], CHAIN.ERA),
start: async () => 1684324400,
}
}
}
start: async () => 1690848000, // 2023/08/01 00:00:00
},
[CHAIN.ARBITRUM]: {
fetch: fetch(FEE_ADDRESS[CHAIN.ARBITRUM], CHAIN.ARBITRUM),
start: async () => 1698883200, // 2023/11/02 00:00:00
},
},
};

export default adapter;

0 comments on commit aa47748

Please sign in to comment.