Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
@0x/contracts-asset-proxy: Add getMarketMarginPremium() to IDydx.
Browse files Browse the repository at this point in the history
`@0x/contracts-dev-utils`: Handle dydx market premiums.
  • Loading branch information
merklejerk committed Feb 8, 2020
1 parent 9e18fa8 commit 52a2983
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contracts/asset-proxy/contracts/src/interfaces/IDydx.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ interface IDydx {
view
returns (Price memory price);

/// @dev Get the margin premium for a market. A margin premium makes it so that any positions that
/// include the market require a higher collateralization to avoid being liquidated.
/// @param marketId The market to query
/// @return premium The market's margin premium
function getMarketMarginPremium(uint256 marketId)
external
view
returns (D256 memory premium);

/// @dev Get the total supplied and total borrowed values of an account adjusted by the marginPremium
/// of each market. Supplied values are divided by (1 + marginPremium) for each market and
/// borrowed values are multiplied by (1 + marginPremium) for each market. Comparing these
Expand Down
7 changes: 7 additions & 0 deletions contracts/asset-proxy/contracts/test/TestDydxBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ contract TestDydxBridge is
returns (Price memory price)
{}

/// @dev Unsused
function getMarketMarginPremium(uint256 marketId)
external
view
returns (IDydx.D256 memory premium)
{}

/// @dev Unused.
function getAdjustedAccountValues(
AccountInfo calldata account
Expand Down
10 changes: 10 additions & 0 deletions contracts/dev-utils/contracts/src/D18.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ library D18 {
r = _add(int256(a), b);
}

/// @dev Add two decimals.
function add(int256 a, uint256 b)
internal
pure
returns (int256 r)
{
require(int256(b) >= 0, "D18/DECIMAL_VALUE_TOO_BIG");
r = _add(a, int256(b));
}

/// @dev Add two decimals.
function add(uint256 a, uint256 b)
internal
Expand Down
10 changes: 10 additions & 0 deletions contracts/dev-utils/contracts/src/LibDydxBalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ library LibDydxBalance {
}
// The action value is the action rate times the price.
value = D18.mul(price, value);
// Scale by the market premium.
int256 marketPremium = D18.add(
D18.one(),
info.dydx.getMarketMarginPremium(action.marketId).value
);
if (action.actionType == IDydxBridge.BridgeActionType.Deposit) {
value = D18.div(value, marketPremium);
} else {
value = D18.mul(value, marketPremium);
}
}

/// @dev Returns the conversion rate for an action, expressed as units
Expand Down
9 changes: 9 additions & 0 deletions contracts/dev-utils/contracts/test/TestDydx.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ contract TestDydx {
}
}

function getMarketMarginPremium(uint256)
external
view
returns (IDydx.D256 memory premium)
{
// Return 0.
return premium;
}

function getMarketPrice(
uint256 marketId
)
Expand Down

0 comments on commit 52a2983

Please sign in to comment.