Skip to content

Jasper #2318

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

Merged
merged 6 commits into from
Jan 14, 2025
Merged

Jasper #2318

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helpers/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export enum CHAIN {
SVM = "svm",
ASTRZK = "astrzk",
LYRA = "lyra",
BITLAYER = "bitlayer",
BITLAYER = "btr",
XLAYER = "xlayer",
MERLIN = "merlin",
CHILIZ = "chiliz",
Expand Down
1 change: 0 additions & 1 deletion helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const DEFAULTS: any = {
ZETA_RPC: "https://zetachain-evm.blockpi.network/v1/rpc/public,https://zetachain-mainnet-archive.allthatnode.com:8545",
SVM_RPC: "https://rpc.cosvm.net",
XLAYER_RPC: "https://xlayerrpc.okx.com",
BITLAYER_RPC: "https://rpc.bitlayer.org,https://rpc.ankr.com/bitlayer,https://rpc.bitlayer-rpc.com,https://rpc-bitlayer.rockx.com",
PLANQ_RPC: "https://planq-rpc.nodies.app,https://jsonrpc.planq.nodestake.top",
}

Expand Down
81 changes: 46 additions & 35 deletions options/jaspervault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { Balances } from "@defillama/sdk";
import { Chain } from "@defillama/sdk/build/general";
import BigNumber from "bignumber.js";
import ADDRESSES from '../../helpers/coreAssets.json'
import { getAddress } from 'ethers';

const iBTC_arbitrum = '0x050C24dBf1eEc17babE5fc585F06116A259CC77A'
const WSOL_arbitrum = '0x2bcC6D6CdBbDC0a4071e48bb3B969b06B3330c07'
const UNI_arbitrum = '0xFa7F8980b0f1E64A2062791cc3b0871572f1F7f0'
const cbBTC_base = '0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf'
const USDT_btr = '0xfe9f969faf8ad72a83b761138bf25de87eff9dd2'
type TokenContracts = {
[key in Chain]: string[][];
};
Expand All @@ -19,44 +22,42 @@ const contracts: TokenContracts = {
[ADDRESSES.arbitrum.USDC],
[ADDRESSES.arbitrum.USDT],
[ADDRESSES.arbitrum.ARB],
[ADDRESSES.arbitrum.LINK],
[UNI_arbitrum],
[WSOL_arbitrum],
[iBTC_arbitrum]
],
[CHAIN.BASE]: [
[ADDRESSES.base.USDC],
[cbBTC_base],
]
],
[CHAIN.BITLAYER]: [
[USDT_btr]
],
}
const chainsStartTimes: { [chain: string]: number } = {
[CHAIN.ARBITRUM]: 1715175000,
[CHAIN.BASE]: 1723001211,
[CHAIN.BITLAYER]: 1723001211,
}
let tokenDecimals = {}
const subgraphEndpoints: ChainEndpoints = {}
subgraphEndpoints[CHAIN.ARBITRUM] = "https://arb.subgraph.jaspervault.io";
subgraphEndpoints[CHAIN.BASE] = "https://base.subgraph.jaspervault.io";
subgraphEndpoints[CHAIN.BITLAYER] = "https://subgraphs.jaspervault.io/subgraphs/jaspervault-bitlayer";

