# Margin

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

## Custom Structure&#x20;

### Balance:&#x20;

Balance Structure maintains information about the user and their balancers including their position and current worth.&#x20;

{% hint style="danger" %}
This is an internal Accounting structure the values need to be calculated as they do not represent anything!
{% endhint %}

<table><thead><tr><th width="188">Type</th><th width="98">Name</th><th>Description</th></tr></thead><tbody><tr><td>isMarginPositive</td><td>bool</td><td>Indicates whether the margin is positive or not. <br><br>This is not an indicator of withdrawable balance or whether the account is underwater. <br><br>This is used for internal accounting.</td></tr><tr><td>isBuy</td><td>bool</td><td>Default = False. <br><br>Indicates whether the current position is Long/Short.</td></tr><tr><td>margin</td><td>uint256</td><td>Margin Balance this is only equal to the amount deposited when there is no position open. </td></tr><tr><td>position</td><td>uint256</td><td>Amount of asset(s) in position.</td></tr></tbody></table>

## View Functions

### getMarginbalance&#x20;

`function getMarginBalance(address _user)`

Function to return [Balance ](#balance)structure for the user.

### getMargin

`function getMargin(address`` `*`user)`*

Function to return [Balance](#balance).margin for the given user.

### getMarginRatio

`function getMarginRatio(address user`*`)`*&#x20;

Function to return the current margin ratio for a given user.&#x20;

### getMarginRatioAfterSettlement&#x20;

`function getMarginRatioAfterSettlement(address user)`&#x20;

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

### checkInitialMarginFraction

`function checkInitialMarginFraction(address user, bool isBuy, uint256 amount, uint256 price)`&#x20;

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

### checkInitialMarginFraction

`function checkInitialMarginFraction(`[`Balance` ](#balance)`balance, bool isBuy, uint256 amount, uint256 price)`

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

### checkWithdrawableAmount

`function checkWithdrawableAmount(address user)`&#x20;

Function to get withdrawable balance for any given user.&#x20;

### isUnderwater

`function isUnderwater(`[`Balance` ](#balance)`balance, uint256 price)`&#x20;

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

### isUnderwater&#x20;

`function isUnderwater(address user)`

Returns `true` if the address is underwater.

### canLiquidate

`function canLiquidate(address liquidator, address liquidatee)`&#x20;

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

<table><thead><tr><th width="194">Param</th><th width="136">Type</th><th>Description</th></tr></thead><tbody><tr><td>delta</td><td>uint256 </td><td>Difference between global and cached Index.</td></tr><tr><td>isPositive</td><td>bool</td><td>Indicator for whether the difference is positive or negative.</td></tr><tr><td>timestamp</td><td>uint256</td><td>Timestamp for the last cached Index.</td></tr><tr><td>_globalIndex</td><td>int256</td><td>Value of global Index .</td></tr></tbody></table>

## Write Methods&#x20;

### settleAccount&#x20;

`function settleAccount(address user)`

Function to settleAccount for any user and charge funding.

### liquidate

`function liquidate(address`` `*`liquidator, address`*` ``liquidatee)`&#x20;

Function to liquidate any account where `liquidator`is the account that is liquidating and `liquidatee`is the account being liquidated.
