Skip to content

Commit

Permalink
isolate deposit and pledge.
Browse files Browse the repository at this point in the history
  • Loading branch information
studyzy committed Jun 28, 2019
1 parent 44946f3 commit 3e6522e
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 137 deletions.
80 changes: 0 additions & 80 deletions contracts/syscontract/deposit/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/palletone/go-palletone/contracts/syscontract"
pb "github.com/palletone/go-palletone/core/vmContractPub/protos/peer"
"github.com/palletone/go-palletone/dag/constants"
"github.com/palletone/go-palletone/dag/dagconfig"
"github.com/palletone/go-palletone/dag/modules"
)

Expand Down Expand Up @@ -514,19 +513,6 @@ func isFoundationInvoke(stub shim.ChaincodeStubInterface) bool {
// return nil
//}

// 每天计算各节点收益
func handlePledgeReward(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 0 {
return shim.Error("need 0 args")
}
err := handleRewardAllocation(stub)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)

}

// 每天计算各节点收益
//func handleEachDayAward1(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// if len(args) != 0 {
Expand Down Expand Up @@ -751,69 +737,3 @@ func getToday(stub shim.ChaincodeStubInterface) string {
log.Debugf("getToday GetTxTimestamp 10 result:%d, format string:%s", t.Seconds, str)
return str
}

//质押分红处理
func handleRewardAllocation(stub shim.ChaincodeStubInterface) error {
// 判断当天是否处理过
today := getToday(stub)
lastDate, err := getLastPledgeListDate(stub)

if lastDate == today {
return fmt.Errorf("%s pledge reward has been allocated before",today)
}
allM, err := getLastPledgeList(stub)
if err != nil {
return err
}
//计算分红
if allM != nil {
cp, err := stub.GetSystemConfig()
if err != nil {
return err
}
depositDailyReward := cp.DepositDailyReward
allM = pledgeRewardAllocation(allM, depositDailyReward)
} else {
allM = &modules.PledgeList{}
}
allM.Date = today
// 增加新的质押
depositList, err := getAllPledgeDepositRecords(stub)
if err != nil {
return err
}

for _, awardNode := range depositList {

allM.Add(awardNode.Address, awardNode.Amount)
err = delPledgeDepositRecord(stub, awardNode.Address)
if err != nil {
return err
}
}
err = saveLastPledgeList(stub, allM)
if err != nil {
return err
}

//处理提币请求
withdrawList, err := getAllPledgeWithdrawRecords(stub)
if err != nil {
return err
}
gasToken := dagconfig.DagConfig.GetGasToken().ToAsset()
for _, withdraw := range withdrawList {
withdrawAmt, _ := allM.Reduce(withdraw.Address, withdraw.Amount)
if withdrawAmt > 0 {
err := stub.PayOutToken(withdraw.Address, modules.NewAmountAsset(withdraw.Amount, gasToken), 0)
if err != nil {
return err
}
err = delPledgeWithdrawRecord(stub, withdraw.Address) //清空提取请求列表
if err != nil {
return err
}
}
}
return nil
}
26 changes: 13 additions & 13 deletions contracts/syscontract/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,28 @@ func (d *DepositChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response
log.Info("Enter DepositChaincode Contract " + PledgeWithdraw + " Invoke")
return processPledgeWithdraw(stub, args)

case QueryPledgeStatusByAddr://查询某用户的质押状态
case QueryPledgeStatusByAddr: //查询某用户的质押状态
log.Info("Enter DepositChaincode Contract " + QueryPledgeStatusByAddr + " Query")
return queryPledgeStatusByAddr(stub, args)
case QueryAllPledgeHistory://查询质押分红历史
case QueryAllPledgeHistory: //查询质押分红历史
log.Info("Enter DepositChaincode Contract " + QueryAllPledgeHistory + " Query")
return queryAllPledgeHistory(stub, args)

case HandlePledgeReward://质押分红处理
case HandlePledgeReward: //质押分红处理
log.Info("Enter DepositChaincode Contract " + HandlePledgeReward + " Invoke")
return handlePledgeReward(stub, args)

//TODO Devin一个用户,怎么查看自己的流水账?
//case AllPledgeVotes:
// b, err := getVotes(stub)
// if err != nil {
// return shim.Error(err.Error())
// }
// st := strconv.FormatInt(b, 10)
// return shim.Success([]byte(st))
case QueryPledgeList:
log.Info("Enter DepositChaincode Contract " + QueryPledgeList + " Query")
return d.queryPledgeList(stub, args)
return queryPledgeList(stub, args)
//TODO Devin一个用户,怎么查看自己的流水账?
//case AllPledgeVotes:
// b, err := getVotes(stub)
// if err != nil {
// return shim.Error(err.Error())
// }
// st := strconv.FormatInt(b, 10)
// return shim.Success([]byte(st))

//case ExtractPtnList:
// b, err := stub.GetState(ExtractPtnList)
// if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions contracts/syscontract/deposit/developer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func developerPayToDepositContract(stub shim.ChaincodeStubInterface, args []stri
log.Error("get invoke address err: ", "error", err)
return shim.Error(err.Error())
}
// 添加进入质押记录
err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
if err != nil {
return shim.Error(err.Error())
}
// // 添加进入质押记录
// err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
// if err != nil {
// return shim.Error(err.Error())
// }
//获取账户
balance, err := GetNodeBalance(stub, invokeAddr.String())
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions contracts/syscontract/deposit/jury.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func juryPayToDepositContract(stub shim.ChaincodeStubInterface, args []string) p
log.Error("get invoke address err: ", "error", err)
return shim.Error(err.Error())
}
// 添加进入质押记录
err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
if err != nil {
return shim.Error(err.Error())
}
// // 添加进入质押记录
// err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
// if err != nil {
// return shim.Error(err.Error())
// }
//获取账户
balance, err := GetNodeBalance(stub, invokeAddr.String())
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions contracts/syscontract/deposit/mediator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func mediatorPayToDepositContract(stub shim.ChaincodeStubInterface, args []strin
log.Error("get invoke address err: ", "error", err)
return shim.Error(err.Error())
}
// 添加进入质押记录
err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
if err != nil {
return shim.Error(err.Error())
}
// // 添加进入质押记录
// err = pledgeDepositRep(stub, invokeAddr, invokeTokens.Amount)
// if err != nil {
// return shim.Error(err.Error())
// }
// 判断是否已经申请了
md, err := GetMediatorDeposit(stub, invokeAddr.String())
if err != nil {
Expand Down
29 changes: 21 additions & 8 deletions contracts/syscontract/deposit/pledge_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package deposit
import (
"strconv"

"encoding/json"
"github.com/palletone/go-palletone/contracts/shim"
pb "github.com/palletone/go-palletone/core/vmContractPub/protos/peer"
"encoding/json"
)

// 质押PTN
Expand Down Expand Up @@ -48,6 +48,19 @@ func processPledgeDeposit(stub shim.ChaincodeStubInterface, args []string) pb.Re
return shim.Success(nil)
}

