Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
9868e5ed
Commit
9868e5ed
authored
Dec 12, 2019
by
harrylee
Committed by
vipwzw
Dec 26, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix a bug for precision
parent
faf9a573
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
24 deletions
+29
-24
exchange_test.go
plugin/dapp/exchange/executor/exchange_test.go
+9
-4
exchangedb.go
plugin/dapp/exchange/executor/exchangedb.go
+10
-10
kv.go
plugin/dapp/exchange/executor/kv.go
+6
-6
exchange.proto
plugin/dapp/exchange/proto/exchange.proto
+4
-4
exchange.pb.go
plugin/dapp/exchange/types/exchange.pb.go
+0
-0
No files found.
plugin/dapp/exchange/executor/exchange_test.go
View file @
9868e5ed
...
...
@@ -463,14 +463,19 @@ func signTx(tx *types.Transaction, hexPrivKey string) (*types.Transaction, error
}
func
TestTruncate
(
t
*
testing
.
T
)
{
a
:=
float
32
(
1.00000212000000000001
)
b
:=
float
32
(
0.34567
)
c
:=
float
32
(
1234
)
a
:=
float
64
(
1.00000212000000000001
)
b
:=
float
64
(
0.34567
)
c
:=
float
64
(
1234567
)
t
.
Log
(
Truncate
(
a
))
t
.
Log
(
Truncate
(
b
))
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
)
{
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
))))
}
plugin/dapp/exchange/executor/exchangedb.go
View file @
9868e5ed
...
...
@@ -60,16 +60,16 @@ func (a *Action) OpSwap(op int32) int32 {
}
//计算实际花费
func
(
a
*
Action
)
calcActualCost
(
op
int32
,
amount
int64
,
price
float
32
)
int64
{
func
(
a
*
Action
)
calcActualCost
(
op
int32
,
amount
int64
,
price
float
64
)
int64
{
if
op
==
et
.
OpBuy
{
return
int64
(
float
32
(
amount
)
*
Truncate
(
price
))
return
int64
(
float
64
(
amount
)
*
Truncate
(
price
))
}
return
amount
}
//price 精度允许范围小数点后面
7
位数,0<price<1e8
func
CheckPrice
(
price
float
32
)
bool
{
if
(
Truncate
(
price
)
>=
1e8
)
||
(
Truncate
(
price
)
*
float
32
(
1e8
)
<=
0
)
{
//price 精度允许范围小数点后面
8
位数,0<price<1e8
func
CheckPrice
(
price
float
64
)
bool
{
if
(
Truncate
(
price
)
>=
1e8
)
||
(
Truncate
(
price
)
*
float
64
(
1e8
)
<
1
)
{
return
false
}
return
true
...
...
@@ -138,7 +138,7 @@ func (a *Action) LimitOrder(payload *et.LimitOrder) (*types.Receipt, error) {
}
//先检查账户余额
if
payload
.
GetOp
()
==
et
.
OpBuy
{
amount
:=
int64
(
float
32
(
payload
.
GetAmount
())
*
Truncate
(
payload
.
GetPrice
()))
amount
:=
int64
(
float
64
(
payload
.
GetAmount
())
*
Truncate
(
payload
.
GetPrice
()))
rightAccount
:=
rightAssetDB
.
LoadExecAccount
(
a
.
fromaddr
,
a
.
execaddr
)
if
rightAccount
.
Balance
<
amount
{
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) {
return
&
order
,
nil
}
func
findOrderIDListByPrice
(
localdb
dbm
.
Lister
,
left
,
right
*
et
.
Asset
,
price
float
32
,
op
,
direction
int32
,
index
int64
)
([]
*
et
.
OrderID
,
error
)
{
func
findOrderIDListByPrice
(
localdb
dbm
.
Lister
,
left
,
right
*
et
.
Asset
,
price
float
64
,
op
,
direction
int32
,
index
int64
)
([]
*
et
.
OrderID
,
error
)
{
prefix
:=
calcMarketDepthOrderPrefix
(
left
,
right
,
op
,
price
)
key
:=
calcMarketDepthOrderKey
(
left
,
right
,
op
,
price
,
index
)
var
values
[][]
byte
...
...
@@ -490,7 +490,7 @@ func Direction(op int32) int32 {
}
//这里price当作索引来用,首次查询不需要填值
func
QueryMarketDepth
(
localdb
dbm
.
Lister
,
left
,
right
*
et
.
Asset
,
op
int32
,
price
float
32
,
count
int32
)
(
types
.
Message
,
error
)
{
func
QueryMarketDepth
(
localdb
dbm
.
Lister
,
left
,
right
*
et
.
Asset
,
op
int32
,
price
float
64
,
count
int32
)
(
types
.
Message
,
error
)
{
prefix
:=
calcMarketDepthPrefix
(
left
,
right
,
op
)
key
:=
calcMarketDepthKey
(
left
,
right
,
op
,
price
)
var
values
[][]
byte
...
...
@@ -592,6 +592,6 @@ func QueryOrderList(localdb dbm.Lister, statedb dbm.KV, addr string, status, cou
}
//截取小数点后7位
func
Truncate
(
price
float
32
)
float32
{
return
float32
(
math
.
Trunc
(
float64
(
1e8
)
*
float64
(
price
))
/
float64
(
1e8
)
)
func
Truncate
(
price
float
64
)
float64
{
return
math
.
Trunc
(
float64
(
1e8
)
*
float64
(
price
))
/
float64
(
1e8
)
}
plugin/dapp/exchange/executor/kv.go
View file @
9868e5ed
...
...
@@ -30,22 +30,22 @@ func calcMarketDepthPrefix(left, right *types.Asset, op int32) []byte {
}
//市场深度
func
calcMarketDepthKey
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
32
)
[]
byte
{
func
calcMarketDepthKey
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
64
)
[]
byte
{
// 设置精度为1e8
key
:=
fmt
.
Sprintf
(
"%s"
+
"depth-%s-%s-%d:%016d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
32
(
1e8
)))
key
:=
fmt
.
Sprintf
(
"%s"
+
"depth-%s-%s-%d:%016d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
64
(
1e8
)))
return
[]
byte
(
key
)
}
func
calcMarketDepthOrderPrefix
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
32
)
[]
byte
{
func
calcMarketDepthOrderPrefix
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
64
)
[]
byte
{
// 设置精度为1e8
key
:=
fmt
.
Sprintf
(
"%s"
+
"order-%s-%s-%d:%016d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
32
(
1e8
)))
key
:=
fmt
.
Sprintf
(
"%s"
+
"order-%s-%s-%d:%016d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
64
(
1e8
)))
return
[]
byte
(
key
)
}
// localdb中存储市场挂单ID
func
calcMarketDepthOrderKey
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
32
,
index
int64
)
[]
byte
{
func
calcMarketDepthOrderKey
(
left
,
right
*
types
.
Asset
,
op
int32
,
price
float
64
,
index
int64
)
[]
byte
{
// 设置精度为1e8
key
:=
fmt
.
Sprintf
(
"%s"
+
"order-%s-%s-%d:%016d:%022d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
32
(
1e8
)),
index
)
key
:=
fmt
.
Sprintf
(
"%s"
+
"order-%s-%s-%d:%016d:%022d"
,
KeyPrefixLocalDB
,
left
.
GetSymbol
(),
right
.
GetSymbol
(),
op
,
int64
(
Truncate
(
price
)
*
float
64
(
1e8
)),
index
)
return
[]
byte
(
key
)
}
...
...
plugin/dapp/exchange/proto/exchange.proto
View file @
9868e5ed
...
...
@@ -19,7 +19,7 @@ message LimitOrder {
//交易对
asset
rightAsset
=
2
;
//价格
float
price
=
3
;
double
price
=
3
;
//总量
int64
amount
=
4
;
//操作, 1为买,2为卖
...
...
@@ -74,7 +74,7 @@ message Order {
//挂单价
message
OrderPrice
{
float
price
=
1
;
double
price
=
1
;
int64
index
=
2
;
}
//单号
...
...
@@ -91,7 +91,7 @@ message QueryMarketDepth {
//操作, 1为买,2为卖
int32
op
=
3
;
// 这里用价格作为索引值
float
price
=
4
;
double
price
=
4
;
//单页返回多少条记录,默认返回10条,为了系统安全最多单次只能返回20条
int32
count
=
5
;
}
...
...
@@ -102,7 +102,7 @@ message MarketDepth {
//资产2
asset
rightAsset
=
2
;
//价格
float
price
=
3
;
double
price
=
3
;
//总量
int64
amount
=
4
;
//操作, 1为买,2为卖
...
...
plugin/dapp/exchange/types/exchange.pb.go
View file @
9868e5ed
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment