Commit c9fbaf73 authored by pengjun's avatar pengjun

#627 add query interfaces

parent 6cd7e2b2
...@@ -771,7 +771,7 @@ func (action *Action) CollateralizeFeed(feed *pty.CollateralizeFeed) (*types.Rec ...@@ -771,7 +771,7 @@ func (action *Action) CollateralizeFeed(feed *pty.CollateralizeFeed) (*types.Rec
return nil, pty.ErrPriceInvalid return nil, pty.ErrPriceInvalid
} }
collIDRecords, err := queryCollateralizes(action.localDB) collIDRecords, err := queryCollateralizeByStatus(action.localDB, pty.CollateralizeStatusCreated)
if err != nil { if err != nil {
clog.Error("CollateralizePriceFeed", "get collateralize record error", err) clog.Error("CollateralizePriceFeed", "get collateralize record error", err)
return nil, err return nil, err
...@@ -871,10 +871,10 @@ func queryCollateralizeByID(db dbm.KV, CollateralizeID string) (*pty.Collaterali ...@@ -871,10 +871,10 @@ func queryCollateralizeByID(db dbm.KV, CollateralizeID string) (*pty.Collaterali
return &coll, nil return &coll, nil
} }
func queryCollateralizes(localdb dbm.Lister) ([]*pty.CollateralizeRecord, error) { func queryCollateralizeByStatus(localdb dbm.Lister, status int32) ([]*pty.CollateralizeRecord, error) {
data, err := localdb.List(calcCollateralizePrefix(), nil, DefultCount, ListDESC) data, err := localdb.List(calcCollateralizeStatusPrefix(status), nil, DefultCount, ListDESC)
if err != nil { if err != nil {
clog.Debug("queryCollateralizes", "error", err) clog.Debug("queryCollateralizesByStatus", "error", err)
return nil, err return nil, err
} }
...@@ -883,11 +883,53 @@ func queryCollateralizes(localdb dbm.Lister) ([]*pty.CollateralizeRecord, error) ...@@ -883,11 +883,53 @@ func queryCollateralizes(localdb dbm.Lister) ([]*pty.CollateralizeRecord, error)
for _, collBytes := range data { for _, collBytes := range data {
err = types.Decode(collBytes, &coll) err = types.Decode(collBytes, &coll)
if err != nil { if err != nil {
clog.Debug("queryCollateralizeByID", "decode", err) clog.Debug("queryCollateralizesByStatus", "decode", err)
return nil, err return nil, err
} }
colls = append(colls, &coll) colls = append(colls, &coll)
} }
return colls, nil return colls, nil
} }
\ No newline at end of file
func queryCollateralizeByAddr(localdb dbm.Lister, addr string) ([]*pty.CollateralizeRecord, error) {
data, err := localdb.List(calcCollateralizeAddrPrefix(addr), nil, DefultCount, ListDESC)
if err != nil {
clog.Debug("queryCollateralizesByAddr", "error", err)
return nil, err
}
var colls []*pty.CollateralizeRecord
var coll pty.CollateralizeRecord
for _, collBytes := range data {
err = types.Decode(collBytes, &coll)
if err != nil {
clog.Debug("queryCollateralizesByAddr", "decode", err)
return nil, err
}
colls = append(colls, &coll)
}
return colls, nil
}
func queryCollateralizeRecordByStatus(localdb dbm.Lister, status int32) ([]*pty.CollateralizeRecord, error) {
data, err := localdb.List(calcCollateralizeRecordStatusPrefix(status), nil, DefultCount, ListDESC)
if err != nil {
clog.Debug("queryCollateralizeRecordByStatus", "error", err)
return nil, err
}
var colls []*pty.CollateralizeRecord
var coll pty.CollateralizeRecord
for _, collBytes := range data {
err = types.Decode(collBytes, &coll)
if err != nil {
clog.Debug("queryCollateralizesByStatus", "decode", err)
return nil, err
}
colls = append(colls, &coll)
}
return colls, nil
}
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package executor package executor
import ( import (
//"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/collateralize/types" pty "github.com/33cn/plugin/plugin/dapp/collateralize/types"
) )
......
...@@ -3,3 +3,134 @@ ...@@ -3,3 +3,134 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package executor package executor
import (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/collateralize/types"
)
func (c *Collateralize) Query_CollateralizeInfoByID(req *pty.ReqCollateralizeInfo) (types.Message, error) {
coll,err := queryCollateralizeByID(c.GetStateDB(), req.CollateralizeId)
if err != nil {
clog.Error("Query_CollateralizeInfoByID", "id", req.CollateralizeId, "error", err)
return nil, err
}
return &pty.RepCollateralizeCurrentInfo{
Status: coll.Status,
TotalBalance: coll.TotalBalance,
DebtCeiling: coll.DebtCeiling,
LiquidationRatio: coll.LiquidationRatio,
LiquidationPenalty: coll.LiquidationPenalty,
StabilityFee: coll.StabilityFee,
CreateAddr: coll.CreateAddr,
Balance: coll.Balance,
}, nil
}
func (c *Collateralize) Query_CollateralizeInfoByIDs(req *pty.ReqCollateralizeInfos) (types.Message, error) {
infos := &pty.RepCollateralizeCurrentInfos{}
for _, id := range req.CollateralizeIds {
coll,err := queryCollateralizeByID(c.GetStateDB(), id)
if err != nil {
clog.Error("Query_CollateralizeInfoByID", "id", id, "error", err)
return nil, err
}
infos.Infos = append(infos.Infos, &pty.RepCollateralizeCurrentInfo{
Status: coll.Status,
TotalBalance: coll.TotalBalance,
DebtCeiling: coll.DebtCeiling,
LiquidationRatio: coll.LiquidationRatio,
LiquidationPenalty: coll.LiquidationPenalty,
StabilityFee: coll.StabilityFee,
CreateAddr: coll.CreateAddr,
Balance: coll.Balance,
})
}
return infos, nil
}
func (c *Collateralize) Query_CollateralizeByStatus(req *pty.ReqCollateralizeByStatus) (types.Message, error) {
ids := &pty.RepCollateralizeIDs{}
collIDRecords, err := queryCollateralizeByStatus(c.GetLocalDB(), pty.CollateralizeStatusCreated)
if err != nil {
clog.Error("Query_CollateralizeByStatus", "get collateralize record error", err)
return nil, err
}
for _, record := range collIDRecords {
ids.IDs = append(ids.IDs, record.CollateralizeId)
}
return ids, nil
}
func (c *Collateralize) Query_CollateralizeByAddr(req *pty.ReqCollateralizeByAddr) (types.Message, error) {
ids := &pty.RepCollateralizeIDs{}
collIDRecords, err := queryCollateralizeByAddr(c.GetLocalDB(), req.Addr)
if err != nil {
clog.Error("Query_CollateralizeByAddr", "get collateralize record error", err)
return nil, err
}
for _, record := range collIDRecords {
ids.IDs = append(ids.IDs, record.CollateralizeId)
}
return ids, nil
}
func (c *Collateralize) Query_CollateralizeBorrowInfoByAddr(req *pty.ReqCollateralizeBorrowInfoByAddr) (types.Message, error) {
records, err := queryCollateralizeByAddr(c.GetLocalDB(), req.Addr)
if err != nil {
clog.Error("Query_CollateralizeBorrowInfoByAddr", "get collateralize record error", err)
return nil, err
}
for _, record := range records {
if record.CollateralizeId == req.CollateralizeId {
coll, err := queryCollateralizeByID(c.GetStateDB(), record.CollateralizeId)
if err != nil {
clog.Error("Query_CollateralizeBorrowInfoByAddr", "get collateralize record error", err)
return nil, err
}
for _, borrowRecord := range coll.BorrowRecords {
if borrowRecord.AccountAddr == req.Addr {
ret := &pty.RepCollateralizeBorrowInfo{}
ret.Record = borrowRecord
return ret, nil
}
}
}
}
return nil, pty.ErrRecordNotExist
}
func (c *Collateralize) Query_CollateralizeBorrowInfoByStatus(req *pty.ReqCollateralizeBorrowInfoByStatus) (types.Message, error) {
records, err := queryCollateralizeRecordByStatus(c.GetLocalDB(), req.Status)
if err != nil {
clog.Error("Query_CollateralizeBorrowInfoByAddr", "get collateralize record error", err)
return nil, err
}
ret := &pty.RepCollateralizeBorrowInfos{}
for _, record := range records {
coll, err := queryCollateralizeByID(c.GetStateDB(), record.CollateralizeId)
if err != nil {
clog.Error("Query_CollateralizeBorrowInfoByAddr", "get collateralize record error", err)
return nil, err
}
for _, borrowRecord := range coll.BorrowRecords {
if borrowRecord.Status == req.Status {
ret.Record = append(ret.Record, borrowRecord)
}
}
}
return ret, nil
}
\ No newline at end of file
...@@ -86,12 +86,12 @@ message CollateralizeFeed { ...@@ -86,12 +86,12 @@ message CollateralizeFeed {
repeated int64 volume = 3; //成交量 repeated int64 volume = 3; //成交量
} }
// 喂价关闭 // 借贷关闭
message CollateralizeClose { message CollateralizeClose {
string collateralizeId = 1; //借贷期数ID string collateralizeId = 1; //借贷期数ID
} }
// exec_local记录信息 // exec_local 借贷信息
message ReceiptCollateralize { message ReceiptCollateralize {
string collateralizeId = 1; string collateralizeId = 1;
string createAddr = 2; string createAddr = 2;
...@@ -102,28 +102,25 @@ message ReceiptCollateralize { ...@@ -102,28 +102,25 @@ message ReceiptCollateralize {
int32 recordStatus = 7; int32 recordStatus = 7;
} }
message ReqCollateralizeInfo { // exec_local 借贷记录信息
string collateralizeId = 1; message CollateralizeRecord {
}
message ReqCollateralizeBorrowInfo {
string collateralizeId = 1; string collateralizeId = 1;
string addr = 2; string addr = 2;
int64 index = 3;
} }
message ReqCollateralizeBadDebt { // exec_local 借贷记录信息列表
string collateralizeId = 1; message CollateralizeRecords {
repeated CollateralizeRecord records = 1;
} }
message ReqCollateralizeBorrowHistory { // 根据ID查询一期借贷信息
message ReqCollateralizeInfo {
string collateralizeId = 1; string collateralizeId = 1;
string addr = 2;
int32 count = 4;
int32 direction = 5;
int64 index = 6;
} }
message ReplyCollateralizeCurrentInfo { // 返回一期借贷信息
message RepCollateralizeCurrentInfo {
int32 status = 1;//当期借贷的状态,是否关闭 int32 status = 1;//当期借贷的状态,是否关闭
int64 totalBalance = 2; //当期可借贷的总金额(ccny) int64 totalBalance = 2; //当期可借贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny) int64 debtCeiling = 3; //单用户可借出的限额(ccny)
...@@ -134,17 +131,49 @@ message ReplyCollateralizeCurrentInfo { ...@@ -134,17 +131,49 @@ message ReplyCollateralizeCurrentInfo {
int64 balance = 8; //剩余可借贷金额(ccny) int64 balance = 8; //剩余可借贷金额(ccny)
} }
message ReplyCollateralizeBadDebt { // 根据ID列表查询多期借贷信息
repeated BorrowRecord records = 1; message ReqCollateralizeInfos {
repeated string collateralizeIds = 1;
} }
// used for execlocal // 返回多期借贷信息
message CollateralizeRecord { message RepCollateralizeCurrentInfos {
repeated RepCollateralizeCurrentInfo infos = 1;
}
// 根据借贷状态查询
message ReqCollateralizeByStatus {
int32 status = 1;
}
// 根据用户地址查询
message ReqCollateralizeByAddr {
string addr = 1;
}
// 返回借贷ID列表
message RepCollateralizeIDs {
repeated string IDs = 1;
}
// 根据地址和借贷ID混合查询具体借贷记录
message ReqCollateralizeBorrowInfoByAddr {
string collateralizeId = 1; string collateralizeId = 1;
string addr = 2; string addr = 2;
int64 index = 3;
} }
message CollateralizeRecords { // 返回借贷记录
repeated CollateralizeRecord records = 1; message RepCollateralizeBorrowInfo {
BorrowRecord record = 1;
}
// 根据状态和借贷ID混合查询具体借贷记录
message ReqCollateralizeBorrowInfoByStatus {
string collateralizeId = 1;
int32 status = 2;
}
// 返回借贷记录
message RepCollateralizeBorrowInfos {
repeated BorrowRecord record = 1;
} }
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