From f4154d0479b3eb47aa7cdf819bdaa5dec773c9da Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 14 Jul 2023 13:45:35 +0800 Subject: [PATCH] remove lendingstate.EmptyAddress --- XDCxlending/lendingstate/common.go | 5 ++--- XDCxlending/lendingstate/lendingitem.go | 11 ++++++----- XDCxlending/order_processor.go | 16 ++++++++-------- core/lending_pool.go | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/XDCxlending/lendingstate/common.go b/XDCxlending/lendingstate/common.go index 3f52854640..d22abc37da 100644 --- a/XDCxlending/lendingstate/common.go +++ b/XDCxlending/lendingstate/common.go @@ -2,16 +2,15 @@ package lendingstate import ( "encoding/json" - "github.com/XinFinOrg/XDPoSChain/crypto" "math/big" "time" "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/crypto" ) var ( - EmptyAddress = "xdc0000000000000000000000000000000000000000" - EmptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") + EmptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") ) var EmptyHash = common.Hash{} diff --git a/XDCxlending/lendingstate/lendingitem.go b/XDCxlending/lendingstate/lendingitem.go index 13d9ff0ecf..09006738eb 100644 --- a/XDCxlending/lendingstate/lendingitem.go +++ b/XDCxlending/lendingstate/lendingitem.go @@ -2,14 +2,15 @@ package lendingstate import ( "fmt" + "math/big" + "strconv" + "time" + "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/crypto/sha3" "github.com/globalsign/mgo/bson" - "math/big" - "strconv" - "time" ) const ( @@ -243,7 +244,7 @@ func (l *LendingItem) VerifyLendingSide() error { } func (l *LendingItem) VerifyCollateral(state *state.StateDB) error { - if l.CollateralToken.String() == EmptyAddress || l.CollateralToken.String() == l.LendingToken.String() { + if l.CollateralToken.IsZero() || l.CollateralToken == l.LendingToken { return fmt.Errorf("invalid collateral %s", l.CollateralToken.Hex()) } validCollateral := false @@ -329,7 +330,7 @@ func (l *LendingItem) EncodedSide() *big.Int { return big.NewInt(1) } -//verify signatures +// verify signatures func (l *LendingItem) VerifyLendingSignature() error { V := big.NewInt(int64(l.Signature.V)) R := l.Signature.R.Big() diff --git a/XDCxlending/order_processor.go b/XDCxlending/order_processor.go index 3ee8e6aade..2dcfb560e6 100644 --- a/XDCxlending/order_processor.go +++ b/XDCxlending/order_processor.go @@ -3,6 +3,8 @@ package XDCxlending import ( "encoding/json" "fmt" + "math/big" + "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate" "github.com/XinFinOrg/XDPoSChain/common" @@ -10,7 +12,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/log" - "math/big" ) func (l *Lending) CommitOrder(header *types.Header, coinbase common.Address, chain consensus.ChainContext, statedb *state.StateDB, lendingStateDB *lendingstate.LendingStateDB, tradingStateDb *tradingstate.TradingStateDB, lendingOrderBook common.Hash, order *lendingstate.LendingItem) ([]*lendingstate.LendingTrade, []*lendingstate.LendingItem, error) { @@ -262,10 +263,9 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address collateralToken = oldestOrder.CollateralToken borrowFee = lendingstate.GetFee(statedb, oldestOrder.Relayer) } - if collateralToken.String() == lendingstate.EmptyAddress { + if collateralToken.IsZero() { return nil, nil, nil, fmt.Errorf("empty collateral") } - collateralPrice := common.BasePrice depositRate, liquidationRate, recallRate := lendingstate.GetCollateralDetail(statedb, collateralToken) if depositRate == nil || depositRate.Sign() <= 0 { return nil, nil, nil, fmt.Errorf("invalid depositRate %v", depositRate) @@ -953,11 +953,11 @@ func (l *Lending) GetMediumTradePriceBeforeEpoch(chain consensus.ChainContext, s return nil, nil } -//LendToken and CollateralToken must meet at least one of following conditions -//- Have direct pair in XDCX: lendToken/CollateralToken or CollateralToken/LendToken -//- Have pairs with XDC: -//- lendToken/XDC and CollateralToken/XDC -//- XDC/lendToken and XDC/CollateralToken +// LendToken and CollateralToken must meet at least one of following conditions +// - Have direct pair in XDCX: lendToken/CollateralToken or CollateralToken/LendToken +// - Have pairs with XDC: +// - lendToken/XDC and CollateralToken/XDC +// - XDC/lendToken and XDC/CollateralToken func (l *Lending) GetCollateralPrices(header *types.Header, chain consensus.ChainContext, statedb *state.StateDB, tradingStateDb *tradingstate.TradingStateDB, collateralToken common.Address, lendingToken common.Address) (*big.Int, *big.Int, error) { // lendTokenXDCPrice: price of ticker lendToken/XDC // collateralXDCPrice: price of ticker collateralToken/XDC diff --git a/core/lending_pool.go b/core/lending_pool.go index 4f600b2e58..26d7a1395a 100644 --- a/core/lending_pool.go +++ b/core/lending_pool.go @@ -431,7 +431,7 @@ func (pool *LendingPool) validateNewLending(cloneStateDb *state.StateDB, cloneLe return ErrInvalidLendingType } if tx.Side() == lendingstate.Borrowing { - if tx.CollateralToken().String() == lendingstate.EmptyAddress || tx.CollateralToken().String() == tx.LendingToken().String() { + if tx.CollateralToken().IsZero() || tx.CollateralToken() == tx.LendingToken() { return ErrInvalidLendingCollateral } validCollateral := false @@ -541,7 +541,7 @@ func (pool *LendingPool) validateBalance(cloneStateDb *state.StateDB, cloneLendi // collateralPrice = BTC/USD (eg: 8000 USD) // lendTokenXDCPrice: price of lendingToken in XDC quote var lendTokenXDCPrice, collateralPrice, collateralTokenDecimal *big.Int - if collateralToken.String() != lendingstate.EmptyAddress { + if !collateralToken.IsZero() { collateralTokenDecimal, err = XDCXServ.GetTokenDecimal(pool.chain, cloneStateDb, collateralToken) if err != nil { return fmt.Errorf("validateOrder: failed to get collateralTokenDecimal. err: %v", err)