Skip to content

Commit d28c7f6

Browse files
rebase from dev-upgrade
2 parents 1ce186d + 1ca7d76 commit d28c7f6

File tree

213 files changed

+10622
-2891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+10622
-2891
lines changed

XDCx/XDCx.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ func (XDCx *XDCX) GetAveragePriceLastEpoch(chain consensus.ChainContext, statedb
292292

293293
// return tokenQuantity (after convert from XDC to token), tokenPriceInXDC, error
294294
func (XDCx *XDCX) ConvertXDCToToken(chain consensus.ChainContext, statedb *state.StateDB, tradingStateDb *tradingstate.TradingStateDB, token common.Address, quantity *big.Int) (*big.Int, *big.Int, error) {
295-
if token.String() == common.XDCNativeAddress {
295+
if token == common.XDCNativeAddressBinary {
296296
return quantity, common.BasePrice, nil
297297
}
298-
tokenPriceInXDC, err := XDCx.GetAveragePriceLastEpoch(chain, statedb, tradingStateDb, token, common.HexToAddress(common.XDCNativeAddress))
298+
tokenPriceInXDC, err := XDCx.GetAveragePriceLastEpoch(chain, statedb, tradingStateDb, token, common.XDCNativeAddressBinary)
299299
if err != nil || tokenPriceInXDC == nil || tokenPriceInXDC.Sign() <= 0 {
300300
return common.Big0, common.Big0, err
301301
}
@@ -595,10 +595,11 @@ func (XDCx *XDCX) GetTriegc() *prque.Prque {
595595

596596
func (XDCx *XDCX) GetTradingStateRoot(block *types.Block, author common.Address) (common.Hash, error) {
597597
for _, tx := range block.Transactions() {
598-
from := *(tx.From())
599-
if tx.To() != nil && tx.To().Hex() == common.TradingStateAddr && from.String() == author.String() {
600-
if len(tx.Data()) >= 32 {
601-
return common.BytesToHash(tx.Data()[:32]), nil
598+
to := tx.To()
599+
if to != nil && *to == common.TradingStateAddrBinary && *tx.From() == author {
600+
data := tx.Data()
601+
if len(data) >= 32 {
602+
return common.BytesToHash(data[:32]), nil
602603
}
603604
}
604605
}

XDCx/order_processor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ func (XDCx *XDCX) processOrderList(coinbase common.Address, chain consensus.Chai
236236
maxTradedQuantity = tradingstate.CloneBigInt(amount)
237237
}
238238
var quotePrice *big.Int
239-
if oldestOrder.QuoteToken.String() != common.XDCNativeAddress {
240-
quotePrice = tradingStateDB.GetLastPrice(tradingstate.GetTradingOrderBookHash(oldestOrder.QuoteToken, common.HexToAddress(common.XDCNativeAddress)))
239+
if oldestOrder.QuoteToken != common.XDCNativeAddressBinary {
240+
quotePrice = tradingStateDB.GetLastPrice(tradingstate.GetTradingOrderBookHash(oldestOrder.QuoteToken, common.XDCNativeAddressBinary))
241241
log.Debug("TryGet quotePrice QuoteToken/XDC", "quotePrice", quotePrice)
242242
if quotePrice == nil || quotePrice.Sign() == 0 {
243-
inversePrice := tradingStateDB.GetLastPrice(tradingstate.GetTradingOrderBookHash(common.HexToAddress(common.XDCNativeAddress), oldestOrder.QuoteToken))
243+
inversePrice := tradingStateDB.GetLastPrice(tradingstate.GetTradingOrderBookHash(common.XDCNativeAddressBinary, oldestOrder.QuoteToken))
244244
quoteTokenDecimal, err := XDCx.GetTokenDecimal(chain, statedb, oldestOrder.QuoteToken)
245245
if err != nil || quoteTokenDecimal.Sign() == 0 {
246246
return nil, nil, nil, fmt.Errorf("Fail to get tokenDecimal. Token: %v . Err: %v", oldestOrder.QuoteToken.String(), err)
@@ -374,10 +374,10 @@ func (XDCx *XDCX) getTradeQuantity(quotePrice *big.Int, coinbase common.Address,
374374
if err != nil || quoteTokenDecimal.Sign() == 0 {
375375
return tradingstate.Zero, false, nil, fmt.Errorf("Fail to get tokenDecimal. Token: %v . Err: %v", makerOrder.QuoteToken.String(), err)
376376
}
377-
if makerOrder.QuoteToken.String() == common.XDCNativeAddress {
377+
if makerOrder.QuoteToken == common.XDCNativeAddressBinary {
378378
quotePrice = quoteTokenDecimal
379379
}
380-
if takerOrder.ExchangeAddress.String() == makerOrder.ExchangeAddress.String() {
380+
if takerOrder.ExchangeAddress == makerOrder.ExchangeAddress {
381381
if err := tradingstate.CheckRelayerFee(takerOrder.ExchangeAddress, new(big.Int).Mul(common.RelayerFee, big.NewInt(2)), statedb); err != nil {
382382
log.Debug("Reject order Taker Exchnage = Maker Exchange , relayer not enough fee ", "err", err)
383383
return tradingstate.Zero, false, nil, nil

XDCx/order_processor_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package XDCx
22

33
import (
4-
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
5-
"github.com/XinFinOrg/XDPoSChain/common"
6-
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
74
"math/big"
85
"reflect"
96
"testing"
7+
8+
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
9+
"github.com/XinFinOrg/XDPoSChain/common"
10+
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
1011
)
1112

1213
func Test_getCancelFeeV1(t *testing.T) {
@@ -103,9 +104,9 @@ func Test_getCancelFee(t *testing.T) {
103104
XDCx.SetTokenDecimal(testTokenB, tokenBDecimal)
104105

105106
// set tokenAPrice = 1 XDC
106-
tradingStateDb.SetMediumPriceBeforeEpoch(tradingstate.GetTradingOrderBookHash(testTokenA, common.HexToAddress(common.XDCNativeAddress)), common.BasePrice)
107+
tradingStateDb.SetMediumPriceBeforeEpoch(tradingstate.GetTradingOrderBookHash(testTokenA, common.XDCNativeAddressBinary), common.BasePrice)
107108
// set tokenBPrice = 1 XDC
108-
tradingStateDb.SetMediumPriceBeforeEpoch(tradingstate.GetTradingOrderBookHash(common.HexToAddress(common.XDCNativeAddress), testTokenB), tokenBDecimal)
109+
tradingStateDb.SetMediumPriceBeforeEpoch(tradingstate.GetTradingOrderBookHash(common.XDCNativeAddressBinary, testTokenB), tokenBDecimal)
109110

110111
type CancelFeeArg struct {
111112
feeRate *big.Int
@@ -127,7 +128,7 @@ func Test_getCancelFee(t *testing.T) {
127128
feeRate: common.Big0,
128129
order: &tradingstate.OrderItem{
129130
BaseToken: testTokenA,
130-
QuoteToken: common.HexToAddress(common.XDCNativeAddress),
131+
QuoteToken: common.XDCNativeAddressBinary,
131132
Quantity: new(big.Int).SetUint64(10000),
132133
Side: tradingstate.Ask,
133134
},
@@ -142,7 +143,7 @@ func Test_getCancelFee(t *testing.T) {
142143
feeRate: common.Big0,
143144
order: &tradingstate.OrderItem{
144145
BaseToken: testTokenA,
145-
QuoteToken: common.HexToAddress(common.XDCNativeAddress),
146+
QuoteToken: common.XDCNativeAddressBinary,
146147
Quantity: new(big.Int).SetUint64(10000),
147148
Side: tradingstate.Bid,
148149
},
@@ -156,7 +157,7 @@ func Test_getCancelFee(t *testing.T) {
156157
CancelFeeArg{
157158
feeRate: new(big.Int).SetUint64(10), // 10/10000= 0.1%
158159
order: &tradingstate.OrderItem{
159-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
160+
BaseToken: common.XDCNativeAddressBinary,
160161
QuoteToken: testTokenA,
161162
Quantity: new(big.Int).SetUint64(10000),
162163
Side: tradingstate.Ask,
@@ -172,7 +173,7 @@ func Test_getCancelFee(t *testing.T) {
172173
feeRate: new(big.Int).SetUint64(10), // 10/10000= 0.1%
173174
order: &tradingstate.OrderItem{
174175
Quantity: new(big.Int).SetUint64(10000),
175-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
176+
BaseToken: common.XDCNativeAddressBinary,
176177
QuoteToken: testTokenA,
177178
Side: tradingstate.Bid,
178179
},
@@ -188,7 +189,7 @@ func Test_getCancelFee(t *testing.T) {
188189
CancelFeeArg{
189190
feeRate: common.Big0,
190191
order: &tradingstate.OrderItem{
191-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
192+
BaseToken: common.XDCNativeAddressBinary,
192193
QuoteToken: testTokenA,
193194
Quantity: new(big.Int).SetUint64(10000),
194195
Side: tradingstate.Ask,
@@ -203,7 +204,7 @@ func Test_getCancelFee(t *testing.T) {
203204
CancelFeeArg{
204205
feeRate: common.Big0,
205206
order: &tradingstate.OrderItem{
206-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
207+
BaseToken: common.XDCNativeAddressBinary,
207208
QuoteToken: testTokenA,
208209
Quantity: new(big.Int).SetUint64(10000),
209210
Side: tradingstate.Bid,
@@ -218,7 +219,7 @@ func Test_getCancelFee(t *testing.T) {
218219
CancelFeeArg{
219220
feeRate: new(big.Int).SetUint64(10), // 10/10000= 0.1%
220221
order: &tradingstate.OrderItem{
221-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
222+
BaseToken: common.XDCNativeAddressBinary,
222223
QuoteToken: testTokenA,
223224
Quantity: new(big.Int).SetUint64(10000),
224225
Side: tradingstate.Ask,
@@ -234,7 +235,7 @@ func Test_getCancelFee(t *testing.T) {
234235
feeRate: new(big.Int).SetUint64(10), // 10/10000= 0.1%
235236
order: &tradingstate.OrderItem{
236237
Quantity: new(big.Int).SetUint64(10000),
237-
BaseToken: common.HexToAddress(common.XDCNativeAddress),
238+
BaseToken: common.XDCNativeAddressBinary,
238239
QuoteToken: testTokenA,
239240
Side: tradingstate.Bid,
240241
},

XDCx/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (XDCx *XDCX) GetTokenDecimal(chain consensus.ChainContext, statedb *state.S
4848
if tokenDecimal, ok := XDCx.tokenDecimalCache.Get(tokenAddr); ok {
4949
return tokenDecimal.(*big.Int), nil
5050
}
51-
if tokenAddr.String() == common.XDCNativeAddress {
51+
if tokenAddr == common.XDCNativeAddressBinary {
5252
XDCx.tokenDecimalCache.Add(tokenAddr, common.BasePrice)
5353
return common.BasePrice, nil
5454
}

XDCx/tradingstate/orderitem.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (o *OrderItem) verifyRelayer(state *state.StateDB) error {
239239
return nil
240240
}
241241

242-
//verify signatures
242+
// verify signatures
243243
func (o *OrderItem) verifySignature() error {
244244
bigstr := o.Nonce.String()
245245
n, err := strconv.ParseInt(bigstr, 10, 64)
@@ -269,7 +269,7 @@ func (o *OrderItem) verifyOrderType() error {
269269
return nil
270270
}
271271

272-
//verify order side
272+
// verify order side
273273
func (o *OrderItem) verifyOrderSide() error {
274274

275275
if o.Side != Bid && o.Side != Ask {
@@ -356,11 +356,11 @@ func VerifyPair(statedb *state.StateDB, exchangeAddress, baseToken, quoteToken c
356356

357357
func VerifyBalance(statedb *state.StateDB, XDCxStateDb *TradingStateDB, order *types.OrderTransaction, baseDecimal, quoteDecimal *big.Int) error {
358358
var quotePrice *big.Int
359-
if order.QuoteToken().String() != common.XDCNativeAddress {
360-
quotePrice = XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(order.QuoteToken(), common.HexToAddress(common.XDCNativeAddress)))
359+
if order.QuoteToken() != common.XDCNativeAddressBinary {
360+
quotePrice = XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(order.QuoteToken(), common.XDCNativeAddressBinary))
361361
log.Debug("TryGet quotePrice QuoteToken/XDC", "quotePrice", quotePrice)
362362
if quotePrice == nil || quotePrice.Sign() == 0 {
363-
inversePrice := XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(common.HexToAddress(common.XDCNativeAddress), order.QuoteToken()))
363+
inversePrice := XDCxStateDb.GetLastPrice(GetTradingOrderBookHash(common.XDCNativeAddressBinary, order.QuoteToken()))
364364
log.Debug("TryGet inversePrice XDC/QuoteToken", "inversePrice", inversePrice)
365365
if inversePrice != nil && inversePrice.Sign() > 0 {
366366
quotePrice = new(big.Int).Mul(common.BasePrice, quoteDecimal)

XDCx/tradingstate/relayer_state.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ func CheckRelayerFee(relayer common.Address, fee *big.Int, statedb *state.StateD
159159
}
160160
func AddTokenBalance(addr common.Address, value *big.Int, token common.Address, statedb *state.StateDB) error {
161161
// XDC native
162-
if token.String() == common.XDCNativeAddress {
162+
if token == common.XDCNativeAddressBinary {
163163
balance := statedb.GetBalance(addr)
164-
log.Debug("ApplyXDCXMatchedTransaction settle balance: ADD TOKEN XDC NATIVE BEFORE", "token", token.String(), "address", addr.String(), "balance", balance, "orderValue", value)
164+
log.Debug("ApplyXDCXMatchedTransaction settle balance: ADD TOKEN XDC NATIVE BEFORE", "token", common.XDCNativeAddress, "address", addr.String(), "balance", balance, "orderValue", value)
165165
statedb.AddBalance(addr, value)
166166
balance = statedb.GetBalance(addr)
167167
log.Debug("ApplyXDCXMatchedTransaction settle balance: ADD XDC NATIVE BALANCE AFTER", "token", token.String(), "address", addr.String(), "balance", balance, "orderValue", value)
@@ -186,10 +186,9 @@ func AddTokenBalance(addr common.Address, value *big.Int, token common.Address,
186186

187187
func SubTokenBalance(addr common.Address, value *big.Int, token common.Address, statedb *state.StateDB) error {
188188
// XDC native
189-
if token.String() == common.XDCNativeAddress {
190-
189+
if token == common.XDCNativeAddressBinary {
191190
balance := statedb.GetBalance(addr)
192-
log.Debug("ApplyXDCXMatchedTransaction settle balance: SUB XDC NATIVE BALANCE BEFORE", "token", token.String(), "address", addr.String(), "balance", balance, "orderValue", value)
191+
log.Debug("ApplyXDCXMatchedTransaction settle balance: SUB XDC NATIVE BALANCE BEFORE", "token", common.XDCNativeAddress, "address", addr.String(), "balance", balance, "orderValue", value)
193192
if balance.Cmp(value) < 0 {
194193
return errors.Errorf("value %s in token %s not enough , have : %s , want : %s ", addr.String(), token.String(), balance, value)
195194
}
@@ -219,7 +218,7 @@ func SubTokenBalance(addr common.Address, value *big.Int, token common.Address,
219218

220219
func CheckSubTokenBalance(addr common.Address, value *big.Int, token common.Address, statedb *state.StateDB, mapBalances map[common.Address]map[common.Address]*big.Int) (*big.Int, error) {
221220
// XDC native
222-
if token.String() == common.XDCNativeAddress {
221+
if token == common.XDCNativeAddressBinary {
223222
var balance *big.Int
224223
if value := mapBalances[token][addr]; value != nil {
225224
balance = value
@@ -256,7 +255,7 @@ func CheckSubTokenBalance(addr common.Address, value *big.Int, token common.Addr
256255

257256
func CheckAddTokenBalance(addr common.Address, value *big.Int, token common.Address, statedb *state.StateDB, mapBalances map[common.Address]map[common.Address]*big.Int) (*big.Int, error) {
258257
// XDC native
259-
if token.String() == common.XDCNativeAddress {
258+
if token == common.XDCNativeAddressBinary {
260259
var balance *big.Int
261260
if value := mapBalances[token][addr]; value != nil {
262261
balance = value
@@ -308,7 +307,7 @@ func CheckSubRelayerFee(relayer common.Address, fee *big.Int, statedb *state.Sta
308307

309308
func GetTokenBalance(addr common.Address, token common.Address, statedb *state.StateDB) *big.Int {
310309
// XDC native
311-
if token.String() == common.XDCNativeAddress {
310+
if token == common.XDCNativeAddressBinary {
312311
return statedb.GetBalance(addr)
313312
}
314313
// TRC tokens
@@ -323,7 +322,7 @@ func GetTokenBalance(addr common.Address, token common.Address, statedb *state.S
323322

324323
func SetTokenBalance(addr common.Address, balance *big.Int, token common.Address, statedb *state.StateDB) error {
325324
// XDC native
326-
if token.String() == common.XDCNativeAddress {
325+
if token == common.XDCNativeAddressBinary {
327326
statedb.SetBalance(addr, balance)
328327
return nil
329328
}

XDCx/tradingstate/settle_balance.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
5252
log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "makerFee", makerFee, "defaultFee", defaultFee)
5353
return result, ErrQuantityTradeTooSmall
5454
}
55-
if quoteToken.String() != common.XDCNativeAddress && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 {
55+
if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 {
5656
// defaultFeeInXDC
5757
defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice)
5858
defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal)
@@ -69,7 +69,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
6969
log.Debug("takerFee too small", "quoteTokenQuantity", quoteTokenQuantity, "takerFee", takerFee, "exTakerReceivedFee", exTakerReceivedFee, "quotePrice", quotePrice, "defaultFeeInXDC", defaultFeeInXDC)
7070
return result, ErrQuantityTradeTooSmall
7171
}
72-
} else if quoteToken.String() == common.XDCNativeAddress {
72+
} else if quoteToken == common.XDCNativeAddressBinary {
7373
exMakerReceivedFee := makerFee
7474
if (exMakerReceivedFee.Cmp(common.RelayerFee) <= 0 && exMakerReceivedFee.Sign() > 0) || defaultFee.Cmp(common.RelayerFee) <= 0 {
7575
log.Debug("makerFee too small", "quantityToTrade", quantityToTrade, "makerFee", makerFee, "exMakerReceivedFee", exMakerReceivedFee, "makerFeeRate", makerFeeRate, "defaultFee", defaultFee)
@@ -108,7 +108,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
108108
log.Debug("quantity trade too small", "quoteTokenQuantity", quoteTokenQuantity, "takerFee", takerFee)
109109
return result, ErrQuantityTradeTooSmall
110110
}
111-
if quoteToken.String() != common.XDCNativeAddress && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 {
111+
if quoteToken != common.XDCNativeAddressBinary && quotePrice != nil && quotePrice.Cmp(common.Big0) > 0 {
112112
// defaultFeeInXDC
113113
defaultFeeInXDC := new(big.Int).Mul(defaultFee, quotePrice)
114114
defaultFeeInXDC = new(big.Int).Div(defaultFeeInXDC, quoteTokenDecimal)
@@ -126,7 +126,7 @@ func GetSettleBalance(quotePrice *big.Int, takerSide string, takerFeeRate *big.I
126126
log.Debug("takerFee too small", "quoteTokenQuantity", quoteTokenQuantity, "takerFee", takerFee, "exTakerReceivedFee", exTakerReceivedFee, "quotePrice", quotePrice, "defaultFeeInXDC", defaultFeeInXDC)
127127
return result, ErrQuantityTradeTooSmall
128128
}
129-
} else if quoteToken.String() == common.XDCNativeAddress {
129+
} else if quoteToken == common.XDCNativeAddressBinary {
130130
exMakerReceivedFee := makerFee
131131
if (exMakerReceivedFee.Cmp(common.RelayerFee) <= 0 && exMakerReceivedFee.Sign() > 0) || defaultFee.Cmp(common.RelayerFee) <= 0 {
132132
log.Debug("makerFee too small", "quantityToTrade", quantityToTrade, "makerFee", makerFee, "exMakerReceivedFee", exMakerReceivedFee, "makerFeeRate", makerFeeRate, "defaultFee", defaultFee)

0 commit comments

Comments
 (0)