function getDecimals(token_address: string) {
let decimalPlaces = 0;
if (token_address == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
decimalPlaces = 18
}
else {
if (tokenDecimals[token_address.toLowerCase()] != undefined) {
decimalPlaces = tokenDecimals[token_address.toLowerCase()]
}
else {
const checksumAddress = getAddress(token_address);
if (tokenDecimals[checksumAddress] != undefined) {
decimalPlaces = tokenDecimals[checksumAddress]
}
else {
return 0;
}
}
}
return decimalPlaces;
token_address = token_address.toLowerCase()
if (token_address == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
return 18

if (tokenDecimals[token_address] != undefined)
return tokenDecimals[token_address]
return 0;
}

// event logs can be found here: https://github.com/DefiLlama/dimension-adapters/pull/1492#issuecomment-2118112517
function calculateNotionalVolume(balances: Balances, orders: any[]) {
function calculateNotionalVolume(balances: Balances, orders: any[], chain: Chain) {
for (const order of orders) {
let orderDetails = order.callOrder || order.putOrder;
if (!orderDetails) {
Expand All @@ -68,7 +69,7 @@ function calculateNotionalVolume(balances: Balances, orders: any[]) {
if (order.callOrder) {
let decimals_strikeAsset: number = getDecimals(orderDetails.strikeAsset);
if (decimals_strikeAsset == 0) {
console.error("No decimals data found for strikeAsset", orderDetails.strikeAsset);
console.error("No decimals data found for strikeAsset", orderDetails.strikeAsset, chain);
continue;
}
let decimals_underlyingAsset = getDecimals(orderDetails.underlyingAsset);
Expand All @@ -90,7 +91,7 @@ function calculateNotionalVolume(balances: Balances, orders: any[]) {
if (order.callOrder) {
let decimals_strikeAsset: number = getDecimals(orderDetails.strikeAsset);
if (decimals_strikeAsset == 0) {
console.error("No decimals data found for strikeAsset", orderDetails.strikeAsset);
console.error("No decimals data found for strikeAsset", orderDetails.strikeAsset, chain);
continue;
}
let notionalValue = new BigNumber(orderDetails.strikeAmount).dividedBy(new BigNumber(10).pow(decimals_strikeAsset)).multipliedBy(new BigNumber(orderDetails.quantity).dividedBy(new BigNumber(10).pow(18)))
Expand All @@ -101,7 +102,7 @@ function calculateNotionalVolume(balances: Balances, orders: any[]) {
else if (order.putOrder) {
let decimals_lockAsset: number = getDecimals(orderDetails.lockAsset);
if (decimals_lockAsset == 0) {
console.error("No decimals data found for lockAsset", orderDetails.lockAsset);
console.error("No decimals data found for lockAsset", orderDetails.lockAsset, chain);
continue;
}
let notionalValue = new BigNumber(orderDetails.lockAmount).dividedBy(new BigNumber(10).pow(decimals_lockAsset)).multipliedBy(new BigNumber(orderDetails.quantity).dividedBy(new BigNumber(10).pow(18)))
Expand Down Expand Up @@ -254,13 +255,6 @@ export async function fetchSubgraphData({ createBalances, startTimestamp, endTim
const now = endTimestamp;
const startOfDay = startTimestamp;
const tokens = contracts[chain].map(i => i[0]);
if (chain === CHAIN.ARBITRUM) tokens.push('0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9')
const decimals = await api.multiCall({ abi: 'erc20:decimals', calls: tokens, chain });

tokenDecimals = tokens.reduce((obj, token, index) => {
obj[token] = decimals[index];
return obj;
}, {});

const [
dailyCallData, dailyCallDataV2, dailyPutData, dailyPutDataV2, dailyPremiumData,
Expand All @@ -280,7 +274,26 @@ export async function fetchSubgraphData({ createBalances, startTimestamp, endTim
// const totalNotionalVolume = createBalances()
// const totalPremiumVolume = createBalances()

calculateNotionalVolume(dailyNotionalVolume, [...dailyCallData.callOrderEntities, ...dailyCallDataV2.callOrderEntities, ...dailyPutData.putOrderEntities, ...dailyPutDataV2.putOrderEntities]);
const tokenSet= new Set<string>()
const allOrders = [...dailyCallData.callOrderEntities, ...dailyCallDataV2.callOrderEntities, ...dailyPutData.putOrderEntities, ...dailyPutDataV2.putOrderEntities]
allOrders.forEach((order: any)=>{
const fields = ['callOrder', 'putOrder']
for (const field of fields) {
const { strikeAsset, underlyingAsset, lockAsset } = order[field] ?? {}
if (strikeAsset) tokenSet.add(strikeAsset.toLowerCase())
if (underlyingAsset) tokenSet.add(underlyingAsset.toLowerCase())
if (lockAsset) tokenSet.add(lockAsset.toLowerCase())
}
if (order.strikeAsset) tokenSet.add(order.strikeAsset)
})

let decimals = await api.multiCall({ abi: 'erc20:decimals', calls: tokens, });

tokens.map((token, index) => {
tokenDecimals[token.toLowerCase()] = decimals[index];
});

calculateNotionalVolume(dailyNotionalVolume,allOrders, chain);
calculatePremiumVolume(dailyPremiumVolume, dailyPremiumData.optionPremiums);
// calculateNotionalVolume(totalNotionalVolume, [...totalCallData.callOrderEntities, ...totalPutData.putOrderEntities]);
// calculatePremiumVolume(totalPremiumVolume, totalPremiumData.optionPremiums);
Expand All @@ -293,8 +306,6 @@ export async function fetchSubgraphData({ createBalances, startTimestamp, endTim
}
}



const adapter: Adapter = {
version: 2,
adapter: Object.keys(subgraphEndpoints).reduce((acc, chain) => {
Expand Down
Loading