Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Sep 4, 2023
1 parent 611a505 commit 5f06d60
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
21 changes: 17 additions & 4 deletions plugin/evm/orderbook/memory_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ func (db *InMemoryDatabase) GetNaughtyTraders(oraclePrices map[Market]*big.Int,
}
// has orders that might be cancellable
availableMargin := getAvailableMargin(trader, pendingFunding, oraclePrices, db.LastPrice, db.configService.getMinAllowableMargin(), markets)
if availableMargin.Cmp(big.NewInt(0)) == -1 {
// availableMargin := getAvailableMarginWithDebugInfo(addr, trader, pendingFunding, oraclePrices, db.LastPrice, db.configService.getMinAllowableMargin(), markets)
if availableMargin.Sign() == -1 {
foundCancellableOrders := db.determineOrdersToCancel(addr, trader, availableMargin, oraclePrices, ordersToCancel)
if foundCancellableOrders {
log.Info("negative available margin", "trader", addr.String(), "availableMargin", prettifyScaledBigInt(availableMargin, 6))
Expand Down Expand Up @@ -931,18 +932,30 @@ func getBlankTrader() *Trader {
}

func getAvailableMargin(trader *Trader, pendingFunding *big.Int, oraclePrices map[Market]*big.Int, lastPrices map[Market]*big.Int, minAllowableMargin *big.Int, markets []Market) *big.Int {
// log.Info("in getAvailableMargin", "trader", trader, "pendingFunding", pendingFunding, "oraclePrices", oraclePrices, "lastPrices", lastPrices)
margin := new(big.Int).Sub(getNormalisedMargin(trader), pendingFunding)
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(trader, margin, Min_Allowable_Margin, oraclePrices, lastPrices, markets)
utilisedMargin := divideByBasePrecision(new(big.Int).Mul(notionalPosition, minAllowableMargin))
// print margin, notionalPosition, unrealizePnL, utilisedMargin
// log.Info("stats", "margin", margin, "notionalPosition", notionalPosition, "unrealizePnL", unrealizePnL, "utilisedMargin", utilisedMargin, "Reserved", trader.Margin.Reserved)
return new(big.Int).Sub(
new(big.Int).Add(margin, unrealizePnL),
new(big.Int).Add(utilisedMargin, trader.Margin.Reserved),
)
}

func getAvailableMarginWithDebugInfo(addr common.Address, trader *Trader, pendingFunding *big.Int, oraclePrices map[Market]*big.Int, lastPrices map[Market]*big.Int, minAllowableMargin *big.Int, markets []Market) *big.Int {
margin := new(big.Int).Sub(getNormalisedMargin(trader), pendingFunding)
notionalPosition, unrealizePnL := getTotalNotionalPositionAndUnrealizedPnl(trader, margin, Min_Allowable_Margin, oraclePrices, lastPrices, markets)
utilisedMargin := divideByBasePrecision(new(big.Int).Mul(notionalPosition, minAllowableMargin))
availableMargin := new(big.Int).Sub(
new(big.Int).Add(margin, unrealizePnL),
new(big.Int).Add(utilisedMargin, trader.Margin.Reserved),
)
if availableMargin.Sign() == -1 {
log.Info("availableMargin < 0", "addr", addr.String(), "pendingFunding", pendingFunding, "margin", margin, "notionalPosition", notionalPosition, "unrealizePnL", unrealizePnL, "utilisedMargin", utilisedMargin, "Reserved", trader.Margin.Reserved)
log.Info("prices", "oraclePrices", oraclePrices, "lastPrices", lastPrices)
}
return availableMargin
}

// deepCopyOrder deep copies the LimitOrder struct
func deepCopyOrder(order *Order) Order {
lifecycleList := &order.LifecycleList
Expand Down
4 changes: 2 additions & 2 deletions plugin/evm/orderbook/tx_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package orderbook
import (
"context"
"crypto/ecdsa"
"encoding/hex"
// "encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (lotp *limitOrderTxProcessor) ExecuteMatchedOrdersTx(longOrder Order, short
return err
}

log.Info("ExecuteMatchedOrdersTx", "orders[0]", hex.EncodeToString(orders[0]), "orders[1]", hex.EncodeToString(orders[1]), "fillAmount", prettifyScaledBigInt(fillAmount, 18))
// log.Info("ExecuteMatchedOrdersTx", "orders[0]", hex.EncodeToString(orders[0]), "orders[1]", hex.EncodeToString(orders[1]), "fillAmount", prettifyScaledBigInt(fillAmount, 18))
txHash, err := lotp.executeLocalTx(lotp.orderBookContractAddress, lotp.orderBookABI, "executeMatchedOrders", orders, fillAmount)
log.Info("ExecuteMatchedOrdersTx", "LongOrder", longOrder, "ShortOrder", shortOrder, "fillAmount", prettifyScaledBigInt(fillAmount, 18), "txHash", txHash.String(), "err", err)
return err
Expand Down
2 changes: 2 additions & 0 deletions precompile/contracts/bibliophile/margin_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
// "github.com/ethereum/go-ethereum/log"
)

const (
Expand Down Expand Up @@ -48,5 +49,6 @@ func GetAvailableMargin(stateDB contract.StateDB, trader common.Address, blockTi
margin := output.Margin
utitlizedMargin := divide1e6(big.NewInt(0).Mul(notionalPostion, GetMinAllowableMargin(stateDB)))
reservedMargin := getReservedMargin(stateDB, trader)
// log.Info("GetAvailableMargin", "trader", trader, "notionalPostion", notionalPostion, "margin", margin, "utitlizedMargin", utitlizedMargin, "reservedMargin", reservedMargin)
return big.NewInt(0).Sub(big.NewInt(0).Sub(margin, utitlizedMargin), reservedMargin)
}

0 comments on commit 5f06d60

Please sign in to comment.