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
Registry
Registry
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
693250a0
Commit
693250a0
authored
Sep 03, 2019
by
linj
Committed by
vipwzw
Sep 05, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
移动代码
parent
41f69ba3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
369 additions
and
367 deletions
+369
-367
query.go
plugin/dapp/trade/executor/query.go
+0
-367
query_v1.go
plugin/dapp/trade/executor/query_v1.go
+369
-0
No files found.
plugin/dapp/trade/executor/query.go
View file @
693250a0
...
@@ -5,10 +5,6 @@
...
@@ -5,10 +5,6 @@
package
executor
package
executor
import
(
import
(
"strconv"
"strings"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
)
)
...
@@ -33,174 +29,6 @@ func (t *trade) Query_GetOneOrder(req *pty.ReqAddrAssets) (types.Message, error)
...
@@ -33,174 +29,6 @@ func (t *trade) Query_GetOneOrder(req *pty.ReqAddrAssets) (types.Message, error)
}
}
// query reply utils
// query reply utils
func
(
t
*
trade
)
replyReplySellOrderfromID
(
key
[]
byte
)
*
pty
.
ReplySellOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
sellIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
sellIDPrefix
)
{
if
sellorder
,
err
:=
getSellOrderFromID
(
key
,
t
.
GetStateDB
());
err
==
nil
{
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
return
sellOrder2reply
(
sellorder
)
}
}
else
{
// txhash as key
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"GetOnesSellOrder "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2sellOrderReply
(
txResult
)
}
return
nil
}
func
(
t
*
trade
)
replyReplyBuyOrderfromID
(
key
[]
byte
)
*
pty
.
ReplyBuyOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
buyIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
buyIDPrefix
)
{
if
buyOrder
,
err
:=
getBuyOrderFromID
(
key
,
t
.
GetStateDB
());
err
==
nil
{
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
return
buyOrder2reply
(
buyOrder
)
}
}
else
{
// txhash as key
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"replyReplyBuyOrderfromID "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2buyOrderReply
(
txResult
)
}
return
nil
}
func
sellOrder2reply
(
sellOrder
*
pty
.
SellOrder
)
*
pty
.
ReplySellOrder
{
reply
:=
&
pty
.
ReplySellOrder
{
TokenSymbol
:
sellOrder
.
TokenSymbol
,
Owner
:
sellOrder
.
Address
,
AmountPerBoardlot
:
sellOrder
.
AmountPerBoardlot
,
MinBoardlot
:
sellOrder
.
MinBoardlot
,
PricePerBoardlot
:
sellOrder
.
PricePerBoardlot
,
TotalBoardlot
:
sellOrder
.
TotalBoardlot
,
SoldBoardlot
:
sellOrder
.
SoldBoardlot
,
BuyID
:
""
,
Status
:
sellOrder
.
Status
,
SellID
:
sellOrder
.
SellID
,
TxHash
:
strings
.
Replace
(
sellOrder
.
SellID
,
sellIDPrefix
,
"0x"
,
1
),
Height
:
sellOrder
.
Height
,
Key
:
sellOrder
.
SellID
,
AssetExec
:
sellOrder
.
AssetExec
,
}
return
reply
}
func
txResult2sellOrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplySellOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
amount
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
txhash
:=
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
())
reply
:=
&
pty
.
ReplySellOrder
{
TokenSymbol
:
receipt
.
Base
.
TokenSymbol
,
Owner
:
receipt
.
Base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
receipt
.
Base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
receipt
.
Base
.
TotalBoardlot
,
SoldBoardlot
:
receipt
.
Base
.
SoldBoardlot
,
BuyID
:
receipt
.
Base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
receipt
.
Base
.
Status
],
SellID
:
""
,
TxHash
:
txhash
,
Height
:
receipt
.
Base
.
Height
,
Key
:
txhash
,
AssetExec
:
receipt
.
Base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
}
return
nil
}
func
buyOrder2reply
(
buyOrder
*
pty
.
BuyLimitOrder
)
*
pty
.
ReplyBuyOrder
{
reply
:=
&
pty
.
ReplyBuyOrder
{
TokenSymbol
:
buyOrder
.
TokenSymbol
,
Owner
:
buyOrder
.
Address
,
AmountPerBoardlot
:
buyOrder
.
AmountPerBoardlot
,
MinBoardlot
:
buyOrder
.
MinBoardlot
,
PricePerBoardlot
:
buyOrder
.
PricePerBoardlot
,
TotalBoardlot
:
buyOrder
.
TotalBoardlot
,
BoughtBoardlot
:
buyOrder
.
BoughtBoardlot
,
BuyID
:
buyOrder
.
BuyID
,
Status
:
buyOrder
.
Status
,
SellID
:
""
,
TxHash
:
strings
.
Replace
(
buyOrder
.
BuyID
,
buyIDPrefix
,
"0x"
,
1
),
Height
:
buyOrder
.
Height
,
Key
:
buyOrder
.
BuyID
,
AssetExec
:
buyOrder
.
AssetExec
,
}
return
reply
}
func
txResult2buyOrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyBuyOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
amount
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
txhash
:=
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
())
reply
:=
&
pty
.
ReplyBuyOrder
{
TokenSymbol
:
receipt
.
Base
.
TokenSymbol
,
Owner
:
receipt
.
Base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
receipt
.
Base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
receipt
.
Base
.
TotalBoardlot
,
BoughtBoardlot
:
receipt
.
Base
.
BoughtBoardlot
,
BuyID
:
""
,
Status
:
pty
.
SellOrderStatus2Int
[
receipt
.
Base
.
Status
],
SellID
:
receipt
.
Base
.
SellID
,
TxHash
:
txhash
,
Height
:
receipt
.
Base
.
Height
,
Key
:
txhash
,
AssetExec
:
receipt
.
Base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
}
return
nil
}
const
(
const
(
orderStatusInvalid
=
iota
orderStatusInvalid
=
iota
...
@@ -233,201 +61,6 @@ func fromStatus(status int32) (st, ty int32) {
...
@@ -233,201 +61,6 @@ func fromStatus(status int32) (st, ty int32) {
return
orderStatusInvalid
,
orderTypeInvalid
return
orderStatusInvalid
,
orderTypeInvalid
}
}
// SellMarkMarket BuyMarket 没有tradeOrder 需要调用这个函数进行转化
// BuyRevoke, SellRevoke 也需要
// SellLimit/BuyLimit 有order 但order 里面没有 bolcktime, 直接访问 order 还需要再次访问 block, 还不如直接访问交易
func
buyBase2Order
(
base
*
pty
.
ReceiptBuyBase
,
txHash
string
,
blockTime
int64
)
*
pty
.
ReplyTradeOrder
{
amount
,
err
:=
strconv
.
ParseFloat
(
base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
key
:=
txHash
if
len
(
base
.
BuyID
)
>
0
{
key
=
base
.
BuyID
}
//txhash := common.ToHex(txResult.GetTx().Hash())
reply
:=
&
pty
.
ReplyTradeOrder
{
TokenSymbol
:
base
.
TokenSymbol
,
Owner
:
base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
base
.
TotalBoardlot
,
TradedBoardlot
:
base
.
BoughtBoardlot
,
BuyID
:
base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
base
.
Status
],
SellID
:
base
.
SellID
,
TxHash
:
txHash
,
Height
:
base
.
Height
,
Key
:
key
,
BlockTime
:
blockTime
,
IsSellOrder
:
false
,
AssetExec
:
base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
func
sellBase2Order
(
base
*
pty
.
ReceiptSellBase
,
txHash
string
,
blockTime
int64
)
*
pty
.
ReplyTradeOrder
{
amount
,
err
:=
strconv
.
ParseFloat
(
base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
//txhash := common.ToHex(txResult.GetTx().Hash())
key
:=
txHash
if
len
(
base
.
SellID
)
>
0
{
key
=
base
.
SellID
}
reply
:=
&
pty
.
ReplyTradeOrder
{
TokenSymbol
:
base
.
TokenSymbol
,
Owner
:
base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
base
.
TotalBoardlot
,
TradedBoardlot
:
base
.
SoldBoardlot
,
BuyID
:
base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
base
.
Status
],
SellID
:
base
.
SellID
,
TxHash
:
txHash
,
Height
:
base
.
Height
,
Key
:
key
,
BlockTime
:
blockTime
,
IsSellOrder
:
true
,
AssetExec
:
base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
func
txResult2OrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyTradeOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeBuyRevoke
{
var
receipt
pty
.
ReceiptTradeBuyRevoke
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
var
receipt
pty
.
ReceiptTradeSellRevoke
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show revoke 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
}
return
nil
}
func
limitOrderTxResult2Order
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyTradeOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeSellLimit
{
var
receipt
pty
.
ReceiptTradeSellLimit
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeBuyLimit
{
var
receipt
pty
.
ReceiptTradeBuyLimit
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
}
return
nil
}
func
(
t
*
trade
)
loadOrderFromKey
(
key
[]
byte
)
*
pty
.
ReplyTradeOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
sellIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
sellIDPrefix
)
{
txHash
:=
strings
.
Replace
(
string
(
key
),
sellIDPrefix
,
"0x"
,
1
)
txResult
,
err
:=
getTx
([]
byte
(
txHash
),
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
txResult
)
if
err
!=
nil
{
return
nil
}
reply
:=
limitOrderTxResult2Order
(
txResult
)
sellOrder
,
err
:=
getSellOrderFromID
(
key
,
t
.
GetStateDB
())
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
reply
.
TradedBoardlot
=
sellOrder
.
SoldBoardlot
reply
.
Status
=
sellOrder
.
Status
return
reply
}
else
if
strings
.
HasPrefix
(
string
(
key
),
buyIDPrefix
)
{
txHash
:=
strings
.
Replace
(
string
(
key
),
buyIDPrefix
,
"0x"
,
1
)
txResult
,
err
:=
getTx
([]
byte
(
txHash
),
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
txResult
)
if
err
!=
nil
{
return
nil
}
reply
:=
limitOrderTxResult2Order
(
txResult
)
buyOrder
,
err
:=
getBuyOrderFromID
(
key
,
t
.
GetStateDB
())
if
err
!=
nil
{
return
nil
}
reply
.
TradedBoardlot
=
buyOrder
.
BoughtBoardlot
reply
.
Status
=
buyOrder
.
Status
return
reply
}
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2OrderReply
(
txResult
)
}
func
(
t
*
trade
)
GetOnesOrderWithStatus
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
func
(
t
*
trade
)
GetOnesOrderWithStatus
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
orderStatus
,
orderType
:=
fromStatus
(
req
.
Status
)
orderStatus
,
orderType
:=
fromStatus
(
req
.
Status
)
if
orderStatus
==
orderStatusInvalid
||
orderType
==
orderTypeInvalid
{
if
orderStatus
==
orderStatusInvalid
||
orderType
==
orderTypeInvalid
{
...
...
plugin/dapp/trade/executor/query_v1.go
View file @
693250a0
package
executor
package
executor
import
(
import
(
"strconv"
"strings"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
)
)
...
@@ -230,3 +234,368 @@ func (t *trade) GetOnesBuyOrdersWithStatus(req *pty.ReqAddrAssets) (types.Messag
...
@@ -230,3 +234,368 @@ func (t *trade) GetOnesBuyOrdersWithStatus(req *pty.ReqAddrAssets) (types.Messag
return
&
replys
,
nil
return
&
replys
,
nil
}
}
// utils
func
(
t
*
trade
)
loadOrderFromKey
(
key
[]
byte
)
*
pty
.
ReplyTradeOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
sellIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
sellIDPrefix
)
{
txHash
:=
strings
.
Replace
(
string
(
key
),
sellIDPrefix
,
"0x"
,
1
)
txResult
,
err
:=
getTx
([]
byte
(
txHash
),
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
txResult
)
if
err
!=
nil
{
return
nil
}
reply
:=
limitOrderTxResult2Order
(
txResult
)
sellOrder
,
err
:=
getSellOrderFromID
(
key
,
t
.
GetStateDB
())
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
reply
.
TradedBoardlot
=
sellOrder
.
SoldBoardlot
reply
.
Status
=
sellOrder
.
Status
return
reply
}
else
if
strings
.
HasPrefix
(
string
(
key
),
buyIDPrefix
)
{
txHash
:=
strings
.
Replace
(
string
(
key
),
buyIDPrefix
,
"0x"
,
1
)
txResult
,
err
:=
getTx
([]
byte
(
txHash
),
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
txResult
)
if
err
!=
nil
{
return
nil
}
reply
:=
limitOrderTxResult2Order
(
txResult
)
buyOrder
,
err
:=
getBuyOrderFromID
(
key
,
t
.
GetStateDB
())
if
err
!=
nil
{
return
nil
}
reply
.
TradedBoardlot
=
buyOrder
.
BoughtBoardlot
reply
.
Status
=
buyOrder
.
Status
return
reply
}
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"loadOrderFromKey "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2OrderReply
(
txResult
)
}
func
limitOrderTxResult2Order
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyTradeOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeSellLimit
{
var
receipt
pty
.
ReceiptTradeSellLimit
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeBuyLimit
{
var
receipt
pty
.
ReceiptTradeBuyLimit
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
}
return
nil
}
func
txResult2OrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyTradeOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeBuyRevoke
{
var
receipt
pty
.
ReceiptTradeBuyRevoke
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
buyBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
var
receipt
pty
.
ReceiptTradeSellRevoke
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show revoke 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
else
if
log
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
return
sellBase2Order
(
receipt
.
Base
,
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
()),
txResult
.
Blocktime
)
}
}
return
nil
}
// SellMarkMarket BuyMarket 没有tradeOrder 需要调用这个函数进行转化
// BuyRevoke, SellRevoke 也需要
// SellLimit/BuyLimit 有order 但order 里面没有 bolcktime, 直接访问 order 还需要再次访问 block, 还不如直接访问交易
func
buyBase2Order
(
base
*
pty
.
ReceiptBuyBase
,
txHash
string
,
blockTime
int64
)
*
pty
.
ReplyTradeOrder
{
amount
,
err
:=
strconv
.
ParseFloat
(
base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
key
:=
txHash
if
len
(
base
.
BuyID
)
>
0
{
key
=
base
.
BuyID
}
//txhash := common.ToHex(txResult.GetTx().Hash())
reply
:=
&
pty
.
ReplyTradeOrder
{
TokenSymbol
:
base
.
TokenSymbol
,
Owner
:
base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
base
.
TotalBoardlot
,
TradedBoardlot
:
base
.
BoughtBoardlot
,
BuyID
:
base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
base
.
Status
],
SellID
:
base
.
SellID
,
TxHash
:
txHash
,
Height
:
base
.
Height
,
Key
:
key
,
BlockTime
:
blockTime
,
IsSellOrder
:
false
,
AssetExec
:
base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
func
sellBase2Order
(
base
*
pty
.
ReceiptSellBase
,
txHash
string
,
blockTime
int64
)
*
pty
.
ReplyTradeOrder
{
amount
,
err
:=
strconv
.
ParseFloat
(
base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
//txhash := common.ToHex(txResult.GetTx().Hash())
key
:=
txHash
if
len
(
base
.
SellID
)
>
0
{
key
=
base
.
SellID
}
reply
:=
&
pty
.
ReplyTradeOrder
{
TokenSymbol
:
base
.
TokenSymbol
,
Owner
:
base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
base
.
TotalBoardlot
,
TradedBoardlot
:
base
.
SoldBoardlot
,
BuyID
:
base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
base
.
Status
],
SellID
:
base
.
SellID
,
TxHash
:
txHash
,
Height
:
base
.
Height
,
Key
:
key
,
BlockTime
:
blockTime
,
IsSellOrder
:
true
,
AssetExec
:
base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
func
(
t
*
trade
)
replyReplySellOrderfromID
(
key
[]
byte
)
*
pty
.
ReplySellOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
sellIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
sellIDPrefix
)
{
if
sellorder
,
err
:=
getSellOrderFromID
(
key
,
t
.
GetStateDB
());
err
==
nil
{
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
return
sellOrder2reply
(
sellorder
)
}
}
else
{
// txhash as key
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"GetOnesSellOrder "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2sellOrderReply
(
txResult
)
}
return
nil
}
func
(
t
*
trade
)
replyReplyBuyOrderfromID
(
key
[]
byte
)
*
pty
.
ReplyBuyOrder
{
tradelog
.
Debug
(
"trade Query"
,
"id"
,
string
(
key
),
"check-prefix"
,
buyIDPrefix
)
if
strings
.
HasPrefix
(
string
(
key
),
buyIDPrefix
)
{
if
buyOrder
,
err
:=
getBuyOrderFromID
(
key
,
t
.
GetStateDB
());
err
==
nil
{
tradelog
.
Debug
(
"trade Query"
,
"getSellOrderFromID"
,
string
(
key
))
return
buyOrder2reply
(
buyOrder
)
}
}
else
{
// txhash as key
txResult
,
err
:=
getTx
(
key
,
t
.
GetLocalDB
(),
t
.
GetAPI
())
tradelog
.
Debug
(
"replyReplyBuyOrderfromID "
,
"load txhash"
,
string
(
key
))
if
err
!=
nil
{
return
nil
}
return
txResult2buyOrderReply
(
txResult
)
}
return
nil
}
func
sellOrder2reply
(
sellOrder
*
pty
.
SellOrder
)
*
pty
.
ReplySellOrder
{
reply
:=
&
pty
.
ReplySellOrder
{
TokenSymbol
:
sellOrder
.
TokenSymbol
,
Owner
:
sellOrder
.
Address
,
AmountPerBoardlot
:
sellOrder
.
AmountPerBoardlot
,
MinBoardlot
:
sellOrder
.
MinBoardlot
,
PricePerBoardlot
:
sellOrder
.
PricePerBoardlot
,
TotalBoardlot
:
sellOrder
.
TotalBoardlot
,
SoldBoardlot
:
sellOrder
.
SoldBoardlot
,
BuyID
:
""
,
Status
:
sellOrder
.
Status
,
SellID
:
sellOrder
.
SellID
,
TxHash
:
strings
.
Replace
(
sellOrder
.
SellID
,
sellIDPrefix
,
"0x"
,
1
),
Height
:
sellOrder
.
Height
,
Key
:
sellOrder
.
SellID
,
AssetExec
:
sellOrder
.
AssetExec
,
}
return
reply
}
func
txResult2sellOrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplySellOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
amount
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
txhash
:=
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
())
reply
:=
&
pty
.
ReplySellOrder
{
TokenSymbol
:
receipt
.
Base
.
TokenSymbol
,
Owner
:
receipt
.
Base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
receipt
.
Base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
receipt
.
Base
.
TotalBoardlot
,
SoldBoardlot
:
receipt
.
Base
.
SoldBoardlot
,
BuyID
:
receipt
.
Base
.
BuyID
,
Status
:
pty
.
SellOrderStatus2Int
[
receipt
.
Base
.
Status
],
SellID
:
""
,
TxHash
:
txhash
,
Height
:
receipt
.
Base
.
Height
,
Key
:
txhash
,
AssetExec
:
receipt
.
Base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
}
return
nil
}
func
buyOrder2reply
(
buyOrder
*
pty
.
BuyLimitOrder
)
*
pty
.
ReplyBuyOrder
{
reply
:=
&
pty
.
ReplyBuyOrder
{
TokenSymbol
:
buyOrder
.
TokenSymbol
,
Owner
:
buyOrder
.
Address
,
AmountPerBoardlot
:
buyOrder
.
AmountPerBoardlot
,
MinBoardlot
:
buyOrder
.
MinBoardlot
,
PricePerBoardlot
:
buyOrder
.
PricePerBoardlot
,
TotalBoardlot
:
buyOrder
.
TotalBoardlot
,
BoughtBoardlot
:
buyOrder
.
BoughtBoardlot
,
BuyID
:
buyOrder
.
BuyID
,
Status
:
buyOrder
.
Status
,
SellID
:
""
,
TxHash
:
strings
.
Replace
(
buyOrder
.
BuyID
,
buyIDPrefix
,
"0x"
,
1
),
Height
:
buyOrder
.
Height
,
Key
:
buyOrder
.
BuyID
,
AssetExec
:
buyOrder
.
AssetExec
,
}
return
reply
}
func
txResult2buyOrderReply
(
txResult
*
types
.
TxResult
)
*
pty
.
ReplyBuyOrder
{
logs
:=
txResult
.
Receiptdate
.
Logs
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs"
,
logs
)
for
_
,
log
:=
range
logs
{
if
log
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
err
:=
types
.
Decode
(
log
.
Log
,
&
receipt
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show logs 1 "
,
receipt
)
amount
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
AmountPerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
price
,
err
:=
strconv
.
ParseFloat
(
receipt
.
Base
.
PricePerBoardlot
,
64
)
if
err
!=
nil
{
tradelog
.
Error
(
"txResult2sellOrderReply"
,
"decode receipt"
,
err
)
return
nil
}
txhash
:=
common
.
ToHex
(
txResult
.
GetTx
()
.
Hash
())
reply
:=
&
pty
.
ReplyBuyOrder
{
TokenSymbol
:
receipt
.
Base
.
TokenSymbol
,
Owner
:
receipt
.
Base
.
Owner
,
AmountPerBoardlot
:
int64
(
amount
*
float64
(
types
.
TokenPrecision
)),
MinBoardlot
:
receipt
.
Base
.
MinBoardlot
,
PricePerBoardlot
:
int64
(
price
*
float64
(
types
.
Coin
)),
TotalBoardlot
:
receipt
.
Base
.
TotalBoardlot
,
BoughtBoardlot
:
receipt
.
Base
.
BoughtBoardlot
,
BuyID
:
""
,
Status
:
pty
.
SellOrderStatus2Int
[
receipt
.
Base
.
Status
],
SellID
:
receipt
.
Base
.
SellID
,
TxHash
:
txhash
,
Height
:
receipt
.
Base
.
Height
,
Key
:
txhash
,
AssetExec
:
receipt
.
Base
.
AssetExec
,
}
tradelog
.
Debug
(
"txResult2sellOrderReply"
,
"show reply"
,
reply
)
return
reply
}
}
return
nil
}
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