From 7e1063a90f7f6adc5fe105abdc03137ac917cc26 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Wed, 9 Aug 2023 14:09:19 +0000 Subject: [PATCH 1/2] rysk-finance --- options/rysk-finance/index.ts | 135 ++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 options/rysk-finance/index.ts diff --git a/options/rysk-finance/index.ts b/options/rysk-finance/index.ts new file mode 100644 index 0000000000..71d00c516b --- /dev/null +++ b/options/rysk-finance/index.ts @@ -0,0 +1,135 @@ +import BigNumber from "bignumber.js"; +import request, { gql } from "graphql-request"; +import { BreakdownAdapter, Fetch, FetchResultOptions, IJSON, SimpleAdapter } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { getPrices } from "../../utils/prices" + +interface IoTokenTrade { + timestamp: string + otoken: { + strikeAsset: { + name: string + id: string + decimals: number + } + underlyingAsset: { + name: string + id: string + decimals: number + } + strikePrice: string + decimals: number + } + amount: string; + premium: string; + fee: string; +} + +const query = gql` +query trades($timestampFrom: Int!, $timestampTo: Int!) { + optionsBoughtActions( + where: {timestamp_gt: $timestampFrom, timestamp_lte: $timestampTo} + ) { + otoken { + strikeAsset { + name + id + decimals + } + underlyingAsset { + id + } + strikePrice + decimals + } + amount + premium + fee + timestamp + } +} +` + +const querySold = gql` +query trades($timestampFrom: Int!, $timestampTo: Int!) { + optionsSoldActions( + where: {timestamp_gt: $timestampFrom, timestamp_lte: $timestampTo} + ) { + otoken { + strikeAsset { + name + id + decimals + } + underlyingAsset { + id + } + strikePrice + decimals + } + amount + premium + fee + timestamp + } +} +` + +const normalizeValues = (value: string, decimals: number) => BigNumber(value).dividedBy(10 ** decimals) + +const endpoints = { + [CHAIN.ARBITRUM]: "https://api.goldsky.com/api/public/project_clhf7zaco0n9j490ce421agn4/subgraphs/arbitrum-one/0.1.17/gn", +}; + +const prices = {} as IJSON +const getUnderlyingSpotPrice = async (address: string, timestamp: number) => { + const key = `${CHAIN.ARBITRUM}:${address}` + if (!prices[address]) prices[address] = (await getPrices([key], timestamp))[key].price + return prices[address] +} +const fetch: Fetch = async (timestamp) => { + const timestampFrom = timestamp - 60 * 60 * 24 + const response = await request(endpoints[CHAIN.ARBITRUM], query, { + timestampFrom, + timestampTo: timestamp, + }) as { optionsBoughtActions: IoTokenTrade[] } + + const response_sold = await request(endpoints[CHAIN.ARBITRUM], querySold, { + timestampFrom, + timestampTo: timestamp, + }) as { optionsSoldActions: IoTokenTrade[] } + + const fetchResult: FetchResultOptions = { timestamp: timestampFrom } + const processed = await response.optionsBoughtActions.reduce(async (accP, curr) => { + const acc = await accP + const underlyingAssetSpotPrice = await getUnderlyingSpotPrice(curr.otoken.underlyingAsset.id, +curr.timestamp) + acc.notional = acc.notional.plus( + normalizeValues(curr.amount, 18) + .multipliedBy(underlyingAssetSpotPrice) + ) + acc.premium = acc.premium.plus(normalizeValues(curr.premium, 6)) + return acc + }, Promise.resolve({ notional: BigNumber(0), premium: BigNumber(0) }) as Promise<{ notional: BigNumber, premium: BigNumber }>) + const processedSold = await response_sold.optionsSoldActions.reduce(async (accP, curr) => { + const acc = await accP + const underlyingAssetSpotPrice = await getUnderlyingSpotPrice(curr.otoken.underlyingAsset.id, +curr.timestamp) + acc.notional = acc.notional.plus( + normalizeValues(curr.amount, 18) + .multipliedBy(underlyingAssetSpotPrice) + ) + acc.premium = acc.premium.plus(normalizeValues(curr.premium, 6)) + return acc + }, Promise.resolve({ notional: BigNumber(0), premium: BigNumber(0) }) as Promise<{ notional: BigNumber, premium: BigNumber }>) + fetchResult.dailyNotionalVolume = processed.notional.plus(processedSold.notional).toString() + fetchResult.dailyPremiumVolume = processed.premium.plus(processedSold.premium).toString() + return fetchResult +} +const adapter: SimpleAdapter = { + adapter: { + [CHAIN.ARBITRUM]: { + fetch: fetch, + start: async () => 1681430400 + }, + }, +}; +export default adapter; From 9e1e5243c89d92de3681e284dedcecc1edea6535 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Wed, 9 Aug 2023 14:10:33 +0000 Subject: [PATCH 2/2] fix start time --- options/rysk-finance/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/rysk-finance/index.ts b/options/rysk-finance/index.ts index 71d00c516b..72132ca9c6 100644 --- a/options/rysk-finance/index.ts +++ b/options/rysk-finance/index.ts @@ -128,7 +128,7 @@ const adapter: SimpleAdapter = { adapter: { [CHAIN.ARBITRUM]: { fetch: fetch, - start: async () => 1681430400 + start: async () => 1688428800 }, }, };