Skip to content

Commit

Permalink
fix getAvailableMargin, add ValidateCancelLimitOrderV2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xshinobii committed Mar 14, 2024
1 parent 35cf2f2 commit 3032738
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 11 deletions.
5 changes: 5 additions & 0 deletions contracts/contracts/hubble-v2/interfaces/ITraderViewer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pragma solidity ^0.8.0;

import { IClearingHouse } from "./IClearingHouse.sol";
import { ILimitOrderBook } from "./IJuror.sol";
import { IOrderHandler } from "./IOrderHandler.sol";

interface ITraderViewer {
function getNotionalPositionAndMargin(address trader, bool includeFundingPayments, IClearingHouse.Mode mode) external view returns(uint256 notionalPosition, int256 margin, uint256 requiredMargin);
Expand All @@ -18,4 +20,7 @@ interface ITraderViewer {

function getTotalFundingForCrossMarginPositions(address trader) external view returns(int256 totalFunding);

function validateCancelLimitOrderV2(ILimitOrderBook.Order memory order, address sender, bool assertLowMargin, bool assertOverPositionCap) external view returns (string memory err, bytes32 orderHash, IOrderHandler.CancelOrderRes memory res);

function getRequiredMargin(int256 baseAssetQuantity, uint256 price, uint ammIndex, address trader) external view returns(uint256 requiredMargin);
}
1 change: 1 addition & 0 deletions plugin/evm/orderbook/hubbleutils/margin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func UpgradeVersionV0orV1(blockTimestamp uint64) UpgradeVersion {
return V0
}

// @todo update to newer version wherever it is used in the evm
func GetAvailableMargin(hState *HubbleState, userState *UserState) *big.Int {
notionalPosition, margin := GetNotionalPositionAndMargin(hState, userState, Min_Allowable_Margin)
return GetAvailableMargin_(notionalPosition, margin, userState.ReservedMargin, hState.MinAllowableMargin)
Expand Down
3 changes: 1 addition & 2 deletions precompile/contracts/bibliophile/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func getNotionalPositionAndMargin(stateDB contract.StateDB, input *GetNotionalPo
}
}

func getNotionalPositionAndRequiredMargin(stateDB contract.StateDB, input *GetNotionalPositionAndMarginInput, upgradeVersion hu.UpgradeVersion) GetNotionalPositionAndMarginOutput {
func getNotionalPositionAndRequiredMargin(stateDB contract.StateDB, input *GetNotionalPositionAndMarginInput) GetNotionalPositionAndMarginOutput {
positions, underlyingPrices, accountPreferences, activeMarketIds := getMarketsDataFromDB(stateDB, &input.Trader, input.Mode)
pendingFunding := big.NewInt(0)
if input.IncludeFundingPayments {
Expand All @@ -124,7 +124,6 @@ func getNotionalPositionAndRequiredMargin(stateDB contract.StateDB, input *GetNo
Assets: GetCollaterals(stateDB),
OraclePrices: underlyingPrices,
ActiveMarkets: activeMarketIds,
UpgradeVersion: upgradeVersion,
},
&hu.UserState{
Positions: positions,
Expand Down
6 changes: 3 additions & 3 deletions precompile/contracts/bibliophile/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type BibliophileClient interface {

GetTimeStamp() uint64
GetNotionalPositionAndMargin(trader common.Address, includeFundingPayments bool, mode uint8, upgradeVersion hu.UpgradeVersion) (*big.Int, *big.Int)
GetNotionalPositionAndRequiredMargin(trader common.Address, includeFundingPayments bool, mode uint8, upgradeVersion hu.UpgradeVersion) (*big.Int, *big.Int, *big.Int)
GetNotionalPositionAndRequiredMargin(trader common.Address, includeFundingPayments bool, mode uint8) (*big.Int, *big.Int, *big.Int)
GetCrossMarginAccountData(trader common.Address, mode uint8, upgradeVersion hu.UpgradeVersion) (*big.Int, *big.Int, *big.Int, *big.Int)
GetTotalFundingForCrossMarginPositions(trader *common.Address) *big.Int
GetTraderDataForMarket(trader common.Address, marketId int64, mode uint8) (bool, *big.Int, *big.Int, *big.Int, *big.Int)
Expand Down Expand Up @@ -219,8 +219,8 @@ func (b *bibliophileClient) GetNotionalPositionAndMargin(trader common.Address,
return output.NotionalPosition, output.Margin
}

func (b *bibliophileClient) GetNotionalPositionAndRequiredMargin(trader common.Address, includeFundingPayments bool, mode uint8, upgradeVersion hu.UpgradeVersion) (*big.Int, *big.Int, *big.Int) {
output := getNotionalPositionAndRequiredMargin(b.accessibleState.GetStateDB(), &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: includeFundingPayments, Mode: mode}, upgradeVersion)
func (b *bibliophileClient) GetNotionalPositionAndRequiredMargin(trader common.Address, includeFundingPayments bool, mode uint8) (*big.Int, *big.Int, *big.Int) {
output := getNotionalPositionAndRequiredMargin(b.accessibleState.GetStateDB(), &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: includeFundingPayments, Mode: mode})
return output.NotionalPosition, output.Margin, output.RequiredMargin
}

Expand Down
13 changes: 13 additions & 0 deletions precompile/contracts/bibliophile/margin_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const (
MARGIN_ACCOUNT_GENESIS_ADDRESS = "0x03000000000000000000000000000000000000b1"
JUROR_ADDRESS = "0x03000000000000000000000000000000000000a0"
ORACLE_SLOT int64 = 4
SUPPORTED_COLLATERAL_SLOT int64 = 8
MARGIN_MAPPING_SLOT int64 = 10
Expand Down Expand Up @@ -45,10 +46,22 @@ func getReservedMargin(stateDB contract.StateDB, trader common.Address) *big.Int
}

func GetAvailableMargin(stateDB contract.StateDB, trader common.Address, upgradeVersion hu.UpgradeVersion) *big.Int {
if getPrecompileVersion(stateDB, common.HexToAddress(JUROR_ADDRESS)).Cmp(big.NewInt(0)) == 0 {
return GetAvailableMarginLegacy(stateDB, trader, upgradeVersion)
}
return GetAvailableMarginV2(stateDB, trader)
}

func GetAvailableMarginLegacy(stateDB contract.StateDB, trader common.Address, upgradeVersion hu.UpgradeVersion) *big.Int {
output := getNotionalPositionAndMargin(stateDB, &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: true, Mode: uint8(1)}, upgradeVersion) // Min_Allowable_Margin
return hu.GetAvailableMargin_(output.NotionalPosition, output.Margin, getReservedMargin(stateDB, trader), GetMinAllowableMargin(stateDB))
}

func GetAvailableMarginV2(stateDB contract.StateDB, trader common.Address) *big.Int {
output := getNotionalPositionAndRequiredMargin(stateDB, &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: true, Mode: uint8(1)})
return hu.Sub(output.Margin, hu.Add(output.RequiredMargin, getReservedMargin(stateDB, trader)))
}

func getOracleAddress(stateDB contract.StateDB) common.Address {
return common.BytesToAddress(stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BigToHash(big.NewInt(ORACLE_SLOT))).Bytes())
}
Expand Down
6 changes: 3 additions & 3 deletions precompile/contracts/juror/matching_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ func reducesPosition(positionSize *big.Int, baseAssetQuantity *big.Int) bool {
}

func getRequiredMargin(bibliophile b.BibliophileClient, order ILimitOrderBookOrder) *big.Int {
if false { // @todo find apt condition, maybe by block number?
return getRequiredMarginNew(bibliophile, order.BaseAssetQuantity, order.Price, order.AmmIndex.Int64(), &order.Trader)
if bibliophile.GetPrecompileVersion(common.HexToAddress(SelfAddress)).Cmp(big.NewInt(0)) == 0 {
return getRequiredMarginLegacy(bibliophile, order)
}
return getRequiredMarginLegacy(bibliophile, order)
return getRequiredMarginNew(bibliophile, order.BaseAssetQuantity, order.Price, order.AmmIndex.Int64(), &order.Trader)
}

func getRequiredMarginNew(bibliophile b.BibliophileClient, baseAsset *big.Int, price *big.Int, marketId int64, trader *common.Address) *big.Int {
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/traderviewer/contract.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getCrossMarginAccountData","outputs":[{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"},{"internalType":"int256","name":"unrealizedPnl","type":"int256"},{"internalType":"int256","name":"pendingFunding","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"bool","name":"includeFundingPayments","type":"bool"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getNotionalPositionAndMargin","outputs":[{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"int256","name":"margin","type":"int256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"}],"name":"getTotalFundingForCrossMarginPositions","outputs":[{"internalType":"int256","name":"totalFunding","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getTraderDataForMarket","outputs":[{"internalType":"bool","name":"isIsolated","type":"bool"},{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"int256","name":"unrealizedPnl","type":"int256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"},{"internalType":"int256","name":"pendingFunding","type":"int256"}],"stateMutability":"view","type":"function"}]
[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getCrossMarginAccountData","outputs":[{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"},{"internalType":"int256","name":"unrealizedPnl","type":"int256"},{"internalType":"int256","name":"pendingFunding","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"bool","name":"includeFundingPayments","type":"bool"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getNotionalPositionAndMargin","outputs":[{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"int256","name":"margin","type":"int256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int256","name":"baseAssetQuantity","type":"int256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"}],"name":"getRequiredMargin","outputs":[{"internalType":"uint256","name":"requiredMargin","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"}],"name":"getTotalFundingForCrossMarginPositions","outputs":[{"internalType":"int256","name":"totalFunding","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"enum IClearingHouse.Mode","name":"mode","type":"uint8"}],"name":"getTraderDataForMarket","outputs":[{"internalType":"bool","name":"isIsolated","type":"bool"},{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"int256","name":"unrealizedPnl","type":"int256"},{"internalType":"uint256","name":"requiredMargin","type":"uint256"},{"internalType":"int256","name":"pendingFunding","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"int256","name":"baseAssetQuantity","type":"int256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"salt","type":"uint256"},{"internalType":"bool","name":"reduceOnly","type":"bool"},{"internalType":"bool","name":"postOnly","type":"bool"}],"internalType":"struct ILimitOrderBook.Order","name":"order","type":"tuple"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"bool","name":"assertLowMargin","type":"bool"},{"internalType":"bool","name":"assertOverPositionCap","type":"bool"}],"name":"validateCancelLimitOrderV2","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"bytes32","name":"orderHash","type":"bytes32"},{"components":[{"internalType":"int256","name":"unfilledAmount","type":"int256"},{"internalType":"address","name":"amm","type":"address"}],"internalType":"struct IOrderHandler.CancelOrderRes","name":"res","type":"tuple"}],"stateMutability":"view","type":"function"}]
Loading

0 comments on commit 3032738

Please sign in to comment.