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
3f71a8a5
Unverified
Commit
3f71a8a5
authored
Jan 24, 2019
by
vipwzw
Committed by
GitHub
Jan 24, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #260 from linj-disanbo/trade-localdb
Trade localdb
parents
556879c1
8c429796
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
475 additions
and
34 deletions
+475
-34
table.go
plugin/dapp/guess/types/table.go
+2
-2
trade.go
plugin/dapp/trade/commands/trade.go
+127
-0
exec_del_local.go
plugin/dapp/trade/executor/exec_del_local.go
+41
-13
exec_local.go
plugin/dapp/trade/executor/exec_local.go
+35
-6
exec_test.go
plugin/dapp/trade/executor/exec_test.go
+53
-0
local_order.go
plugin/dapp/trade/executor/local_order.go
+0
-0
local_order_test.go
plugin/dapp/trade/executor/local_order_test.go
+66
-0
query.go
plugin/dapp/trade/executor/query.go
+80
-1
trade.go
plugin/dapp/trade/executor/trade.go
+47
-12
trade.proto
plugin/dapp/trade/proto/trade.proto
+21
-0
const.go
plugin/dapp/trade/types/const.go
+3
-0
trade.pb.go
plugin/dapp/trade/types/trade.pb.go
+0
-0
No files found.
plugin/dapp/guess/types/table.go
View file @
3f71a8a5
...
...
@@ -16,7 +16,7 @@ index: addr,status,addr_status,admin,admin_status,category_status
*/
var
opt_guess_user
=
&
table
.
Option
{
Prefix
:
"LODB
_
guess"
,
Prefix
:
"LODB
-
guess"
,
Name
:
"user"
,
Primary
:
"index"
,
Index
:
[]
string
{
"addr"
,
"startindex"
},
...
...
@@ -70,7 +70,7 @@ func (tx *GuessUserRow) Get(key string) ([]byte, error) {
}
var
opt_guess_game
=
&
table
.
Option
{
Prefix
:
"LODB
_
guess"
,
Prefix
:
"LODB
-
guess"
,
Name
:
"game"
,
Primary
:
"startindex"
,
Index
:
[]
string
{
"gameid"
,
"status"
,
"admin"
,
"admin_status"
,
"category_status"
},
...
...
plugin/dapp/trade/commands/trade.go
View file @
3f71a8a5
...
...
@@ -30,6 +30,10 @@ func TradeCmd() *cobra.Command {
CreateRawTradeBuyTxCmd
(),
CreateRawTradeRevokeTxCmd
(),
CreateRawBuyLimitTxCmd
(),
CreateRawSellMarketTxCmd
(),
CreateRawBuyRevokeTxCmd
(),
ShowOnesSellOrdersCmd
(),
ShowOnesSellOrdersStatusCmd
(),
ShowTokenSellOrdersStatusCmd
(),
...
...
@@ -555,3 +559,126 @@ func tokenSellRevoke(cmd *cobra.Command, args []string) {
ctx
:=
jsonrpc
.
NewRPCCtx
(
rpcLaddr
,
"trade.CreateRawTradeRevokeTx"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
// BuyLimit SellMarket BuyRevoke transactions
// CreateRawBuyLimitTxCmd : create raw buy limit token transaction
func
CreateRawBuyLimitTxCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"buy_limit"
,
Short
:
"Create a buy limit transaction"
,
Run
:
tokenBuyLimit
,
}
addTokenBuyLimitFlags
(
cmd
)
return
cmd
}
func
addTokenBuyLimitFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"symbol"
,
"s"
,
""
,
"token symbol"
)
cmd
.
MarkFlagRequired
(
"symbol"
)
cmd
.
Flags
()
.
Int64P
(
"min"
,
"m"
,
0
,
"min boardlot"
)
cmd
.
MarkFlagRequired
(
"min"
)
cmd
.
Flags
()
.
Float64P
(
"price"
,
"p"
,
0
,
"price per boardlot"
)
cmd
.
MarkFlagRequired
(
"price"
)
cmd
.
Flags
()
.
Float64P
(
"fee"
,
"f"
,
0
,
"transaction fee"
)
cmd
.
Flags
()
.
Float64P
(
"total"
,
"t"
,
0
,
"total tokens to buy"
)
cmd
.
MarkFlagRequired
(
"total"
)
}
func
tokenBuyLimit
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
symbol
,
_
:=
cmd
.
Flags
()
.
GetString
(
"symbol"
)
min
,
_
:=
cmd
.
Flags
()
.
GetInt64
(
"min"
)
price
,
_
:=
cmd
.
Flags
()
.
GetFloat64
(
"price"
)
fee
,
_
:=
cmd
.
Flags
()
.
GetFloat64
(
"fee"
)
total
,
_
:=
cmd
.
Flags
()
.
GetFloat64
(
"total"
)
priceInt64
:=
int64
(
price
*
1e4
)
feeInt64
:=
int64
(
fee
*
1e4
)
totalInt64
:=
int64
(
total
*
1e8
/
1e6
)
params
:=
&
pty
.
TradeBuyLimitTx
{
TokenSymbol
:
symbol
,
AmountPerBoardlot
:
1e6
,
MinBoardlot
:
min
,
PricePerBoardlot
:
priceInt64
*
1e4
,
TotalBoardlot
:
totalInt64
,
Fee
:
feeInt64
*
1e4
,
AssetExec
:
"token"
,
}
ctx
:=
jsonrpc
.
NewRPCCtx
(
rpcLaddr
,
"trade.CreateRawTradeBuyLimitTx"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
// CreateRawSellMarketTxCmd : create raw sell market token transaction
func
CreateRawSellMarketTxCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"sell_market"
,
Short
:
"Create a sell market transaction"
,
Run
:
sellMarket
,
}
addSellMarketFlags
(
cmd
)
return
cmd
}
func
addSellMarketFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"buy_id"
,
"b"
,
""
,
"buy id"
)
cmd
.
MarkFlagRequired
(
"buy_id"
)
cmd
.
Flags
()
.
Int64P
(
"count"
,
"c"
,
0
,
"count of selling (boardlot)"
)
cmd
.
MarkFlagRequired
(
"count"
)
cmd
.
Flags
()
.
Float64P
(
"fee"
,
"f"
,
0
,
"transaction fee"
)
}
func
sellMarket
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
buyID
,
_
:=
cmd
.
Flags
()
.
GetString
(
"buy_id"
)
fee
,
_
:=
cmd
.
Flags
()
.
GetFloat64
(
"fee"
)
count
,
_
:=
cmd
.
Flags
()
.
GetInt64
(
"count"
)
feeInt64
:=
int64
(
fee
*
1e4
)
params
:=
&
pty
.
TradeSellMarketTx
{
BuyID
:
buyID
,
BoardlotCnt
:
count
,
Fee
:
feeInt64
*
1e4
,
}
ctx
:=
jsonrpc
.
NewRPCCtx
(
rpcLaddr
,
"trade.CreateRawTradeSellMarketTx"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
// CreateRawBuyRevokeTxCmd : create raw buy revoke transaction
func
CreateRawBuyRevokeTxCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"revoke_buy"
,
Short
:
"Create a revoke buy limit transaction"
,
Run
:
buyRevoke
,
}
addBuyRevokeFlags
(
cmd
)
return
cmd
}
func
addBuyRevokeFlags
(
cmd
*
cobra
.
Command
)
{
cmd
.
Flags
()
.
StringP
(
"buy_id"
,
"b"
,
""
,
"buy id"
)
cmd
.
MarkFlagRequired
(
"buy_id"
)
cmd
.
Flags
()
.
Float64P
(
"fee"
,
"f"
,
0
,
"transaction fee"
)
}
func
buyRevoke
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
buyID
,
_
:=
cmd
.
Flags
()
.
GetString
(
"buy_id"
)
fee
,
_
:=
cmd
.
Flags
()
.
GetFloat64
(
"fee"
)
feeInt64
:=
int64
(
fee
*
1e4
)
params
:=
&
pty
.
TradeRevokeBuyTx
{
BuyID
:
buyID
,
Fee
:
feeInt64
*
1e4
,
}
ctx
:=
jsonrpc
.
NewRPCCtx
(
rpcLaddr
,
"trade.CreateRawTradeRevokeBuyTx"
,
params
,
nil
)
ctx
.
RunWithoutMarshal
()
}
plugin/dapp/trade/executor/exec_del_local.go
View file @
3f71a8a5
...
...
@@ -5,46 +5,57 @@
package
executor
import
(
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
)
func
(
t
*
trade
)
ExecDelLocal_SellLimit
(
sell
*
pty
.
TradeForSell
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
0
)
}
func
(
t
*
trade
)
ExecDelLocal_BuyMarket
(
buy
*
pty
.
TradeForBuy
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
buy
.
BoardlotCnt
)
}
func
(
t
*
trade
)
ExecDelLocal_RevokeSell
(
revoke
*
pty
.
TradeForRevokeSell
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
0
)
}
func
(
t
*
trade
)
ExecDelLocal_BuyLimit
(
buy
*
pty
.
TradeForBuyLimit
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
0
)
}
func
(
t
*
trade
)
ExecDelLocal_SellMarket
(
sell
*
pty
.
TradeForSellMarket
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
sell
.
BoardlotCnt
)
}
func
(
t
*
trade
)
ExecDelLocal_RevokeBuy
(
revoke
*
pty
.
TradeForRevokeBuy
,
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
t
.
localDelLog
(
tx
,
receipt
,
index
)
return
t
.
localDelLog
(
tx
,
receipt
,
index
,
0
)
}
func
(
t
*
trade
)
localDelLog
(
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
func
(
t
*
trade
)
localDelLog
(
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
,
tradedBoardlot
int64
)
(
*
types
.
LocalDBSet
,
error
)
{
var
set
types
.
LocalDBSet
table
:=
NewOrderTable
(
t
.
GetLocalDB
())
txIndex
:=
dapp
.
HeightIndexStr
(
t
.
GetHeight
(),
int64
(
index
))
for
i
:=
0
;
i
<
len
(
receipt
.
Logs
);
i
++
{
item
:=
receipt
.
Logs
[
i
]
if
item
.
Ty
==
pty
.
TyLogTradeSellLimit
||
item
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
if
item
.
Ty
==
pty
.
TyLogTradeSellLimit
{
var
receipt
pty
.
ReceiptTradeSellLimit
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteSell
([]
byte
(
receipt
.
Base
.
SellID
),
item
.
Ty
)
kv
:=
t
.
deleteSell
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
,
tradedBoardlot
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
var
receipt
pty
.
ReceiptTradeSellRevoke
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteSell
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
,
tradedBoardlot
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
...
...
@@ -52,15 +63,23 @@ func (t *trade) localDelLog(tx *types.Transaction, receipt *types.ReceiptData, i
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteBuy
(
receipt
.
Base
)
kv
:=
t
.
deleteBuy
(
receipt
.
Base
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyRevoke
{
var
receipt
pty
.
ReceiptTradeBuyRevoke
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteBuyLimit
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
,
tradedBoardlot
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuy
Revoke
||
item
.
Ty
==
pty
.
TyLogTradeBuy
Limit
{
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyLimit
{
var
receipt
pty
.
ReceiptTradeBuyLimit
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteBuyLimit
(
[]
byte
(
receipt
.
Base
.
BuyID
),
item
.
Ty
)
kv
:=
t
.
deleteBuyLimit
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
,
tradedBoardlot
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
...
...
@@ -68,10 +87,19 @@ func (t *trade) localDelLog(tx *types.Transaction, receipt *types.ReceiptData, i
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
deleteSellMarket
(
receipt
.
Base
)
kv
:=
t
.
deleteSellMarket
(
receipt
.
Base
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
}
newKvs
,
err
:=
table
.
Save
()
if
err
!=
nil
{
tradelog
.
Error
(
"trade table.Save failed"
,
"error"
,
err
)
return
nil
,
err
}
set
.
KV
=
append
(
set
.
KV
,
newKvs
...
)
for
_
,
kv
:=
range
set
.
KV
{
t
.
GetLocalDB
()
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
return
&
set
,
nil
}
plugin/dapp/trade/executor/exec_local.go
View file @
3f71a8a5
...
...
@@ -5,6 +5,7 @@
package
executor
import
(
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
)
...
...
@@ -35,16 +36,26 @@ func (t *trade) ExecLocal_RevokeBuy(revoke *pty.TradeForRevokeBuy, tx *types.Tra
func
(
t
*
trade
)
localAddLog
(
tx
*
types
.
Transaction
,
receipt
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
var
set
types
.
LocalDBSet
table
:=
NewOrderTable
(
t
.
GetLocalDB
())
txIndex
:=
dapp
.
HeightIndexStr
(
t
.
GetHeight
(),
int64
(
index
))
for
i
:=
0
;
i
<
len
(
receipt
.
Logs
);
i
++
{
item
:=
receipt
.
Logs
[
i
]
if
item
.
Ty
==
pty
.
TyLogTradeSellLimit
||
item
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
if
item
.
Ty
==
pty
.
TyLogTradeSellLimit
{
var
receipt
pty
.
ReceiptTradeSellLimit
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveSell
([]
byte
(
receipt
.
Base
.
SellID
),
item
.
Ty
)
kv
:=
t
.
saveSell
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeSellRevoke
{
var
receipt
pty
.
ReceiptTradeSellRevoke
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveSell
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyMarket
{
var
receipt
pty
.
ReceiptTradeBuyMarket
...
...
@@ -52,16 +63,25 @@ func (t *trade) localAddLog(tx *types.Transaction, receipt *types.ReceiptData, i
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveBuy
(
receipt
.
Base
)
kv
:=
t
.
saveBuy
(
receipt
.
Base
,
tx
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyRevoke
||
item
.
Ty
==
pty
.
TyLogTradeBuyLimit
{
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyRevoke
{
var
receipt
pty
.
ReceiptTradeBuyRevoke
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveBuyLimit
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeBuyLimit
{
var
receipt
pty
.
ReceiptTradeBuyLimit
err
:=
types
.
Decode
(
item
.
Log
,
&
receipt
)
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveBuyLimit
(
[]
byte
(
receipt
.
Base
.
BuyID
),
item
.
Ty
)
kv
:=
t
.
saveBuyLimit
(
receipt
.
Base
,
item
.
Ty
,
tx
,
txIndex
,
table
)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
else
if
item
.
Ty
==
pty
.
TyLogTradeSellMarket
{
var
receipt
pty
.
ReceiptSellMarket
...
...
@@ -69,11 +89,20 @@ func (t *trade) localAddLog(tx *types.Transaction, receipt *types.ReceiptData, i
if
err
!=
nil
{
panic
(
err
)
//数据错误了,已经被修改了
}
kv
:=
t
.
saveSellMarket
(
receipt
.
Base
)
kv
:=
t
.
saveSellMarket
(
receipt
.
Base
,
tx
,
txIndex
,
table
)
//tradelog.Info("saveSellMarket", "kv", kv)
set
.
KV
=
append
(
set
.
KV
,
kv
...
)
}
}
newKvs
,
err
:=
table
.
Save
()
if
err
!=
nil
{
tradelog
.
Error
(
"trade table.Save failed"
,
"error"
,
err
)
return
nil
,
err
}
set
.
KV
=
append
(
set
.
KV
,
newKvs
...
)
for
_
,
kv
:=
range
set
.
KV
{
t
.
GetLocalDB
()
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
return
&
set
,
nil
}
plugin/dapp/trade/executor/exec_test.go
View file @
3f71a8a5
...
...
@@ -13,6 +13,7 @@ import (
"github.com/33cn/chain33/common/crypto"
dbm
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
"github.com/stretchr/testify/assert"
)
...
...
@@ -83,9 +84,12 @@ func TestTrade_Exec_SellLimit(t *testing.T) {
accA
,
_
:=
account
.
NewAccountDB
(
AssetExecToken
,
Symbol
,
stateDB
)
accA
.
SaveExecAccount
(
address
.
ExecAddress
(
"trade"
),
&
accountA
)
_
,
ldb
,
kvdb
:=
util
.
CreateTestDB
()
driver
:=
newTrade
()
driver
.
SetEnv
(
env
.
blockHeight
,
env
.
blockTime
,
env
.
difficulty
)
driver
.
SetStateDB
(
stateDB
)
driver
.
SetLocalDB
(
kvdb
)
sell
:=
&
pty
.
TradeSellTx
{
TokenSymbol
:
Symbol
,
...
...
@@ -124,6 +128,15 @@ func TestTrade_Exec_SellLimit(t *testing.T) {
assert
.
Equal
(
t
,
int64
(
0
),
sellOrder
.
SoldBoardlot
)
assert
.
Equal
(
t
,
string
(
Nodes
[
0
]),
sellOrder
.
Address
)
receiptDataSell
:=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
,
}
_
,
err
=
driver
.
ExecLocal
(
tx
,
receiptDataSell
,
env
.
index
)
assert
.
Nil
(
t
,
err
)
kvdb
.
Commit
()
// test buy market
buy
:=
&
pty
.
TradeBuyTx
{
SellID
:
sellOrder
.
SellID
,
BoardlotCnt
:
buyArgs
.
total
,
...
...
@@ -157,6 +170,27 @@ func TestTrade_Exec_SellLimit(t *testing.T) {
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
expect
.
total
,
sellOrder
.
TotalBoardlot
-
sellOrder
.
SoldBoardlot
)
receiptDataBuy
:=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
,
}
_
,
err
=
driver
.
ExecLocal
(
tx
,
receiptDataBuy
,
env
.
index
)
assert
.
Nil
(
t
,
err
)
req
:=
&
pty
.
ReqAddrAssets
{
Addr
:
string
(
Nodes
[
0
]),
Status
:
pty
.
TradeOrderStatusOnSale
,
Token
:
nil
,
Direction
:
1
,
Count
:
10
,
FromKey
:
""
,
}
resp
,
err
:=
driver
.
Query
(
"GetOnesOrderWithStatus"
,
types
.
Encode
(
req
))
assert
.
Nil
(
t
,
err
)
orders
,
ok
:=
resp
.
(
*
pty
.
ReplyTradeOrders
)
assert
.
True
(
t
,
ok
)
assert
.
Equal
(
t
,
1
,
len
(
orders
.
Orders
))
ldb
.
Close
()
}
func
TestTrade_Exec_BuyLimit
(
t
*
testing
.
T
)
{
...
...
@@ -185,6 +219,8 @@ func TestTrade_Exec_BuyLimit(t *testing.T) {
}
stateDB
,
_
:=
dbm
.
NewGoMemDB
(
"1"
,
"2"
,
100
)
_
,
ldb
,
kvdb
:=
util
.
CreateTestDB
()
accB
:=
account
.
NewCoinsAccount
()
accB
.
SetDB
(
stateDB
)
accB
.
SaveExecAccount
(
address
.
ExecAddress
(
"trade"
),
&
accountB
)
...
...
@@ -195,6 +231,7 @@ func TestTrade_Exec_BuyLimit(t *testing.T) {
driver
:=
newTrade
()
driver
.
SetEnv
(
env
.
blockHeight
,
env
.
blockTime
,
env
.
difficulty
)
driver
.
SetStateDB
(
stateDB
)
driver
.
SetLocalDB
(
kvdb
)
buy
:=
&
pty
.
TradeBuyLimitTx
{
TokenSymbol
:
Symbol
,
...
...
@@ -233,6 +270,13 @@ func TestTrade_Exec_BuyLimit(t *testing.T) {
assert
.
Equal
(
t
,
int64
(
0
),
buyLimitOrder
.
BoughtBoardlot
)
assert
.
Equal
(
t
,
string
(
Nodes
[
1
]),
buyLimitOrder
.
Address
)
receiptDataBuy
:=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
,
}
_
,
err
=
driver
.
ExecLocal
(
tx
,
receiptDataBuy
,
env
.
index
)
assert
.
Nil
(
t
,
err
)
sell
:=
&
pty
.
TradeSellMarketTx
{
BuyID
:
buyLimitOrder
.
BuyID
,
BoardlotCnt
:
sellArgs
.
total
,
...
...
@@ -266,6 +310,15 @@ func TestTrade_Exec_BuyLimit(t *testing.T) {
err
=
types
.
Decode
(
receipt
.
KV
[
4
]
.
Value
,
&
buyLimitOrder
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
expect
.
total
,
buyLimitOrder
.
TotalBoardlot
-
buyLimitOrder
.
BoughtBoardlot
)
receiptDataSell
:=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
,
}
_
,
err
=
driver
.
ExecLocal
(
tx
,
receiptDataSell
,
env
.
index
)
assert
.
Nil
(
t
,
err
)
ldb
.
Close
()
}
func
signTx
(
tx
*
types
.
Transaction
,
hexPrivKey
string
)
(
*
types
.
Transaction
,
error
)
{
...
...
plugin/dapp/trade/executor/local_order.go
0 → 100644
View file @
3f71a8a5
This diff is collapsed.
Click to expand it.
plugin/dapp/trade/executor/local_order_test.go
0 → 100644
View file @
3f71a8a5
package
executor
import
(
"testing"
"github.com/33cn/chain33/system/dapp"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
//"github.com/33cn/chain33/common/db"
//"github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/util"
"github.com/stretchr/testify/assert"
)
var
order1
=
&
pty
.
LocalOrder
{
AssetSymbol
:
"A"
,
Owner
:
"O1"
,
AmountPerBoardlot
:
1
,
MinBoardlot
:
1
,
PricePerBoardlot
:
1
,
TotalBoardlot
:
10
,
TradedBoardlot
:
0
,
BuyID
:
"B1"
,
Status
:
pty
.
TradeOrderStatusOnBuy
,
SellID
:
""
,
TxHash
:
nil
,
Height
:
1
,
Key
:
"B1"
,
BlockTime
:
1
,
IsSellOrder
:
false
,
AssetExec
:
"a"
,
TxIndex
:
dapp
.
HeightIndexStr
(
1
,
1
),
IsFinished
:
false
,
}
var
order2
=
&
pty
.
LocalOrder
{
AssetSymbol
:
"A"
,
Owner
:
"O1"
,
AmountPerBoardlot
:
1
,
MinBoardlot
:
1
,
PricePerBoardlot
:
1
,
TotalBoardlot
:
10
,
TradedBoardlot
:
0
,
BuyID
:
"B2"
,
Status
:
pty
.
TradeOrderStatusOnBuy
,
SellID
:
""
,
TxHash
:
nil
,
Height
:
2
,
Key
:
"B2"
,
BlockTime
:
2
,
IsSellOrder
:
false
,
AssetExec
:
"a"
,
TxIndex
:
dapp
.
HeightIndexStr
(
2
,
1
),
IsFinished
:
false
,
}
func
TestListAll
(
t
*
testing
.
T
)
{
dir
,
ldb
,
tdb
:=
util
.
CreateTestDB
()
t
.
Log
(
dir
,
ldb
,
tdb
)
odb
:=
NewOrderTable
(
tdb
)
odb
.
Add
(
order1
)
odb
.
Add
(
order2
)
kvs
,
err
:=
odb
.
Save
()
assert
.
Nil
(
t
,
err
)
t
.
Log
(
kvs
)
ldb
.
Close
()
}
plugin/dapp/trade/executor/query.go
View file @
3f71a8a5
...
...
@@ -60,6 +60,10 @@ func (t *trade) Query_GetOnesOrderWithStatus(req *pty.ReqAddrAssets) (types.Mess
return
t
.
GetOnesOrderWithStatus
(
req
)
}
func
(
t
*
trade
)
Query_GetOneOrder
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
return
t
.
GetOneOrder
(
req
)
}
func
(
t
*
trade
)
GetOnesSellOrder
(
addrTokens
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
var
keys
[][]
byte
if
0
==
len
(
addrTokens
.
Token
)
{
...
...
@@ -641,7 +645,7 @@ func (t *trade) loadOrderFromKey(key []byte) *pty.ReplyTradeOrder {
return
txResult2OrderReply
(
txResult
)
}
func
(
t
*
trade
)
GetOnesOrderWithStatus
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
func
(
t
*
trade
)
GetOnesOrderWithStatus
V1
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
fromKey
:=
[]
byte
(
""
)
if
len
(
req
.
FromKey
)
!=
0
{
order
:=
t
.
loadOrderFromKey
([]
byte
(
req
.
FromKey
))
...
...
@@ -674,3 +678,78 @@ func (t *trade) GetOnesOrderWithStatus(req *pty.ReqAddrAssets) (types.Message, e
}
return
&
replys
,
nil
}
func
(
t
*
trade
)
GetOnesOrderWithStatus
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
orderStatus
,
orderType
:=
fromStatus
(
req
.
Status
)
if
orderStatus
==
orderStatusInvalid
||
orderType
==
orderTypeInvalid
{
return
nil
,
types
.
ErrInvalidParam
}
// 使用 owner isFinished 组合
var
order
pty
.
LocalOrder
if
orderStatus
==
orderStatusOn
{
order
.
IsFinished
=
false
}
else
{
order
.
IsFinished
=
true
}
order
.
Owner
=
req
.
Addr
if
len
(
req
.
FromKey
)
>
0
{
order
.
TxIndex
=
req
.
FromKey
}
rows
,
err
:=
list
(
t
.
GetLocalDB
(),
"owner_isFinished"
,
&
order
,
req
.
Count
,
req
.
Direction
)
if
err
!=
nil
{
tradelog
.
Error
(
"GetOnesOrderWithStatus"
,
"err"
,
err
)
return
nil
,
err
}
var
replys
pty
.
ReplyTradeOrders
for
_
,
row
:=
range
rows
{
o
,
ok
:=
row
.
Data
.
(
*
pty
.
LocalOrder
)
if
!
ok
{
tradelog
.
Error
(
"GetOnesOrderWithStatus"
,
"err"
,
"bad row type"
)
return
nil
,
types
.
ErrTypeAsset
}
reply
:=
fmtReply
(
o
)
replys
.
Orders
=
append
(
replys
.
Orders
,
reply
)
}
return
&
replys
,
nil
}
func
fmtReply
(
order
*
pty
.
LocalOrder
)
*
pty
.
ReplyTradeOrder
{
return
&
pty
.
ReplyTradeOrder
{
TokenSymbol
:
order
.
AssetSymbol
,
Owner
:
order
.
Owner
,
AmountPerBoardlot
:
order
.
AmountPerBoardlot
,
MinBoardlot
:
order
.
MinBoardlot
,
PricePerBoardlot
:
order
.
PricePerBoardlot
,
TotalBoardlot
:
order
.
TotalBoardlot
,
TradedBoardlot
:
order
.
TradedBoardlot
,
BuyID
:
order
.
BuyID
,
Status
:
order
.
Status
,
SellID
:
order
.
SellID
,
TxHash
:
order
.
TxHash
[
0
],
Height
:
order
.
Height
,
Key
:
order
.
TxIndex
,
BlockTime
:
order
.
BlockTime
,
IsSellOrder
:
order
.
IsSellOrder
,
AssetExec
:
order
.
AssetExec
,
}
}
func
(
t
*
trade
)
GetOneOrder
(
req
*
pty
.
ReqAddrAssets
)
(
types
.
Message
,
error
)
{
query
:=
NewOrderTable
(
t
.
GetLocalDB
())
tradelog
.
Debug
(
"query GetData dbg"
,
"primary"
,
req
.
FromKey
)
row
,
err
:=
query
.
GetData
([]
byte
(
req
.
FromKey
))
if
err
!=
nil
{
tradelog
.
Error
(
"query GetData failed"
,
"key"
,
req
.
FromKey
,
"err"
,
err
)
return
nil
,
err
}
o
,
ok
:=
row
.
Data
.
(
*
pty
.
LocalOrder
)
if
!
ok
{
tradelog
.
Error
(
"query GetData failed"
,
"err"
,
"bad row type"
)
return
nil
,
types
.
ErrTypeAsset
}
reply
:=
fmtReply
(
o
)
return
reply
,
nil
}
plugin/dapp/trade/executor/trade.go
View file @
3f71a8a5
...
...
@@ -19,6 +19,7 @@ trade执行器支持trade的创建和交易,
import
(
log
"github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/common/db/table"
drivers
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/trade/types"
...
...
@@ -81,8 +82,16 @@ func genSaveSellKv(sellorder *pty.SellOrder) []*types.KeyValue {
return
kv
}
func
(
t
*
trade
)
saveSell
(
sellID
[]
byte
,
ty
int32
)
[]
*
types
.
KeyValue
{
sellorder
:=
t
.
getSellOrderFromDb
(
sellID
)
func
(
t
*
trade
)
saveSell
(
base
*
pty
.
ReceiptSellBase
,
ty
int32
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
sellorder
:=
t
.
getSellOrderFromDb
([]
byte
(
base
.
SellID
))
if
ty
==
pty
.
TyLogTradeSellLimit
&&
sellorder
.
SoldBoardlot
==
0
{
newOrder
:=
t
.
genSellLimit
(
tx
,
base
,
sellorder
,
txIndex
)
tradelog
.
Info
(
"Table"
,
"sell-add"
,
newOrder
)
ldb
.
Add
(
newOrder
)
}
else
{
t
.
updateSellLimit
(
tx
,
base
,
sellorder
,
txIndex
,
ldb
)
}
return
genSaveSellKv
(
sellorder
)
}
...
...
@@ -106,20 +115,29 @@ func genDeleteSellKv(sellorder *pty.SellOrder) []*types.KeyValue {
return
kv
}
func
(
t
*
trade
)
deleteSell
(
sellID
[]
byte
,
ty
int32
)
[]
*
types
.
KeyValue
{
sellorder
:=
t
.
getSellOrderFromDb
(
sellID
)
func
(
t
*
trade
)
deleteSell
(
base
*
pty
.
ReceiptSellBase
,
ty
int32
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
,
tradedBoardlot
int64
)
[]
*
types
.
KeyValue
{
sellorder
:=
t
.
getSellOrderFromDb
([]
byte
(
base
.
SellID
))
if
ty
==
pty
.
TyLogTradeSellLimit
&&
sellorder
.
SoldBoardlot
==
0
{
ldb
.
Del
([]
byte
(
txIndex
))
}
else
{
t
.
rollBackSellLimit
(
tx
,
base
,
sellorder
,
txIndex
,
ldb
,
tradedBoardlot
)
}
return
genDeleteSellKv
(
sellorder
)
}
func
(
t
*
trade
)
saveBuy
(
receiptTradeBuy
*
pty
.
ReceiptBuyBase
)
[]
*
types
.
KeyValue
{
func
(
t
*
trade
)
saveBuy
(
receiptTradeBuy
*
pty
.
ReceiptBuyBase
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
//tradelog.Info("save", "buy", receiptTradeBuy)
var
kv
[]
*
types
.
KeyValue
order
:=
t
.
genBuyMarket
(
tx
,
receiptTradeBuy
,
txIndex
)
tradelog
.
Debug
(
"trade BuyMarket save local"
,
"order"
,
order
)
ldb
.
Add
(
order
)
return
saveBuyMarketOrderKeyValue
(
kv
,
receiptTradeBuy
,
pty
.
TradeOrderStatusBoughtOut
,
t
.
GetHeight
())
}
func
(
t
*
trade
)
deleteBuy
(
receiptTradeBuy
*
pty
.
ReceiptBuyBase
)
[]
*
types
.
KeyValue
{
func
(
t
*
trade
)
deleteBuy
(
receiptTradeBuy
*
pty
.
ReceiptBuyBase
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
var
kv
[]
*
types
.
KeyValue
ldb
.
Del
([]
byte
(
txIndex
))
return
deleteBuyMarketOrderKeyValue
(
kv
,
receiptTradeBuy
,
pty
.
TradeOrderStatusBoughtOut
,
t
.
GetHeight
())
}
...
...
@@ -145,8 +163,17 @@ func genSaveBuyLimitKv(buyOrder *pty.BuyLimitOrder) []*types.KeyValue {
return
kv
}
func
(
t
*
trade
)
saveBuyLimit
(
buyID
[]
byte
,
ty
int32
)
[]
*
types
.
KeyValue
{
buyOrder
:=
t
.
getBuyOrderFromDb
(
buyID
)
func
(
t
*
trade
)
saveBuyLimit
(
buy
*
pty
.
ReceiptBuyBase
,
ty
int32
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
buyOrder
:=
t
.
getBuyOrderFromDb
([]
byte
(
buy
.
BuyID
))
tradelog
.
Debug
(
"Table"
,
"buy-add"
,
buyOrder
)
if
buyOrder
.
Status
==
pty
.
TradeOrderStatusOnBuy
&&
buy
.
BoughtBoardlot
==
0
{
order
:=
t
.
genBuyLimit
(
tx
,
buy
,
txIndex
)
tradelog
.
Info
(
"Table"
,
"buy-add"
,
order
)
ldb
.
Add
(
order
)
}
else
{
t
.
updateBuyLimit
(
tx
,
buy
,
buyOrder
,
txIndex
,
ldb
)
}
return
genSaveBuyLimitKv
(
buyOrder
)
}
...
...
@@ -170,18 +197,26 @@ func genDeleteBuyLimitKv(buyOrder *pty.BuyLimitOrder) []*types.KeyValue {
return
kv
}
func
(
t
*
trade
)
deleteBuyLimit
(
buyID
[]
byte
,
ty
int32
)
[]
*
types
.
KeyValue
{
buyOrder
:=
t
.
getBuyOrderFromDb
(
buyID
)
func
(
t
*
trade
)
deleteBuyLimit
(
buy
*
pty
.
ReceiptBuyBase
,
ty
int32
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
,
traded
int64
)
[]
*
types
.
KeyValue
{
buyOrder
:=
t
.
getBuyOrderFromDb
([]
byte
(
buy
.
BuyID
))
if
ty
==
pty
.
TyLogTradeBuyLimit
&&
buy
.
BoughtBoardlot
==
0
{
ldb
.
Del
([]
byte
(
txIndex
))
}
else
{
t
.
rollbackBuyLimit
(
tx
,
buy
,
buyOrder
,
txIndex
,
ldb
,
traded
)
}
return
genDeleteBuyLimitKv
(
buyOrder
)
}
func
(
t
*
trade
)
saveSellMarket
(
receiptTradeBuy
*
pty
.
ReceiptSellBase
)
[]
*
types
.
KeyValue
{
func
(
t
*
trade
)
saveSellMarket
(
receiptTradeBuy
*
pty
.
ReceiptSellBase
,
tx
*
types
.
Transaction
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
var
kv
[]
*
types
.
KeyValue
order
:=
t
.
genSellMarket
(
tx
,
receiptTradeBuy
,
txIndex
)
ldb
.
Add
(
order
)
return
saveSellMarketOrderKeyValue
(
kv
,
receiptTradeBuy
,
pty
.
TradeOrderStatusSoldOut
,
t
.
GetHeight
())
}
func
(
t
*
trade
)
deleteSellMarket
(
receiptTradeBuy
*
pty
.
ReceiptSellBase
)
[]
*
types
.
KeyValue
{
func
(
t
*
trade
)
deleteSellMarket
(
receiptTradeBuy
*
pty
.
ReceiptSellBase
,
txIndex
string
,
ldb
*
table
.
Table
)
[]
*
types
.
KeyValue
{
var
kv
[]
*
types
.
KeyValue
ldb
.
Del
([]
byte
(
txIndex
))
return
deleteSellMarketOrderKeyValue
(
kv
,
receiptTradeBuy
,
pty
.
TradeOrderStatusSoldOut
,
t
.
GetHeight
())
}
...
...
plugin/dapp/trade/proto/trade.proto
View file @
3f71a8a5
...
...
@@ -284,6 +284,27 @@ message ReqBuyToken {
string
buyer
=
2
;
}
message
LocalOrder
{
string
assetSymbol
=
1
;
string
owner
=
2
;
int64
amountPerBoardlot
=
3
;
int64
minBoardlot
=
4
;
int64
pricePerBoardlot
=
5
;
int64
totalBoardlot
=
6
;
int64
tradedBoardlot
=
7
;
string
buyID
=
8
;
int32
status
=
9
;
string
sellID
=
10
;
repeated
string
txHash
=
11
;
int64
height
=
12
;
string
key
=
13
;
int64
blockTime
=
14
;
bool
isSellOrder
=
15
;
string
assetExec
=
16
;
string
txIndex
=
17
;
bool
isFinished
=
18
;
}
service
trade
{
rpc
CreateRawTradeSellTx
(
TradeForSell
)
returns
(
UnsignTx
)
{}
rpc
CreateRawTradeBuyTx
(
TradeForBuy
)
returns
(
UnsignTx
)
{}
...
...
plugin/dapp/trade/types/const.go
View file @
3f71a8a5
...
...
@@ -35,6 +35,9 @@ const (
TradeOrderStatusOnBuy
TradeOrderStatusBoughtOut
TradeOrderStatusBuyRevoked
TradeOrderStatusSellHalfRevoked
TradeOrderStatusBuyHalfRevoked
TradeOrderStatusGroupComplete
)
//SellOrderStatus : sell order status map
...
...
plugin/dapp/trade/types/trade.pb.go
View file @
3f71a8a5
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