// 每天计算各节点收益
func handlePledgeReward(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 0 {
return shim.Error("need 0 args")
}
err := handleRewardAllocation(stub)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)

}

// 普通节点修改质押mediator
//func normalNodeChangeVote(stub shim.ChaincodeStubInterface, args []string) pb.Response {
//
Expand Down Expand Up @@ -92,7 +105,7 @@ func processPledgeWithdraw(stub shim.ChaincodeStubInterface, args []string) pb.R
return shim.Error(err.Error())
}
// 保存质押提取
err= pledgeWithdrawRep(stub,inAddr,ptnAccount)
err = pledgeWithdrawRep(stub, inAddr, ptnAccount)
if err != nil {
return shim.Error(err.Error())
}
Expand All @@ -102,27 +115,27 @@ func queryPledgeStatusByAddr(stub shim.ChaincodeStubInterface, args []string) pb
if len(args) != 1 {
return shim.Error("need 1 arg, Address")
}
status,err:= getPledgeStatus(stub,args[0])
status, err := getPledgeStatus(stub, args[0])
if err != nil {
return shim.Error(err.Error())
}
data,_:= json.Marshal(status)
data, _ := json.Marshal(status)
return shim.Success(data)
}
func queryAllPledgeHistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {

history,err:= getAllPledgeRewardHistory(stub)
history, err := getAllPledgeRewardHistory(stub)
if err != nil {
return shim.Error(err.Error())
}
data,_:= json.Marshal(history)
data, _ := json.Marshal(history)
return shim.Success(data)
}
func (d *DepositChaincode) queryPledgeList(stub shim.ChaincodeStubInterface, args []string) pb.Response {
func queryPledgeList(stub shim.ChaincodeStubInterface, args []string) pb.Response {
list, err := getLastPledgeList(stub)
if err != nil {
return shim.Error(err.Error())
}
result, _ := json.Marshal(list)
return shim.Success(result)
}
}
Loading

0 comments on commit 3e6522e

Please sign in to comment.