From 611a50597eed7c300cead170324d995e55320023 Mon Sep 17 00:00:00 2001 From: atvanguard <3612498+atvanguard@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:59:07 +0100 Subject: [PATCH] Debug margin (#107) * Fix GetAvailableMargin * v5activation --------- Co-authored-by: Shubham Goyal --- precompile/contracts/bibliophile/api.go | 5 +++-- precompile/contracts/bibliophile/client.go | 3 ++- precompile/contracts/bibliophile/margin_account.go | 12 ++++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/precompile/contracts/bibliophile/api.go b/precompile/contracts/bibliophile/api.go index 012aecfd92..64f1fa9658 100644 --- a/precompile/contracts/bibliophile/api.go +++ b/precompile/contracts/bibliophile/api.go @@ -2,6 +2,7 @@ package bibliophile import ( "math/big" + "time" "github.com/ava-labs/subnet-evm/precompile/contract" "github.com/ethereum/go-ethereum/common" @@ -28,7 +29,7 @@ func GetClearingHouseVariables(stateDB contract.StateDB, trader common.Address) Trader: trader, IncludeFundingPayments: false, Mode: 0, - }, big.NewInt(0)) + }, big.NewInt(time.Now().Unix())) totalFunding := GetTotalFunding(stateDB, &trader) positionSizes := getPosSizes(stateDB, &trader) underlyingPrices := GetUnderlyingPrices(stateDB) @@ -118,7 +119,7 @@ func GetAMMVariables(stateDB contract.StateDB, ammAddress common.Address, ammInd minAllowableMargin := GetMinAllowableMargin(stateDB) takerFee := GetTakerFee(stateDB) totalMargin := GetNormalizedMargin(stateDB, trader) - availableMargin := GetAvailableMargin(stateDB, trader) + availableMargin := GetAvailableMargin(stateDB, trader, big.NewInt(time.Now().Unix())) reduceOnlyAmount := getReduceOnlyAmount(stateDB, trader, big.NewInt(ammIndex)) longOpenOrdersAmount := getLongOpenOrdersAmount(stateDB, trader, big.NewInt(ammIndex)) shortOpenOrdersAmount := getShortOpenOrdersAmount(stateDB, trader, big.NewInt(ammIndex)) diff --git a/precompile/contracts/bibliophile/client.go b/precompile/contracts/bibliophile/client.go index 46271614d9..7869645ea3 100644 --- a/precompile/contracts/bibliophile/client.go +++ b/precompile/contracts/bibliophile/client.go @@ -176,5 +176,6 @@ func (b *bibliophileClient) GetReduceOnlyAmount(trader common.Address, ammIndex } func (b *bibliophileClient) GetAvailableMargin(trader common.Address) *big.Int { - return GetAvailableMargin(b.accessibleState.GetStateDB(), trader) + blockTimestamp := new(big.Int).SetUint64(b.accessibleState.GetBlockContext().Timestamp()) + return GetAvailableMargin(b.accessibleState.GetStateDB(), trader, blockTimestamp) } diff --git a/precompile/contracts/bibliophile/margin_account.go b/precompile/contracts/bibliophile/margin_account.go index c44a8f97a1..7d6df2e7e3 100644 --- a/precompile/contracts/bibliophile/margin_account.go +++ b/precompile/contracts/bibliophile/margin_account.go @@ -32,10 +32,18 @@ func getReservedMargin(stateDB contract.StateDB, trader common.Address) *big.Int return stateDB.GetState(common.HexToAddress(MARGIN_ACCOUNT_GENESIS_ADDRESS), common.BytesToHash(baseMappingHash)).Big() } -func GetAvailableMargin(stateDB contract.StateDB, trader common.Address) *big.Int { +// Monday, 4 September 2023 10:05:00 +var V5ActivationDate *big.Int = new(big.Int).SetInt64(1693821900) + +func GetAvailableMargin(stateDB contract.StateDB, trader common.Address, blockTimestamp *big.Int) *big.Int { includeFundingPayment := true mode := uint8(1) // Min_Allowable_Margin - output := GetNotionalPositionAndMargin(stateDB, &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: includeFundingPayment, Mode: mode}, nil) + var output GetNotionalPositionAndMarginOutput + if blockTimestamp != nil && blockTimestamp.Cmp(V5ActivationDate) == 1 { + output = GetNotionalPositionAndMargin(stateDB, &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: includeFundingPayment, Mode: mode}, blockTimestamp) + } else { + output = GetNotionalPositionAndMargin(stateDB, &GetNotionalPositionAndMarginInput{Trader: trader, IncludeFundingPayments: includeFundingPayment, Mode: mode}, nil) + } notionalPostion := output.NotionalPosition margin := output.Margin utitlizedMargin := divide1e6(big.NewInt(0).Mul(notionalPostion, GetMinAllowableMargin(stateDB)))