Skip to content
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

drop contango V1 #10637

Closed
wants to merge 1 commit into from
Closed
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
140 changes: 0 additions & 140 deletions projects/contango-v2/index.js

This file was deleted.

168 changes: 127 additions & 41 deletions projects/contango/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,139 @@
const { getLogs } = require("../helper/cache/getLogs")
const { sumTokens2 } = require('../helper/unwrapLPs')
const sdk = require('@defillama/sdk')
const { cachedGraphQuery } = require("../helper/cache");

const CONTANGO_PROXY = "0x6Cae28b3D09D8f8Fc74ccD496AC986FC84C0C24E";
const CONTANGO_LENS_PROXY = "0xe03835Dfae2644F37049c1feF13E8ceD6b1Bb72a";

const config = {
arbitrum: {
ladle: '0x93343c08e2055b7793a3336d659be348fc1b08f9',
fromBlock: 129978,
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-arbitrum",
},
optimism: {
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-optimism",
},
ethereum: {
ladle: '0x30E7348163016B3b6E1621A3Cb40e8CF33CE97db',
fromBlock: 16074982,
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-mainnet",
},
}
polygon: {
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-polygon",
},
xdai: {
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-gnosis",
},
base: {
contango: CONTANGO_PROXY,
contango_lens: CONTANGO_LENS_PROXY,
grapUrl:
"https://graph.contango.xyz:18000/subgraphs/name/contango-xyz/v2-base",
},
};

module.exports = {
doublecounted: true,
methodology: `Counts the tokens locked in the positions to be used as margin + user's tokens locked in the protocol's vault. Borrowed coins are discounted from the TVL, so only the position margins are counted. The reason behind this is that the protocol only added the user's margin to the underlying money market. Adding the borrowed coins to the TVL can be used as a proxy for the protocol's open interest.`,
};

Object.keys(config).forEach(chain => {
const { ladle, fromBlock } = config[chain]
Object.keys(config).forEach((chain) => {
const { contango, contango_lens, grapUrl } = config[chain];
module.exports[chain] = {
tvl: async (api) => {
const block = api.block
const cauldron = await sdk.api2.abi.call({
target: ladle,
abi: 'address:cauldron',
chain, block,
})

const logs = await getLogs({
api, target: cauldron, fromBlock,
topic: 'IlkAdded(bytes6,bytes6)', eventAbi: abis.IlkAdded,
})
const ilkIds = logs.map((log) => log.args.ilkId)

const joins = await sdk.api2.abi.multiCall({
target: ladle,
calls: ilkIds,
abi: abis.joins,
chain, block,
})
const tokens = await sdk.api2.abi.multiCall({
abi: 'address:asset',
calls: joins,
chain, block,
})
const tokensAndOwners = joins.map((t, i) => ([tokens[i], t]))
return sumTokens2({ chain, block, tokensAndOwners, })
await Promise.all([
positionsTvl(api, contango_lens, grapUrl, false),
vaultTvl(api, contango, grapUrl),
]);
return api.getBalances();
},
borrowed: async (api) =>
positionsTvl(api, contango_lens, grapUrl, true),
};
});

async function positionsTvl(
api,
contangoLens,
grapUrl,
borrowed,
) {
const cacheKey = `contango-positions-${api.chain}`;
const positions = await cachedGraphQuery(cacheKey, grapUrl, graphQueries.position, {
api,
useBlock: true,
fetchById: true,
safeBlockLimit: 3000,
})
const parts = positions.map(({ id, instrument: { base, quote } }) => [
id,
[base.id, quote.id],
]);

const balances = await api.multiCall({
target: contangoLens,
calls: parts.map(([id]) => id),
abi: abis.balances,
});

balances.forEach(([collateral, debt], i) => {
const [base, quote] = parts[i][1];
if (borrowed) {
api.add(quote, debt);
} else {
api.add(quote, -debt);
api.add(base, collateral);
}
}
})
});
}

async function vaultTvl(api, contango, grapUrl) {
const cacheKey = `contango-vaultAssets-${api.chain}`;
const assets = await cachedGraphQuery(cacheKey, grapUrl, graphQueries.asset, {
api,
useBlock: true,
fetchById: true,
safeBlockLimit: 3000,
})

const vault = await api.call({ abi: "address:vault", target: contango });

await api.sumTokens({ owner: vault, tokens: assets.map(({ id }) => id) });
}

const abis = {
joins: "function joins(bytes6) view returns (address)",
IlkAdded: "event IlkAdded(bytes6 indexed seriesId, bytes6 indexed ilkId)",
}
balances:
"function balances(bytes32 positionId) view returns (uint256 collateral, uint256 debt)",
};

const graphQueries = {
position: `
query MyQuery($lastId: ID, $block: Int) {
positions(
block: {number: $block}
where: {and: [{id_gt: $lastId}, {quantity_not: "0"}]}
first: 1000
) {
id
instrument {
base {
id
}
quote {
id
}
}
}
}`,
asset: `
query MyQuery($lastId: ID, $block: Int) {
assets(block: {number: $block}, where: {id_gt: $lastId} first: 1000) {
id
}
}`,
};
Loading