Commit 5523229c authored by pengjun's avatar pengjun

Merge branch 'issue-627-collateralize'

parents 344d8691 e93d5a11
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}"
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 (
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/collateralize/types"
)
var clog = log.New("module", "execs.collateralize")
var driverName = pty.CollateralizeX
func InitExecType() {
ety := types.LoadExecutorType(driverName)
ety.InitFuncList(types.ListMethod(&Collateralize{}))
}
type subConfig struct {
ParaRemoteGrpcClient string `json:"paraRemoteGrpcClient"`
}
var cfg subConfig
// Init collateralize
func Init(name string, cfg *types.Chain33Config, sub []byte) {
driverName := GetName()
if name != driverName {
panic("system dapp can't be rename")
}
if sub != nil {
types.MustDecode(sub, &cfg)
}
drivers.Register(cfg, driverName, newCollateralize, cfg.GetDappFork(driverName, "Enable"))
InitExecType()
}
// GetName for Collateralize
func GetName() string {
return newCollateralize().GetName()
}
// Collateralize driver
type Collateralize struct {
drivers.DriverBase
}
func newCollateralize() drivers.Driver {
c := &Collateralize{}
c.SetChild(c)
c.SetExecutorType(types.LoadExecutorType(driverName))
return c
}
// GetDriverName for Collateralize
func (c *Collateralize) GetDriverName() string {
return pty.CollateralizeX
}
func (c *Collateralize) addCollateralizeID(collateralizeId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeKey(collateralizeId, index)
record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) deleteCollateralizeID(collateralizeId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeKey(collateralizeId, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) addCollateralizeStatus(status int32, collateralizeId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeStatusKey(status, index)
record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) deleteCollateralizeStatus(status int32, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeStatusKey(status, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) addCollateralizeAddr(addr string, collateralizeId string, status int32, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeAddrKey(addr, index)
record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId,
Status:status,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) deleteCollateralizeAddr(addr string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeAddrKey(addr, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) addCollateralizeRecordStatus(recordStatus int32, collateralizeId string, recordId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeRecordStatusKey(recordStatus, index)
record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId,
RecordId:recordId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) deleteCollateralizeRecordStatus(recordStatus int32, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeRecordStatusKey(recordStatus, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) addCollateralizeRecordAddr(recordAddr string, collateralizeId string, recordId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeRecordAddrKey(recordAddr, index)
record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId,
RecordId:recordId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Collateralize) deleteCollateralizeRecordAddr(recordAddr string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeRecordAddrKey(recordAddr, 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 *Collateralize) CheckReceiptExecOk() bool {
return true
}
// ExecutorOrder 设置localdb的EnableRead
func (c *Collateralize) ExecutorOrder() int64 {
cfg := c.GetAPI().GetConfig()
if cfg.IsFork(c.GetHeight(), "ForkLocalDBAccess") {
return drivers.ExecLocalSameTime
}
return c.DriverBase.ExecutorOrder()
}
\ No newline at end of file
This diff is collapsed.
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
/*
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/collateralize/types"
)
// Exec_Create Action
func (c *Collateralize) Exec_Create(payload *pty.CollateralizeCreate, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeCreate(payload)
}
// Exec_Borrow Action
func (c *Collateralize) Exec_Borrow(payload *pty.CollateralizeBorrow, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeBorrow(payload)
}
// Exec_Repay Action
func (c *Collateralize) Exec_Repay(payload *pty.CollateralizeRepay, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeRepay(payload)
}
// Exec_Repay Action
func (c *Collateralize) Exec_Append(payload *pty.CollateralizeAppend, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeAppend(payload)
}
// Exec_Feed Action
func (c *Collateralize) Exec_Feed(payload *pty.CollateralizeFeed, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewCollateralizeAction(c, tx, index)
return actiondb.CollateralizeFeed(payload)
}
// Exec_Close Action
func (c *Collateralize) Exec_Close(payload *pty.CollateralizeClose, tx *types.Transaction, index int) (*types.Receipt, error) {
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)
}
// 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/collateralize/types"
)
func (c *Collateralize) execDelLocal(tx *types.Transaction, receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
for _, item := range receiptData.Logs {
if item.Ty == pty.TyLogCollateralizeCreate || item.Ty == pty.TyLogCollateralizeBorrow || item.Ty == pty.TyLogCollateralizeAppend ||
item.Ty == pty.TyLogCollateralizeRepay || item.Ty == pty.TyLogCollateralizeFeed || item.Ty == pty.TyLogCollateralizeClose {
var collateralizeLog pty.ReceiptCollateralize
err := types.Decode(item.Log, &collateralizeLog)
if err != nil {
return nil, err
}
switch item.Ty {
case pty.TyLogCollateralizeCreate:
set.KV = append(set.KV, c.deleteCollateralizeStatus(collateralizeLog.Status, collateralizeLog.Index)...)
set.KV = append(set.KV, c.deleteCollateralizeAddr(collateralizeLog.CreateAddr, collateralizeLog.Index)...)
break
case pty.TyLogCollateralizeBorrow:
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.Index)...)
set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.Index)...)
break
case pty.TyLogCollateralizeAppend:
if collateralizeLog.Status == pty.CollateralizeUserStatusWarning {
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.PreStatus, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
//set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.Index)...)
}
break
case pty.TyLogCollateralizeRepay:
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.PreStatus, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
break
case pty.TyLogCollateralizeFeed:
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.RecordId, collateralizeLog.PreIndex)...)
//// 如果没有被清算,需要把地址索引更新
//if collateralizeLog.Status == pty.CollateralizeUserStatusWarning || collateralizeLog.Status == pty.CollateralizeUserStatusExpire {
// set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.Index)...)
//}
break
case pty.TyLogCollateralizeClose:
set.KV = append(set.KV, c.deleteCollateralizeStatus(collateralizeLog.Status, collateralizeLog.Index)...)
set.KV = append(set.KV, c.addCollateralizeStatus(pty.CollateralizeStatusCreated, collateralizeLog.CollateralizeId,
collateralizeLog.PreIndex)...)
//set.KV = append(set.KV, c.addCollateralizeAddr(collateralizeLog.CreateAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.PreStatus, collateralizeLog.PreIndex)...)
break
}
}
}
return set, nil
}
// ExecDelLocal_Create Action
func (c *Collateralize) ExecDelLocal_Create(payload *pty.CollateralizeCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Borrow Action
func (c *Collateralize) ExecDelLocal_Borrow(payload *pty.CollateralizeBorrow, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Repay Action
func (c *Collateralize) ExecDelLocal_Repay(payload *pty.CollateralizeRepay, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Append Action
func (c *Collateralize) ExecDelLocal_Append(payload *pty.CollateralizeAppend, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Feed Action
func (c *Collateralize) ExecDelLocal_Feed(payload *pty.CollateralizeFeed, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execDelLocal(tx, receiptData)
}
// ExecDelLocal_Close Action
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
// 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/collateralize/types"
)
func (c *Collateralize) execLocal(tx *types.Transaction, receipt *types.ReceiptData) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
for _, item := range receipt.Logs {
if item.Ty == pty.TyLogCollateralizeCreate || item.Ty == pty.TyLogCollateralizeBorrow || item.Ty == pty.TyLogCollateralizeAppend ||
item.Ty == pty.TyLogCollateralizeRepay || item.Ty == pty.TyLogCollateralizeFeed || item.Ty == pty.TyLogCollateralizeClose {
var collateralizeLog pty.ReceiptCollateralize
err := types.Decode(item.Log, &collateralizeLog)
if err != nil {
return nil, err
}
switch item.Ty {
case pty.TyLogCollateralizeCreate:
set.KV = append(set.KV, c.addCollateralizeStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId, collateralizeLog.Index)...)
set.KV = append(set.KV, c.addCollateralizeAddr(collateralizeLog.CreateAddr, collateralizeLog.CollateralizeId, collateralizeLog.Status, collateralizeLog.Index)...)
break
case pty.TyLogCollateralizeBorrow:
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.Index)...)
set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.Index)...)
break
case pty.TyLogCollateralizeAppend:
if collateralizeLog.Status == pty.CollateralizeUserStatusWarning {
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.PreStatus, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.PreIndex)...)
//set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.RecordId, collateralizeLog.Index)...)
}
break
case pty.TyLogCollateralizeRepay:
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.PreStatus, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.PreIndex)...)
break
case pty.TyLogCollateralizeFeed:
set.KV = append(set.KV, c.deleteCollateralizeRecordStatus(collateralizeLog.PreStatus, collateralizeLog.PreIndex)...)
set.KV = append(set.KV, c.addCollateralizeRecordStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId,
collateralizeLog.RecordId, collateralizeLog.Index)...)
//set.KV = append(set.KV, c.deleteCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.PreIndex)...)
//// 如果没有被清算,需要把地址索引更新
//if collateralizeLog.Status == pty.CollateralizeUserStatusWarning || collateralizeLog.Status == pty.CollateralizeUserStatusExpire {
// set.KV = append(set.KV, c.addCollateralizeRecordAddr(collateralizeLog.AccountAddr, collateralizeLog.CollateralizeId,
// collateralizeLog.RecordId, collateralizeLog.Index)...)
//}
break
case pty.TyLogCollateralizeClose:
set.KV = append(set.KV, c.addCollateralizeStatus(collateralizeLog.Status, collateralizeLog.CollateralizeId, collateralizeLog.Index)...)
set.KV = append(set.KV, c.deleteCollateralizeStatus(collateralizeLog.PreStatus, collateralizeLog.PreIndex)...)
//set.KV = append(set.KV, c.deleteCollateralizeAddr(collateralizeLog.CreateAddr, collateralizeLog.PreIndex)...)
break
}
}
}
return set, nil
}
// ExecLocal_Create Action
func (c *Collateralize) ExecLocal_Create(payload *pty.CollateralizeCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Borrow Action
func (c *Collateralize) ExecLocal_Borrow(payload *pty.CollateralizeBorrow, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Repay Action
func (c *Collateralize) ExecLocal_Repay(payload *pty.CollateralizeRepay, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Repay Action
func (c *Collateralize) ExecLocal_Append(payload *pty.CollateralizeAppend, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Feed Action
func (c *Collateralize) ExecLocal_Feed(payload *pty.CollateralizeFeed, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(tx, receiptData)
}
// ExecLocal_Close Action
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)
}
// 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 calcCollateralizeKey(collateralizeID string, index int64) []byte {
key := fmt.Sprintf("LODB-collateralize-ID:%s:%018d", collateralizeID, index)
return []byte(key)
}
func calcCollateralizeStatusPrefix(status int32) []byte {
key := fmt.Sprintf("LODB-collateralize-status:%d", status)
return []byte(key)
}
func calcCollateralizeStatusKey(status int32, index int64) []byte {
key := fmt.Sprintf("LODB-collateralize-status:%d:%018d", status, index)
return []byte(key)
}
func calcCollateralizeAddrPrefix(addr string) []byte {
key := fmt.Sprintf("LODB-collateralize-addr:%s", addr)
return []byte(key)
}
func calcCollateralizeAddrKey(addr string, index int64) []byte {
key := fmt.Sprintf("LODB-collateralize-addr:%s:%018d", addr, index)
return []byte(key)
}
func calcCollateralizePriceKey(time string) []byte {
key := fmt.Sprintf("LODB-collateralize-price:%s", time)
return []byte(key)
}
func calcCollateralizeRecordAddrPrefix(addr string) []byte {
key := fmt.Sprintf("LODB-collateralize-record-addr:%s", addr)
return []byte(key)
}
func calcCollateralizeRecordAddrKey(addr string, index int64) []byte {
key := fmt.Sprintf("LODB-collateralize-record-addr:%s:%018d", addr, index)
return []byte(key)
}
func calcCollateralizeRecordStatusPrefix(status string) []byte {
key := fmt.Sprintf("LODB-collateralize-record-status:%s", status)
return []byte(key)
}
func calcCollateralizeRecordStatusKey(status int32, index int64) []byte {
key := fmt.Sprintf("LODB-collateralize-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/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
}
info := &pty.RepCollateralizeCurrentInfo{
Status: coll.Status,
TotalBalance: coll.TotalBalance,
DebtCeiling: coll.DebtCeiling,
LiquidationRatio: coll.LiquidationRatio,
StabilityFeeRatio: coll.StabilityFeeRatio,
CreateAddr: coll.CreateAddr,
Balance: coll.Balance,
Period: coll.Period,
CollateralizeId: coll.CollateralizeId,
CollBalance: coll.CollBalance,
}
info.BorrowRecords = append(info.BorrowRecords, coll.BorrowRecords...)
info.BorrowRecords = append(info.BorrowRecords, coll.InvalidRecords...)
return info, 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
}
info := &pty.RepCollateralizeCurrentInfo{
Status: coll.Status,
TotalBalance: coll.TotalBalance,
DebtCeiling: coll.DebtCeiling,
LiquidationRatio: coll.LiquidationRatio,
StabilityFeeRatio: coll.StabilityFeeRatio,
CreateAddr: coll.CreateAddr,
Balance: coll.Balance,
Period: coll.Period,
CollateralizeId: coll.CollateralizeId,
CollBalance: coll.CollBalance,
}
info.BorrowRecords = append(info.BorrowRecords, coll.BorrowRecords...)
info.BorrowRecords = append(info.BorrowRecords, coll.InvalidRecords...)
infos.Infos = append(infos.Infos, info)
}
return infos, nil
}
func (c *Collateralize) Query_CollateralizeByStatus(req *pty.ReqCollateralizeByStatus) (types.Message, error) {
ids := &pty.RepCollateralizeIDs{}
collIDRecords, err := queryCollateralizeByStatus(c.GetLocalDB(), req.Status, req.Index)
if err != nil {
clog.Error("Query_CollateralizeByStatus", "get collateralize record error", err)
return nil, err
}
ids.IDs = append(ids.IDs, collIDRecords...)
return ids, nil
}
func (c *Collateralize) Query_CollateralizeByAddr(req *pty.ReqCollateralizeByAddr) (types.Message, error) {
ids := &pty.RepCollateralizeIDs{}
collIDRecords, err := queryCollateralizeByAddr(c.GetLocalDB(), req.Addr, req.Status, req.Index)
if err != nil {
clog.Error("Query_CollateralizeByAddr", "get collateralize record error", err)
return nil, err
}
ids.IDs = append(ids.IDs, collIDRecords...)
return ids, nil
}
func (c *Collateralize) Query_CollateralizeRecordByID(req *pty.ReqCollateralizeRecord) (types.Message, error) {
ret := &pty.RepCollateralizeRecord{}
issuRecord, err := queryCollateralizeRecordByID(c.GetStateDB(), req.CollateralizeId, req.RecordId)
if err != nil {
clog.Error("Query_IssuanceRecordByID", "get collateralize record error", err)
return nil, err
}
ret.Record = issuRecord
return ret, nil
}
func (c *Collateralize) Query_CollateralizeRecordByAddr(req *pty.ReqCollateralizeRecordByAddr) (types.Message, error) {
ret := &pty.RepCollateralizeRecords{}
records, err := queryCollateralizeRecordByAddr(c.GetStateDB(), c.GetLocalDB(), req.Addr, req.Index)
if err != nil {
clog.Error("Query_CollateralizeRecordByAddr", "get collateralize record error", err)
return nil, err
}
if req.Status == 0 {
ret.Records = records
} else {
for _,record := range records {
if record.Status == req.Status {
ret.Records = append(ret.Records, record)
}
}
}
return ret, nil
}
func (c *Collateralize) Query_CollateralizeRecordByStatus(req *pty.ReqCollateralizeRecordByStatus) (types.Message, error) {
ret := &pty.RepCollateralizeRecords{}
records, err := queryCollateralizeRecordByStatus(c.GetStateDB(), c.GetLocalDB(), req.Status, req.Index)
if err != nil {
clog.Error("Query_CollateralizeRecordByStatus", "get collateralize record error", err)
return nil, err
}
ret.Records = records
return ret, nil
}
func (c *Collateralize) Query_CollateralizeConfig(req *pty.ReqCollateralizeRecordByAddr) (types.Message, error) {
config, err := getCollateralizeConfig(c.GetStateDB())
if err != nil {
clog.Error("Query_CollateralizeConfig", "get collateralize config error", err)
return nil, err
}
collIDRecords, err := queryCollateralizeByStatus(c.GetLocalDB(), pty.CollateralizeStatusCreated, 0)
if err != nil {
clog.Debug("Query_CollateralizeByStatus", "get collateralize record error", err)
}
collBalance := config.CollTotalBalance
for _, id := range collIDRecords {
coll, err := queryCollateralizeByID(c.GetStateDB(), id)
if err != nil {
clog.Error("Query_CollateralizeInfoByID", "id", id, "error", err)
return nil, err
}
collBalance -= coll.TotalBalance
}
ret := &pty.RepCollateralizeConfig{
CollTotalBalance:config.CollTotalBalance,
DebtCeiling: config.DebtCeiling,
LiquidationRatio: config.LiquidationRatio,
StabilityFeeRatio: config.StabilityFeeRatio,
Period: config.Period,
CollBalance: collBalance,
CurrentTime: config.CurrentTime,
}
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
// 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 collateralize
import (
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/collateralize/commands"
"github.com/33cn/plugin/plugin/dapp/collateralize/executor"
"github.com/33cn/plugin/plugin/dapp/collateralize/types"
)
func init() {
pluginmgr.Register(&pluginmgr.PluginBase{
Name: types.CollateralizeX,
ExecName: executor.GetName(),
Exec: executor.Init,
Cmd: commands.CollateralizeCmd,
})
}
all:
sh ./create_protobuf.sh
syntax = "proto3";
package types;
// 放贷信息
message Collateralize {
string collateralizeId = 1; //放贷ID,一期放贷对应一个ID
int64 totalBalance = 2; //当期放贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny)
float liquidationRatio = 4; //清算比例
float stabilityFeeRatio = 5; //稳定费率
string createAddr = 6; //创建人地址
int64 balance = 7; //放贷剩余金额(ccny)
repeated BorrowRecord borrowRecords = 8; //借贷记录
repeated BorrowRecord InvalidRecords = 9; //失效的借贷记录
int32 status = 10;//当期借贷的状态,是否关闭
float latestLiquidationPrice = 11;//最高清算价格
int64 period = 12;//借贷最大期限
int64 latestExpireTime = 13;//最近超期时间
int64 index = 14;//当前索引
int64 preIndex = 15;//上一个索引
int64 collBalance = 16;//抵押bty
}
// 借出记录
message BorrowRecord {
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;//上一次抵押状态,用于告警恢复
string recordId = 11;//借贷id,标识一次借出记录
string collateralizeId = 12;//放贷id
int64 index = 13;//当前索引
int64 preIndex = 14;//上级索引
}
// 资产价格记录
message AssetPriceRecord {
int64 recordTime = 1; //价格记录时间
float btyPrice = 2; //bty价格
float btcPrice = 3; //btc价格
float ethPrice = 4; //eth价格
}
// action
message CollateralizeAction {
oneof value {
CollateralizeCreate create = 1; //创建一期借贷
CollateralizeBorrow borrow = 2; //借贷
CollateralizeRepay repay = 3; //清算
CollateralizeAppend append = 4; //追加
CollateralizeFeed feed = 5; //喂价
CollateralizeClose close = 6; //关闭
CollateralizeManage manage = 7; //全局配置
}
int32 ty = 10;
}
message CollateralizeManage {
int64 debtCeiling = 1; //单用户可借出的限额(ccny)
float liquidationRatio = 2; //清算比例
float stabilityFeeRatio = 3; //稳定费
int64 period = 4; //合约期限
int64 collTotalBalance = 5; //放贷总量
int64 currentTime = 6; //设置时间
}
message CollateralizeAddr {
repeated string superAddrs = 1; //大户地址
}
// 创建放贷
message CollateralizeCreate {
int64 totalBalance = 1; //可借贷总金额
}
// 质押借出
message CollateralizeBorrow {
string collateralizeId = 1; //借贷期数ID
int64 value = 2; //借贷价值(ccny)
}
// 质押清算
message CollateralizeRepay {
string collateralizeId = 1; //借贷期数ID
string recordId = 2; //借贷ID
}
// 追加抵押物
message CollateralizeAppend {
string collateralizeId = 1; //借贷期数ID
string recordId = 2; //借贷ID
int64 collateralValue = 3; //追加价值(bty)
}
// 喂价
message CollateralizeFeed {
int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...)
repeated float price = 2; //喂价
repeated int64 volume = 3; //成交量
}
// 放贷关闭
message CollateralizeClose {
string collateralizeId = 1; //借贷期数ID
}
// exec_local 放贷信息
message ReceiptCollateralize {
string collateralizeId = 1;
string createAddr = 2;
string accountAddr = 3;
string recordId = 4;
int32 status = 5;
int32 PreStatus = 6;
int64 index = 7;
int64 preIndex = 8;
}
// exec_local 放贷记录信息
message CollateralizeRecord {
string collateralizeId = 1;
string addr = 2;
string recordId = 3;
int32 status = 4;
int64 index = 5;
}
// exec_local 放贷记录信息列表
message CollateralizeRecords {
repeated CollateralizeRecord records = 1;
}
// 根据ID查询一期放贷信息
message ReqCollateralizeInfo {
string collateralizeId = 1;
}
// 返回一期放贷信息
message RepCollateralizeCurrentInfo {
int32 status = 1;//当期借贷的状态,是否关闭
int64 totalBalance = 2; //当期可借贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny)
float liquidationRatio = 4; //清算比例
float stabilityFeeRatio = 5; //稳定费
string createAddr = 6; //创建人地址
int64 balance = 7; //剩余可借贷金额(ccny)
int64 period = 8; //合约期限
string collateralizeId = 9; //放贷ID
int64 collBalance = 10;//抵押bty
repeated BorrowRecord borrowRecords = 11;//借贷记录
}
// 根据ID列表查询多期放贷信息
message ReqCollateralizeInfos {
repeated string collateralizeIds = 1;
}
// 返回多期放贷信息
message RepCollateralizeCurrentInfos {
repeated RepCollateralizeCurrentInfo infos = 1;
}
// 根据放贷状态查询
message ReqCollateralizeByStatus {
int32 status = 1;
int64 index = 2;
}
// 根据用户地址查询
message ReqCollateralizeByAddr {
string addr = 1;
int32 status = 2;
int64 index = 3;
}
// 返回放贷ID列表
message RepCollateralizeIDs {
repeated string IDs = 1;
}
// 根据地址和借贷ID混合查询具体借贷记录
message ReqCollateralizeRecordByAddr {
string collateralizeId = 1;
string addr = 2;
int32 status = 3;
int64 index = 4;
}
// 根据状态和借贷ID混合查询具体借贷记录
message ReqCollateralizeRecordByStatus {
string collateralizeId = 1;
int32 status = 2;
int64 index = 3;
}
// 返回借贷记录
message RepCollateralizeRecords {
repeated BorrowRecord records = 1;
}
// 精确查找借贷记录
message ReqCollateralizeRecord {
string collateralizeId = 1;
string recordId = 2;
}
// 返回借贷记录
message RepCollateralizeRecord {
BorrowRecord record = 1;
}
// 返回放贷配置
message RepCollateralizeConfig {
int64 debtCeiling = 1; //单用户可借出的限额(ccny)
float liquidationRatio = 2; //清算比例
float stabilityFeeRatio = 3; //稳定费
int64 period = 4; //合约期限
int64 collTotalBalance = 5; //放贷总量
int64 collBalance = 6; //剩余放贷额度
int64 currentTime = 7; //设置时间
}
// 返回最新抵押物价格
message RepCollateralizePrice {
float price = 1; //当前抵押物最新价格
}
\ No newline at end of file
#!/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/"
This diff is collapsed.
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
import "errors"
// Errors for lottery
var (
ErrRiskParam = errors.New("ErrRiskParam")
ErrCollateralizeRepeatHash = errors.New("ErrCollateralizeRepeatHash")
ErrCollateralizeStatus = errors.New("ErrCollateralizeStatus")
ErrCollateralizeExceedDebtCeiling = errors.New("ErrCollateralizeExceedDebtCeiling")
ErrPriceInvalid = errors.New("ErrPriceInvalid")
ErrAssetType = errors.New("ErrAssetType")
ErrRecordNotExist = errors.New("ErrRecordNotExist")
ErrCollateralizeErrCloser = errors.New("ErrCollateralizeErrCloser")
ErrRepayValueInsufficient = errors.New("ErrRepayValueInsufficient")
ErrCollateralizeAccountExist = errors.New("ErrCollateralizeAccountExist")
ErrCollateralizeLowBalance = errors.New("ErrCollateralizeLowBalance")
ErrCollateralizeBalanceInvalid = errors.New("ErrCollateralizeBalanceInvalid")
ErrPermissionDeny = errors.New("ErrPermissionDeny")
ErrCollateralizeRecordNotEmpty = errors.New("ErrCollateralizeRecordNotEmpty")
)
// 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
// CollateralizeCreateTx for construction
type CollateralizeCreateTx struct {
TotalBalance int64 `json:"totalBalance"`
Fee int64 `json:"fee"`
}
// CollateralizeBorrowTx for construction
type CollateralizeBorrowTx struct {
CollateralizeID string `json:"collateralizeId"`
Value int64 `json:"value"`
Fee int64 `json:"fee"`
}
// CollateralizeRepayTx for construction
type CollateralizeRepayTx struct {
CollateralizeID string `json:"collateralizeId"`
RecordID string `json:"recordID"`
Fee int64 `json:"fee"`
}
// CollateralizeAppednTx for construction
type CollateralizeAppendTx struct {
CollateralizeID string `json:"collateralizeId"`
RecordID string `json:"recordID"`
Value int64 `json:"value"`
Fee int64 `json:"fee"`
}
// CollateralizeFeedTx for construction
type CollateralizeFeedTx struct {
Price []float32 `json:"price"`
Volume []int64 `json:"volume"`
Fee int64 `json:"fee"`
}
// CollateralizeCloseTx for construction
type CollateralizeCloseTx struct {
CollateralizeID string `json:"collateralizeId"`
Fee int64 `json:"fee"`
}
// CollateralizeManageTx for construction
type CollateralizeManageTx struct {
DebtCeiling int64 `json:"debtCeiling"`
LiquidationRatio float32 `json:"liquidationRatio"`
StabilityFeeRatio float32 `json:"stabilityFeeRatio"`
Period int64 `json:"period"`
CollTotalBalance int64 `json:"collTotalBalance"`
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
//Collateralize op
const (
CollateralizeActionCreate = 1 + iota
CollateralizeActionBorrow
CollateralizeActionRepay
CollateralizeActionAppend
CollateralizeActionFeed
CollateralizeActionClose
CollateralizeActionManage
//log for Collateralize
TyLogCollateralizeCreate = 731
TyLogCollateralizeBorrow = 732
TyLogCollateralizeRepay = 733
TyLogCollateralizeAppend = 734
TyLogCollateralizeFeed = 735
TyLogCollateralizeClose = 736
)
// Collateralize name
const (
CollateralizeX = "collateralize"
CCNYTokenName = "CCNY"
CollateralizePreLiquidationRatio = 1.1 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算
)
//Collateralize status
const (
CollateralizeStatusCreated = 1 + iota
CollateralizeStatusClose
)
//暂时只支持bty
//const (
// CollateralizeAssetTypeBty = 1 + iota
// CollateralizeAssetTypeBtc
// CollateralizeAssetTypeEth
//)
const (
CollateralizeUserStatusCreate = 1 + iota
CollateralizeUserStatusWarning
CollateralizeUserStatusSystemLiquidate
CollateralizeUserStatusExpire
CollateralizeUserStatusExpireLiquidate
CollateralizeUserStatusClose
)
\ No newline at end of file
package init
import (
_ "github.com/33cn/plugin/plugin/dapp/autonomy" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/blackwhite" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/cert" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/dposvote" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/echo" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/evm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/game" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/guess" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/hashlock" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/js" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/lottery" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/multisig" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/norm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/oracle" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/paracross" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/pokerbull" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/privacy" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/relay" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/retrieve" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/ticket" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/token" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/trade" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/unfreeze" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/valnode" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/autonomy" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/blackwhite" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/cert" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/collateralize" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/dposvote" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/echo" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/evm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/game" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/guess" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/hashlock" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/issuance" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/js" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/lottery" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/multisig" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/norm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/oracle" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/paracross" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/pokerbull" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/privacy" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/relay" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/retrieve" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/ticket" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/token" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/trade" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/unfreeze" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/valnode" //auto gen
)
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}"
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
/*
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_Debt(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)
}
// Exec_Manage Action
func (c *Issuance) Exec_Manage(payload *pty.IssuanceManage, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewIssuanceAction(c, tx, index)
return actiondb.IssuanceManage(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 {
if item.Ty == pty.TyLogIssuanceCreate || item.Ty == pty.TyLogIssuanceDebt || item.Ty == pty.TyLogIssuanceRepay ||
item.Ty == pty.TyLogIssuanceFeed || item.Ty == pty.TyLogIssuanceClose {
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.deleteIssuanceStatus(issuanceLog.Status, issuanceLog.Index)...)
break
case pty.TyLogIssuanceDebt:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(issuanceLog.Status, issuanceLog.Index)...)
set.KV = append(set.KV, c.deleteIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.Index)...)
break
case pty.TyLogIssuanceRepay:
set.KV = append(set.KV, c.addIssuanceRecordStatus(issuanceLog.PreStatus, issuanceLog.AccountAddr, issuanceLog.PreIndex,
issuanceLog.DebtId, issuanceLog.IssuanceId)...)
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(issuanceLog.Status, issuanceLog.Index)...)
//set.KV = append(set.KV, c.addIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.PreIndex, issuanceLog.DebtId,
// issuanceLog.IssuanceId)...)
break
case pty.TyLogIssuanceFeed:
set.KV = append(set.KV, c.addIssuanceRecordStatus(issuanceLog.Status, issuanceLog.AccountAddr, issuanceLog.PreIndex,
issuanceLog.DebtId, issuanceLog.IssuanceId)...)
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(issuanceLog.Status, issuanceLog.Index)...)
//set.KV = append(set.KV, c.addIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.PreIndex, issuanceLog.DebtId,
// issuanceLog.IssuanceId)...)
//// 如果没有被清算,需要把地址索引更新
//if issuanceLog.Status == pty.IssuanceUserStatusWarning || issuanceLog.Status == pty.IssuanceUserStatusExpire {
// set.KV = append(set.KV, c.deleteIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.Index)...)
//}
set.KV = append(set.KV, c.deleteIssuancePriceRecord(issuanceLog.RecordTime)...)
break
case pty.TyLogIssuanceClose:
set.KV = append(set.KV, c.deleteIssuanceStatus(issuanceLog.Status, issuanceLog.Index)...)
set.KV = append(set.KV, c.addIssuanceStatus(issuanceLog.PreStatus, issuanceLog.PreIndex, issuanceLog.IssuanceId)...)
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 {
if item.Ty == pty.TyLogIssuanceCreate || item.Ty == pty.TyLogIssuanceDebt || item.Ty == pty.TyLogIssuanceRepay ||
item.Ty == pty.TyLogIssuanceFeed || item.Ty == pty.TyLogIssuanceClose {
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.Status, issuanceLog.Index, issuanceLog.IssuanceId)...)
break
case pty.TyLogIssuanceDebt:
set.KV = append(set.KV, c.addIssuanceRecordStatus(issuanceLog.Status, issuanceLog.AccountAddr, issuanceLog.Index,
issuanceLog.DebtId, issuanceLog.IssuanceId)...)
set.KV = append(set.KV, c.addIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.Index, issuanceLog.DebtId,
issuanceLog.IssuanceId)...)
break
case pty.TyLogIssuanceRepay:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(issuanceLog.PreStatus, issuanceLog.PreIndex)...)
set.KV = append(set.KV, c.addIssuanceRecordStatus(issuanceLog.Status, issuanceLog.AccountAddr, issuanceLog.Index,
issuanceLog.DebtId, issuanceLog.IssuanceId)...)
//set.KV = append(set.KV, c.deleteIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.PreIndex)...)
break
case pty.TyLogIssuanceFeed:
set.KV = append(set.KV, c.deleteIssuanceRecordStatus(issuanceLog.PreStatus, issuanceLog.PreIndex)...)
set.KV = append(set.KV, c.addIssuanceRecordStatus(issuanceLog.Status, issuanceLog.AccountAddr, issuanceLog.Index,
issuanceLog.DebtId, issuanceLog.IssuanceId)...)
//set.KV = append(set.KV, c.deleteIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.PreIndex)...)
//// 如果没有被清算,需要把地址索引更新
//if issuanceLog.Status == pty.IssuanceUserStatusWarning || issuanceLog.Status == pty.IssuanceUserStatusExpire {
// set.KV = append(set.KV, c.addIssuanceRecordAddr(issuanceLog.AccountAddr, issuanceLog.Index, issuanceLog.DebtId,
// issuanceLog.IssuanceId)...)
//}
set.KV = append(set.KV, c.addIssuancePriceRecord(issuanceLog.RecordTime, issuanceLog.BtyPrice)...)
break
case pty.TyLogIssuanceClose:
set.KV = append(set.KV, c.addIssuanceStatus(issuanceLog.Status, issuanceLog.Index, issuanceLog.IssuanceId)...)
set.KV = append(set.KV, c.deleteIssuanceStatus(issuanceLog.PreStatus, issuanceLog.PreIndex)...)
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 InitExecType() {
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, cfg *types.Chain33Config, sub []byte) {
driverName := GetName()
if name != driverName {
panic("system dapp can't be rename")
}
if sub != nil {
types.MustDecode(sub, &cfg)
}
drivers.Register(cfg, driverName, newIssuance, cfg.GetDappFork(driverName, "Enable"))
InitExecType()
}
// 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(index int64, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceKey(issuanceId, index)
record := &pty.IssuanceRecord{
IssuanceId:issuanceId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceID(index int64, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceKey(issuanceId, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceStatus(status int32, index int64, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceStatusKey(status, index)
record := &pty.IssuanceRecord{
IssuanceId:issuanceId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceStatus(status int32, index int64) (kvs []*types.KeyValue) {
key := calcIssuanceStatusKey(status, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceRecordAddr(accountAddr string, index int64, debtId string, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceRecordAddrKey(accountAddr, index)
record := &pty.IssuanceRecord{
IssuanceId:issuanceId,
DebtId: debtId,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceRecordAddr(accountAddr string, index int64) (kvs []*types.KeyValue) {
key := calcIssuanceRecordAddrKey(accountAddr, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuanceRecordStatus(recordStatus int32, accountAddr string, index int64, debtId string, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceRecordStatusKey(recordStatus, index)
record := &pty.IssuanceRecord{
IssuanceId:issuanceId,
DebtId: debtId,
Addr: accountAddr,
Index: index,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuanceRecordStatus(status int32, index int64) (kvs []*types.KeyValue) {
key := calcIssuanceRecordStatusKey(status, index)
kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) addIssuancePriceRecord(recordTime int64, price float32) (kvs []*types.KeyValue) {
key := calcIssuancePriceKey(string(recordTime))
record := &pty.IssuanceAssetPriceRecord{
RecordTime:recordTime,
BtyPrice:price,
}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
return kvs
}
func (c *Issuance) deleteIssuancePriceRecord(recordTime int64) (kvs []*types.KeyValue) {
key := calcIssuancePriceKey(string(recordTime))
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 {
cfg := c.GetAPI().GetConfig()
if cfg.IsFork(c.GetHeight(), "ForkLocalDBAccess") {
return drivers.ExecLocalSameTime
}
return c.DriverBase.ExecutorOrder()
}
\ No newline at end of file
This diff is collapsed.
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:%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 calcIssuanceRecordAddrPrefix(addr string) []byte {
key := fmt.Sprintf("LODB-issuance-record-addr:%s", addr)
return []byte(key)
}
func calcIssuanceRecordAddrKey(addr string, index int64) []byte {
key := fmt.Sprintf("LODB-issuance-record-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 calcIssuanceRecordStatusPrefix(status string) []byte {
key := fmt.Sprintf("LODB-issuance-record-status:%s", 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,
CollateralValue: issu.CollateralValue,
DebtValue: issu.DebtValue,
Period: issu.Period,
IssuId: issu.IssuanceId,
CreateTime: issu.CreateTime,
}, 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,
CollateralValue: issu.CollateralValue,
DebtValue: issu.DebtValue,
Period: issu.Period,
IssuId: issu.IssuanceId,
CreateTime: issu.CreateTime,
})
}
return infos, nil
}
func (c *Issuance) Query_IssuanceByStatus(req *pty.ReqIssuanceByStatus) (types.Message, error) {
ids := &pty.RepIssuanceIDs{}
issuIDRecords, err := queryIssuanceByStatus(c.GetLocalDB(), req.Status, req.Index)
if err != nil {
clog.Error("Query_IssuanceByStatus", "get issuance error", err)
return nil, err
}
ids.IDs = append(ids.IDs, issuIDRecords...)
return ids, nil
}
func (c *Issuance) Query_IssuanceRecordByID(req *pty.ReqIssuanceDebtInfo) (types.Message, error) {
ret := &pty.RepIssuanceDebtInfo{}
issuRecord, err := queryIssuanceRecordByID(c.GetStateDB(), req.IssuanceId, req.DebtId)
if err != nil {
clog.Error("Query_IssuanceRecordByID", "get issuance record error", err)
return nil, err
}
ret.Record = issuRecord
return ret, nil
}
func (c *Issuance) Query_IssuanceRecordsByAddr(req *pty.ReqIssuanceRecordsByAddr) (types.Message, error) {
ret := &pty.RepIssuanceRecords{}
records, err := queryIssuanceRecordByAddr(c.GetStateDB(), c.GetLocalDB(), req.Addr, req.Index)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByAddr", "get issuance record error", err)
return nil, err
}
if req.Status == 0 {
ret.Records = records
} else {
for _,record := range records {
if record.Status == req.Status {
ret.Records = append(ret.Records, record)
}
}
}
return ret, nil
}
func (c *Issuance) Query_IssuanceRecordsByStatus(req *pty.ReqIssuanceRecordsByStatus) (types.Message, error) {
ret := &pty.RepIssuanceRecords{}
records, err := queryIssuanceRecordsByStatus(c.GetStateDB(), c.GetLocalDB(), req.Status, req.Index)
if err != nil {
clog.Error("Query_IssuanceDebtInfoByStatus", "get issuance record error", err)
return nil, err
}
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
// 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;//发行地址
int64 index = 16;//当前索引
int64 preIndex = 17;//上级索引
}
// 抵押记录
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;//上一次抵押状态,用于告警恢复
string debtId = 11;//借贷id
string issuId = 12;//发行id
int64 index = 13;//当前索引
int64 preIndex = 14;//上级索引
}
// 资产价格记录
message IssuanceAssetPriceRecord {
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
string debtId = 2; //抵押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;
string debtId = 3;
int32 status = 4;
int32 preStatus = 5;
int64 index = 6;
int64 preIndex = 7;
int64 recordTime = 8; //价格记录时间
float btyPrice = 9; //bty价格
}
// exec_local 抵押记录信息
message IssuanceRecord {
string issuanceId = 1;
string addr = 2;
string debtId = 3;
int64 index = 4;
}
// 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数量
int64 period = 8; //发行最大期限
string issuId = 9; //发行ID
int64 createTime = 10;//创建时间
}
// 根据ID列表查询多期发行信息
message ReqIssuanceInfos {
repeated string issuanceIds = 1;
}
// 返回多期发行信息
message RepIssuanceCurrentInfos {
repeated RepIssuanceCurrentInfo infos = 1;
}
// 根据发行状态查询
message ReqIssuanceByStatus {
int32 status = 1;
int64 index = 2;
}
// 返回发行ID列表
message RepIssuanceIDs {
repeated string IDs = 1;
}
// 根据用户地址查询抵押记录
message ReqIssuanceRecordsByAddr {
string issuanceId = 1;
string addr = 2;
int32 status = 3;
int64 index = 4;
}
// 根据状态查询抵押记录
message ReqIssuanceRecordsByStatus {
string issuanceId = 1;
int32 status = 2;
int64 index = 3;
}
// 返回记录列表
message RepIssuanceRecords {
repeated DebtRecord records = 1;
}
// 精确查找抵押记录
message ReqIssuanceDebtInfo {
string issuanceId = 1;
string debtId = 2;
}
// 返回记录
message RepIssuanceDebtInfo {
DebtRecord record = 1;
}
// 返回最新抵押物价格
message RepIssuancePrice {
float price = 1; //当前抵押物最新价格
}
\ 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 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.RegFork(IssuanceX, InitFork)
types.RegExec(IssuanceX, InitExecutor)
}
func InitFork(cfg *types.Chain33Config) {
cfg.RegisterDappFork(IssuanceX, "Enable", 0)
}
func InitExecutor(cfg *types.Chain33Config) {
types.RegistorExecutor(IssuanceX, NewType(cfg))
}
// IssuanceType def
type IssuanceType struct {
types.ExecTypeBase
}
// NewType method
func NewType(cfg *types.Chain33Config) *IssuanceType {
c := &IssuanceType{}
c.SetChild(c)
c.SetConfig(cfg)
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)
cfg := issuance.GetConfig()
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(cfg, &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(cfg, &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(cfg, &param)
} else if action == "IssuancePriceFeed" {
var param IssuanceFeedTx
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawIssuanceFeedTx(cfg, &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(cfg, &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(cfg, &param)
} else {
return nil, types.ErrNotSupport
}
}
// GetTypeMap method
func (issuance IssuanceType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": IssuanceActionCreate,
"Debt": IssuanceActionDebt,
"Repay": IssuanceActionRepay,
"Feed": IssuanceActionFeed,
"Close": IssuanceActionClose,
"Manage": IssuanceActionManage,
}
}
// CreateRawIssuanceCreateTx method
func CreateRawIssuanceCreateTx(cfg *types.Chain33Config, parm *IssuanceCreateTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceCreateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceCreate{
TotalBalance: parm.TotalBalance,
DebtCeiling: parm.DebtCeiling,
LiquidationRatio: parm.LiquidationRatio,
Period: parm.Period,
}
create := &IssuanceAction{
Ty: IssuanceActionCreate,
Value: &IssuanceAction_Create{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(IssuanceX)),
Payload: types.Encode(create),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceDebtTx method
func CreateRawIssuanceDebtTx(cfg *types.Chain33Config, 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(cfg.ExecName(IssuanceX)),
Payload: types.Encode(debt),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceRepayTx method
func CreateRawIssuanceRepayTx(cfg *types.Chain33Config, parm *IssuanceRepayTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawIssuanceRepayTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &IssuanceRepay{
IssuanceId: parm.IssuanceID,
DebtId: parm.DebtID,
}
repay := &IssuanceAction{
Ty: IssuanceActionRepay,
Value: &IssuanceAction_Repay{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(IssuanceX)),
Payload: types.Encode(repay),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceFeedTx method
func CreateRawIssuanceFeedTx(cfg *types.Chain33Config, 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(cfg.ExecName(IssuanceX)),
Payload: types.Encode(feed),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceCloseTx method
func CreateRawIssuanceCloseTx(cfg *types.Chain33Config, 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(cfg.ExecName(IssuanceX)),
Payload: types.Encode(close),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawIssuanceManageTx method
func CreateRawIssuanceManageTx(cfg *types.Chain33Config, 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(cfg.ExecName(IssuanceX)),
Payload: types.Encode(manage),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(IssuanceX)),
}
name := cfg.ExecName(IssuanceX)
tx, err := types.FormatTx(cfg, 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"`
DebtID string `json:"debtId"`
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
)
const (
PriceFeedKey = "issuance-price-feed"
GuarantorKey = "issuance-guarantor"
ManageKey = "issuance-manage"
)
\ 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