Commit 238360b5 authored by pengjun's avatar pengjun

#627 1. 添加fund权限,和manage区分 2. 修复多记录平仓的错误

parent 6ea027d7
......@@ -448,6 +448,36 @@ func TestCollateralize(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value)
}
p71 := &pkt.CollateralizeBorrowTx{
CollateralizeID: common.ToHex(collateralizeID),
Value: 100,
}
createTx, err = pkt.CreateRawCollateralizeBorrowTx(env.cfg, p71)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = []byte(pkt.CollateralizeX)
createTx, err = signTx(createTx, PrivKeyB)
if err != nil {
t.Error("RPC_Default_Process sign", "err", err)
}
exec.SetEnv(env.blockHeight+1, env.blockTime+1, env.difficulty)
receipt, err = exec.Exec(createTx, int(1))
assert.Nil(t, err)
assert.NotNil(t, receipt)
t.Log(receipt)
for _, kv := range receipt.KV {
env.db.Set(kv.Key, kv.Value)
}
receiptData = &types.ReceiptData{Ty: receipt.Ty, Logs: receipt.Logs}
set, err = exec.ExecLocal(createTx, receiptData, int(1))
assert.Nil(t, err)
assert.NotNil(t, set)
for _, kv := range set.KV {
env.kvdb.Set(kv.Key, kv.Value)
}
p8 := &pkt.CollateralizeFeedTx{}
p8.Price = append(p8.Price, 0.28)
p8.Volume = append(p8.Volume, 100)
......
......@@ -894,12 +894,24 @@ func getGuarantorAddr(db dbm.KV) (string, error) {
return item.GetArr().Value[0], nil
}
func removeLiquidateRecord(borrowRecords []*pty.BorrowRecord, remove pty.BorrowRecord) []*pty.BorrowRecord {
for index, record := range borrowRecords {
if record.RecordId == remove.RecordId {
borrowRecords = append(borrowRecords[:index], borrowRecords[index+1:]...)
break
}
}
return borrowRecords
}
// 系统清算
func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
var removeRecord []*pty.BorrowRecord
for index, borrowRecord := range coll.BorrowRecords {
for _, borrowRecord := range coll.BorrowRecords {
if (borrowRecord.LiquidationPrice*PriceWarningRate)/1e4 < price {
// 价格恢复,告警记录恢复
if borrowRecord.Status == pty.CollateralizeUserStatusWarning {
......@@ -913,6 +925,9 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*
// 价格低于清算线,记录清算
if borrowRecord.LiquidationPrice >= price {
// 价格低于清算线,记录清算
clog.Debug("systemLiquidation", "coll id", borrowRecord.CollateralizeId, "record id", borrowRecord.RecordId, "account", borrowRecord.AccountAddr, "price", price)
getGuarantorAddr, err := getGuarantorAddr(action.db)
if err != nil {
if err != nil {
......@@ -935,7 +950,7 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*
borrowRecord.PreStatus = borrowRecord.Status
borrowRecord.Status = pty.CollateralizeUserStatusSystemLiquidate
coll.InvalidRecords = append(coll.InvalidRecords, borrowRecord)
coll.BorrowRecords = append(coll.BorrowRecords[:index], coll.BorrowRecords[index+1:]...)
removeRecord = append(removeRecord, borrowRecord)
coll.CollBalance -= borrowRecord.CollateralValue
log := action.GetFeedReceiptLog(coll, borrowRecord)
......@@ -952,6 +967,11 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*
}
}
// 删除被清算的记录
for _, record := range removeRecord {
coll.BorrowRecords = removeLiquidateRecord(coll.BorrowRecords, *record)
}
// 保存
coll.LatestLiquidationPrice = getLatestLiquidationPrice(coll)
coll.LatestExpireTime = getLatestExpireTime(coll)
......@@ -967,18 +987,22 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*
func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
var removeRecord []*pty.BorrowRecord
for index, borrowRecord := range coll.BorrowRecords {
for _, borrowRecord := range coll.BorrowRecords {
if borrowRecord.ExpireTime-ExpireWarningTime > action.blocktime {
continue
}
// 超过超时时间,记录清算
if borrowRecord.ExpireTime <= action.blocktime {
// 价格低于清算线,记录清算
clog.Debug("expireLiquidation", "coll id", borrowRecord.CollateralizeId, "record id", borrowRecord.RecordId, "account", borrowRecord.AccountAddr, "time", action.blocktime)
getGuarantorAddr, err := getGuarantorAddr(action.db)
if err != nil {
if err != nil {
clog.Error("systemLiquidation", "getGuarantorAddr", err)
clog.Error("expireLiquidation", "getGuarantorAddr", err)
continue
}
}
......@@ -986,7 +1010,7 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
// 抵押物转移
receipt, err := action.coinsAccount.ExecTransferFrozen(coll.CreateAddr, getGuarantorAddr, action.execaddr, borrowRecord.CollateralValue)
if err != nil {
clog.Error("systemLiquidation", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", borrowRecord.CollateralValue, "error", err)
clog.Error("expireLiquidation", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", borrowRecord.CollateralValue, "error", err)
continue
}
logs = append(logs, receipt.Logs...)
......@@ -997,7 +1021,7 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
borrowRecord.PreStatus = borrowRecord.Status
borrowRecord.Status = pty.CollateralizeUserStatusExpireLiquidate
coll.InvalidRecords = append(coll.InvalidRecords, borrowRecord)
coll.BorrowRecords = append(coll.BorrowRecords[:index], coll.BorrowRecords[index+1:]...)
removeRecord = append(removeRecord, borrowRecord)
coll.CollBalance -= borrowRecord.CollateralValue
log := action.GetFeedReceiptLog(coll, borrowRecord)
......@@ -1014,6 +1038,11 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
}
}
// 删除被清算的记录
for _, record := range removeRecord {
coll.BorrowRecords = removeLiquidateRecord(coll.BorrowRecords, *record)
}
// 保存
coll.LatestLiquidationPrice = getLatestLiquidationPrice(coll)
coll.LatestExpireTime = getLatestExpireTime(coll)
......
......@@ -97,6 +97,7 @@ func initEnv() *execEnv {
accA.SetDB(stateDB)
accA.SaveExecAccount(execAddr, &accountA)
manageKeySet("issuance-manage", accountA.Addr, stateDB)
manageKeySet("issuance-fund", accountA.Addr, stateDB)
tokenAccA, _ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB)
tokenAccA.SaveExecAccount(execAddr, &accountAToken)
......@@ -378,6 +379,36 @@ func TestIssuance(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value)
}
p61 := &pkt.IssuanceDebtTx{
IssuanceID: common.ToHex(issuanceID),
Value: 100,
}
createTx, err = pkt.CreateRawIssuanceDebtTx(env.cfg, p61)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = []byte(pkt.IssuanceX)
createTx, err = signTx(createTx, PrivKeyB)
if err != nil {
t.Error("RPC_Default_Process sign", "err", err)
}
exec.SetEnv(env.blockHeight+1, env.blockTime+1, env.difficulty)
receipt, err = exec.Exec(createTx, int(1))
assert.Nil(t, err)
assert.NotNil(t, receipt)
t.Log(receipt)
for _, kv := range receipt.KV {
env.db.Set(kv.Key, kv.Value)
}
receiptData = &types.ReceiptData{Ty: receipt.Ty, Logs: receipt.Logs}
set, err = exec.ExecLocal(createTx, receiptData, int(1))
assert.Nil(t, err)
assert.NotNil(t, set)
for _, kv := range set.KV {
env.kvdb.Set(kv.Key, kv.Value)
}
p7 := &pkt.IssuanceFeedTx{}
p7.Price = append(p7.Price, 0.28)
p7.Volume = append(p7.Volume, 100)
......
......@@ -355,7 +355,7 @@ func (action *Action) IssuanceCreate(create *pty.IssuanceCreate) (*types.Receipt
var receipt *types.Receipt
// 是否配置管理用户
if !isRightAddr(pty.ManageKey, action.fromaddr, action.db) {
if !isRightAddr(pty.FundKey, action.fromaddr, action.db) {
clog.Error("IssuanceCreate", "addr", action.fromaddr, "error", "Address has no permission to create")
return nil, pty.ErrPermissionDeny
}
......@@ -648,7 +648,7 @@ func (action *Action) IssuanceRepay(repay *pty.IssuanceRepay) (*types.Receipt, e
// 检查
if !action.CheckExecTokenAccount(action.fromaddr, debtRecord.DebtValue, false) {
clog.Error("IssuanceRepay", "CollID", issu.IssuanceId, "addr", action.fromaddr, "execaddr", action.execaddr, "error", types.ErrInsufficientBalance)
clog.Error("IssuanceRepay", "CollID", issu.IssuanceId, "addr", action.fromaddr, "execaddr", action.execaddr, "amount", debtRecord.DebtValue, "error", types.ErrInsufficientBalance)
return nil, types.ErrNoBalance
}
......@@ -701,12 +701,24 @@ func (action *Action) IssuanceRepay(repay *pty.IssuanceRepay) (*types.Receipt, e
return receipt, nil
}
func removeLiquidateRecord(debtRecords []*pty.DebtRecord, remove pty.DebtRecord) []*pty.DebtRecord {
for index, record := range debtRecords {
if record.DebtId == remove.DebtId {
debtRecords = append(debtRecords[:index], debtRecords[index+1:]...)
break
}
}
return debtRecords
}
// 系统清算
func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
var removeRecord []*pty.DebtRecord
for index, debtRecord := range issu.DebtRecords {
for _, debtRecord := range issu.DebtRecords {
if (debtRecord.LiquidationPrice*PriceWarningRate)/1e4 < price {
// 价格恢复,告警记录恢复
if debtRecord.Status == pty.IssuanceUserStatusWarning {
......@@ -718,8 +730,10 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types
continue
}
// 价格低于清算线,记录清算
if debtRecord.LiquidationPrice >= price {
// 价格低于清算线,记录清算
clog.Debug("systemLiquidation", "issuance id", debtRecord.IssuId, "record id", debtRecord.DebtId, "account", debtRecord.AccountAddr, "price", price)
getGuarantorAddr, err := getGuarantorAddr(action.db)
if err != nil {
if err != nil {
......@@ -742,7 +756,7 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types
debtRecord.PreStatus = debtRecord.Status
debtRecord.Status = pty.IssuanceUserStatusSystemLiquidate
issu.InvalidRecords = append(issu.InvalidRecords, debtRecord)
issu.DebtRecords = append(issu.DebtRecords[:index], issu.DebtRecords[index+1:]...)
removeRecord = append(removeRecord, debtRecord)
log := action.GetFeedReceiptLog(issu, debtRecord)
logs = append(logs, log)
......@@ -760,6 +774,11 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types
}
}
// 删除被清算的记录
for _, record := range removeRecord {
issu.DebtRecords = removeLiquidateRecord(issu.DebtRecords, *record)
}
// 保存
issu.LatestLiquidationPrice = getLatestLiquidationPrice(issu)
issu.LatestExpireTime = getLatestExpireTime(issu)
......@@ -775,18 +794,22 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types
func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
var removeRecord []*pty.DebtRecord
for index, debtRecord := range issu.DebtRecords {
for _, debtRecord := range issu.DebtRecords {
if debtRecord.ExpireTime-ExpireWarningTime > action.blocktime {
continue
}
// 超过超时时间,记录清算
if debtRecord.ExpireTime <= action.blocktime {
// 超过清算线,记录清算
clog.Debug("expireLiquidation", "issuance id", debtRecord.IssuId, "record id", debtRecord.DebtId, "account", debtRecord.AccountAddr, "time", action.blocktime)
getGuarantorAddr, err := getGuarantorAddr(action.db)
if err != nil {
if err != nil {
clog.Error("systemLiquidation", "getGuarantorAddr", err)
clog.Error("expireLiquidation", "getGuarantorAddr", err)
continue
}
}
......@@ -794,7 +817,7 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
// 抵押物转移
receipt, err := action.coinsAccount.ExecTransferFrozen(issu.IssuerAddr, getGuarantorAddr, action.execaddr, debtRecord.CollateralValue)
if err != nil {
clog.Error("systemLiquidation", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", debtRecord.CollateralValue, "error", err)
clog.Error("expireLiquidation", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", debtRecord.CollateralValue, "error", err)
continue
}
logs = append(logs, receipt.Logs...)
......@@ -805,7 +828,7 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
debtRecord.PreStatus = debtRecord.Status
debtRecord.Status = pty.IssuanceUserStatusExpireLiquidate
issu.InvalidRecords = append(issu.InvalidRecords, debtRecord)
issu.DebtRecords = append(issu.DebtRecords[:index], issu.DebtRecords[index+1:]...)
removeRecord = append(removeRecord, debtRecord)
log := action.GetFeedReceiptLog(issu, debtRecord)
logs = append(logs, log)
......@@ -821,6 +844,11 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
}
}
// 删除被清算的记录
for _, record := range removeRecord {
issu.DebtRecords = removeLiquidateRecord(issu.DebtRecords, *record)
}
// 保存
issu.LatestLiquidationPrice = getLatestLiquidationPrice(issu)
issu.LatestExpireTime = getLatestExpireTime(issu)
......@@ -931,8 +959,8 @@ func (action *Action) IssuanceClose(close *pty.IssuanceClose) (*types.Receipt, e
return nil, err
}
if !isRightAddr(pty.ManageKey, action.fromaddr, action.db) {
clog.Error("IssuanceClose", "addr", action.fromaddr, "error", "Address has no permission to close")
if action.fromaddr != issuance.IssuerAddr {
clog.Error("IssuanceClose", "IssuanceId", issuance.IssuanceId, "error", "account error", "create", issuance.IssuerAddr, "from", action.fromaddr)
return nil, pty.ErrPermissionDeny
}
......
......@@ -47,4 +47,5 @@ const (
PriceFeedKey = "issuance-price-feed"
GuarantorKey = "issuance-guarantor"
ManageKey = "issuance-manage"
FundKey = "issuance-fund"
)
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