From 6325e5b92bd7ab8ff2de4c192a499bf7c221581f Mon Sep 17 00:00:00 2001 From: zhangzhiqiang <745124335@qq.com> Date: Wed, 14 Aug 2019 13:46:09 +0800 Subject: [PATCH 1/5] add new transaction addLiquidity --- src/chains/iris/coinswap.js | 3 +-- src/chains/iris/proto/tx.proto | 11 +++++++++-- src/chains/iris/tx/tx.js | 36 +++++++++++++++++++++++----------- src/util/utils.js | 17 ---------------- test/test_tx_iris.js | 2 +- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/chains/iris/coinswap.js b/src/chains/iris/coinswap.js index f0416b2..2719618 100644 --- a/src/chains/iris/coinswap.js +++ b/src/chains/iris/coinswap.js @@ -95,11 +95,10 @@ module.exports = class Stake { amount: Utils.toString(req.msg.output.coin.amount), }, }; - let deadline = Utils.marshalUTCString(req.msg.deadline); return new MsgSwapOrder({ input:input, output:output, - deadline:deadline, + deadline:req.msg.deadline, isBuyOrder:req.msg.isBuyOrder, }); } diff --git a/src/chains/iris/proto/tx.proto b/src/chains/iris/proto/tx.proto index 23c8b45..cefb7c5 100644 --- a/src/chains/iris/proto/tx.proto +++ b/src/chains/iris/proto/tx.proto @@ -59,8 +59,15 @@ message SwapOutput { message MsgSwapOrder { required SwapInput input = 1; required SwapOutput output = 2; - required string deadline = 3;//UTC - required bool is_buy_order = 4; + required int64 deadline = 3; + required bool isBuyOrder = 4; +} +message MsgAddLiquidity { + required Coin maxToken = 1; + required string exactIrisAmt = 2; + required string minLiquidity = 3; + required int64 deadline = 4; + required bytes sender = 5; } message StdFee { diff --git a/src/chains/iris/tx/tx.js b/src/chains/iris/tx/tx.js index 2c99b26..9f60724 100644 --- a/src/chains/iris/tx/tx.js +++ b/src/chains/iris/tx/tx.js @@ -2640,7 +2640,7 @@ $root.irisnet = (function() { * @interface IMsgSwapOrder * @property {irisnet.tx.ISwapInput} input MsgSwapOrder input * @property {irisnet.tx.ISwapOutput} output MsgSwapOrder output - * @property {string} deadline MsgSwapOrder deadline + * @property {number|Long} deadline MsgSwapOrder deadline * @property {boolean} isBuyOrder MsgSwapOrder isBuyOrder */ @@ -2677,11 +2677,11 @@ $root.irisnet = (function() { /** * MsgSwapOrder deadline. - * @member {string} deadline + * @member {number|Long} deadline * @memberof irisnet.tx.MsgSwapOrder * @instance */ - MsgSwapOrder.prototype.deadline = ""; + MsgSwapOrder.prototype.deadline = $util.Long ? $util.Long.fromBits(0,0,false) : 0; /** * MsgSwapOrder isBuyOrder. @@ -2717,7 +2717,7 @@ $root.irisnet = (function() { writer = $Writer.create(); $root.irisnet.tx.SwapInput.encode(message.input, writer.uint32(/* id 1, wireType 2 =*/10).fork()).ldelim(); $root.irisnet.tx.SwapOutput.encode(message.output, writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim(); - writer.uint32(/* id 3, wireType 2 =*/26).string(message.deadline); + writer.uint32(/* id 3, wireType 0 =*/24).int64(message.deadline); writer.uint32(/* id 4, wireType 0 =*/32).bool(message.isBuyOrder); return writer; }; @@ -2760,7 +2760,7 @@ $root.irisnet = (function() { message.output = $root.irisnet.tx.SwapOutput.decode(reader, reader.uint32()); break; case 3: - message.deadline = reader.string(); + message.deadline = reader.int64(); break; case 4: message.isBuyOrder = reader.bool(); @@ -2818,8 +2818,8 @@ $root.irisnet = (function() { if (error) return "output." + error; } - if (!$util.isString(message.deadline)) - return "deadline: string expected"; + if (!$util.isInteger(message.deadline) && !(message.deadline && $util.isInteger(message.deadline.low) && $util.isInteger(message.deadline.high))) + return "deadline: integer|Long expected"; if (typeof message.isBuyOrder !== "boolean") return "isBuyOrder: boolean expected"; return null; @@ -2848,7 +2848,14 @@ $root.irisnet = (function() { message.output = $root.irisnet.tx.SwapOutput.fromObject(object.output); } if (object.deadline != null) - message.deadline = String(object.deadline); + if ($util.Long) + (message.deadline = $util.Long.fromValue(object.deadline)).unsigned = false; + else if (typeof object.deadline === "string") + message.deadline = parseInt(object.deadline, 10); + else if (typeof object.deadline === "number") + message.deadline = object.deadline; + else if (typeof object.deadline === "object") + message.deadline = new $util.LongBits(object.deadline.low >>> 0, object.deadline.high >>> 0).toNumber(); if (object.isBuyOrder != null) message.isBuyOrder = Boolean(object.isBuyOrder); return message; @@ -2870,7 +2877,11 @@ $root.irisnet = (function() { if (options.defaults) { object.input = null; object.output = null; - object.deadline = ""; + if ($util.Long) { + var long = new $util.Long(0, 0, false); + object.deadline = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long; + } else + object.deadline = options.longs === String ? "0" : 0; object.isBuyOrder = false; } if (message.input != null && message.hasOwnProperty("input")) @@ -2878,7 +2889,10 @@ $root.irisnet = (function() { if (message.output != null && message.hasOwnProperty("output")) object.output = $root.irisnet.tx.SwapOutput.toObject(message.output, options); if (message.deadline != null && message.hasOwnProperty("deadline")) - object.deadline = message.deadline; + if (typeof message.deadline === "number") + object.deadline = options.longs === String ? String(message.deadline) : message.deadline; + else + object.deadline = options.longs === String ? $util.Long.prototype.toString.call(message.deadline) : options.longs === Number ? new $util.LongBits(message.deadline.low >>> 0, message.deadline.high >>> 0).toNumber() : message.deadline; if (message.isBuyOrder != null && message.hasOwnProperty("isBuyOrder")) object.isBuyOrder = message.isBuyOrder; return object; @@ -3758,4 +3772,4 @@ $root.irisnet = (function() { return irisnet; })(); -module.exports = $root; \ No newline at end of file +module.exports = $root; diff --git a/src/util/utils.js b/src/util/utils.js index 13fc807..dec5482 100644 --- a/src/util/utils.js +++ b/src/util/utils.js @@ -73,23 +73,6 @@ module.exports = class Utils { let eSet = new Set(target); return !(srcLen == eSet.size) } - - static marshalUTCString(date) { - let utcDateStr = date.toISOString(); - let nanoIndex = utcDateStr.lastIndexOf("."); - let prefix = utcDateStr.substr(0, nanoIndex); - let nano = utcDateStr.substr(nanoIndex + 1, utcDateStr.length - 1); - let index = nano.length - 2; - let offset = 0; - while (index >= 0 && nano[index] === "0") { - index-- - } - if (index < 0) { - return `${prefix}Z` - } - let suffix = nano.substr(0, index + 1); - return `${prefix}.${suffix}Z` - } }; function evil(fn) { diff --git a/test/test_tx_iris.js b/test/test_tx_iris.js index e69199e..9c20b8b 100644 --- a/test/test_tx_iris.js +++ b/test/test_tx_iris.js @@ -315,7 +315,7 @@ describe('iris transaction', function () { amount: "10000000000000000000" }, }, - deadline:new Date(), + deadline:new Date().getTime(), isBuyOrder:true } }; From 50ca9cf4d5932471e01d4219cd8048fb11aa9ac9 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang <745124335@qq.com> Date: Wed, 14 Aug 2019 15:00:45 +0800 Subject: [PATCH 2/5] add new transaction removeLiquidity --- config.json | 8 + src/chains/base.js | 2 + src/chains/iris/builder.js | 6 + src/chains/iris/coinswap.js | 218 ++++++++++-- src/chains/iris/proto/tx.proto | 8 + src/chains/iris/tx/tx.js | 610 +++++++++++++++++++++++++++++++++ test/test_account.js | 2 +- test/test_tx_iris.js | 63 +++- 8 files changed, 872 insertions(+), 45 deletions(-) diff --git a/config.json b/config.json index 3ab4961..0d6e280 100644 --- a/config.json +++ b/config.json @@ -25,6 +25,14 @@ "type": "withdraw_delegation_reward", "prefix": "irishub/distr/MsgWithdrawDelegationReward" }, + "addLiquidity": { + "type": "add_liquidity", + "prefix": "irishub/coinswap/MsgAddLiquidity" + }, + "removeLiquidity": { + "type": "remove_liquidity", + "prefix": "irishub/coinswap/MsgRemoveLiquidity" + }, "swapOrder": { "type": "swap_order", "prefix": "irishub/coinswap/MsgSwapOrder" diff --git a/src/chains/base.js b/src/chains/base.js index 84536c4..45d514c 100644 --- a/src/chains/base.js +++ b/src/chains/base.js @@ -99,4 +99,6 @@ amino.RegisterConcrete(R_Iris.irisnet.tx.MsgWithdrawDelegatorRewardsAll, Config. amino.RegisterConcrete(R_Iris.irisnet.tx.MsgWithdrawDelegatorReward, Config.iris.tx.withdrawDelegationReward.prefix); amino.RegisterConcrete(R_Iris.irisnet.tx.StdTx, Config.iris.tx.stdTx.prefix); amino.RegisterConcrete(R_Iris.irisnet.tx.MsgSwapOrder, Config.iris.tx.swapOrder.prefix); +amino.RegisterConcrete(R_Iris.irisnet.tx.MsgAddLiquidity, Config.iris.tx.addLiquidity.prefix); +amino.RegisterConcrete(R_Iris.irisnet.tx.MsgRemoveLiquidity, Config.iris.tx.removeLiquidity.prefix); module.exports = amino; \ No newline at end of file diff --git a/src/chains/iris/builder.js b/src/chains/iris/builder.js index 864d0a7..015d052 100644 --- a/src/chains/iris/builder.js +++ b/src/chains/iris/builder.js @@ -46,6 +46,12 @@ class IrisBuilder extends Builder { case Config.iris.tx.withdrawDelegationReward.type: { msg = Distribution.CreateMsgWithdrawDelegatorReward(req); break; + } case Config.iris.tx.addLiquidity.type: { + msg = Coinswap.createMsgAddLiquidity(req); + break; + } case Config.iris.tx.removeLiquidity.type: { + msg = Coinswap.createMsgRemoveLiquidity(req); + break; } case Config.iris.tx.swapOrder.type: { msg = Coinswap.createMsgSwapOrder(req); break; diff --git a/src/chains/iris/coinswap.js b/src/chains/iris/coinswap.js index 2719618..8d85d9b 100644 --- a/src/chains/iris/coinswap.js +++ b/src/chains/iris/coinswap.js @@ -7,16 +7,16 @@ const Utils = require('../../util/utils'); const MsgSwapOrder = Root.irisnet.tx.MsgSwapOrder; MsgSwapOrder.prototype.type = Config.iris.tx.swapOrder.prefix; MsgSwapOrder.prototype.GetSignBytes = function () { - let sender = BECH32.encode(Config.iris.bech32.accAddr,this.input.address); - let receiver = BECH32.encode(Config.iris.bech32.accAddr,this.output.address); + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.input.address); + let receiver = BECH32.encode(Config.iris.bech32.accAddr, this.output.address); let msg = { input: { - address:sender, - coin:this.input.coin + address: sender, + coin: this.input.coin }, output: { - address:receiver, - coin:this.output.coin + address: receiver, + coin: this.output.coin }, deadline: this.deadline, is_buy_order: this.isBuyOrder @@ -38,68 +38,212 @@ MsgSwapOrder.prototype.ValidateBasic = function () { throw new Error("isBuyOrder is empty"); } }; -MsgSwapOrder.prototype.GetMsg = function(){ +MsgSwapOrder.prototype.GetMsg = function () { return this }; -MsgSwapOrder.prototype.GetDisplayContent = function (){ - let sender = BECH32.encode(Config.iris.bech32.accAddr,this.input.address); - let receiver = BECH32.encode(Config.iris.bech32.accAddr,this.output.address); +MsgSwapOrder.prototype.GetDisplayContent = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.input.address); + let receiver = BECH32.encode(Config.iris.bech32.accAddr, this.output.address); return { - i18n_tx_type:"i18n_swap_order", - i18n_input:{ - address:sender, - coin:this.input.coin + i18n_tx_type: "i18n_swap_order", + i18n_input: { + address: sender, + coin: this.input.coin }, - i18n_output:{ - address:receiver, - coin:this.output.coin + i18n_output: { + address: receiver, + coin: this.output.coin }, i18n_deadline: this.deadline, i18n_is_buy_order: this.isBuyOrder } }; -MsgSwapOrder.prototype.toJSON = function(){ - let sender = BECH32.encode(Config.iris.bech32.accAddr,this.input.address); - let receiver = BECH32.encode(Config.iris.bech32.accAddr,this.output.address); +MsgSwapOrder.prototype.toJSON = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.input.address); + let receiver = BECH32.encode(Config.iris.bech32.accAddr, this.output.address); return { - input:{ - address:sender, - coin:this.input.coin + input: { + address: sender, + coin: this.input.coin }, - output:{ - address:receiver, - coin:this.output.coin + output: { + address: receiver, + coin: this.output.coin }, deadline: this.deadline, is_buy_order: this.isBuyOrder } }; +const MsgAddLiquidity = Root.irisnet.tx.MsgAddLiquidity; +MsgAddLiquidity.prototype.type = Config.iris.tx.addLiquidity.prefix; +MsgAddLiquidity.prototype.GetSignBytes = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + let msg = { + max_token: this.maxToken, + exact_iris_amt: this.exactIrisAmt, + min_liquidity: this.minLiquidity, + deadline: this.deadline, + sender: sender + }; + let sortMsg = Utils.sortObjectKeys(msg); + return Amino.MarshalJSON(this.type, sortMsg) +}; +MsgAddLiquidity.prototype.ValidateBasic = function () { + if (Utils.isEmpty(this.maxToken)) { + throw new Error("maxToken is empty"); + } + if (Utils.isEmpty(this.exactIrisAmt)) { + throw new Error("exactIrisAmt is empty"); + } + if (Utils.isEmpty(this.minLiquidity)) { + throw new Error("minLiquidity is empty"); + } + if (Utils.isEmpty(this.deadline)) { + throw new Error("deadline is empty"); + } + if (Utils.isEmpty(this.sender)) { + throw new Error("sender is empty"); + } +}; +MsgAddLiquidity.prototype.GetMsg = function () { + return this +}; +MsgAddLiquidity.prototype.GetDisplayContent = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + return { + i18n_tx_type: "i18n_add_liquidity", + i18n_max_token: this.maxToken, + i18n_exact_iris_amt: this.exactIrisAmt, + i18n_deadline: this.deadline, + i18n_min_liquidity: this.minLiquidity, + i18n_sender: sender + } +}; +MsgAddLiquidity.prototype.toJSON = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + return { + max_token: this.maxToken, + exact_iris_amt: this.exactIrisAmt, + min_liquidity: this.minLiquidity, + deadline: this.deadline, + sender: sender + } +}; + +const MsgRemoveLiquidity = Root.irisnet.tx.MsgRemoveLiquidity; +MsgRemoveLiquidity.prototype.type = Config.iris.tx.removeLiquidity.prefix; +MsgRemoveLiquidity.prototype.GetSignBytes = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + let msg = { + min_token: this.minToken, + withdraw_liquidity: this.withdrawLiquidity, + min_iris_amt: this.minIrisAmt, + deadline: this.deadline, + sender: sender + }; + let sortMsg = Utils.sortObjectKeys(msg); + return Amino.MarshalJSON(this.type, sortMsg) +}; +MsgRemoveLiquidity.prototype.ValidateBasic = function () { + if (Utils.isEmpty(this.minToken)) { + throw new Error("minToken is empty"); + } + if (Utils.isEmpty(this.withdrawLiquidity)) { + throw new Error("withdrawLiquidity is empty"); + } + if (Utils.isEmpty(this.minIrisAmt)) { + throw new Error("minIrisAmt is empty"); + } + if (Utils.isEmpty(this.deadline)) { + throw new Error("deadline is empty"); + } + if (Utils.isEmpty(this.sender)) { + throw new Error("sender is empty"); + } +}; +MsgRemoveLiquidity.prototype.GetMsg = function () { + return this +}; +MsgRemoveLiquidity.prototype.GetDisplayContent = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + return { + i18n_tx_type: "i18n_remove_liquidity", + i18n_min_token: this.minToken, + i18n_withdraw_liquidity: this.withdrawLiquidity, + i18n_min_iris_amt: this.minIrisAmt, + i18n_deadline: this.deadline, + i18n_sender: sender + } +}; +MsgRemoveLiquidity.prototype.toJSON = function () { + let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); + return { + min_token: this.minToken, + withdraw_liquidity: this.withdrawLiquidity, + min_iris_amt: this.minIrisAmt, + deadline: this.deadline, + sender: sender + } +}; + module.exports = class Stake { + static createMsgAddLiquidity(req) { + let maxToken = { + denom: req.msg.max_token.denom, + amount: Utils.toString(req.msg.max_token.amount), + }; + let exactIrisAmt = Utils.toString(req.msg.exact_iris_amt); + let minLiquidity = Utils.toString(req.msg.min_liquidity); + return new MsgAddLiquidity({ + maxToken: maxToken, + exactIrisAmt: exactIrisAmt, + minLiquidity: minLiquidity, + deadline: req.msg.deadline, + sender: BECH32.decode(req.from).words + }) + } + + static createMsgRemoveLiquidity(req) { + let withdrawLiquidity = { + denom: req.msg.withdraw_liquidity.denom, + amount: Utils.toString(req.msg.withdraw_liquidity.amount), + }; + let minIrisAmt = Utils.toString(req.msg.min_iris_amt); + let minToken = Utils.toString(req.msg.min_token); + return new MsgRemoveLiquidity({ + minToken: minToken, + withdrawLiquidity: withdrawLiquidity, + minIrisAmt: minIrisAmt, + deadline: req.msg.deadline, + sender: BECH32.decode(req.from).words + }) + } + static createMsgSwapOrder(req) { let sender = BECH32.decode(req.msg.input.address).words; let receiver = BECH32.decode(req.msg.output.address).words; let input = { - address : sender, - coin : { - denom: req.msg.input.coin.denom, - amount: Utils.toString(req.msg.input.coin.amount), - }, + address: sender, + coin: { + denom: req.msg.input.coin.denom, + amount: Utils.toString(req.msg.input.coin.amount), + }, }; let output = { - address : receiver, - coin : { + address: receiver, + coin: { denom: req.msg.output.coin.denom, amount: Utils.toString(req.msg.output.coin.amount), }, }; return new MsgSwapOrder({ - input:input, - output:output, - deadline:req.msg.deadline, - isBuyOrder:req.msg.isBuyOrder, + input: input, + output: output, + deadline: req.msg.deadline, + isBuyOrder: req.msg.isBuyOrder, }); } }; \ No newline at end of file diff --git a/src/chains/iris/proto/tx.proto b/src/chains/iris/proto/tx.proto index cefb7c5..8737e97 100644 --- a/src/chains/iris/proto/tx.proto +++ b/src/chains/iris/proto/tx.proto @@ -70,6 +70,14 @@ message MsgAddLiquidity { required bytes sender = 5; } +message MsgRemoveLiquidity { + required string minToken = 1; + required Coin withdrawLiquidity = 2; + required string minIrisAmt = 3; + required int64 deadline = 4; + required bytes sender = 5; +} + message StdFee { repeated Coin amount = 1; required int64 gas = 2; diff --git a/src/chains/iris/tx/tx.js b/src/chains/iris/tx/tx.js index 9f60724..82a9b5c 100644 --- a/src/chains/iris/tx/tx.js +++ b/src/chains/iris/tx/tx.js @@ -2912,6 +2912,616 @@ $root.irisnet = (function() { return MsgSwapOrder; })(); + tx.MsgAddLiquidity = (function() { + + /** + * Properties of a MsgAddLiquidity. + * @memberof irisnet.tx + * @interface IMsgAddLiquidity + * @property {irisnet.tx.ICoin} maxToken MsgAddLiquidity maxToken + * @property {string} exactIrisAmt MsgAddLiquidity exactIrisAmt + * @property {string} minLiquidity MsgAddLiquidity minLiquidity + * @property {number|Long} deadline MsgAddLiquidity deadline + * @property {Uint8Array} sender MsgAddLiquidity sender + */ + + /** + * Constructs a new MsgAddLiquidity. + * @memberof irisnet.tx + * @classdesc Represents a MsgAddLiquidity. + * @implements IMsgAddLiquidity + * @constructor + * @param {irisnet.tx.IMsgAddLiquidity=} [properties] Properties to set + */ + function MsgAddLiquidity(properties) { + if (properties) + for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) + if (properties[keys[i]] != null) + this[keys[i]] = properties[keys[i]]; + } + + /** + * MsgAddLiquidity maxToken. + * @member {irisnet.tx.ICoin} maxToken + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + */ + MsgAddLiquidity.prototype.maxToken = null; + + /** + * MsgAddLiquidity exactIrisAmt. + * @member {string} exactIrisAmt + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + */ + MsgAddLiquidity.prototype.exactIrisAmt = ""; + + /** + * MsgAddLiquidity minLiquidity. + * @member {string} minLiquidity + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + */ + MsgAddLiquidity.prototype.minLiquidity = ""; + + /** + * MsgAddLiquidity deadline. + * @member {number|Long} deadline + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + */ + MsgAddLiquidity.prototype.deadline = $util.Long ? $util.Long.fromBits(0,0,false) : 0; + + /** + * MsgAddLiquidity sender. + * @member {Uint8Array} sender + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + */ + MsgAddLiquidity.prototype.sender = $util.newBuffer([]); + + /** + * Creates a new MsgAddLiquidity instance using the specified properties. + * @function create + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {irisnet.tx.IMsgAddLiquidity=} [properties] Properties to set + * @returns {irisnet.tx.MsgAddLiquidity} MsgAddLiquidity instance + */ + MsgAddLiquidity.create = function create(properties) { + return new MsgAddLiquidity(properties); + }; + + /** + * Encodes the specified MsgAddLiquidity message. Does not implicitly {@link irisnet.tx.MsgAddLiquidity.verify|verify} messages. + * @function encode + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {irisnet.tx.IMsgAddLiquidity} message MsgAddLiquidity message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MsgAddLiquidity.encode = function encode(message, writer) { + if (!writer) + writer = $Writer.create(); + $root.irisnet.tx.Coin.encode(message.maxToken, writer.uint32(/* id 1, wireType 2 =*/10).fork()).ldelim(); + writer.uint32(/* id 2, wireType 2 =*/18).string(message.exactIrisAmt); + writer.uint32(/* id 3, wireType 2 =*/26).string(message.minLiquidity); + writer.uint32(/* id 4, wireType 0 =*/32).int64(message.deadline); + writer.uint32(/* id 5, wireType 2 =*/42).bytes(message.sender); + return writer; + }; + + /** + * Encodes the specified MsgAddLiquidity message, length delimited. Does not implicitly {@link irisnet.tx.MsgAddLiquidity.verify|verify} messages. + * @function encodeDelimited + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {irisnet.tx.IMsgAddLiquidity} message MsgAddLiquidity message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MsgAddLiquidity.encodeDelimited = function encodeDelimited(message, writer) { + return this.encode(message, writer).ldelim(); + }; + + /** + * Decodes a MsgAddLiquidity message from the specified reader or buffer. + * @function decode + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @param {number} [length] Message length if known beforehand + * @returns {irisnet.tx.MsgAddLiquidity} MsgAddLiquidity + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MsgAddLiquidity.decode = function decode(reader, length) { + if (!(reader instanceof $Reader)) + reader = $Reader.create(reader); + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.irisnet.tx.MsgAddLiquidity(); + while (reader.pos < end) { + var tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.maxToken = $root.irisnet.tx.Coin.decode(reader, reader.uint32()); + break; + case 2: + message.exactIrisAmt = reader.string(); + break; + case 3: + message.minLiquidity = reader.string(); + break; + case 4: + message.deadline = reader.int64(); + break; + case 5: + message.sender = reader.bytes(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + if (!message.hasOwnProperty("maxToken")) + throw $util.ProtocolError("missing required 'maxToken'", { instance: message }); + if (!message.hasOwnProperty("exactIrisAmt")) + throw $util.ProtocolError("missing required 'exactIrisAmt'", { instance: message }); + if (!message.hasOwnProperty("minLiquidity")) + throw $util.ProtocolError("missing required 'minLiquidity'", { instance: message }); + if (!message.hasOwnProperty("deadline")) + throw $util.ProtocolError("missing required 'deadline'", { instance: message }); + if (!message.hasOwnProperty("sender")) + throw $util.ProtocolError("missing required 'sender'", { instance: message }); + return message; + }; + + /** + * Decodes a MsgAddLiquidity message from the specified reader or buffer, length delimited. + * @function decodeDelimited + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @returns {irisnet.tx.MsgAddLiquidity} MsgAddLiquidity + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MsgAddLiquidity.decodeDelimited = function decodeDelimited(reader) { + if (!(reader instanceof $Reader)) + reader = new $Reader(reader); + return this.decode(reader, reader.uint32()); + }; + + /** + * Verifies a MsgAddLiquidity message. + * @function verify + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {Object.} message Plain object to verify + * @returns {string|null} `null` if valid, otherwise the reason why it is not + */ + MsgAddLiquidity.verify = function verify(message) { + if (typeof message !== "object" || message === null) + return "object expected"; + { + var error = $root.irisnet.tx.Coin.verify(message.maxToken); + if (error) + return "maxToken." + error; + } + if (!$util.isString(message.exactIrisAmt)) + return "exactIrisAmt: string expected"; + if (!$util.isString(message.minLiquidity)) + return "minLiquidity: string expected"; + if (!$util.isInteger(message.deadline) && !(message.deadline && $util.isInteger(message.deadline.low) && $util.isInteger(message.deadline.high))) + return "deadline: integer|Long expected"; + if (!(message.sender && typeof message.sender.length === "number" || $util.isString(message.sender))) + return "sender: buffer expected"; + return null; + }; + + /** + * Creates a MsgAddLiquidity message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {Object.} object Plain object + * @returns {irisnet.tx.MsgAddLiquidity} MsgAddLiquidity + */ + MsgAddLiquidity.fromObject = function fromObject(object) { + if (object instanceof $root.irisnet.tx.MsgAddLiquidity) + return object; + var message = new $root.irisnet.tx.MsgAddLiquidity(); + if (object.maxToken != null) { + if (typeof object.maxToken !== "object") + throw TypeError(".irisnet.tx.MsgAddLiquidity.maxToken: object expected"); + message.maxToken = $root.irisnet.tx.Coin.fromObject(object.maxToken); + } + if (object.exactIrisAmt != null) + message.exactIrisAmt = String(object.exactIrisAmt); + if (object.minLiquidity != null) + message.minLiquidity = String(object.minLiquidity); + if (object.deadline != null) + if ($util.Long) + (message.deadline = $util.Long.fromValue(object.deadline)).unsigned = false; + else if (typeof object.deadline === "string") + message.deadline = parseInt(object.deadline, 10); + else if (typeof object.deadline === "number") + message.deadline = object.deadline; + else if (typeof object.deadline === "object") + message.deadline = new $util.LongBits(object.deadline.low >>> 0, object.deadline.high >>> 0).toNumber(); + if (object.sender != null) + if (typeof object.sender === "string") + $util.base64.decode(object.sender, message.sender = $util.newBuffer($util.base64.length(object.sender)), 0); + else if (object.sender.length) + message.sender = object.sender; + return message; + }; + + /** + * Creates a plain object from a MsgAddLiquidity message. Also converts values to other types if specified. + * @function toObject + * @memberof irisnet.tx.MsgAddLiquidity + * @static + * @param {irisnet.tx.MsgAddLiquidity} message MsgAddLiquidity + * @param {$protobuf.IConversionOptions} [options] Conversion options + * @returns {Object.} Plain object + */ + MsgAddLiquidity.toObject = function toObject(message, options) { + if (!options) + options = {}; + var object = {}; + if (options.defaults) { + object.maxToken = null; + object.exactIrisAmt = ""; + object.minLiquidity = ""; + if ($util.Long) { + var long = new $util.Long(0, 0, false); + object.deadline = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long; + } else + object.deadline = options.longs === String ? "0" : 0; + if (options.bytes === String) + object.sender = ""; + else { + object.sender = []; + if (options.bytes !== Array) + object.sender = $util.newBuffer(object.sender); + } + } + if (message.maxToken != null && message.hasOwnProperty("maxToken")) + object.maxToken = $root.irisnet.tx.Coin.toObject(message.maxToken, options); + if (message.exactIrisAmt != null && message.hasOwnProperty("exactIrisAmt")) + object.exactIrisAmt = message.exactIrisAmt; + if (message.minLiquidity != null && message.hasOwnProperty("minLiquidity")) + object.minLiquidity = message.minLiquidity; + if (message.deadline != null && message.hasOwnProperty("deadline")) + if (typeof message.deadline === "number") + object.deadline = options.longs === String ? String(message.deadline) : message.deadline; + else + object.deadline = options.longs === String ? $util.Long.prototype.toString.call(message.deadline) : options.longs === Number ? new $util.LongBits(message.deadline.low >>> 0, message.deadline.high >>> 0).toNumber() : message.deadline; + if (message.sender != null && message.hasOwnProperty("sender")) + object.sender = options.bytes === String ? $util.base64.encode(message.sender, 0, message.sender.length) : options.bytes === Array ? Array.prototype.slice.call(message.sender) : message.sender; + return object; + }; + + /** + * Converts this MsgAddLiquidity to JSON. + * @function toJSON + * @memberof irisnet.tx.MsgAddLiquidity + * @instance + * @returns {Object.} JSON object + */ + MsgAddLiquidity.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return MsgAddLiquidity; + })(); + + tx.MsgRemoveLiquidity = (function() { + + /** + * Properties of a MsgRemoveLiquidity. + * @memberof irisnet.tx + * @interface IMsgRemoveLiquidity + * @property {string} minToken MsgRemoveLiquidity minToken + * @property {irisnet.tx.ICoin} withdrawLiquidity MsgRemoveLiquidity withdrawLiquidity + * @property {string} minIrisAmt MsgRemoveLiquidity minIrisAmt + * @property {number|Long} deadline MsgRemoveLiquidity deadline + * @property {Uint8Array} sender MsgRemoveLiquidity sender + */ + + /** + * Constructs a new MsgRemoveLiquidity. + * @memberof irisnet.tx + * @classdesc Represents a MsgRemoveLiquidity. + * @implements IMsgRemoveLiquidity + * @constructor + * @param {irisnet.tx.IMsgRemoveLiquidity=} [properties] Properties to set + */ + function MsgRemoveLiquidity(properties) { + if (properties) + for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) + if (properties[keys[i]] != null) + this[keys[i]] = properties[keys[i]]; + } + + /** + * MsgRemoveLiquidity minToken. + * @member {string} minToken + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + */ + MsgRemoveLiquidity.prototype.minToken = ""; + + /** + * MsgRemoveLiquidity withdrawLiquidity. + * @member {irisnet.tx.ICoin} withdrawLiquidity + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + */ + MsgRemoveLiquidity.prototype.withdrawLiquidity = null; + + /** + * MsgRemoveLiquidity minIrisAmt. + * @member {string} minIrisAmt + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + */ + MsgRemoveLiquidity.prototype.minIrisAmt = ""; + + /** + * MsgRemoveLiquidity deadline. + * @member {number|Long} deadline + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + */ + MsgRemoveLiquidity.prototype.deadline = $util.Long ? $util.Long.fromBits(0,0,false) : 0; + + /** + * MsgRemoveLiquidity sender. + * @member {Uint8Array} sender + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + */ + MsgRemoveLiquidity.prototype.sender = $util.newBuffer([]); + + /** + * Creates a new MsgRemoveLiquidity instance using the specified properties. + * @function create + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {irisnet.tx.IMsgRemoveLiquidity=} [properties] Properties to set + * @returns {irisnet.tx.MsgRemoveLiquidity} MsgRemoveLiquidity instance + */ + MsgRemoveLiquidity.create = function create(properties) { + return new MsgRemoveLiquidity(properties); + }; + + /** + * Encodes the specified MsgRemoveLiquidity message. Does not implicitly {@link irisnet.tx.MsgRemoveLiquidity.verify|verify} messages. + * @function encode + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {irisnet.tx.IMsgRemoveLiquidity} message MsgRemoveLiquidity message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MsgRemoveLiquidity.encode = function encode(message, writer) { + if (!writer) + writer = $Writer.create(); + writer.uint32(/* id 1, wireType 2 =*/10).string(message.minToken); + $root.irisnet.tx.Coin.encode(message.withdrawLiquidity, writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim(); + writer.uint32(/* id 3, wireType 2 =*/26).string(message.minIrisAmt); + writer.uint32(/* id 4, wireType 0 =*/32).int64(message.deadline); + writer.uint32(/* id 5, wireType 2 =*/42).bytes(message.sender); + return writer; + }; + + /** + * Encodes the specified MsgRemoveLiquidity message, length delimited. Does not implicitly {@link irisnet.tx.MsgRemoveLiquidity.verify|verify} messages. + * @function encodeDelimited + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {irisnet.tx.IMsgRemoveLiquidity} message MsgRemoveLiquidity message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MsgRemoveLiquidity.encodeDelimited = function encodeDelimited(message, writer) { + return this.encode(message, writer).ldelim(); + }; + + /** + * Decodes a MsgRemoveLiquidity message from the specified reader or buffer. + * @function decode + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @param {number} [length] Message length if known beforehand + * @returns {irisnet.tx.MsgRemoveLiquidity} MsgRemoveLiquidity + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MsgRemoveLiquidity.decode = function decode(reader, length) { + if (!(reader instanceof $Reader)) + reader = $Reader.create(reader); + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.irisnet.tx.MsgRemoveLiquidity(); + while (reader.pos < end) { + var tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.minToken = reader.string(); + break; + case 2: + message.withdrawLiquidity = $root.irisnet.tx.Coin.decode(reader, reader.uint32()); + break; + case 3: + message.minIrisAmt = reader.string(); + break; + case 4: + message.deadline = reader.int64(); + break; + case 5: + message.sender = reader.bytes(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + if (!message.hasOwnProperty("minToken")) + throw $util.ProtocolError("missing required 'minToken'", { instance: message }); + if (!message.hasOwnProperty("withdrawLiquidity")) + throw $util.ProtocolError("missing required 'withdrawLiquidity'", { instance: message }); + if (!message.hasOwnProperty("minIrisAmt")) + throw $util.ProtocolError("missing required 'minIrisAmt'", { instance: message }); + if (!message.hasOwnProperty("deadline")) + throw $util.ProtocolError("missing required 'deadline'", { instance: message }); + if (!message.hasOwnProperty("sender")) + throw $util.ProtocolError("missing required 'sender'", { instance: message }); + return message; + }; + + /** + * Decodes a MsgRemoveLiquidity message from the specified reader or buffer, length delimited. + * @function decodeDelimited + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @returns {irisnet.tx.MsgRemoveLiquidity} MsgRemoveLiquidity + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MsgRemoveLiquidity.decodeDelimited = function decodeDelimited(reader) { + if (!(reader instanceof $Reader)) + reader = new $Reader(reader); + return this.decode(reader, reader.uint32()); + }; + + /** + * Verifies a MsgRemoveLiquidity message. + * @function verify + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {Object.} message Plain object to verify + * @returns {string|null} `null` if valid, otherwise the reason why it is not + */ + MsgRemoveLiquidity.verify = function verify(message) { + if (typeof message !== "object" || message === null) + return "object expected"; + if (!$util.isString(message.minToken)) + return "minToken: string expected"; + { + var error = $root.irisnet.tx.Coin.verify(message.withdrawLiquidity); + if (error) + return "withdrawLiquidity." + error; + } + if (!$util.isString(message.minIrisAmt)) + return "minIrisAmt: string expected"; + if (!$util.isInteger(message.deadline) && !(message.deadline && $util.isInteger(message.deadline.low) && $util.isInteger(message.deadline.high))) + return "deadline: integer|Long expected"; + if (!(message.sender && typeof message.sender.length === "number" || $util.isString(message.sender))) + return "sender: buffer expected"; + return null; + }; + + /** + * Creates a MsgRemoveLiquidity message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {Object.} object Plain object + * @returns {irisnet.tx.MsgRemoveLiquidity} MsgRemoveLiquidity + */ + MsgRemoveLiquidity.fromObject = function fromObject(object) { + if (object instanceof $root.irisnet.tx.MsgRemoveLiquidity) + return object; + var message = new $root.irisnet.tx.MsgRemoveLiquidity(); + if (object.minToken != null) + message.minToken = String(object.minToken); + if (object.withdrawLiquidity != null) { + if (typeof object.withdrawLiquidity !== "object") + throw TypeError(".irisnet.tx.MsgRemoveLiquidity.withdrawLiquidity: object expected"); + message.withdrawLiquidity = $root.irisnet.tx.Coin.fromObject(object.withdrawLiquidity); + } + if (object.minIrisAmt != null) + message.minIrisAmt = String(object.minIrisAmt); + if (object.deadline != null) + if ($util.Long) + (message.deadline = $util.Long.fromValue(object.deadline)).unsigned = false; + else if (typeof object.deadline === "string") + message.deadline = parseInt(object.deadline, 10); + else if (typeof object.deadline === "number") + message.deadline = object.deadline; + else if (typeof object.deadline === "object") + message.deadline = new $util.LongBits(object.deadline.low >>> 0, object.deadline.high >>> 0).toNumber(); + if (object.sender != null) + if (typeof object.sender === "string") + $util.base64.decode(object.sender, message.sender = $util.newBuffer($util.base64.length(object.sender)), 0); + else if (object.sender.length) + message.sender = object.sender; + return message; + }; + + /** + * Creates a plain object from a MsgRemoveLiquidity message. Also converts values to other types if specified. + * @function toObject + * @memberof irisnet.tx.MsgRemoveLiquidity + * @static + * @param {irisnet.tx.MsgRemoveLiquidity} message MsgRemoveLiquidity + * @param {$protobuf.IConversionOptions} [options] Conversion options + * @returns {Object.} Plain object + */ + MsgRemoveLiquidity.toObject = function toObject(message, options) { + if (!options) + options = {}; + var object = {}; + if (options.defaults) { + object.minToken = ""; + object.withdrawLiquidity = null; + object.minIrisAmt = ""; + if ($util.Long) { + var long = new $util.Long(0, 0, false); + object.deadline = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long; + } else + object.deadline = options.longs === String ? "0" : 0; + if (options.bytes === String) + object.sender = ""; + else { + object.sender = []; + if (options.bytes !== Array) + object.sender = $util.newBuffer(object.sender); + } + } + if (message.minToken != null && message.hasOwnProperty("minToken")) + object.minToken = message.minToken; + if (message.withdrawLiquidity != null && message.hasOwnProperty("withdrawLiquidity")) + object.withdrawLiquidity = $root.irisnet.tx.Coin.toObject(message.withdrawLiquidity, options); + if (message.minIrisAmt != null && message.hasOwnProperty("minIrisAmt")) + object.minIrisAmt = message.minIrisAmt; + if (message.deadline != null && message.hasOwnProperty("deadline")) + if (typeof message.deadline === "number") + object.deadline = options.longs === String ? String(message.deadline) : message.deadline; + else + object.deadline = options.longs === String ? $util.Long.prototype.toString.call(message.deadline) : options.longs === Number ? new $util.LongBits(message.deadline.low >>> 0, message.deadline.high >>> 0).toNumber() : message.deadline; + if (message.sender != null && message.hasOwnProperty("sender")) + object.sender = options.bytes === String ? $util.base64.encode(message.sender, 0, message.sender.length) : options.bytes === Array ? Array.prototype.slice.call(message.sender) : message.sender; + return object; + }; + + /** + * Converts this MsgRemoveLiquidity to JSON. + * @function toJSON + * @memberof irisnet.tx.MsgRemoveLiquidity + * @instance + * @returns {Object.} JSON object + */ + MsgRemoveLiquidity.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return MsgRemoveLiquidity; + })(); + tx.StdFee = (function() { /** diff --git a/test/test_account.js b/test/test_account.js index aea0b2b..c9140ad 100644 --- a/test/test_account.js +++ b/test/test_account.js @@ -38,7 +38,7 @@ describe('account', function () { it('should recover', function () { let crypto = Irisnet.getCrypto(chainName, 'testnet'); - let seed = "tube lonely pause spring gym veteran know want grid tired taxi such same mesh charge orient bracket ozone concert once good quick dry boss"; + let seed = "error nerve credit mail coyote melt property afford design wool dune sibling loan tunnel acid joke father bid home pupil giant share age warrior"; let keyPair2 = crypto.recover(seed, Irisnet.config.language.en); console.log(JSON.stringify(keyPair2)) }); diff --git a/test/test_tx_iris.js b/test/test_tx_iris.js index 9c20b8b..fcce994 100644 --- a/test/test_tx_iris.js +++ b/test/test_tx_iris.js @@ -3,20 +3,21 @@ const chai = require('chai'); const assert = chai.assert; const common = require('./common'); -const url ="http://irisnet-lcd.dev.rainbow.one/tx/broadcast"; +//const url ="http://irisnet-lcd.dev.rainbow.one/tx/broadcast"; +const url ="http://localhost:1317/tx/broadcast"; const chainName ="iris"; describe('iris transaction', function () { - let chain_id = "hash"; - let from = "faa1aake3umjllpd9es5d3qmry4egcne0f8a8u5rsx"; + let chain_id = "irishub-test"; + let from = "faa1f3vflz39qr5sjzfkqmkzkr5dy7t646wyexy92y"; let gas = 10000; - let account_number = 0; + let account_number = 2; let fees = {denom: "iris-atto", amount: 600000000000000000}; let memo = "1"; - let privateKey = "465820F3AC6B406F8D599E558ACC48A135C57A2537CB96EF544A3D66449E7D63"; - let pubKey = "fap1addwnpepqtdme789cpm8zww058ndlhzpwst3s0mxnhdhu5uyps0wjucaufha6v3ce99"; + let privateKey = "80D45E1FAB9ACF59254F23C376E3AEAF139C847CD7A3126CDFD5216568730C90"; + let pubKey = "fap1addwnpepqwqw5pshzzswemf6t00xvf0ccf2fxslaz40dp76uyad5mgujfju4zt8km3u"; let chain = Irisnet.config.chain.iris; @@ -53,7 +54,7 @@ describe('iris transaction', function () { chain_id: chain_id, from: from, account_number: account_number, - sequence: 57, + sequence: 3, fees: fees, gas: gas, memo: memo, @@ -321,6 +322,52 @@ describe('iris transaction', function () { }; extracted(tx); }); + + it('test MsgAddLiquidity', function () { + let tx = { + chain_id: chain_id, + from: from, + account_number: account_number, + sequence: 26, + fees: fees, + gas: gas, + memo: memo, + type: Irisnet.config.iris.tx.addLiquidity.type, + msg: { + max_token : { + denom: "iris-atto", + amount: "10000000000000000000" + }, + exact_iris_amt: "10000000000000000000", + min_liquidity: "10000000000000000000", + deadline:new Date().getTime() + } + }; + extracted(tx); + }); + + it('test MsgRemoveLiquidity', function () { + let tx = { + chain_id: chain_id, + from: from, + account_number: account_number, + sequence: 26, + fees: fees, + gas: gas, + memo: memo, + type: Irisnet.config.iris.tx.removeLiquidity.type, + msg: { + withdraw_liquidity : { + denom: "iris-atto", + amount: "10000000000000000000" + }, + min_iris_amt: "10000000000000000000", + min_token: "10000000000000000000", + deadline:new Date().getTime() + } + }; + extracted(tx); + }); }); //冷钱包调用 @@ -338,6 +385,7 @@ describe('iris transaction', function () { //console.log("======待提交交易======"); //④步骤③的结果调用GetData,得到交易字符串,回传给联网的钱包,并发送该内容给irishub-server console.log(JSON.stringify(stdTx.GetData())); + console.log(JSON.stringify(stdTx.GetDisplayContent())); //以下步骤为异常处理:在请求irishub-server超时的时候,服务器可能没有任何返回结果,这笔交易状态为止,所以需要客户端计算出 //本次交易的hash,校准该笔交易的状态。调用步骤③结构的Hash,可以得到交易hash以及本次交易内容的base64编码(以后考虑使用该编码内容替换 @@ -369,4 +417,5 @@ describe('iris transaction', function () { function verify(act,exp,data) { assert.notExists(act.check_tx.code,`tx commit failed,${act.check_tx.log}`); + assert.equal(act.hash,exp.hash) } \ No newline at end of file From ab0ec255d970cee1b994cdcaaab6c26456b935a4 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang <745124335@qq.com> Date: Wed, 14 Aug 2019 19:20:39 +0800 Subject: [PATCH 3/5] fix bug --- src/chains/iris/coinswap.js | 45 ++++++++++++++++++++++++----- src/chains/iris/tx/tx_serializer.js | 2 +- src/util/utils.js | 2 ++ test/test_tx_iris.js | 44 +++++++++++++--------------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/chains/iris/coinswap.js b/src/chains/iris/coinswap.js index 8d85d9b..07fe1b9 100644 --- a/src/chains/iris/coinswap.js +++ b/src/chains/iris/coinswap.js @@ -22,7 +22,7 @@ MsgSwapOrder.prototype.GetSignBytes = function () { is_buy_order: this.isBuyOrder }; let sortMsg = Utils.sortObjectKeys(msg); - return Amino.MarshalJSON(this.type, sortMsg) + return Amino.MarshalJSON(this.type, sortMsg); }; MsgSwapOrder.prototype.ValidateBasic = function () { if (Utils.isEmpty(this.input)) { @@ -39,7 +39,22 @@ MsgSwapOrder.prototype.ValidateBasic = function () { } }; MsgSwapOrder.prototype.GetMsg = function () { - return this + let sender = BECH32.fromWords(this.input.address); + let receiver = BECH32.fromWords(this.output.address); + let input = { + address: sender, + coin: this.input.coin + }; + let output = { + address: receiver, + coin: this.output.coin + }; + return { + input: input, + output: output, + deadline: this.deadline, + isBuyOrder: this.isBuyOrder + } }; MsgSwapOrder.prototype.GetDisplayContent = function () { let sender = BECH32.encode(Config.iris.bech32.accAddr, this.input.address); @@ -108,7 +123,14 @@ MsgAddLiquidity.prototype.ValidateBasic = function () { } }; MsgAddLiquidity.prototype.GetMsg = function () { - return this + let sender = BECH32.fromWords(this.sender); + return { + maxToken: this.maxToken, + exactIrisAmt: this.exactIrisAmt, + minLiquidity: this.minLiquidity, + deadline: this.deadline, + sender: sender + } }; MsgAddLiquidity.prototype.GetDisplayContent = function () { let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); @@ -164,7 +186,14 @@ MsgRemoveLiquidity.prototype.ValidateBasic = function () { } }; MsgRemoveLiquidity.prototype.GetMsg = function () { - return this + let sender = BECH32.fromWords(this.sender); + return { + minToken: this.minToken, + withdrawLiquidity: this.withdrawLiquidity, + minIrisAmt: this.minIrisAmt, + deadline: this.deadline, + sender: sender + } }; MsgRemoveLiquidity.prototype.GetDisplayContent = function () { let sender = BECH32.encode(Config.iris.bech32.accAddr, this.sender); @@ -188,7 +217,7 @@ MsgRemoveLiquidity.prototype.toJSON = function () { } }; -module.exports = class Stake { +module.exports = class CoinSwap { static createMsgAddLiquidity(req) { let maxToken = { denom: req.msg.max_token.denom, @@ -200,7 +229,7 @@ module.exports = class Stake { maxToken: maxToken, exactIrisAmt: exactIrisAmt, minLiquidity: minLiquidity, - deadline: req.msg.deadline, + deadline: Utils.toString(req.msg.deadline), sender: BECH32.decode(req.from).words }) } @@ -216,7 +245,7 @@ module.exports = class Stake { minToken: minToken, withdrawLiquidity: withdrawLiquidity, minIrisAmt: minIrisAmt, - deadline: req.msg.deadline, + deadline: Utils.toString(req.msg.deadline), sender: BECH32.decode(req.from).words }) } @@ -242,7 +271,7 @@ module.exports = class Stake { return new MsgSwapOrder({ input: input, output: output, - deadline: req.msg.deadline, + deadline: Utils.toString(req.msg.deadline), isBuyOrder: req.msg.isBuyOrder, }); } diff --git a/src/chains/iris/tx/tx_serializer.js b/src/chains/iris/tx/tx_serializer.js index 7cb1379..79ef654 100644 --- a/src/chains/iris/tx/tx_serializer.js +++ b/src/chains/iris/tx/tx_serializer.js @@ -56,6 +56,7 @@ class TxSerializer { //stdTx amion编码前缀[auth/StdTx] let txPreBuf = Buffer.from(amino.GetRegisterInfo(config.iris.tx.stdTx.prefix).prefix); + let msgPreBuf = Buffer.from(info.prefix); let buf = Buffer.from(""); @@ -80,7 +81,6 @@ class TxSerializer { let uvarintBuf = Buffer.from(codec.Uvarint.encode(buf.length)); let bz = Buffer.concat([uvarintBuf, buf]); - const crypto = require('crypto'); const hash = crypto.createHash('sha256'); hash.update(bz); diff --git a/src/util/utils.js b/src/util/utils.js index dec5482..c883eef 100644 --- a/src/util/utils.js +++ b/src/util/utils.js @@ -19,6 +19,8 @@ module.exports = class Utils { tmp[k] = sort(obj[k]); } else if (obj[k] != null && typeof (obj[k]) === "function") { tmp[k] = evil(obj[k].toString()) + } else if (obj[k] != null && typeof (obj[k]) === "boolean") { + tmp[k] = obj[k] } else { tmp[k] = new String(obj[k]).toString(); } diff --git a/test/test_tx_iris.js b/test/test_tx_iris.js index fcce994..a9607d4 100644 --- a/test/test_tx_iris.js +++ b/test/test_tx_iris.js @@ -4,7 +4,7 @@ const assert = chai.assert; const common = require('./common'); //const url ="http://irisnet-lcd.dev.rainbow.one/tx/broadcast"; -const url ="http://localhost:1317/tx/broadcast"; +const url ="http://localhost:1317/tx/broadcast?commit=true"; const chainName ="iris"; @@ -12,7 +12,7 @@ describe('iris transaction', function () { let chain_id = "irishub-test"; let from = "faa1f3vflz39qr5sjzfkqmkzkr5dy7t646wyexy92y"; - let gas = 10000; + let gas = 20000; let account_number = 2; let fees = {denom: "iris-atto", amount: 600000000000000000}; let memo = "1"; @@ -177,7 +177,7 @@ describe('iris transaction', function () { chain_id: chain_id, from: from, account_number: account_number, - sequence: 53, + sequence: 21, fees: fees, gas: gas, memo: memo, @@ -296,7 +296,7 @@ describe('iris transaction', function () { chain_id: chain_id, from: from, account_number: account_number, - sequence: 26, + sequence: 34, fees: fees, gas: gas, memo: memo, @@ -306,17 +306,17 @@ describe('iris transaction', function () { address:from, coin:{ denom: "iris-atto", - amount: "10000000000000000000" + amount: "10000000000000000000000000000000" }, }, output : { address:from, coin:{ - denom: "iris-atto", - amount: "10000000000000000000" + denom: "btc-min", + amount: "1" }, }, - deadline:new Date().getTime(), + deadline:1565777966877, isBuyOrder:true } }; @@ -328,18 +328,18 @@ describe('iris transaction', function () { chain_id: chain_id, from: from, account_number: account_number, - sequence: 26, + sequence: 29, fees: fees, gas: gas, memo: memo, type: Irisnet.config.iris.tx.addLiquidity.type, msg: { max_token : { - denom: "iris-atto", - amount: "10000000000000000000" + denom: "btc-min", + amount: "10" }, - exact_iris_amt: "10000000000000000000", - min_liquidity: "10000000000000000000", + exact_iris_amt: "10000000000000000000000", + min_liquidity: "100000000000000000", deadline:new Date().getTime() } }; @@ -351,18 +351,18 @@ describe('iris transaction', function () { chain_id: chain_id, from: from, account_number: account_number, - sequence: 26, + sequence: 30, fees: fees, gas: gas, memo: memo, type: Irisnet.config.iris.tx.removeLiquidity.type, msg: { withdraw_liquidity : { - denom: "iris-atto", - amount: "10000000000000000000" + denom: "u-btc-min", + amount: "10000000000000000000000" }, - min_iris_amt: "10000000000000000000", - min_token: "10000000000000000000", + min_iris_amt: "10000000000000000", + min_token: "1", deadline:new Date().getTime() } }; @@ -372,7 +372,7 @@ describe('iris transaction', function () { //冷钱包调用 function extracted(tx, chain = 'iris') { - let builder = Irisnet.getBuilder(chain); + let builder = Irisnet.getBuilder(chain,'testnet'); //①先用联网的钱包构造一笔交易 let stdTx = builder.buildTx(tx); //②把步骤①的结构序列化为字符串,装入二维码 @@ -385,7 +385,6 @@ describe('iris transaction', function () { //console.log("======待提交交易======"); //④步骤③的结果调用GetData,得到交易字符串,回传给联网的钱包,并发送该内容给irishub-server console.log(JSON.stringify(stdTx.GetData())); - console.log(JSON.stringify(stdTx.GetDisplayContent())); //以下步骤为异常处理:在请求irishub-server超时的时候,服务器可能没有任何返回结果,这笔交易状态为止,所以需要客户端计算出 //本次交易的hash,校准该笔交易的状态。调用步骤③结构的Hash,可以得到交易hash以及本次交易内容的base64编码(以后考虑使用该编码内容替换 @@ -393,12 +392,9 @@ describe('iris transaction', function () { let resp = common.sendBySync("POST",url,stdTx.GetData()); let result = stdTx.Hash(); - console.log("data:", result.data); - console.log("hash", result.hash); - console.log(`hash=${result.hash}`) assert.notExists(resp.code,`tx commit failed,${resp.raw_log}`); - //console.log("displayContent", JSON.stringify(stdTx.GetDisplayContent())); + assert.equal(result.hash,resp.hash) } From 932d63b79043a2d697284a9cdf8e4c2568145f13 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang <745124335@qq.com> Date: Tue, 24 Sep 2019 16:53:57 +0800 Subject: [PATCH 4/5] update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 61d5810..91b6af4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "irisnet-crypto", - "version": "1.3.3", + "version": "1.3.4", "description": "irisnet-crypto", "main": "index.js", "scripts": { From b0386ea06f7aa7a5fe101c36f14111008746ffd3 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang <745124335@qq.com> Date: Thu, 28 Nov 2019 15:51:53 +0800 Subject: [PATCH 5/5] add parameter to GetData method for supporting to switch tx mode --- src/chains/cosmos/stdTx.js | 15 ++++++++++++--- test/common.js | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/chains/cosmos/stdTx.js b/src/chains/cosmos/stdTx.js index 48586b1..8c30067 100644 --- a/src/chains/cosmos/stdTx.js +++ b/src/chains/cosmos/stdTx.js @@ -99,7 +99,16 @@ class StdTx { } - GetData() { + /** + * + * @param mode string {block|sync|async} + * block: the client waits for the tx to be committed in a block. + * sync: the client waits for a CheckTx execution response only. + * async: the client returns immediately without execute CheckTx + * @returns {{mode: string, tx: {msg: [], fee: {amount: *, gas: string}, memo: *, signatures: []}}} + * @constructor + */ + GetData(mode = 'sync') { let signatures = []; if (this.signatures){ this.signatures.forEach(function(sig) { @@ -137,7 +146,7 @@ class StdTx { signatures: signatures, memo: this.memo }, - 'mode': 'sync' + 'mode': mode } } @@ -180,4 +189,4 @@ module.exports = class Bank { stdMsg.ValidateBasic(); return new StdTx(stdMsg) } -}; \ No newline at end of file +}; diff --git a/test/common.js b/test/common.js index ab56327..65f7bc6 100644 --- a/test/common.js +++ b/test/common.js @@ -41,7 +41,7 @@ async function verifyTx(url, tx, privateKey, chainName,callback) { //console.log(JSON.stringify(stdTx)); let exp = stdTx.Hash(); //console.log(JSON.stringify(exp)); - let payload = stdTx.GetData(); + let payload = stdTx.GetData("block"); let response = await sendByAsync("POST",url,payload); callback(response,exp,payload); @@ -96,4 +96,4 @@ function sendByAsync(method,url,data){ }) } -module.exports = {randomWord,randomHex,verifyTx,verifyAccount,sendBySync,getSequence}; \ No newline at end of file +module.exports = {randomWord,randomHex,verifyTx,verifyAccount,sendBySync,getSequence};