From aeea430a6009e689097ee04c74ae35fc090f67d1 Mon Sep 17 00:00:00 2001 From: paladz <453256728@qq.com> Date: Mon, 12 Aug 2019 13:47:06 +0800 Subject: [PATCH] add vote reward memo --- toolbar/apinode/transaction.go | 27 +++++++++++++++++++++- toolbar/consensusreward/consensus_reward.go | 2 +- .../settlementvotereward/settlementreward.go | 20 +++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/toolbar/apinode/transaction.go b/toolbar/apinode/transaction.go index 52208898..682b6ed5 100644 --- a/toolbar/apinode/transaction.go +++ b/toolbar/apinode/transaction.go @@ -1,6 +1,7 @@ package apinode import ( + "encoding/hex" "encoding/json" "github.com/vapor/blockchain/txbuilder" @@ -44,9 +45,33 @@ func (c *ControlAddressAction) MarshalJSON() ([]byte, error) { }) } -func (n *Node) BatchSendBTM(accountID, password string, outputs map[string]uint64) (string, error) { +type RetireAction struct { + *bc.AssetAmount + Arbitrary []byte +} + +func (r *RetireAction) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + Type string `json:"type"` + Arbitrary string `json:"arbitrary"` + *bc.AssetAmount + }{ + Type: "retire", + Arbitrary: hex.EncodeToString(r.Arbitrary), + AssetAmount: r.AssetAmount, + }) +} + +func (n *Node) BatchSendBTM(accountID, password string, outputs map[string]uint64, memo []byte) (string, error) { totalBTM := uint64(1000000) actions := []interface{}{} + if len(memo) > 0 { + actions = append(actions, &RetireAction{ + Arbitrary: memo, + AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: 1}, + }) + } + for address, amount := range outputs { actions = append(actions, &ControlAddressAction{ Address: address, diff --git a/toolbar/consensusreward/consensus_reward.go b/toolbar/consensusreward/consensus_reward.go index 8e3de025..66505630 100644 --- a/toolbar/consensusreward/consensus_reward.go +++ b/toolbar/consensusreward/consensus_reward.go @@ -71,7 +71,7 @@ func (s *StandbyNodeReward) Settlement() error { return nil } - txID, err := s.node.BatchSendBTM(s.cfg.RewardConf.AccountID, s.cfg.RewardConf.Password, rewards) + txID, err := s.node.BatchSendBTM(s.cfg.RewardConf.AccountID, s.cfg.RewardConf.Password, rewards, nil) if err == nil { log.WithFields(log.Fields{ "tx_hash": txID, diff --git a/toolbar/vote_reward/settlementvotereward/settlementreward.go b/toolbar/vote_reward/settlementvotereward/settlementreward.go index e110b95e..a6fdbccf 100644 --- a/toolbar/vote_reward/settlementvotereward/settlementreward.go +++ b/toolbar/vote_reward/settlementvotereward/settlementreward.go @@ -2,6 +2,7 @@ package settlementvotereward import ( "bytes" + "encoding/json" "math/big" "github.com/jinzhu/gorm" @@ -36,6 +37,13 @@ type SettlementReward struct { endHeight uint64 } +type memo struct { + StartHeight uint64 `json:"start_height"` + EndHeight uint64 `json:"end_height"` + NodePubkey string `json:"node_pubkey"` + RewardRatio uint64 `json:"reward_ratio"` +} + func NewSettlementReward(db *gorm.DB, cfg *config.Config, startHeight, endHeight uint64) *SettlementReward { return &SettlementReward{ db: db, @@ -85,8 +93,18 @@ func (s *SettlementReward) Settlement() error { return errNotRewardTx } + data, err := json.Marshal(&memo{ + StartHeight: s.startHeight, + EndHeight: s.endHeight, + NodePubkey: s.rewardCfg.XPub, + RewardRatio: s.rewardCfg.RewardRatio, + }) + if err != nil { + return err + } + // send transactions - _, err := s.node.BatchSendBTM(s.rewardCfg.AccountID, s.rewardCfg.Password, s.rewards) + _, err = s.node.BatchSendBTM(s.rewardCfg.AccountID, s.rewardCfg.Password, s.rewards, data) return err } -- 2.11.0