Skip to content

Commit

Permalink
Debug margin (#107)
Browse files Browse the repository at this point in the history
* Fix GetAvailableMargin

* v5activation

---------

Co-authored-by: Shubham Goyal <[email protected]>
  • Loading branch information
atvanguard and lumos42 authored Sep 4, 2023
1 parent b856238 commit 611a505
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions precompile/contracts/bibliophile/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bibliophile

import (
"math/big"
"time"

"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion precompile/contracts/bibliophile/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 10 additions & 2 deletions precompile/contracts/bibliophile/margin_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 611a505

Please sign in to comment.