From 2017a73f0b1d96fff6cf9c0102eda8847a3d89e2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 17 Oct 2023 17:11:29 +0800 Subject: [PATCH] update --- cmd/haobase/assets/freeze_assets.go | 46 ++++++++++----------------- cmd/haobase/assets/init.go | 42 ------------------------ cmd/haobase/assets/transfer_assets.go | 21 ++++++------ cmd/haobase/assets/unfreeze_assets.go | 33 ++++++++++--------- cmd/haobase/orders/limit_order.go | 4 +-- cmd/haobase/orders/market_order.go | 8 ++--- 6 files changed, 51 insertions(+), 103 deletions(-) diff --git a/cmd/haobase/assets/freeze_assets.go b/cmd/haobase/assets/freeze_assets.go index 5a6a1068..3d8abde3 100644 --- a/cmd/haobase/assets/freeze_assets.go +++ b/cmd/haobase/assets/freeze_assets.go @@ -3,6 +3,7 @@ package assets import ( "fmt" + "github.com/yzimhao/trading_engine/utils" "xorm.io/xorm" ) @@ -14,35 +15,24 @@ func QueryFreeze(db *xorm.Session, business_id string) (*assetsFreeze, error) { } if !has { - return nil, fmt.Errorf("failed to query frozen records") + return nil, fmt.Errorf("冻结记录不存在") } return &row, nil } -func FreezeAssets(db *xorm.Session, enable_transaction bool, user_id string, symbol string, freeze_amount, business_id string, behavior OpBehavior) (success bool, err error) { - return freezeAssets(db, enable_transaction, user_id, symbol, freeze_amount, business_id, behavior) +func FreezeAssets(db *xorm.Session, user_id string, symbol string, freeze_amount, business_id string, behavior OpBehavior) (success bool, err error) { + return freezeAssets(db, user_id, symbol, freeze_amount, business_id, behavior) } -func FreezeTotalAssets(db *xorm.Session, enable_transaction bool, user_id string, symbol string, business_id string, behavior OpBehavior) (success bool, err error) { - return freezeAssets(db, enable_transaction, user_id, symbol, "0", business_id, behavior) +func FreezeTotalAssets(db *xorm.Session, user_id string, symbol string, business_id string, behavior OpBehavior) (success bool, err error) { + return freezeAssets(db, user_id, symbol, "0", business_id, behavior) } -func freezeAssets(db *xorm.Session, enable_transaction bool, user_id string, symbol string, freeze_amount, business_id string, behavior OpBehavior) (success bool, err error) { +func freezeAssets(db *xorm.Session, user_id string, symbol string, freeze_amount, business_id string, behavior OpBehavior) (success bool, err error) { - if check_number_lt_zero(freeze_amount) { - return false, fmt.Errorf("freeze amount should be >= 0") - } - - if enable_transaction { - db.Begin() - defer func() { - if err != nil { - db.Rollback() - } else { - db.Commit() - } - }() + if utils.D(freeze_amount).Cmp(utils.D("0")) < 0 { + return false, fmt.Errorf("冻结金额必须大于等于0") } item := Assets{UserId: user_id, Symbol: symbol} @@ -51,23 +41,19 @@ func freezeAssets(db *xorm.Session, enable_transaction bool, user_id string, sym return false, err } - if d(freeze_amount).Equal(d("0")) { - freeze_amount = d(item.Available).String() + //冻结金额为0,冻结全部可用 + if utils.D(freeze_amount).Equal(utils.D("0")) { + freeze_amount = utils.D(item.Available).String() } - item.Available = number_sub(item.Available, freeze_amount) - item.Freeze = number_add(item.Freeze, freeze_amount) + item.Available = utils.D(item.Available).Sub(utils.D(freeze_amount)).String() + item.Freeze = utils.D(item.Freeze).Add(utils.D(freeze_amount)).String() - if check_number_lt_zero(item.Available) { - return false, fmt.Errorf("available balance less than zero") - } - - if check_number_lt_zero(item.Freeze) { - return false, fmt.Errorf("freeze balance less than zero") + if utils.D(item.Available).Cmp(utils.D("0")) < 0 { + return false, fmt.Errorf("冻结金额超出可用资金") } _, err = db.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, symbol).AllCols().Update(&item) - if err != nil { return false, err } diff --git a/cmd/haobase/assets/init.go b/cmd/haobase/assets/init.go index a04fbbb7..57a8954c 100644 --- a/cmd/haobase/assets/init.go +++ b/cmd/haobase/assets/init.go @@ -1,7 +1,6 @@ package assets import ( - "github.com/shopspring/decimal" "github.com/sirupsen/logrus" "github.com/yzimhao/trading_engine/utils/app" ) @@ -41,47 +40,6 @@ func UserAssets(user_id string, symbol []string) []Assets { return rows } -func d(s string) decimal.Decimal { - ss, _ := decimal.NewFromString(s) - return ss -} - -func number_add(s1, s2 string) string { - return d(s1).Add(d(s2)).String() -} - -func number_sub(s1, s2 string) string { - return d(s1).Sub(d(s2)).String() -} - -func check_number_lt_zero(s string) bool { - if d(s).Cmp(decimal.Zero) < 0 { - return true - } else { - return false - } -} - -func check_number_gt_zero(s string) bool { - if d(s).Cmp(decimal.Zero) > 0 { - return true - } else { - return false - } -} - -func check_number_eq_zero(s string) bool { - if d(s).Cmp(decimal.Zero) == 0 { - return true - } else { - return false - } -} - -func number(num string) string { - return d(num).String() -} - func DemoData() { SysRecharge("user1", "usd", "10000.00", "C001") SysRecharge("user1", "jpy", "10000.00", "C002") diff --git a/cmd/haobase/assets/transfer_assets.go b/cmd/haobase/assets/transfer_assets.go index d1f45be3..68258782 100644 --- a/cmd/haobase/assets/transfer_assets.go +++ b/cmd/haobase/assets/transfer_assets.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/yzimhao/trading_engine/utils" "github.com/yzimhao/trading_engine/utils/app" "xorm.io/xorm" ) @@ -39,8 +40,8 @@ func transfer(db *xorm.Session, from, to string, symbol string, amount string, b } //非根账户检查余额 if from != UserRoot { - if check_number_lt_zero(from_user.Available) { - return false, fmt.Errorf("available balance less than zero") + if utils.D(from_user.Available).Cmp(utils.D("0")) <= 0 { + return false, fmt.Errorf("可用资产不足") } } @@ -49,9 +50,9 @@ func transfer(db *xorm.Session, from, to string, symbol string, amount string, b if err != nil { return false, err } - from_before := number(from_user.Total) - from_user.Total = number_sub(from_user.Total, amount) - from_user.Available = number_sub(from_user.Available, amount) + from_before := utils.D(from_user.Total) + from_user.Total = utils.D(from_user.Total).Sub(utils.D(amount)).String() + from_user.Available = utils.D(from_user.Available).Sub(utils.D(amount)).String() if !has_from { from_user.Freeze = "0" _, err = db.Table(new(Assets)).Insert(&from_user) @@ -62,9 +63,9 @@ func transfer(db *xorm.Session, from, to string, symbol string, amount string, b return false, err } - to_before := number(to_user.Total) - to_user.Total = number_add(to_user.Total, amount) - to_user.Available = number_add(to_user.Available, amount) + to_before := utils.D(to_user.Total) + to_user.Total = utils.D(to_user.Total).Add(utils.D(amount)).String() + to_user.Available = utils.D(to_user.Available).Add(utils.D(amount)).String() if !has_to { to_user.Freeze = "0" _, err = db.Table(new(Assets)).Insert(&to_user) @@ -79,7 +80,7 @@ func transfer(db *xorm.Session, from, to string, symbol string, amount string, b from_log := assetsLog{ UserId: from, Symbol: symbol, - Before: from_before, + Before: from_before.String(), Amount: "-" + amount, After: from_user.Total, BusinessId: business_id, @@ -94,7 +95,7 @@ func transfer(db *xorm.Session, from, to string, symbol string, amount string, b to_log := assetsLog{ UserId: to, Symbol: symbol, - Before: to_before, + Before: to_before.String(), Amount: amount, After: to_user.Total, BusinessId: business_id, diff --git a/cmd/haobase/assets/unfreeze_assets.go b/cmd/haobase/assets/unfreeze_assets.go index 5ae5392b..9e999986 100644 --- a/cmd/haobase/assets/unfreeze_assets.go +++ b/cmd/haobase/assets/unfreeze_assets.go @@ -3,6 +3,7 @@ package assets import ( "fmt" + "github.com/yzimhao/trading_engine/utils" "xorm.io/xorm" ) @@ -15,8 +16,9 @@ func UnfreezeAllAssets(db *xorm.Session, user_id string, business_id string) (su } func unfreezeAssets(db *xorm.Session, user_id string, business_id, unfreeze_amount string) (success bool, err error) { - if check_number_lt_zero(unfreeze_amount) { - return false, fmt.Errorf("unfreeze amount should be >= 0") + + if utils.D(unfreeze_amount).Cmp(utils.D("0")) >= 0 { + return false, fmt.Errorf("解冻金额必须大于等于0") } row := assetsFreeze{UserId: user_id, BusinessId: business_id} @@ -27,27 +29,29 @@ func unfreezeAssets(db *xorm.Session, user_id string, business_id, unfreeze_amou } if !has { - return false, fmt.Errorf("not found business_id") + return false, fmt.Errorf("未找到冻结记录") } if row.Status == FreezeStatusDone { - return false, fmt.Errorf("repeat unfreeze") + return false, fmt.Errorf("冻结记录已经解冻") } - if d(unfreeze_amount).Equal(d("0")) { + //解冻金额为0,则解冻全部 + if utils.D(unfreeze_amount).Equal(utils.D("0")) { unfreeze_amount = row.FreezeAmount } - row.FreezeAmount = number_sub(row.FreezeAmount, unfreeze_amount) + freeze_amount := utils.D(row.FreezeAmount).Sub(utils.D(unfreeze_amount)) - if check_number_lt_zero(row.FreezeAmount) { - return false, fmt.Errorf("unfreeze amount must lt freeze amount") + if freeze_amount.Cmp(utils.D("0")) < 0 { + return false, fmt.Errorf("数据错误,解冻后金额为负数") } - if check_number_eq_zero(row.FreezeAmount) { + if freeze_amount.Equal(utils.D("0")) { row.Status = FreezeStatusDone } + row.FreezeAmount = freeze_amount.String() _, err = db.Table(new(assetsFreeze)).Where("business_id=?", business_id).AllCols().Update(&row) if err != nil { return false, err @@ -55,21 +59,20 @@ func unfreezeAssets(db *xorm.Session, user_id string, business_id, unfreeze_amou //解冻资产为可用 assets := Assets{UserId: user_id, Symbol: row.Symbol} - _, err = db.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, row.Symbol).ForUpdate().Get(&assets) + _, err = db.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, row.Symbol).Get(&assets) if err != nil { return false, err } - assets.Available = number_add(assets.Available, unfreeze_amount) - assets.Freeze = number_sub(assets.Freeze, unfreeze_amount) + assets.Available = utils.D(assets.Available).Add(utils.D(unfreeze_amount)).String() + assets.Freeze = utils.D(assets.Freeze).Sub(utils.D(unfreeze_amount)).String() - if check_number_lt_zero(assets.Freeze) { - return false, fmt.Errorf("freeze amount some wrong") + if utils.D(assets.Freeze).Cmp(utils.D("0")) < 0 { + return false, fmt.Errorf("数据出错,冻结金额为负数") } _, err = db.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, row.Symbol).Update(&assets) if err != nil { return false, err } - return true, nil } diff --git a/cmd/haobase/orders/limit_order.go b/cmd/haobase/orders/limit_order.go index 5e033d43..edde6f58 100644 --- a/cmd/haobase/orders/limit_order.go +++ b/cmd/haobase/orders/limit_order.go @@ -58,7 +58,7 @@ func limit_order(user_id string, symbol string, side trading_core.OrderSide, pri //冻结相应资产 if neworder.OrderSide == trading_core.OrderSideSell { //卖单部分fee在订单成交后结算的部分收取 - _, err = assets.FreezeAssets(db, false, user_id, varieties.Target.Symbol, qty, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeAssets(db, user_id, varieties.Target.Symbol, qty, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err } @@ -71,7 +71,7 @@ func limit_order(user_id string, symbol string, side trading_core.OrderSide, pri //fee、tradeamount字段在结算程序中修改 neworder.FreezeQty = freeze_amount - _, err = assets.FreezeAssets(db, false, user_id, varieties.Base.Symbol, freeze_amount, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeAssets(db, user_id, varieties.Base.Symbol, freeze_amount, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err } diff --git a/cmd/haobase/orders/market_order.go b/cmd/haobase/orders/market_order.go index 272d38d7..dfa0f90b 100644 --- a/cmd/haobase/orders/market_order.go +++ b/cmd/haobase/orders/market_order.go @@ -53,14 +53,14 @@ func market_order_qty(user_id string, symbol string, side trading_core.OrderSide //冻结资产 if neworder.OrderSide == trading_core.OrderSideSell { - _, err = assets.FreezeAssets(db, false, user_id, varieties.Target.Symbol, qty, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeAssets(db, user_id, varieties.Target.Symbol, qty, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err } neworder.FreezeQty = qty } else if neworder.OrderSide == trading_core.OrderSideBuy { //冻结所有可用 - _, err = assets.FreezeTotalAssets(db, false, user_id, varieties.Base.Symbol, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeTotalAssets(db, user_id, varieties.Base.Symbol, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err } @@ -138,7 +138,7 @@ func market_order_amount(user_id string, symbol string, side trading_core.OrderS }() if neworder.OrderSide == trading_core.OrderSideSell { - _, err = assets.FreezeTotalAssets(db, false, user_id, varieties.Target.Symbol, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeTotalAssets(db, user_id, varieties.Target.Symbol, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err } @@ -150,7 +150,7 @@ func market_order_amount(user_id string, symbol string, side trading_core.OrderS neworder.FreezeQty = freeze.FreezeAmount } else if neworder.OrderSide == trading_core.OrderSideBuy { - _, err = assets.FreezeAssets(db, false, user_id, varieties.Base.Symbol, amount, neworder.OrderId, assets.Behavior_Trade) + _, err = assets.FreezeAssets(db, user_id, varieties.Base.Symbol, amount, neworder.OrderId, assets.Behavior_Trade) if err != nil { return nil, err }