-
Notifications
You must be signed in to change notification settings - Fork 41
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
BEP70 - Support busd pair listing and trading #710
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
57b3caa
add busd pair support
fc1ffd4
support busd pairs for listing and trading
ddbeca3
add more test cases
8ed1e06
change SupportedListSymbols[] to single BUSDSymbol
0e46d38
extract methods; update BEP number
018598e
refactor
65ae54a
refactor keeper
53e2c6e
busd fee refactor
rickyyangz 7b9e938
Merge pull request #722 from binance-chain/busd
forcodedancing ba142c2
add check
c1fa20a
Merge branch 'develop' into support_busd
forcodedancing 72f09d9
order and group upgrade config
5d9010b
order and group upgrade config
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ import ( | |
"math" | ||
"math/big" | ||
|
||
"github.com/binance-chain/node/common/upgrade" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
tmlog "github.com/tendermint/tendermint/libs/log" | ||
|
@@ -42,16 +44,18 @@ var ( | |
) | ||
|
||
type FeeManager struct { | ||
cdc *wire.Codec | ||
logger tmlog.Logger | ||
FeeConfig FeeConfig | ||
cdc *wire.Codec | ||
logger tmlog.Logger | ||
FeeConfig FeeConfig | ||
BUSDSymbol string | ||
} | ||
|
||
func NewFeeManager(cdc *wire.Codec, storeKey sdk.StoreKey, logger tmlog.Logger) *FeeManager { | ||
func NewFeeManager(cdc *wire.Codec, storeKey sdk.StoreKey, busdSymbol string, logger tmlog.Logger) *FeeManager { | ||
return &FeeManager{ | ||
cdc: cdc, | ||
logger: logger, | ||
FeeConfig: NewFeeConfig(), | ||
cdc: cdc, | ||
logger: logger, | ||
FeeConfig: NewFeeConfig(), | ||
BUSDSymbol: busdSymbol, | ||
} | ||
} | ||
|
||
|
@@ -134,7 +138,7 @@ func (m *FeeManager) calcTradeFeeForSingleTransfer(balances sdk.Coins, tran *Tra | |
} | ||
|
||
func (m *FeeManager) calcNativeFee(inSymbol string, inQty int64, engines map[string]*matcheng.MatchEng) (fee int64, isOverflow bool) { | ||
var nativeNotional *big.Int | ||
var nativeNotional = big.NewInt(0) | ||
if isNativeToken(inSymbol) { | ||
nativeNotional = big.NewInt(inQty) | ||
} else { | ||
|
@@ -143,15 +147,44 @@ func (m *FeeManager) calcNativeFee(inSymbol string, inQty int64, engines map[str | |
if engine, ok := engines[utils.Assets2TradingPair(inSymbol, types.NativeTokenSymbol)]; ok { | ||
// XYZ_BNB | ||
nativeNotional = utils.CalBigNotional(engine.LastTradePrice, inQty) | ||
} else { | ||
} else if engine, ok := engines[utils.Assets2TradingPair(types.NativeTokenSymbol, inSymbol)]; ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you help extract such code into a method, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
// BNB_XYZ | ||
engine := engines[utils.Assets2TradingPair(types.NativeTokenSymbol, inSymbol)] | ||
var amount big.Int | ||
nativeNotional = amount.Div( | ||
amount.Mul( | ||
big.NewInt(inQty), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(engine.LastTradePrice)) | ||
} else { | ||
// for BUSD pairs, it is possible that there is no trading pair between BNB and inAsset, e.g., BUSD -> XYZ | ||
if sdk.IsUpgrade(upgrade.BEP_BUSD) { | ||
var intermediateAmount = big.NewInt(0) | ||
if market, ok := engines[utils.Assets2TradingPair(m.BUSDSymbol, inSymbol)]; ok { | ||
var tmp big.Int | ||
intermediateAmount = tmp.Div(tmp.Mul( | ||
big.NewInt(inQty), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(market.LastTradePrice)) | ||
} else if market, ok := engines[utils.Assets2TradingPair(inSymbol, m.BUSDSymbol)]; ok { | ||
intermediateAmount = utils.CalBigNotional(market.LastTradePrice, inQty) | ||
} | ||
|
||
var intermediateAmountTmp int64 | ||
if intermediateAmount.IsInt64() { | ||
intermediateAmountTmp = intermediateAmount.Int64() | ||
} else { | ||
intermediateAmountTmp = math.MaxInt64 | ||
} | ||
if market, ok := engines[utils.Assets2TradingPair(types.NativeTokenSymbol, m.BUSDSymbol)]; ok { | ||
var tmp big.Int | ||
nativeNotional = tmp.Div(tmp.Mul( | ||
big.NewInt(intermediateAmountTmp), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(market.LastTradePrice)) | ||
} else if market, ok := engines[utils.Assets2TradingPair(m.BUSDSymbol, types.NativeTokenSymbol)]; ok { | ||
nativeNotional = utils.CalBigNotional(market.LastTradePrice, intermediateAmountTmp) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -238,18 +271,47 @@ func (m *FeeManager) CalcFixedFee(balances sdk.Coins, eventType transferEventTyp | |
} else { | ||
// the amount may overflow int64, so use big.Int instead. | ||
// TODO: (perf) may remove the big.Int use to improve the performance | ||
var amount *big.Int | ||
var amount = big.NewInt(0) | ||
if market, ok := engines[utils.Assets2TradingPair(inAsset, types.NativeTokenSymbol)]; ok { | ||
// XYZ_BNB | ||
var tmp big.Int | ||
amount = tmp.Div(tmp.Mul( | ||
big.NewInt(feeAmount), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(market.LastTradePrice)) | ||
} else { | ||
} else if market, ok := engines[utils.Assets2TradingPair(types.NativeTokenSymbol, inAsset)]; ok { | ||
// BNB_XYZ | ||
market = engines[utils.Assets2TradingPair(types.NativeTokenSymbol, inAsset)] | ||
amount = utils.CalBigNotional(market.LastTradePrice, feeAmount) | ||
} else { | ||
// for BUSD pairs, it is possible that there is no trading pair between BNB and inAsset, e.g., BUSD -> XYZ | ||
if sdk.IsUpgrade(upgrade.BEP_BUSD) { | ||
var intermediateAmount = big.NewInt(0) | ||
if market, ok := engines[utils.Assets2TradingPair(m.BUSDSymbol, types.NativeTokenSymbol)]; ok { | ||
var tmp big.Int | ||
intermediateAmount = tmp.Div(tmp.Mul( | ||
big.NewInt(feeAmount), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(market.LastTradePrice)) | ||
} else if market, ok := engines[utils.Assets2TradingPair(types.NativeTokenSymbol, m.BUSDSymbol)]; ok { | ||
intermediateAmount = utils.CalBigNotional(market.LastTradePrice, feeAmount) | ||
} | ||
|
||
var intermediateAmountTmp int64 | ||
if intermediateAmount.IsInt64() { | ||
intermediateAmountTmp = intermediateAmount.Int64() | ||
} else { | ||
intermediateAmountTmp = math.MaxInt64 | ||
} | ||
if market, ok := engines[utils.Assets2TradingPair(inAsset, m.BUSDSymbol)]; ok { | ||
var tmp big.Int | ||
amount = tmp.Div(tmp.Mul( | ||
big.NewInt(intermediateAmountTmp), | ||
big.NewInt(cmnUtils.Fixed8One.ToInt64())), | ||
big.NewInt(market.LastTradePrice)) | ||
} else if market, ok := engines[utils.Assets2TradingPair(m.BUSDSymbol, inAsset)]; ok { | ||
amount = utils.CalBigNotional(market.LastTradePrice, intermediateAmountTmp) | ||
} | ||
} | ||
} | ||
|
||
if amount.IsInt64() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please follow the naming convension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will rename it after we have the BEP finalized. Or, can we have the BEP NO. now?