Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #110 from zhiqiang-bianjie/develop
Browse files Browse the repository at this point in the history
R4R: Support gov deposit&vote Tx
  • Loading branch information
Haifeng Xi authored Sep 2, 2019
2 parents 63c292a + 621b3f5 commit ff37460
Show file tree
Hide file tree
Showing 10 changed files with 779 additions and 81 deletions.
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
"type": "withdraw_delegation_reward",
"prefix": "irishub/distr/MsgWithdrawDelegationReward"
},
"deposit": {
"type": "deposit",
"prefix": "irishub/gov/MsgDeposit"
},
"vote": {
"type": "vote",
"prefix": "irishub/gov/MsgVote"
},
"stdTx": {
"type": "",
"prefix": "irishub/bank/StdTx"
Expand Down Expand Up @@ -119,4 +127,4 @@
"ethermint": "ethermint",
"cosmos": "cosmos"
}
}
}
4 changes: 3 additions & 1 deletion src/chains/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ amino.RegisterConcrete(R_Iris.irisnet.tx.MsgBeginRedelegate, Config.iris.tx.rede
amino.RegisterConcrete(R_Iris.irisnet.tx.MsgBeginUnbonding, Config.iris.tx.undelegate.prefix);
amino.RegisterConcrete(R_Iris.irisnet.tx.MsgWithdrawDelegatorRewardsAll, Config.iris.tx.withdrawDelegationRewardsAll.prefix);
amino.RegisterConcrete(R_Iris.irisnet.tx.MsgWithdrawDelegatorReward, Config.iris.tx.withdrawDelegationReward.prefix);
amino.RegisterConcrete(R_Iris.irisnet.tx.MsgDeposit, Config.iris.tx.deposit.prefix);
amino.RegisterConcrete(R_Iris.irisnet.tx.MsgVote, Config.iris.tx.vote.prefix);
amino.RegisterConcrete(R_Iris.irisnet.tx.StdTx, Config.iris.tx.stdTx.prefix);
module.exports = amino;
module.exports = amino;
4 changes: 2 additions & 2 deletions src/chains/iris/bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class StdTx {
}

module.exports = class Bank {
static CreateMsgSend(req) {
static createMsgSend(req) {
let coins = [];
if (!Utils.isEmpty(req.msg.coins)) {
req.msg.coins.forEach(function(item) {
Expand Down Expand Up @@ -360,4 +360,4 @@ module.exports = class Bank {
static NewStdSignMsg(chainID, accnum, sequence, fee, msg, memo, msgType) {
return new StdSignMsg(chainID, accnum, sequence, fee, msg, memo, msgType)
}
};
};
23 changes: 16 additions & 7 deletions src/chains/iris/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Old = require('old');
const Bank = require('./bank');
const Stake = require('./stake');
const Distribution = require('./distribution');
const Gov = require('./gov');
const IrisKeypair = require('./keypair');
const Codec = require("../../util/codec");
const Config = require('../../../config');
Expand All @@ -23,27 +24,35 @@ class IrisBuilder extends Builder {
let msg;
switch (req.type) {
case Config.iris.tx.transfer.type: {
msg = Bank.CreateMsgSend(req);
msg = Bank.createMsgSend(req);
break;
}
case Config.iris.tx.delegate.type: {
msg = Stake.CreateMsgDelegate(req);
msg = Stake.createMsgDelegate(req);
break;
}
case Config.iris.tx.undelegate.type: {
msg = Stake.CreateMsgBeginUnbonding(req);
msg = Stake.createMsgBeginUnbonding(req);
break;
}
case Config.iris.tx.redelegate.type: {
msg = Stake.CreateMsgBeginRedelegate(req);
msg = Stake.createMsgBeginRedelegate(req);
break;
}
case Config.iris.tx.withdrawDelegationRewardsAll.type: {
msg = Distribution.CreateMsgWithdrawDelegatorRewardsAll(req);
msg = Distribution.createMsgWithdrawDelegatorRewardsAll(req);
break;
}
case Config.iris.tx.withdrawDelegationReward.type: {
msg = Distribution.CreateMsgWithdrawDelegatorReward(req);
msg = Distribution.createMsgWithdrawDelegatorReward(req);
break;
}
case Config.iris.tx.deposit.type: {
msg = Gov.createMsgDeposit(req);
break;
}
case Config.iris.tx.vote.type: {
msg = Gov.createMsgVote(req);
break;
}
default: {
Expand Down Expand Up @@ -99,4 +108,4 @@ class IrisBuilder extends Builder {
return stdTx
}
}
module.exports = Old(IrisBuilder);
module.exports = Old(IrisBuilder);
6 changes: 3 additions & 3 deletions src/chains/iris/distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class MsgWithdrawDelegatorReward extends Builder.Msg {

module.exports = class Distribution {

static CreateMsgWithdrawDelegatorRewardsAll(req) {
static createMsgWithdrawDelegatorRewardsAll(req) {
return new MsgWithdrawDelegatorRewardsAll(req.from);
}

static CreateMsgWithdrawDelegatorReward(req) {
static createMsgWithdrawDelegatorReward(req) {
return new MsgWithdrawDelegatorReward(req.from,req.msg.validator_addr);
}
};
};
176 changes: 125 additions & 51 deletions src/chains/iris/gov.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,139 @@
'use strict';

const Builder = require("../../builder");
const Config = require('../../../config');
const Root = require('./tx/tx');
const BECH32 = require('bech32');
const Utils = require('../../util/utils');
const Amino = require('../base');
const Bank = require('./bank');

class SubmitProposalMsg extends Builder.Msg{
constructor(title, description, proposalKind,proposer,deposit) {
super();
this.Title = title;
this.Description = description;
this.ProposalKind = proposalKind;
this.Proposer = proposer;
this.InitialDeposit = deposit;
}

GetSignBytes() {
let msg = {
"Title": this.Title,
"Description": this.Description,
"ProposalKind": this.ProposalKind,
"Proposer": this.Proposer,
"InitialDeposit": this.InitialDeposit
};
let sortMsg = Utils.sortObjectKeys(msg);
return Amino.MarshalJSON(this.Type(),sortMsg)
}
ValidateBasic() {
if (Utils.isEmpty(this.Title)){
throw new Error("Title is empty");
}
const voteOptionMapping = {
0x00: 'Empty',
0x01: 'Yes',
0x02: 'Abstain',
0x03: 'No',
0x04: 'NoWithVeto'
};

if (Utils.isEmpty(this.Description)){
throw new Error("Description is empty");
}
const MsgDeposit = Root.irisnet.tx.MsgDeposit;
MsgDeposit.prototype.type = Config.iris.tx.deposit.prefix;
MsgDeposit.prototype.GetSignBytes = function () {
let depositor = BECH32.encode(Config.iris.bech32.accAddr, this.depositor);
let signMsg = {
proposal_id: `${this.proposalID}`,
depositor: depositor,
amount: this.amount
};
return Utils.sortObjectKeys(signMsg);
};

if (Utils.isEmpty(this.ProposalKind)){
throw new Error("delegation must great than 0");
}
MsgDeposit.prototype.ValidateBasic = function () {
if (Utils.isEmpty(this.amount)) {
throw new Error("amount is empty");
}
if (Utils.isEmpty(this.proposalID)) {
throw new Error("proposal_id is empty");
}
if (Utils.isEmpty(this.depositor)) {
throw new Error("depositor is empty");
}
};

if (Utils.isEmpty(this.Proposer)){
throw new Error("Proposer must great than 0");
}
MsgDeposit.prototype.GetMsg = function () {
let depositor = BECH32.fromWords(this.depositor);
return {
proposalID: this.proposalID,
depositor: depositor,
amount: this.amount
}
};

if (Utils.isEmpty(this.InitialDeposit)){
throw new Error("InitialDeposit must great than 0");
}
MsgDeposit.prototype.GetDisplayContent = function () {
let depositor = BECH32.encode(Config.iris.bech32.accAddr, this.depositor);
return {
i18n_tx_type: "i18n_deposit",
i18n_proposal_id:this.proposalID,
i18n_depositor: depositor,
i18n_amount: this.amount
}
};
MsgDeposit.prototype.toJSON = function () {
let depositor = BECH32.encode(Config.iris.bech32.accAddr, this.depositor);
return {
proposal_id: this.proposalID,
depositor: depositor,
amount: this.amount
}
};

Type(){
return "cosmos-sdk/MsgSubmitProposal";
const MsgVote = Root.irisnet.tx.MsgVote;
MsgVote.prototype.type = Config.iris.tx.vote.prefix;
MsgVote.prototype.GetSignBytes = function () {
let voter = BECH32.encode(Config.iris.bech32.accAddr, this.voter);
let signMsg = {
proposal_id: `${this.proposalID}`,
voter: voter,
option: voteOptionMapping[this.option]
};
return Utils.sortObjectKeys(signMsg);
};
MsgVote.prototype.ValidateBasic = function () {
if (Utils.isEmpty(this.option)) {
throw new Error("option is empty");
}
if (Utils.isEmpty(this.proposalID)) {
throw new Error("proposal_id is empty");
}
if (Utils.isEmpty(this.voter)) {
throw new Error("voter is empty");
}
}
};
MsgVote.prototype.GetMsg = function () {
let voter = BECH32.fromWords(this.voter);
return {
proposalID: this.proposalID,
voter: voter,
option: this.option
}
};
MsgVote.prototype.GetDisplayContent = function () {
let voter = BECH32.encode(Config.iris.bech32.accAddr, this.voter);
return {
i18n_tx_type: "i18n_vote",
i18n_proposal_id: this.proposalID,
i18n_voter: voter,
option: voteOptionMapping[this.option]
}
};
MsgVote.prototype.toJSON = function () {
let voter = BECH32.encode(Config.iris.bech32.accAddr, this.voter);
return {
proposal_id: this.proposalID,
voter: voter,
option: voteOptionMapping[this.option]
}
};

module.exports = class Gov {
static createMsgDeposit(req){
let coins = [];
if (!Utils.isEmpty(req.msg.amount)) {
req.msg.amount.forEach(function(item) {
coins.push({
denom: item.denom,
amount: Utils.toString(item.amount),
});
});
}
return new MsgDeposit({
proposalID: `${req.msg.proposal_id}`,
depositor: BECH32.decode(req.from).words,
amount: coins
})
}

// TODO
static GetSubmitProposalMsg(acc, validatorAddr, coins, fee, gas, memo){
let stdFee = Bank.NewStdFee(fee, gas);
let msg = new SubmitProposalMsg(acc.address, validatorAddr, coins);
let signMsg = Bank.NewStdSignMsg(acc.chain_id, acc.account_number, acc.sequence, stdFee, msg,memo);
return signMsg;
static createMsgVote(req){
return new MsgVote({
proposalID: `${req.msg.proposal_id}`,
voter: BECH32.decode(req.from).words,
option: req.msg.option
})
}
};
};
14 changes: 13 additions & 1 deletion src/chains/iris/proto/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ message MsgWithdrawDelegatorReward {
required bytes validatorAddr = 2;
}

message MsgDeposit {
required int64 proposalID = 1;
required bytes depositor = 2;
repeated Coin amount = 3;
}

message MsgVote {
required int64 proposalID = 1;
required bytes voter = 2;
required uint64 option = 3;
}

message StdFee {
repeated Coin amount = 1;
required int64 gas = 2;
Expand All @@ -66,4 +78,4 @@ message StdTx {
required StdFee fee = 2;
repeated StdSignature signatures = 3;
optional string memo = 4; //空值需要处理
}
}
8 changes: 4 additions & 4 deletions src/chains/iris/stake.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class MsgBeginRedelegate extends Builder.Msg {
}

module.exports = class Stake {
static CreateMsgDelegate(req) {
static createMsgDelegate(req) {
let delegation = {
denom: req.msg.delegation.denom,
amount: Utils.toString(req.msg.delegation.amount),
Expand All @@ -214,15 +214,15 @@ module.exports = class Stake {
return msg;
}

static CreateMsgBeginUnbonding(req) {
static createMsgBeginUnbonding(req) {
let shares = Utils.toDecString(req.msg.shares_amount);
let msg = new MsgBeginUnbonding(req.from, req.msg.validator_addr, shares);
return msg;
}

static CreateMsgBeginRedelegate(req) {
static createMsgBeginRedelegate(req) {
let shares = Utils.toDecString(req.msg.shares_amount);
let msg = new MsgBeginRedelegate(req.from, req.msg.validator_src_addr, req.msg.validator_dst_addr, shares);
return msg;
};
};
};
Loading

0 comments on commit ff37460

Please sign in to comment.