Commit 172ed60e authored by pengjun's avatar pengjun

#627 add price query api

parent f4596d49
......@@ -335,6 +335,27 @@ func CollateralizeQueryConfig(cmd *cobra.Command, args []string) {
ctx.Run()
}
func CollateralizeQueryPriceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "price",
Short: "Query latest price",
Run: CollateralizeQueryPrice,
}
return cmd
}
func CollateralizeQueryPrice(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
var params rpctypes.Query4Jrpc
params.Execer = pkt.CollateralizeX
params.FuncName = "CollateralizePrice"
var res pkt.RepCollateralizePrice
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}
// CollateralizeQueryCmd 查询命令行
func CollateralizeQueryCmd() *cobra.Command {
cmd := &cobra.Command{
......@@ -345,6 +366,7 @@ func CollateralizeQueryCmd() *cobra.Command {
addCollateralizeQueryFlags(cmd)
cmd.AddCommand(
CollateralizeQueryCfgCmd(),
CollateralizeQueryPriceCmd(),
)
return cmd
}
......
......@@ -457,7 +457,7 @@ func calcLiquidationPrice(value int64, colValue int64) float32 {
}
// 获取最近抵押物价格
func (action *Action)getLatestPrice(db dbm.KV) (float32, error) {
func getLatestPrice(db dbm.KV) (float32, error) {
data, err := db.Get(PriceKey())
if err != nil {
clog.Error("getLatestPrice", "get", err)
......@@ -537,7 +537,7 @@ func (action *Action) CollateralizeBorrow(borrow *pty.CollateralizeBorrow) (*typ
clog.Debug("CollateralizeBorrow", "value", borrow.GetValue())
// 获取抵押物价格
lastPrice, err := action.getLatestPrice(action.db)
lastPrice, err := getLatestPrice(action.db)
if err != nil {
clog.Error("CollateralizeBorrow", "CollID", coll.CollateralizeId, "addr", action.fromaddr, "execaddr", action.execaddr, "err", err)
return nil, err
......@@ -740,7 +740,7 @@ func (action *Action) CollateralizeAppend(cAppend *pty.CollateralizeAppend) (*ty
clog.Debug("CollateralizeAppend", "value", cAppend.CollateralValue)
// 获取抵押物价格
lastPrice, err := action.getLatestPrice(action.db)
lastPrice, err := getLatestPrice(action.db)
if err != nil {
clog.Error("CollateralizeBorrow", "CollID", coll.CollateralizeId, "addr", action.fromaddr, "execaddr", action.execaddr, "err", err)
return nil, err
......
......@@ -158,3 +158,13 @@ func (c *Collateralize) Query_CollateralizeConfig(req *pty.ReqCollateralizeRecor
return ret, nil
}
func (c *Collateralize) Query_CollateralizePrice(req *pty.ReqCollateralizeRecordByAddr) (types.Message, error) {
price, err := getLatestPrice(c.GetStateDB())
if err != nil {
clog.Error("Query_CollateralizePrice", "error", err)
return nil, err
}
return &pty.RepCollateralizePrice{Price:price}, nil
}
\ No newline at end of file
......@@ -224,3 +224,8 @@ message RepCollateralizeConfig {
int64 collBalance = 6; //剩余放贷额度
int64 currentTime = 7; //设置时间
}
// 返回最新抵押物价格
message RepCollateralizePrice {
float price = 1; //当前抵押物最新价格
}
\ No newline at end of file
......@@ -269,6 +269,27 @@ func IssuanceManage(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
func IssuacneQueryPriceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "price",
Short: "Query latest price",
Run: IssuanceQueryPrice,
}
return cmd
}
func IssuanceQueryPrice(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
var params rpctypes.Query4Jrpc
params.Execer = pkt.IssuanceX
params.FuncName = "IssuancePrice"
var res pkt.RepIssuancePrice
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}
// IssuanceQueryCmd 查询命令行
func IssuanceQueryCmd() *cobra.Command {
cmd := &cobra.Command{
......@@ -277,6 +298,9 @@ func IssuanceQueryCmd() *cobra.Command {
Run: IssuanceQuery,
}
addIssuanceQueryFlags(cmd)
cmd.AddCommand(
IssuacneQueryPriceCmd(),
)
return cmd
}
......
......@@ -458,7 +458,7 @@ func getBtyNumToFrozen(value int64, price float32, ratio float32) (int64,error)
}
// 获取最近抵押物价格
func (action *Action)getLatestPrice(db dbm.KV) (float32, error) {
func getLatestPrice(db dbm.KV) (float32, error) {
data, err := db.Get(PriceKey())
if err != nil {
clog.Error("getLatestPrice", "get", err)
......@@ -544,7 +544,7 @@ func (action *Action) IssuanceDebt(debt *pty.IssuanceDebt) (*types.Receipt, erro
clog.Debug("IssuanceDebt", "value", debt.GetValue())
// 获取抵押物价格
lastPrice, err := action.getLatestPrice(action.db)
lastPrice, err := getLatestPrice(action.db)
if err != nil {
clog.Error("IssuanceDebt", "CollID", issu.IssuanceId, "addr", action.fromaddr, "execaddr", action.execaddr, "err", err)
return nil, err
......
......@@ -112,3 +112,13 @@ func (c *Issuance) Query_IssuanceRecordsByStatus(req *pty.ReqIssuanceRecordsBySt
ret.Records = append(ret.Records, records...)
return ret, nil
}
func (c *Issuance) Query_IssuancePrice(req *pty.ReqIssuanceRecordsByStatus) (types.Message, error) {
price, err := getLatestPrice(c.GetStateDB())
if err != nil {
clog.Error("Query_CollateralizePrice", "error", err)
return nil, err
}
return &pty.RepIssuancePrice{Price:price}, nil
}
\ No newline at end of file
......@@ -192,3 +192,8 @@ message ReqIssuanceDebtInfo {
message RepIssuanceDebtInfo {
DebtRecord record = 1;
}
// 返回最新抵押物价格
message RepIssuancePrice {
float price = 1; //当前抵押物最新价格
}
\ No newline at end of file
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