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

feat: margin zero #12971

Merged
merged 8 commits into from
Jan 9, 2025
Merged

feat: margin zero #12971

merged 8 commits into from
Jan 9, 2025

Conversation

0xradishmz
Copy link
Contributor

@0xradishmz 0xradishmz commented Jan 5, 2025

NOTE

Please enable "Allow edits by maintainers" while putting up the PR.


  • If you would like to add a volume adapter please submit the PR here.
  • If you would like to add a liquidations adapter, please refer to this readme document for details.
  1. Once your adapter has been merged, it takes time to show on the UI. If more than 24 hours have passed, please let us know in Discord.
  2. Sorry, We no longer accept fetch adapter for new projects, we prefer the tvl to computed from blockchain data, if you have trouble with creating a the adapter, please hop onto our discord, we are happy to assist you.
  3. Please fill the form below only if the PR is for listing a new protocol else it can be ignored/replaced with reason/details about the PR
  4. For updating listing info It is a different repo, you can find your listing in this file: https://github.com/DefiLlama/defillama-server/blob/master/defi/src/protocols/data2.ts, you can edit it there and put up a PR
  5. Do not edit/push package-lock.json file as part of your changes, we use lockfileVersion 2, and most use v1 and using that messes up our CI
  6. No need to go to our discord and announce that you've created a PR, we monitor all PRs and will review it asap

Name (to be shown on DefiLlama):

Margin Zero

Twitter Link:

https://x.com/marginzero_xyz

List of audit links if any:

https://github.com/marginzero-xyz/contracts/blob/main/audits/yAudits/MarginZero_yAudits_December2024_OptionsOTM.pdf

Website Link:

https://www.marginzero.xyz/

Logo (High resolution, will be shown with rounded borders):

image

Current TVL:
Treasury Addresses (if the protocol has treasury)
Chain:

Sonic (cross chain soon)

Coingecko ID (so your TVL can appear on Coingecko, leave empty if not listed): (https://api.coingecko.com/api/v3/coins/list)
Coinmarketcap ID (so your TVL can appear on Coinmarketcap, leave empty if not listed): (https://api.coinmarketcap.com/data-api/v3/map/all?listing_status=active,inactive,untracked&start=1&limit=10000)
Short Description (to be shown on DefiLlama):

MarginZero is an omni-chain, oracle-less and permission-less options & protected perpetual protocol

Token address and ticker if any:
Category (full list at https://defillama.com/categories) *Please choose only one:

Options

Oracle Provider(s): Specify the oracle(s) used (e.g., Chainlink, Band, API3, TWAP, etc.):
Implementation Details: Briefly describe how the oracle is integrated into your project:
Documentation/Proof: Provide links to documentation or any other resources that verify the oracle's usage:
forkedFrom (Does your project originate from another project):
methodology (what is being counted as tvl, how is tvl being calculated):

TVL is the total amount of tokens deposited in DEX's through Margin Zero's handler contracts.

Github org/user (Optional, if your code is open source, we can track activity):

https://github.com/marginzero-xyz

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     21.83 k

total                    21.83 k 

@g1nt0ki g1nt0ki self-assigned this Jan 6, 2025
Copy link
Member

@g1nt0ki g1nt0ki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

const chainConfig = {
146: {
chainName: "sonic",
subgraph: "https://api.goldsky.com/api/public/project_cm58q8wq01kbk01ts09lc52kp/subgraphs/mz-subgraph/main/gn"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way to get tokenIds of the uni v3 positions? we have helper code to unwrap and pull tvl from it, would simplify this adapter code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our contracts do not deposit through Nft position manager contract but rather directly into the pool, so we don't have the v3 tokenIds

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

function LiquidityRangesQuery(first, skip) {
return `{ liquidityRanges(first: ${first}, skip: ${skip}) {pool liquidity tickLower tickUpper }}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a liquidity_gt: 100 to filter out expired positions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, what do you mean by expired positions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xradishmz when liquidity is withdrawn, I saw a few positions with 0 liquidity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xradishmz when liquidity is withdrawn, I saw a few positions with 0 liquidity

sounds good, thanks for clarification



async function tvl(sdk) {
if (sdk.api.chainId && chainConfig[sdk.api.chainId]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont think this check is needed

}


async function tvl(sdk) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change sdk -> api


let counter = 0;
while (true) {
const { liquidityRanges } = await cachedGraphQuery('marign-zero/tvl', subgraphUrl, LiquidityRangesQuery(1000, 1000 * counter))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use fetchById here (also, update the query), like https://github.com/0xradishmz/DefiLlama-Adapters/blob/pr/0xradishmz/12971/projects/panoptic/index.js#L63

noticed that panoptic & this adapter have the same bug, if we use cached graph data, we will end up with incorrect liquidity data & wrong tvl, but using tokenIds fixing it (so we fetch liquidity on chain)

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     21.39 k

total                    21.39 k 

@0xradishmz 0xradishmz requested a review from g1nt0ki January 8, 2025 17:51
@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.37 k

total                    17.37 k 

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.37 k

total                    17.37 k 

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.38 k

total                    17.38 k 

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.28 k

total                    17.28 k 

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.28 k

total                    17.28 k 

@g1nt0ki
Copy link
Member

g1nt0ki commented Jan 9, 2025

@0xradishmz thanks for the PR

@llamabutler
Copy link

The adapter at projects/margin-zero exports TVL:

sonic                     17.56 k

total                    17.56 k 

@g1nt0ki g1nt0ki merged commit 007159c into DefiLlama:main Jan 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants