Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug margin #107

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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