Commit 550958aa authored by pengjun's avatar pengjun

#617 add issuance contract

parent 85aa1039
......@@ -237,7 +237,7 @@ func CollateralizeManageRawTxCmd() *cobra.Command {
Short: "manage a collateralize",
Run: CollateralizeManage,
}
addCollateralizeCloseFlags(cmd)
addCollateralizeManageFlags(cmd)
return cmd
}
......@@ -246,7 +246,6 @@ func addCollateralizeManageFlags(cmd *cobra.Command) {
cmd.Flags().Float32P("liquidationRatio", "l", 0, "liquidationRatio")
cmd.Flags().Float32P("stabilityFeeRatio", "s", 0, "stabilityFeeRatio")
cmd.Flags().Uint64P("period", "p", 0, "period")
cmd.Flags().StringP("addr", "a", "", "addr")
}
func CollateralizeManage(cmd *cobra.Command, args []string) {
......@@ -255,23 +254,13 @@ func CollateralizeManage(cmd *cobra.Command, args []string) {
liquidationRatio, _ := cmd.Flags().GetFloat32("liquidationRatio")
stabilityFeeRatio, _ := cmd.Flags().GetFloat32("stabilityFeeRatio")
period, _ := cmd.Flags().GetUint64("period")
addr, _ := cmd.Flags().GetString("addr")
var params *rpctypes.CreateTxIn
if addr == "" {
params = &rpctypes.CreateTxIn{
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.CollateralizeX),
ActionName: "CollateralizeManage",
Payload: []byte(fmt.Sprintf("{\"debtCeiling\":%d, \"liquidationRatio\":%f, \"stabilityFeeRatio\":%f, \"period\":%s,}",
debtCeiling, liquidationRatio, stabilityFeeRatio, period)),
}
} else {
params = &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.CollateralizeX),
ActionName: "CollateralizeManage",
Payload: []byte(fmt.Sprintf("{[\"addr\":%s]}", addr)),
}
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
......
......@@ -44,3 +44,9 @@ func (c *Collateralize) Exec_Close(payload *pty.CollateralizeClose, tx *types.Tr
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeClose(payload)
}
// Exec_Close Action
func (c *Collateralize) Exec_Manage(payload *pty.CollateralizeManage, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeManage(payload)
}
......@@ -78,3 +78,8 @@ func (c *Collateralize) ExecDelLocal_Feed(payload *pty.CollateralizeFeed, tx *ty
func (c *Collateralize) ExecDelLocal_Close(payload *pty.CollateralizeClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Manage Action
func (c *Collateralize) ExecDelLocal_Manage(payload *pty.CollateralizeManage, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
\ No newline at end of file
......@@ -76,3 +76,8 @@ func (c *Collateralize) ExecLocal_Feed(payload *pty.CollateralizeFeed, tx *types
func (c *Collateralize) ExecLocal_Close(payload *pty.CollateralizeClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Manage Action
func (c *Collateralize) ExecLocal_Manage(payload *pty.CollateralizeManage, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
......@@ -11,11 +11,6 @@ func calcCollateralizeKey(collateralizeID string, index int64) []byte {
return []byte(key)
}
func calcCollateralizePrefix() []byte {
key := fmt.Sprintf("LODB-Collateralize-ID:")
return []byte(key)
}
func calcCollateralizeStatusPrefix(status int32) []byte {
key := fmt.Sprintf("LODB-Collateralize-status-index:%d", status)
return []byte(key)
......
......@@ -31,6 +31,7 @@ message BorrowRecord {
int32 status = 7; //抵押状态,是否被清算
int64 liquidateTime = 8; //清算时间
int64 expireTime = 9; //超时清算时间
int32 preStatus = 10;//上一次抵押状态,用于告警恢复
}
// 资产价格记录
......@@ -39,7 +40,6 @@ message AssetPriceRecord {
float btyPrice = 2; //bty价格
float btcPrice = 3; //btc价格
float ethPrice = 4; //eth价格
int64 preRecordTime = 5; //上一次记录时间
}
// action
......@@ -57,11 +57,6 @@ message CollateralizeAction {
}
message CollateralizeManage {
CollateralizeConfig cfg = 1; //借贷配置
CollateralizeAddr addr = 2; //地址配置
}
message CollateralizeConfig {
int64 debtCeiling = 1; //单用户可借出的限额(ccny)
float liquidationRatio = 2; //清算比例
float stabilityFeeRatio = 3; //稳定费
......@@ -141,9 +136,8 @@ message RepCollateralizeCurrentInfo {
int64 debtCeiling = 3; //单用户可借出的限额(ccny)
float liquidationRatio = 4; //清算比例
float stabilityFeeRatio = 5; //稳定费
int64 liquidationPenalty = 6; //清算罚金
string createAddr = 7; //创建人地址
int64 balance = 8; //剩余可借贷金额(ccny)
string createAddr = 6; //创建人地址
int64 balance = 7; //剩余可借贷金额(ccny)
}
// 根据ID列表查询多期借贷信息
......
......@@ -116,11 +116,7 @@ func (Collateralize CollateralizeType) CreateTx(action string, message json.RawM
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
if param.Addr != nil {
return CreateRawCollateralizeManageAddrTx(&param)
}
return CreateRawCollateralizeManageConfigTx(&param)
return CreateRawCollateralizeManageTx(&param)
} else {
return nil, types.ErrNotSupport
}
......@@ -178,13 +174,13 @@ func CreateRawCollateralizeBorrowTx(parm *CollateralizeBorrowTx) (*types.Transac
CollateralizeId: parm.CollateralizeID,
Value: parm.Value,
}
Borrow := &CollateralizeAction{
borrow := &CollateralizeAction{
Ty: CollateralizeActionBorrow,
Value: &CollateralizeAction_Borrow{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(Borrow),
Payload: types.Encode(borrow),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
......@@ -207,13 +203,13 @@ func CreateRawCollateralizeRepayTx(parm *CollateralizeRepayTx) (*types.Transacti
CollateralizeId: parm.CollateralizeID,
Value: parm.Value,
}
Repay := &CollateralizeAction{
repay := &CollateralizeAction{
Ty: CollateralizeActionRepay,
Value: &CollateralizeAction_Repay{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(Repay),
Payload: types.Encode(repay),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
......@@ -236,13 +232,13 @@ func CreateRawCollateralizeAppendTx(parm *CollateralizeAppendTx) (*types.Transac
CollateralizeId: parm.CollateralizeID,
CollateralValue: parm.Value,
}
Repay := &CollateralizeAction{
append := &CollateralizeAction{
Ty: CollateralizeActionAppend,
Value: &CollateralizeAction_Append{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(Repay),
Payload: types.Encode(append),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
......@@ -265,13 +261,13 @@ func CreateRawCollateralizeFeedTx(parm *CollateralizeFeedTx) (*types.Transaction
Price: parm.Price,
Volume: parm.Volume,
}
Feed := &CollateralizeAction{
feed := &CollateralizeAction{
Ty: CollateralizeActionFeed,
Value: &CollateralizeAction_Feed{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(Feed),
Payload: types.Encode(feed),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
......@@ -312,57 +308,27 @@ func CreateRawCollateralizeCloseTx(parm *CollateralizeCloseTx) (*types.Transacti
return tx, nil
}
// CreateRawCollateralizeManageConfigTx method
func CreateRawCollateralizeManageConfigTx(parm *CollateralizeManageTx) (*types.Transaction, error) {
// CreateRawCollateralizeManageTx method
func CreateRawCollateralizeManageTx(parm *CollateralizeManageTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawCollateralizeManageTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &CollateralizeManage{}
v.Cfg = &CollateralizeConfig{
v := &CollateralizeManage{
DebtCeiling: parm.DebtCeiling,
LiquidationRatio: parm.LiquidationRatio,
StabilityFeeRatio: parm.StabilityFeeRatio,
Period: parm.Period,
}
close := &CollateralizeAction{
manage := &CollateralizeAction{
Ty: CollateralizeActionManage,
Value: &CollateralizeAction_Manage{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(close),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
name := types.ExecName(CollateralizeX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawCollateralizeManageAddrTx method
func CreateRawCollateralizeManageAddrTx(parm *CollateralizeManageTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawCollateralizeManageTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &CollateralizeManage{}
v.Addr = &CollateralizeAddr{SuperAddrs:parm.Addr}
close := &CollateralizeAction{
Ty: CollateralizeActionManage,
Value: &CollateralizeAction_Manage{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(CollateralizeX)),
Payload: types.Encode(close),
Payload: types.Encode(manage),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(CollateralizeX)),
}
......
......@@ -50,6 +50,5 @@ type CollateralizeManageTx struct {
LiquidationRatio float32 `json:"liquidationRatio"`
StabilityFeeRatio float32 `json:"stabilityFeeRatio"`
Period int64 `json:"period"`
Addr []string `json:"addr"`
Fee int64 `json:"fee"`
}
all:
chmod +x ./build.sh
./build.sh $(OUT) $(FLAG)
\ No newline at end of file
#!/bin/sh
strpwd=$(pwd)
strcmd=${strpwd##*dapp/}
strapp=${strcmd%/cmd*}
OUT_DIR="${1}/$strapp"
#FLAG=$2
mkdir -p "${OUT_DIR}"
cp ./build/* "${OUT_DIR}"
OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}"
cp ./build/test-rpc.sh "${OUT_TESTDIR}"
package commands
import (
"fmt"
"github.com/spf13/cobra"
jsonrpc "github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
pkt "github.com/33cn/plugin/plugin/dapp/issuance/types"
"strconv"
)
// IssuanceCmd 斗牛游戏命令行
func IssuanceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "issuance",
Short: "Issuance command",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
IssuanceCreateRawTxCmd(),
IssuanceDebtRawTxCmd(),
IssuanceRepayRawTxCmd(),
IssuancePriceFeedRawTxCmd(),
IssuanceCloseRawTxCmd(),
IssuanceManageRawTxCmd(),
IssuanceQueryCmd(),
)
return cmd
}
// IssuanceCreateRawTxCmd 生成开始交易命令行
func IssuanceCreateRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a issuance",
Run: IssuanceCreate,
}
addIssuanceCreateFlags(cmd)
return cmd
}
func addIssuanceCreateFlags(cmd *cobra.Command) {
cmd.Flags().Uint64P("balance", "b", 0, "balance")
cmd.Flags().Uint64P("debtCeiling", "d", 0, "debtCeiling")
cmd.Flags().Float32P("liquidationRatio", "l", 0, "liquidationRatio")
cmd.Flags().Uint64P("period", "p", 0, "period")
}
func IssuanceCreate(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
balance, _ := cmd.Flags().GetUint64("balance")
debtCeiling, _ := cmd.Flags().GetUint64("debtCeiling")
liquidationRatio, _ := cmd.Flags().GetFloat32("liquidationRatio")
period, _ := cmd.Flags().GetUint64("period")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuanceCreate",
Payload: []byte(fmt.Sprintf("{\"balance\":%d, \"debtCeiling\":%d, \"liquidationRatio\":%f, \"period\":%d,}",
balance, debtCeiling, liquidationRatio, period)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuanceDebtRawTxCmd 生成开始交易命令行
func IssuanceDebtRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "debt",
Short: "Debt a issuance",
Run: IssuanceDebt,
}
addIssuanceDebtFlags(cmd)
return cmd
}
func addIssuanceDebtFlags(cmd *cobra.Command) {
cmd.Flags().StringP("issuanceID", "g", "", "issuance ID")
cmd.MarkFlagRequired("issuanceID")
cmd.Flags().Uint64P("value", "v", 0, "value")
cmd.MarkFlagRequired("value")
}
func IssuanceDebt(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
issuanceID, _ := cmd.Flags().GetString("issuanceID")
value, _ := cmd.Flags().GetUint64("value")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuanceDebt",
Payload: []byte(fmt.Sprintf("{\"issuanceID\":%s,\"value\":%d}", issuanceID, value)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuanceRepayRawTxCmd 生成开始交易命令行
func IssuanceRepayRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "repay",
Short: "Repay a issuance",
Run: IssuanceRepay,
}
addIssuanceRepayFlags(cmd)
return cmd
}
func addIssuanceRepayFlags(cmd *cobra.Command) {
cmd.Flags().StringP("issuanceID", "g", "", "issuance ID")
cmd.MarkFlagRequired("issuanceID")
}
func IssuanceRepay(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
issuanceID, _ := cmd.Flags().GetString("issuanceID")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuanceRepay",
Payload: []byte(fmt.Sprintf("{\"issuanceID\":%s}", issuanceID)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuancePriceFeedRawTxCmd 生成开始交易命令行
func IssuancePriceFeedRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "feed",
Short: "price feed",
Run: IssuancePriceFeed,
}
addIssuancePriceFeedFlags(cmd)
return cmd
}
func addIssuancePriceFeedFlags(cmd *cobra.Command) {
cmd.Flags().Float32P("price", "p", 0, "price")
cmd.MarkFlagRequired("price")
cmd.Flags().Uint64P("volume", "v", 0, "volume")
cmd.MarkFlagRequired("volume")
}
func IssuancePriceFeed(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
price, _ := cmd.Flags().GetFloat32("price")
volume, _ := cmd.Flags().GetUint64("volume")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuancePriceFeed",
Payload: []byte(fmt.Sprintf("{[\"price\":%s],[\"volume\":%d]}", price, volume)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuanceCloseRawTxCmd 生成开始交易命令行
func IssuanceCloseRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "close",
Short: "close a issuance",
Run: IssuanceClose,
}
addIssuanceCloseFlags(cmd)
return cmd
}
func addIssuanceCloseFlags(cmd *cobra.Command) {
cmd.Flags().StringP("issuanceID", "g", "", "issuance ID")
cmd.MarkFlagRequired("issuanceID")
}
func IssuanceClose(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
issuanceID, _ := cmd.Flags().GetString("issuanceID")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuanceClose",
Payload: []byte(fmt.Sprintf("{\"issuanceID\":%s}", issuanceID)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuanceManageRawTxCmd 生成开始交易命令行
func IssuanceManageRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "manage",
Short: "manage a issuance",
Run: IssuanceManage,
}
addIssuanceManageFlags(cmd)
return cmd
}
func addIssuanceManageFlags(cmd *cobra.Command) {
cmd.Flags().StringP("addr", "a", "", "addr")
}
func IssuanceManage(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
addr, _ := cmd.Flags().GetString("addr")
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.IssuanceX),
ActionName: "IssuanceManage",
Payload: []byte(fmt.Sprintf("{[\"addr\":%s]}", addr)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
// IssuanceQueryCmd 查询命令行
func IssuanceQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "Query result",
Run: IssuanceQuery,
}
addIssuanceQueryFlags(cmd)
return cmd
}
func addIssuanceQueryFlags(cmd *cobra.Command) {
cmd.Flags().StringP("issuanceID", "g", "", "issuance ID")
cmd.Flags().StringP("address", "a", "", "address")
cmd.Flags().StringP("index", "i", "", "index")
cmd.Flags().StringP("status", "s", "", "status")
cmd.Flags().StringP("issuanceIDs", "d", "", "issuance IDs")
}
func IssuanceQuery(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
issuanceID, _ := cmd.Flags().GetString("issuanceID")
address, _ := cmd.Flags().GetString("address")
statusStr, _ := cmd.Flags().GetString("status")
// indexstr, _ := cmd.Flags().GetString("index")
issuanceIDs, _ := cmd.Flags().GetString("issuanceIDs")
var params rpctypes.Query4Jrpc
params.Execer = pkt.IssuanceX
//if indexstr != "" {
// index, err := strconv.ParseInt(indexstr, 10, 64)
// if err != nil {
// fmt.Println(err)
// cmd.Help()
// return
// }
// req.Index = index
//}
status, err := strconv.ParseInt(statusStr, 10, 32)
if err != nil {
fmt.Println(err)
cmd.Help()
return
}
if issuanceID != "" {
if statusStr != "" {
params.FuncName = "IssuanceDebtInfoByStatus"
req := &pkt.ReqIssuanceDebtInfoByStatus{
IssuanceId: issuanceID,
Status: int32(status),
}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceDebtInfos
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if address != "" {
params.FuncName = "IssuanceDebtInfoByAddr"
req := &pkt.ReqIssuanceDebtInfoByAddr{
IssuanceId: issuanceID,
Addr: address,
}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceDebtInfos
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else {
params.FuncName = "IssuanceInfoByID"
req := &pkt.ReqIssuanceInfo{
IssuanceId: issuanceID,
}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceCurrentInfo
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}
} else if address != "" {
params.FuncName = "IssuanceByAddr"
req := &pkt.ReqIssuanceByAddr{Addr: address}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceIDs
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if statusStr != "" {
params.FuncName = "IssuanceByStatus"
req := &pkt.ReqIssuanceByStatus{Status:int32(status)}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceIDs
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if issuanceIDs != "" {
params.FuncName = "IssuanceInfoByIDs"
var issuanceIDsS []string
issuanceIDsS = append(issuanceIDsS, issuanceIDs)
issuanceIDsS = append(issuanceIDsS, issuanceIDs)
req := &pkt.ReqIssuanceInfos{IssuanceIds: issuanceIDsS}
params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceCurrentInfos
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else {
fmt.Println("Error: requeres at least one of gameID, address or status")
cmd.Help()
}
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
/*
waiting for update
*/
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
)
// Exec_Create Action
func (c *Issuance) Exec_Create(payload *pty.IssuanceCreate, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceCreate(payload)
}
// Exec_Borrow Action
func (c *Issuance) Exec_Borrow(payload *pty.IssuanceDebt, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceDebt(payload)
}
// Exec_Repay Action
func (c *Issuance) Exec_Repay(payload *pty.IssuanceRepay, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceRepay(payload)
}
// Exec_Feed Action
func (c *Issuance) Exec_Feed(payload *pty.IssuanceFeed, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceFeed(payload)
}
// Exec_Close Action
func (c *Issuance) Exec_Close(payload *pty.IssuanceClose, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceClose(payload)
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
)
func (c *Issuance) execDelLocal(tx *types.Transaction, receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
for _, item := range receiptData.Logs {
var IssuanceLog pty.ReceiptIssuance
err := types.Decode(item.Log, &IssuanceLog)
if err != nil {
return nil, err
}
switch item.Ty {
case pty.TyLogIssuanceCreate:
kv := c.deleteIssuanceStatus(&IssuanceLog)
set.KV = append(set.KV, kv...)
break
case pty.TyLogIssuanceDebt:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(&IssuanceLog)...)
set.KV = append(set.KV, c.deleteIssuanceAddr(&IssuanceLog)...)
break
case pty.TyLogIssuanceRepay:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(&IssuanceLog)...)
set.KV = append(set.KV, c.addIssuanceAddr(&IssuanceLog)...)
break
case pty.TyLogIssuanceFeed:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(&IssuanceLog)...)
if IssuanceLog.RecordStatus == pty.IssuanceUserStatusSystemLiquidate {
set.KV = append(set.KV, c.addIssuanceAddr(&IssuanceLog)...)
}
break
case pty.TyLogIssuanceClose:
kv := c.addIssuanceStatus(&IssuanceLog)
set.KV = append(set.KV, kv...)
break
}
}
return set, nil
}
// ExecDelLocal_Create Action
func (c *Issuance) ExecDelLocal_Create(payload *pty.IssuanceCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Debt Action
func (c *Issuance) ExecDelLocal_Debt(payload *pty.IssuanceDebt, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Repay Action
func (c *Issuance) ExecDelLocal_Repay(payload *pty.IssuanceRepay, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Feed Action
func (c *Issuance) ExecDelLocal_Feed(payload *pty.IssuanceFeed, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Close Action
func (c *Issuance) ExecDelLocal_Close(payload *pty.IssuanceClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
\ No newline at end of file
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
//"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
)
func (c *Issuance) execLocal(tx *types.Transaction, receipt *types.ReceiptData) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
for _, item := range receipt.Logs {
var IssuanceLog pty.ReceiptIssuance
err := types.Decode(item.Log, &IssuanceLog)
if err != nil {
return nil, err
}
switch item.Ty {
case pty.TyLogIssuanceCreate:
set.KV = append(set.KV, c.addIssuanceStatus(&IssuanceLog)...)
break
case pty.TyLogIssuanceDebt:
set.KV = append(set.KV, c.addIssuanceRecordStatus(&IssuanceLog)...)
set.KV = append(set.KV, c.addIssuanceAddr(&IssuanceLog)...)
break
case pty.TyLogIssuanceRepay:
set.KV = append(set.KV, c.addIssuanceRecordStatus(&IssuanceLog)...)
set.KV = append(set.KV, c.deleteIssuanceAddr(&IssuanceLog)...)
break
case pty.TyLogIssuanceFeed:
set.KV = append(set.KV, c.addIssuanceRecordStatus(&IssuanceLog)...)
if IssuanceLog.RecordStatus == pty.IssuanceUserStatusSystemLiquidate {
set.KV = append(set.KV, c.deleteIssuanceAddr(&IssuanceLog)...)
}
break
case pty.TyLogIssuanceClose:
set.KV = append(set.KV, c.deleteIssuanceStatus(&IssuanceLog)...)
break
}
}
return set, nil
}
// ExecLocal_Create Action
func (c *Issuance) ExecLocal_Create(payload *pty.IssuanceCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Debt Action
func (c *Issuance) ExecLocal_Debt(payload *pty.IssuanceDebt, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Repay Action
func (c *Issuance) ExecLocal_Repay(payload *pty.IssuanceRepay, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Feed Action
func (c *Issuance) ExecLocal_Feed(payload *pty.IssuanceFeed, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Close Action
func (c *Issuance) ExecLocal_Close(payload *pty.IssuanceClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
log "github.com/33cn/chain33/common/log/log15"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
)
var clog = log.New("module", "execs.issuance")
var driverName = pty.IssuanceX
func init() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&Issuance{}))
}
type subConfig struct {
ParaRemoteGrpcClient string `json:"paraRemoteGrpcClient"`
}
var cfg subConfig
// Init issuance
func Init(name string, sub []byte) {
driverName := GetName()
if name != driverName {
panic("system dapp can't be rename")
}
if sub != nil {
types.MustDecode(sub, &cfg)
}
drivers.Register(driverName, newIssuance, types.GetDappFork(driverName, "Enable"))
}
// GetName for Issuance
func GetName() string {
return newIssuance().GetName()
}
// Issuance driver
type Issuance struct {
drivers.DriverBase
}
func newIssuance() drivers.Driver {
c := &Issuance{}
c.SetChild(c)
c.SetExecutorType(types.LoadExecutorType(driverName))
return c
}
// GetDriverName for Issuance
func (c *Issuance) GetDriverName() string {
return pty.IssuanceX
}
func (c *Issuance) addIssuanceID(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceKey(issuancelog.IssuanceId, issuancelog.Index)
record := &pty.IssuanceRecord{
IssuanceId:issuancelog.IssuanceId,
Index: issuancelog.Index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceID(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceKey(issuancelog.IssuanceId, issuancelog.Index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceStatus(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceStatusKey(issuancelog.Status, issuancelog.Index)
record := &pty.IssuanceRecord{
IssuanceId:issuancelog.IssuanceId,
Index: issuancelog.Index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceStatus(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceStatusKey(issuancelog.Status, issuancelog.Index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceAddr(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceAddrKey(issuancelog.AccountAddr, issuancelog.Index)
record := &pty.IssuanceRecord{
IssuanceId:issuancelog.IssuanceId,
Index: issuancelog.Index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceAddr(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceAddrKey(issuancelog.AccountAddr, issuancelog.Index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceRecordStatus(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceRecordStatusKey(issuancelog.RecordStatus, issuancelog.Index)
record := &pty.IssuanceRecord{
IssuanceId:issuancelog.IssuanceId,
Addr: issuancelog.AccountAddr,
Index: issuancelog.Index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceRecordStatus(issuancelog *pty.ReceiptIssuance) (kvs []*types.KeyValue) {
key := calcIssuanceRecordStatusKey(issuancelog.RecordStatus, issuancelog.Index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
// CheckReceiptExecOk return true to check if receipt ty is ok
func (c *Issuance) CheckReceiptExecOk() bool {
return true
}
// ExecutorOrder 设置localdb的EnableRead
func (c *Issuance) ExecutorOrder() int64 {
if types.IsFork(c.GetHeight(), "ForkLocalDBAccess") {
return drivers.ExecLocalSameTime
}
return c.DriverBase.ExecutorOrder()
}
\ No newline at end of file
This diff is collapsed.
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import "fmt"
func calcIssuanceKey(issuanceID string, index int64) []byte {
key := fmt.Sprintf("LODB-Issuance-ID:%s:%018d", issuanceID, index)
return []byte(key)
}
func calcIssuanceStatusPrefix(status int32) []byte {
key := fmt.Sprintf("LODB-Issuance-status-index:%d", status)
return []byte(key)
}
func calcIssuanceStatusKey(status int32, index int64) []byte {
key := fmt.Sprintf("LODB-Issuance-status:%d:%018d", status, index)
return []byte(key)
}
func calcIssuanceAddrPrefix(addr string) []byte {
key := fmt.Sprintf("LODB-Issuance-addr:%s", addr)
return []byte(key)
}
func calcIssuanceAddrKey(addr string, index int64) []byte {
key := fmt.Sprintf("LODB-Issuance-addr:%s:%018d", addr, index)
return []byte(key)
}
func calcIssuancePriceKey(time string) []byte {
key := fmt.Sprintf("LODB-Issuance-price:%s", time)
return []byte(key)
}
func calcIssuanceLatestPriceKey() []byte {
key := fmt.Sprintf("LODB-Issuance-latest-price")
return []byte(key)
}
func calcIssuanceRecordStatusPrefix(status int32) []byte {
key := fmt.Sprintf("LODB-Issuance-record-status:%d", status)
return []byte(key)
}
func calcIssuanceRecordStatusKey(status int32, index int64) []byte {
key := fmt.Sprintf("LODB-Issuance-record-status:%d:%018d", status, index)
return []byte(key)
}
\ No newline at end of file
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
)
func (c *Issuance) Query_IssuanceInfoByID(req *pty.ReqIssuanceInfo) (types.Message, error) {
issu,err := queryIssuanceByID(c.GetStateDB(), req.IssuanceId)
if err != nil {
clog.Error("Query_IssuanceInfoByID", "id", req.IssuanceId, "error", err)
return nil, err
}
return &pty.RepIssuanceCurrentInfo{
Status: issu.Status,
TotalBalance: issu.TotalBalance,
DebtCeiling: issu.DebtCeiling,
LiquidationRatio: issu.LiquidationRatio,
Balance: issu.Balance,
}, nil
}
func (c *Issuance) Query_IssuanceInfoByIDs(req *pty.ReqIssuanceInfos) (types.Message, error) {
infos := &pty.RepIssuanceCurrentInfos{}
for _, id := range req.IssuanceIds {
issu,err := queryIssuanceByID(c.GetStateDB(), id)
if err != nil {
clog.Error("Query_IssuanceInfoByID", "id", id, "error", err)
return nil, err
}
infos.Infos = append(infos.Infos, &pty.RepIssuanceCurrentInfo{
Status: issu.Status,
TotalBalance: issu.TotalBalance,
DebtCeiling: issu.DebtCeiling,
LiquidationRatio: issu.LiquidationRatio,
Balance: issu.Balance,
})
}
return infos, nil
}
func (c *Issuance) Query_IssuanceByStatus(req *pty.ReqIssuanceByStatus) (types.Message, error) {
ids := &pty.RepIssuanceIDs{}
issuIDRecords, err := queryIssuanceByStatus(c.GetLocalDB(), req.Status)
if err != nil {
clog.Error("Query_IssuanceByStatus", "get issuance record error", err)
return nil, err
}
for _, record := range issuIDRecords {
ids.IDs = append(ids.IDs, record.IssuanceId)
}
return ids, nil
}
func (c *Issuance) Query_IssuanceByAddr(req *pty.ReqIssuanceByAddr) (types.Message, error) {
ids := &pty.RepIssuanceIDs{}
issuIDRecords, err := queryIssuanceByAddr(c.GetLocalDB(), req.Addr)
if err != nil {
clog.Error("Query_IssuanceByAddr", "get issuance record error", err)
return nil, err
}
for _, record := range issuIDRecords {
ids.IDs = append(ids.IDs, record.IssuanceId)
}
return ids, nil
}
func (c *Issuance) Query_IssuanceDebtInfoByAddr(req *pty.ReqIssuanceDebtInfoByAddr) (types.Message, error) {
records, err := queryIssuanceByAddr(c.GetLocalDB(), req.Addr)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByAddr", "get issuance record error", err)
return nil, err
}
ret := &pty.RepIssuanceDebtInfos{}
for _, record := range records {
if record.IssuanceId == req.IssuanceId {
issu, err := queryIssuanceByID(c.GetStateDB(), record.IssuanceId)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByAddr", "get issuance record error", err)
return nil, err
}
for _, borrowRecord := range issu.DebtRecords {
if borrowRecord.AccountAddr == req.Addr {
ret.Record = append(ret.Record, borrowRecord)
}
}
for _, borrowRecord := range issu.InvalidRecords {
if borrowRecord.AccountAddr == req.Addr {
ret.Record = append(ret.Record, borrowRecord)
}
}
}
}
return nil, pty.ErrRecordNotExist
}
func (c *Issuance) Query_IssuanceDebtInfoByStatus(req *pty.ReqIssuanceDebtInfoByStatus) (types.Message, error) {
records, err := queryIssuanceRecordByStatus(c.GetLocalDB(), req.Status)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByAddr", "get issuance record error", err)
return nil, err
}
ret := &pty.RepIssuanceDebtInfos{}
for _, record := range records {
issu, err := queryIssuanceByID(c.GetStateDB(), record.IssuanceId)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByAddr", "get issuance record error", err)
return nil, err
}
for _, borrowRecord := range issu.DebtRecords {
if borrowRecord.Status == req.Status {
ret.Record = append(ret.Record, borrowRecord)
}
}
for _, borrowRecord := range issu.InvalidRecords {
if borrowRecord.Status == req.Status {
ret.Record = append(ret.Record, borrowRecord)
}
}
}
return ret, nil
}
\ No newline at end of file
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package issuance
import (
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/issuance/commands"
"github.com/33cn/plugin/plugin/dapp/issuance/executor"
"github.com/33cn/plugin/plugin/dapp/issuance/types"
)
func init() {
pluginmgr.Register(&pluginmgr.PluginBase{
Name: types.IssuanceX,
ExecName: executor.GetName(),
Exec: executor.Init,
Cmd: commands.IssuanceCmd,
})
}
all:
sh ./create_protobuf.sh
#!/bin/sh
chain33_path=$(go list -f '{{.Dir}}' "github.com/33cn/chain33")
protoc --go_out=plugins=grpc:../types ./*.proto --proto_path=. --proto_path="${chain33_path}/types/proto/"
syntax = "proto3";
package types;
// 借贷信息
message Issuance {
string issuanceId = 1; //借贷ID,一期借贷对应一个ID
int64 totalBalance = 2; //当期可借贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny)
float liquidationRatio = 4; //清算比例
int64 collateralValue = 5; //抵押物总数量(bty)
int64 debtValue = 6; //产生的ccny数量
repeated DebtRecord debtRecords = 7; //大户抵押记录
repeated DebtRecord invalidRecords = 8; //大户抵押记录
int32 status = 9; //当期借贷的状态,是否关闭
float latestLiquidationPrice = 10; //最高清算价格
int64 period = 11;//借贷最大期限
int64 latestExpireTime = 12;//最近超期时间
int64 createTime = 13;//创建时间
int64 balance = 14;//剩余可发行ccny
string issuerAddr = 15;//发行地址
}
// 抵押记录
message DebtRecord {
string accountAddr = 1; //借贷人地址
int64 startTime = 2; //借贷时间
int64 collateralValue = 3; //抵押物价值(bty)
float collateralPrice = 4; //抵押物价格
int64 debtValue = 5; //债务价值(ccny)
float liquidationPrice = 6; //抵押物清算价格
int32 status = 7; //抵押状态,是否被清算
int64 liquidateTime = 8; //清算时间
int64 expireTime = 9; //超时清算时间
int32 preStatus = 10;//上一次抵押状态,用于告警恢复
}
// 资产价格记录
message AssetPriceRecord {
int64 recordTime = 1; //价格记录时间
float btyPrice = 2; //bty价格
}
// action
message IssuanceAction {
oneof value {
IssuanceCreate create = 1; //创建一期借贷
IssuanceDebt debt = 2; //借贷
IssuanceRepay repay = 3; //清算
IssuanceFeed feed = 4; //喂价
IssuanceClose close = 5; //关闭
IssuanceManage manage = 6; //全局配置
}
int32 ty = 10;
}
message IssuanceManage {
repeated string superAddrs = 1; //大户地址
}
// 创建借贷
message IssuanceCreate {
int64 totalBalance = 1; //可借贷总金额
int64 debtCeiling = 2; //单用户可借出的限额(ccny)
float liquidationRatio = 3; //清算比例
int64 period = 4;//借贷最大期限
}
// 质押借出
message IssuanceDebt {
string IssuanceId = 1; //借贷期数ID
int64 value = 2; //借贷价值(ccny)
}
// 质押清算
message IssuanceRepay {
string IssuanceId = 1; //借贷期数ID
}
// 喂价
message IssuanceFeed {
int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...)
repeated float price = 2; //喂价
repeated int64 volume = 3; //成交量
}
// 借贷关闭
message IssuanceClose {
string IssuanceId = 1; //借贷期数ID
}
// exec_local 借贷信息
message ReceiptIssuance {
string IssuanceId = 1;
string accountAddr = 2;
int32 status = 3;
int32 preStatus = 4;
int64 index = 5;
int32 recordStatus = 6;
}
// exec_local 借贷记录信息
message IssuanceRecord {
string IssuanceId = 1;
string addr = 2;
int64 index = 3;
}
// exec_local 借贷记录信息列表
message IssuanceRecords {
repeated IssuanceRecord records = 1;
}
// 根据ID查询一期借贷信息
message ReqIssuanceInfo {
string IssuanceId = 1;
}
// 返回一期借贷信息
message RepIssuanceCurrentInfo {
int32 status = 1;//当期借贷的状态,是否关闭
int64 totalBalance = 2; //当期可借贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny)
float liquidationRatio = 4; //清算比例
int64 balance = 5; //剩余可借贷金额(ccny)
int64 collateralValue = 6; //抵押物总数量(bty)
int64 debtValue = 7; //产生的ccny数量
}
// 根据ID列表查询多期借贷信息
message ReqIssuanceInfos {
repeated string IssuanceIds = 1;
}
// 返回多期借贷信息
message RepIssuanceCurrentInfos {
repeated RepIssuanceCurrentInfo infos = 1;
}
// 根据借贷状态查询
message ReqIssuanceByStatus {
int32 status = 1;
}
// 根据用户地址查询
message ReqIssuanceByAddr {
string addr = 1;
}
// 返回借贷ID列表
message RepIssuanceIDs {
repeated string IDs = 1;
}
// 根据地址和借贷ID混合查询具体借贷记录
message ReqIssuanceDebtInfoByAddr {
string IssuanceId = 1;
string addr = 2;
}
// 根据状态和借贷ID混合查询具体借贷记录
message ReqIssuanceDebtInfoByStatus {
string IssuanceId = 1;
int32 status = 2;
}
// 返回借贷记录
message RepIssuanceDebtInfos {
repeated DebtRecord record = 1;
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import "errors"
// Errors for lottery
var (
ErrRiskParam = errors.New("ErrRiskParam")
ErrIssuanceRepeatHash = errors.New("ErrIssuanceRepeatHash")
ErrIssuanceStatus = errors.New("ErrIssuanceStatus")
ErrIssuanceExceedDebtCeiling = errors.New("ErrIssuanceExceedDebtCeiling")
ErrPriceInvalid = errors.New("ErrPriceInvalid")
ErrAssetType = errors.New("ErrAssetType")
ErrRecordNotExist = errors.New("ErrRecordNotExist")
ErrIssuanceErrCloser = errors.New("ErrIssuanceErrCloser")
ErrRepayValueInsufficient = errors.New("ErrRepayValueInsufficient")
ErrIssuanceAccountExist = errors.New("ErrIssuanceAccountExist")
ErrIssuanceLowBalance = errors.New("ErrIssuanceLowBalance")
ErrIssuanceBalanceInvalid = errors.New("ErrIssuanceBalanceInvalid")
ErrPermissionDeny = errors.New("ErrPermissionDeny")
ErrIssuanceRecordNotEmpty = errors.New("ErrIssuanceRecordNotEmpty")
)
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"encoding/json"
"reflect"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
)
var (
llog = log.New("module", "exectype."+IssuanceX)
)
func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(IssuanceX))
types.RegistorExecutor(IssuanceX, NewType())
types.RegisterDappFork(IssuanceX, "Enable", 0)
}
// IssuanceType def
type IssuanceType struct {
types.ExecTypeBase
}
// NewType method
func NewType() *IssuanceType {
c := &IssuanceType{}
c.SetChild(c)
return c
}
// GetName 获取执行器名称
func (Issuance *IssuanceType) GetName() string {
return IssuanceX
}
// GetLogMap method
func (Issuance *IssuanceType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogIssuanceCreate: {Ty: reflect.TypeOf(ReceiptIssuance{}), Name: "LogIssuanceCreate"},
TyLogIssuanceDebt: {Ty: reflect.TypeOf(ReceiptIssuance{}), Name: "LogIssuanceDebt"},
TyLogIssuanceRepay: {Ty: reflect.TypeOf(ReceiptIssuance{}), Name: "LogIssuanceRepay"},
TyLogIssuanceFeed: {Ty: reflect.TypeOf(ReceiptIssuance{}), Name: "LogIssuanceFeed"},
TyLogIssuanceClose: {Ty: reflect.TypeOf(ReceiptIssuance{}), Name: "LogIssuanceClose"},
}
}
// GetPayload method
func (Issuance *IssuanceType) GetPayload() types.Message {
return &IssuanceAction{}
}
// CreateTx method
func (Issuance IssuanceType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
llog.Debug("Issuance.CreateTx", "action", action)
if action == "IssuanceCreate" {
var param IssuanceCreateTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceCreateTx(&param)
} else if action == "IssuanceDebt" {
var param IssuanceDebtTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceDebtTx(&param)
} else if action == "IssuanceRepay" {
var param IssuanceRepayTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceRepayTx(&param)
} else if action == "IssuanceFeed" {
var param IssuanceFeedTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceFeedTx(&param)
} else if action == "IssuanceClose" {
var param IssuanceCloseTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceCloseTx(&param)
} else if action == "IssuanceManage" {
var param IssuanceManageTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceManageTx(&param)
} else {
return nil, types.ErrNotSupport
}
}
// GetTypeMap method
func (Issuance IssuanceType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": IssuanceActionCreate,
"Borrow": IssuanceActionDebt,
"Repay": IssuanceActionRepay,
"Feed": IssuanceActionFeed,
"Close": IssuanceActionClose,
"Manage": IssuanceActionManage,
}
}
// CreateRawIssuanceCreateTx method
func CreateRawIssuanceCreateTx(parm *IssuanceCreateTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceCreateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceCreate{
TotalBalance: parm.TotalBalance,
}
create := &IssuanceAction{
Ty: IssuanceActionCreate,
Value: &IssuanceAction_Create{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(create),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceDebtTx method
func CreateRawIssuanceDebtTx(parm *IssuanceDebtTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceBorrowTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceDebt{
IssuanceId: parm.IssuanceID,
Value: parm.Value,
}
debt := &IssuanceAction{
Ty: IssuanceActionDebt,
Value: &IssuanceAction_Debt{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(debt),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceRepayTx method
func CreateRawIssuanceRepayTx(parm *IssuanceRepayTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceRepayTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceRepay{
IssuanceId: parm.IssuanceID,
}
repay := &IssuanceAction{
Ty: IssuanceActionRepay,
Value: &IssuanceAction_Repay{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(repay),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceFeedTx method
func CreateRawIssuanceFeedTx(parm *IssuanceFeedTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuancePriceFeedTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceFeed{
Price: parm.Price,
Volume: parm.Volume,
}
feed := &IssuanceAction{
Ty: IssuanceActionFeed,
Value: &IssuanceAction_Feed{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(feed),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceCloseTx method
func CreateRawIssuanceCloseTx(parm *IssuanceCloseTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceCloseTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceClose{
IssuanceId: parm.IssuanceID,
}
close := &IssuanceAction{
Ty: IssuanceActionClose,
Value: &IssuanceAction_Close{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(close),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceManageTx method
func CreateRawIssuanceManageTx(parm *IssuanceManageTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceManageTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceManage{SuperAddrs:parm.Addr}
manage := &IssuanceAction{
Ty: IssuanceActionManage,
Value: &IssuanceAction_Manage{v},
}
tx := &types.Transaction{
Execer: []byte(types.ExecName(IssuanceX)),
Payload: types.Encode(manage),
Fee: parm.Fee,
To: address.ExecAddress(types.ExecName(IssuanceX)),
}
name := types.ExecName(IssuanceX)
tx, err := types.FormatTx(name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
\ No newline at end of file
This diff is collapsed.
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
// IssuanceCreateTx for construction
type IssuanceCreateTx struct {
DebtCeiling int64 `json:"debtCeiling"`
LiquidationRatio float32 `json:"liquidationRatio"`
Period int64 `json:"period"`
TotalBalance int64 `json:"totalBalance"`
Fee int64 `json:"fee"`
}
// IssuanceDebtTx for construction
type IssuanceDebtTx struct {
IssuanceID string `json:"issuanceId"`
Value int64 `json:"value"`
Fee int64 `json:"fee"`
}
// IssuanceRepayTx for construction
type IssuanceRepayTx struct {
IssuanceID string `json:"issuanceId"`
Fee int64 `json:"fee"`
}
// IssuanceFeedTx for construction
type IssuanceFeedTx struct {
Price []float32 `json:"price"`
Volume []int64 `json:"volume"`
Fee int64 `json:"fee"`
}
// IssuanceCloseTx for construction
type IssuanceCloseTx struct {
IssuanceID string `json:"issuanceId"`
Fee int64 `json:"fee"`
}
// IssuanceManageTx for construction
type IssuanceManageTx struct {
Addr []string `json:"addr"`
Fee int64 `json:"fee"`
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
//Issuance op
const (
IssuanceActionCreate = 1 + iota // 创建借贷
IssuanceActionDebt // 大户抵押
IssuanceActionRepay // 大户清算
IssuanceActionFeed // 发行合约喂价
IssuanceActionClose // 关闭借贷
IssuanceActionManage // 借贷管理
//log for Issuance
TyLogIssuanceCreate = 741
TyLogIssuanceDebt = 742
TyLogIssuanceRepay = 743
TyLogIssuanceFeed = 745
TyLogIssuanceClose = 756
)
// Issuance name
const (
IssuanceX = "issuance"
CCNYTokenName = "ccny"
IssuancePreLiquidationRatio = 1.1 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算
)
//Issuance status
const (
IssuanceStatusCreated = 1 + iota
IssuanceStatusClose
)
const (
IssuanceUserStatusCreate = 1 + iota
IssuanceUserStatusWarning
IssuanceUserStatusSystemLiquidate
IssuanceUserStatusExpire
IssuanceUserStatusExpireLiquidate
IssuanceUserStatusClose
)
\ No newline at end of file
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