XTrading.exchange
  • ⭐Getting Started
    • 👋Overview
    • 📖Perpetual Basics
  • 🔥XTrading Alpha
    • 🤖AI Terminal
    • 🦾AI-Enabled Support
    • 🛠️MCP(Model Context Protocol) ready
    • ⌨️Keyboard Shortcuts
    • 📊Change layout
    • 🌟Lite Mode
    • 👆Fat Finger Protection
    • 😎Pro Mode
    • 🔔Notification in TG
  • 💡Product Fundamentals
    • 💸Margin Specs
    • 💰Funding Payments
    • 🔻Liquidation
    • 🖼️NFT Selection Criteria
  • 📑Core Contracts
    • 📖OrderBook
    • 📊Margin
    • 💻Protocol Vault
    • 📃Smart Contract Details
  • 📓Developer Docs
    • 📉Liquidation Doc
    • 📜Whitepaper
  • 🎓Legal Docs
    • 🔏Privacy Policy
    • 🔍Terms and Conditions
  • 📚Additional Resources
    • 🔖Glossary
    • ❓FAQs
    • 🛣️Roadmap
  • 🤝Community
    • 🔎Feature Request Form
    • 🤚Product Feedback Form
  • 💰EARN $XTRA
    • 🤝1,000,000 $XTRA Community Activity Pool
    • ⭐XTrading Ambassador Program - Batch 1
    • 📈Trade on XTrading and Earn Points
  • 🗣️Socials
    • 🔗Website
    • 👽Discord
    • 🐦Twitter
    • 🔊Telegram
    • 📺YouTube
Powered by GitBook
On this page
  • Custom Structure
  • Balance:
  • View Functions
  • getMarginbalance
  • getMargin
  • getMarginRatio
  • getMarginRatioAfterSettlement
  • checkInitialMarginFraction
  • checkInitialMarginFraction
  • checkWithdrawableAmount
  • isUnderwater
  • isUnderwater
  • canLiquidate
  • getGlobalIndex
  • getFundingDelta
  • getIndexDelta
  • Write Methods
  • settleAccount
  • liquidate
  1. Core Contracts

Margin

Margin Ledger maintains a ledger for all users including current position / margin etc.

PreviousOrderBookNextProtocol Vault

Last updated 1 year ago

Margin Contract has useful functions concerning mostly values and ledger upkeep including Funding / Position Value.

Custom Structure

Balance:

Balance Structure maintains information about the user and their balancers including their position and current worth.

This is an internal Accounting structure the values need to be calculated as they do not represent anything!

Type
Name
Description

isMarginPositive

bool

Indicates whether the margin is positive or not. This is not an indicator of withdrawable balance or whether the account is underwater. This is used for internal accounting.

isBuy

bool

Default = False. Indicates whether the current position is Long/Short.

margin

uint256

Margin Balance this is only equal to the amount deposited when there is no position open.

position

uint256

Amount of asset(s) in position.

View Functions

getMarginbalance

function getMarginBalance(address _user)

Function to return structure for the user.

getMargin

function getMargin(address user)

getMarginRatio

function getMarginRatio(address user)

Function to return the current margin ratio for a given user.

getMarginRatioAfterSettlement

function getMarginRatioAfterSettlement(address user)

Function to get margin ratio but after settling the current funding amount pending.

checkInitialMarginFraction

function checkInitialMarginFraction(address user, bool isBuy, uint256 amount, uint256 price)

Function to check whether a particular position for a user will be above IMF for the given user.

checkInitialMarginFraction

Function to check whether a particular custom Balance structure passes IMF with the current mark price.

checkWithdrawableAmount

function checkWithdrawableAmount(address user)

Function to get withdrawable balance for any given user.

isUnderwater

Returns true if a given custom balance structure becomes underwater at a given price.

isUnderwater

function isUnderwater(address user)

Returns true if the address is underwater.

canLiquidate

function canLiquidate(address liquidator, address liquidatee)

Returns true if liquidator can liquidate liquidatee

getGlobalIndex

function getGlobalIndex()

Returns: Timestamp and Value for the latest value of the Global Index.

getFundingDelta

function getFundingDelta(address _user)

Returns the amount of funding accrued for the _user

getIndexDelta

function getIndexDelta(address user)

Returns

Param
Type
Description

delta

uint256

Difference between global and cached Index.

isPositive

bool

Indicator for whether the difference is positive or negative.

timestamp

uint256

Timestamp for the last cached Index.

_globalIndex

int256

Value of global Index .

Write Methods

settleAccount

function settleAccount(address user)

Function to settleAccount for any user and charge funding.

liquidate

function liquidate(address liquidator, address liquidatee)

Function to liquidate any account where liquidatoris the account that is liquidating and liquidateeis the account being liquidated.

Function to return .margin for the given user.

function checkInitialMarginFraction(balance, bool isBuy, uint256 amount, uint256 price)

function isUnderwater(balance, uint256 price)

📑
📊
Balance
Balance
Balance
Balance