Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 17, 2023
1 parent 7f068cd commit 2017a73
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 103 deletions.
46 changes: 16 additions & 30 deletions cmd/haobase/assets/freeze_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assets
import (
"fmt"

"github.com/yzimhao/trading_engine/utils"
"xorm.io/xorm"
)

Expand All @@ -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}
Expand All @@ -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
}
Expand Down
42 changes: 0 additions & 42 deletions cmd/haobase/assets/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package assets

import (
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"github.com/yzimhao/trading_engine/utils/app"
)
Expand Down Expand Up @@ -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")
Expand Down
21 changes: 11 additions & 10 deletions cmd/haobase/assets/transfer_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/yzimhao/trading_engine/utils"
"github.com/yzimhao/trading_engine/utils/app"
"xorm.io/xorm"
)
Expand Down Expand Up @@ -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("可用资产不足")
}
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand Down
33 changes: 18 additions & 15 deletions cmd/haobase/assets/unfreeze_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assets
import (
"fmt"

"github.com/yzimhao/trading_engine/utils"
"xorm.io/xorm"
)

Expand All @@ -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}
Expand All @@ -27,49 +29,50 @@ 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
}

//解冻资产为可用
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
}
4 changes: 2 additions & 2 deletions cmd/haobase/orders/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/haobase/orders/market_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 2017a73

Please sign in to comment.