-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat: implement TVL calculation for Clober Book Manager contract #17158
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
Open
graykode
wants to merge
3
commits into
DefiLlama:main
Choose a base branch
from
clober-dex:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,33 @@ | ||
| const abi = require("./abi.json"); | ||
| const { ethers } = require("ethers"); | ||
| const { getLogs2 } = require('../helper/cache/getLogs') | ||
| const { sumTokens2 } = require('../helper/unwrapLPs') | ||
| const { getUniqueAddresses } = require('../helper/utils') | ||
|
|
||
| const contractV1DeployedChainIds = [1, 137, 42161] | ||
| const contractV1_1DeployedChainIds = [137, 1101, 42161] | ||
|
|
||
| function calculateSalt(chainId, nonce) { | ||
| return ethers.solidityPackedKeccak256(['uint256', 'uint256'], [chainId, nonce]) | ||
| const abi = { | ||
| openEvent: 'event Open(uint192 indexed id, address indexed base, address indexed quote, uint64 unitSize, uint24 makerPolicy, uint24 takerPolicy, address hooks)', | ||
| } | ||
|
|
||
| async function fetchTokenAddressesV1_0(api, chainId){ | ||
| if(!contractV1DeployedChainIds.includes(chainId)){ | ||
| return [] | ||
| } | ||
| return await api.fetchList({ | ||
| lengthAbi: abi.nonce, | ||
| itemAbi: "function computeTokenAddress(uint256) view returns (address)", | ||
| target: "0x93A43391978BFC0bc708d5f55b0Abe7A9ede1B91" | ||
| }) | ||
| const config = { | ||
| monad: { factory: '0x6657d192273731C3cAc646cc82D5F28D0CBE8CCC', fromBlock: 31662843, }, | ||
| } | ||
|
|
||
| async function fetchTokenAddressesV1_1(api, chainId){ | ||
| if (!contractV1_1DeployedChainIds.includes(chainId)) { | ||
| return [] | ||
| } | ||
| const maxNonce = await api.call({ abi: abi.nonce, target: "0x24aC0938C010Fb520F1068e96d78E0458855111D" }) | ||
| return await api.multiCall({ | ||
| abi: "function computeTokenAddress(bytes32) view returns (address)", | ||
| calls: Array.from({length: maxNonce}, (_, i) => i ).map((i, v) => ({ target: "0x58ed1f4913e652baF17C154551bd8E9dbc73fC56", params: calculateSalt(chainId, v) })), | ||
| }) | ||
| function customCacheFunction({ cache, logs }) { | ||
| if (!cache.logs) cache.logs = [] | ||
| const tokens = logs.map(({ base, quote }) => [base, quote]).flat() | ||
| cache.logs.push(...tokens) | ||
| cache.logs = getUniqueAddresses(cache.logs) | ||
| return cache | ||
| } | ||
|
|
||
| async function tvl(api) { | ||
| const chainId = await api.getChainId() | ||
| let tokenAddresses = [...await fetchTokenAddressesV1_0(api, chainId), ...await fetchTokenAddressesV1_1(api, chainId)] | ||
| tokenAddresses = tokenAddresses.flat() | ||
| const markets = await api.multiCall({ abi: abi.market, calls: tokenAddresses }) | ||
| const base = await api.multiCall({ abi: abi.baseToken, calls: markets}) | ||
| const quote = await api.multiCall({ abi: abi.quoteToken, calls: markets}) | ||
| const tokens = [base, quote].flat() | ||
| const symbols = await api.multiCall({ abi: 'erc20:symbol', calls: tokens}) | ||
| const putTokens = tokens.filter((_, i) => symbols[i].includes('$') && symbols[i].endsWith('PUT')) | ||
| const ownerTokens = markets.map((v, i) => ([[base[i], quote[i]], v])) | ||
| const putQutes = await api.multiCall({ abi: abi.quoteToken, calls: putTokens}) | ||
| const putUnderlying = await api.multiCall({ abi: 'address:underlyingToken', calls: putTokens}) | ||
| putTokens.forEach((v, i) => ownerTokens.push([[putQutes[i], putUnderlying[i]], v])) | ||
| return sumTokens2({ api, ownerTokens, blacklistedTokens: putTokens, }) | ||
| const { factory, fromBlock } = config[api.chain] | ||
| const tokens = await getLogs2({ api, factory, eventAbi: abi.openEvent, fromBlock, extraKey: 'open-address', customCacheFunction,}) | ||
| return sumTokens2({ api, owner: factory, tokens, permitFailure: true }) | ||
| } | ||
|
|
||
| module.exports = { | ||
| methodology: "TVL consists of assets deposited into market contracts", | ||
| ethereum: { tvl }, | ||
| polygon: { tvl }, | ||
| arbitrum: { tvl }, | ||
| polygon_zkevm: { tvl } | ||
| } | ||
| methodology: "TVL consists of assets deposited into the Clober Book Manager contract", | ||
| }; | ||
|
|
||
| Object.keys(config).forEach(chain => { | ||
| module.exports[chain] = { tvl } | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd rather keep this for historical accuracy