Commit 9868e5ed authored by harrylee's avatar harrylee Committed by vipwzw

fix a bug for precision

parent faf9a573
...@@ -463,14 +463,19 @@ func signTx(tx *types.Transaction, hexPrivKey string) (*types.Transaction, error ...@@ -463,14 +463,19 @@ func signTx(tx *types.Transaction, hexPrivKey string) (*types.Transaction, error
} }
func TestTruncate(t *testing.T) { func TestTruncate(t *testing.T) {
a := float32(1.00000212000000000001) a := float64(1.00000212000000000001)
b := float32(0.34567) b := float64(0.34567)
c := float32(1234) c := float64(1234567)
t.Log(Truncate(a)) t.Log(Truncate(a))
t.Log(Truncate(b)) t.Log(Truncate(b))
t.Log(Truncate(c)) t.Log(Truncate(c))
t.Log(float64(1.00000212000000000001))
t.Logf("%f", Truncate(float64(1e8)))
t.Log(Truncate(float64(1e-8)))
} }
func TestCheckPrice(t *testing.T) { func TestCheckPrice(t *testing.T) {
t.Log(CheckPrice(0.25)) t.Log(CheckPrice(Truncate(float64(1e8))))
t.Log(CheckPrice(Truncate(float64(1e-8))))
t.Log(CheckPrice(Truncate(float64(1e-9))))
} }
...@@ -60,16 +60,16 @@ func (a *Action) OpSwap(op int32) int32 { ...@@ -60,16 +60,16 @@ func (a *Action) OpSwap(op int32) int32 {
} }
//计算实际花费 //计算实际花费
func (a *Action) calcActualCost(op int32, amount int64, price float32) int64 { func (a *Action) calcActualCost(op int32, amount int64, price float64) int64 {
if op == et.OpBuy { if op == et.OpBuy {
return int64(float32(amount) * Truncate(price)) return int64(float64(amount) * Truncate(price))
} }
return amount return amount
} }
//price 精度允许范围小数点后面7位数,0<price<1e8 //price 精度允许范围小数点后面8位数,0<price<1e8
func CheckPrice(price float32) bool { func CheckPrice(price float64) bool {
if (Truncate(price) >= 1e8) || (Truncate(price)*float32(1e8) <= 0) { if (Truncate(price) >= 1e8) || (Truncate(price)*float64(1e8) < 1) {
return false return false
} }
return true return true
...@@ -138,7 +138,7 @@ func (a *Action) LimitOrder(payload *et.LimitOrder) (*types.Receipt, error) { ...@@ -138,7 +138,7 @@ func (a *Action) LimitOrder(payload *et.LimitOrder) (*types.Receipt, error) {
} }
//先检查账户余额 //先检查账户余额
if payload.GetOp() == et.OpBuy { if payload.GetOp() == et.OpBuy {
amount := int64(float32(payload.GetAmount()) * Truncate(payload.GetPrice())) amount := int64(float64(payload.GetAmount()) * Truncate(payload.GetPrice()))
rightAccount := rightAssetDB.LoadExecAccount(a.fromaddr, a.execaddr) rightAccount := rightAssetDB.LoadExecAccount(a.fromaddr, a.execaddr)
if rightAccount.Balance < amount { if rightAccount.Balance < amount {
elog.Error("LimitOrder.BalanceCheck", "addr", a.fromaddr, "execaddr", a.execaddr, "amount", amount, "err", et.ErrAssetBalance.Error()) elog.Error("LimitOrder.BalanceCheck", "addr", a.fromaddr, "execaddr", a.execaddr, "amount", amount, "err", et.ErrAssetBalance.Error())
...@@ -445,7 +445,7 @@ func findOrderByOrderID(statedb dbm.KV, orderID string) (*et.Order, error) { ...@@ -445,7 +445,7 @@ func findOrderByOrderID(statedb dbm.KV, orderID string) (*et.Order, error) {
return &order, nil return &order, nil
} }
func findOrderIDListByPrice(localdb dbm.Lister, left, right *et.Asset, price float32, op, direction int32, index int64) ([]*et.OrderID, error) { func findOrderIDListByPrice(localdb dbm.Lister, left, right *et.Asset, price float64, op, direction int32, index int64) ([]*et.OrderID, error) {
prefix := calcMarketDepthOrderPrefix(left, right, op, price) prefix := calcMarketDepthOrderPrefix(left, right, op, price)
key := calcMarketDepthOrderKey(left, right, op, price, index) key := calcMarketDepthOrderKey(left, right, op, price, index)
var values [][]byte var values [][]byte
...@@ -490,7 +490,7 @@ func Direction(op int32) int32 { ...@@ -490,7 +490,7 @@ func Direction(op int32) int32 {
} }
//这里price当作索引来用,首次查询不需要填值 //这里price当作索引来用,首次查询不需要填值
func QueryMarketDepth(localdb dbm.Lister, left, right *et.Asset, op int32, price float32, count int32) (types.Message, error) { func QueryMarketDepth(localdb dbm.Lister, left, right *et.Asset, op int32, price float64, count int32) (types.Message, error) {
prefix := calcMarketDepthPrefix(left, right, op) prefix := calcMarketDepthPrefix(left, right, op)
key := calcMarketDepthKey(left, right, op, price) key := calcMarketDepthKey(left, right, op, price)
var values [][]byte var values [][]byte
...@@ -592,6 +592,6 @@ func QueryOrderList(localdb dbm.Lister, statedb dbm.KV, addr string, status, cou ...@@ -592,6 +592,6 @@ func QueryOrderList(localdb dbm.Lister, statedb dbm.KV, addr string, status, cou
} }
//截取小数点后7位 //截取小数点后7位
func Truncate(price float32) float32 { func Truncate(price float64) float64 {
return float32(math.Trunc(float64(1e8)*float64(price)) / float64(1e8)) return math.Trunc(float64(1e8)*float64(price)) / float64(1e8)
} }
...@@ -30,22 +30,22 @@ func calcMarketDepthPrefix(left, right *types.Asset, op int32) []byte { ...@@ -30,22 +30,22 @@ func calcMarketDepthPrefix(left, right *types.Asset, op int32) []byte {
} }
//市场深度 //市场深度
func calcMarketDepthKey(left, right *types.Asset, op int32, price float32) []byte { func calcMarketDepthKey(left, right *types.Asset, op int32, price float64) []byte {
// 设置精度为1e8 // 设置精度为1e8
key := fmt.Sprintf("%s"+"depth-%s-%s-%d:%016d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float32(1e8))) key := fmt.Sprintf("%s"+"depth-%s-%s-%d:%016d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float64(1e8)))
return []byte(key) return []byte(key)
} }
func calcMarketDepthOrderPrefix(left, right *types.Asset, op int32, price float32) []byte { func calcMarketDepthOrderPrefix(left, right *types.Asset, op int32, price float64) []byte {
// 设置精度为1e8 // 设置精度为1e8
key := fmt.Sprintf("%s"+"order-%s-%s-%d:%016d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float32(1e8))) key := fmt.Sprintf("%s"+"order-%s-%s-%d:%016d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float64(1e8)))
return []byte(key) return []byte(key)
} }
// localdb中存储市场挂单ID // localdb中存储市场挂单ID
func calcMarketDepthOrderKey(left, right *types.Asset, op int32, price float32, index int64) []byte { func calcMarketDepthOrderKey(left, right *types.Asset, op int32, price float64, index int64) []byte {
// 设置精度为1e8 // 设置精度为1e8
key := fmt.Sprintf("%s"+"order-%s-%s-%d:%016d:%022d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float32(1e8)), index) key := fmt.Sprintf("%s"+"order-%s-%s-%d:%016d:%022d", KeyPrefixLocalDB, left.GetSymbol(), right.GetSymbol(), op, int64(Truncate(price)*float64(1e8)), index)
return []byte(key) return []byte(key)
} }
......
...@@ -19,7 +19,7 @@ message LimitOrder { ...@@ -19,7 +19,7 @@ message LimitOrder {
//交易对 //交易对
asset rightAsset = 2; asset rightAsset = 2;
//价格 //价格
float price = 3; double price = 3;
//总量 //总量
int64 amount = 4; int64 amount = 4;
//操作, 1为买,2为卖 //操作, 1为买,2为卖
...@@ -74,7 +74,7 @@ message Order { ...@@ -74,7 +74,7 @@ message Order {
//挂单价 //挂单价
message OrderPrice { message OrderPrice {
float price = 1; double price = 1;
int64 index = 2; int64 index = 2;
} }
//单号 //单号
...@@ -91,7 +91,7 @@ message QueryMarketDepth { ...@@ -91,7 +91,7 @@ message QueryMarketDepth {
//操作, 1为买,2为卖 //操作, 1为买,2为卖
int32 op = 3; int32 op = 3;
// 这里用价格作为索引值 // 这里用价格作为索引值
float price = 4; double price = 4;
//单页返回多少条记录,默认返回10条,为了系统安全最多单次只能返回20条 //单页返回多少条记录,默认返回10条,为了系统安全最多单次只能返回20条
int32 count = 5; int32 count = 5;
} }
...@@ -102,7 +102,7 @@ message MarketDepth { ...@@ -102,7 +102,7 @@ message MarketDepth {
//资产2 //资产2
asset rightAsset = 2; asset rightAsset = 2;
//价格 //价格
float price = 3; double price = 3;
//总量 //总量
int64 amount = 4; int64 amount = 4;
//操作, 1为买,2为卖 //操作, 1为买,2为卖
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment