From 04ac0abf19d84e7f6c78d0630ecffda52a50bfd5 Mon Sep 17 00:00:00 2001 From: joon9823 Date: Mon, 3 Jun 2024 12:59:37 +0900 Subject: [PATCH] Rollback to make spend limit optional for BasicAllowance --- package-lock.json | 4 +- package.json | 2 +- .../feegrant/allowances/BasicAllowance.ts | 40 ++++++++++--------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 55273c2..f03c45b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@initia/initia.js", - "version": "0.2.5", + "version": "0.2.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@initia/initia.js", - "version": "0.2.5", + "version": "0.2.6", "license": "MIT", "dependencies": { "@initia/initia.proto": "^0.2.0", diff --git a/package.json b/package.json index bac5858..933703f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@initia/initia.js", - "version": "0.2.5", + "version": "0.2.6", "description": "The JavaScript SDK for Initia", "license": "MIT", "author": "InitiaLabs", diff --git a/src/core/feegrant/allowances/BasicAllowance.ts b/src/core/feegrant/allowances/BasicAllowance.ts index e5e2b68..ebefe6f 100644 --- a/src/core/feegrant/allowances/BasicAllowance.ts +++ b/src/core/feegrant/allowances/BasicAllowance.ts @@ -13,25 +13,27 @@ export class BasicAllowance extends JSONSerializable< BasicAllowance.Data, BasicAllowance.Proto > { - public spend_limit: Coins; + public spend_limit?: Coins; /** * @param spend_limit spend_limit allowed to be spent as fee * @param expiration allowance's expiration */ - constructor(spend_limit: Coins.Input, public expiration?: Date) { + constructor(spend_limit?: Coins.Input, public expiration?: Date) { super(); let hasNotPositive = false; - this.spend_limit = new Coins(spend_limit); - this.spend_limit.map(c => { - // isPositive() from decimal.js returns true when the amount is 0. - // but Coins.IsAllPositive() from cosmos-sdk will return false in same case. - // so we use lessThanorEquenTo(0) instead of isPositive() == false - if (num(c.amount).isLessThanOrEqualTo(0)) { - hasNotPositive = true; - } - }); - if (hasNotPositive) { + if (spend_limit) { + this.spend_limit = new Coins(spend_limit); + this.spend_limit.map(c => { + // isPositive() from decimal.js returns true when the amount is 0. + // but Coins.IsAllPositive() from cosmos-sdk will return false in same case. + // so we use lessThanorEquenTo(0) instead of isPositive() == false + if (num(c.amount).isLessThanOrEqualTo(0)) { + hasNotPositive = true; + } + }); + } + if (spend_limit && hasNotPositive) { throw new Error('spend_limit must be positive'); } } @@ -42,7 +44,7 @@ export class BasicAllowance extends JSONSerializable< } = data; return new BasicAllowance( - Coins.fromAmino(spend_limit), + spend_limit ? Coins.fromAmino(spend_limit) : undefined, expiration ? new Date(expiration) : undefined ); } @@ -52,7 +54,7 @@ export class BasicAllowance extends JSONSerializable< return { type: 'cosmos-sdk/BasicAllowance', value: { - spend_limit: spend_limit.toAmino(), + spend_limit: spend_limit?.toAmino(), expiration: expiration?.toISOString().replace(/\.000Z$/, 'Z'), }, }; @@ -61,7 +63,7 @@ export class BasicAllowance extends JSONSerializable< public static fromData(proto: BasicAllowance.Data): BasicAllowance { const { spend_limit, expiration } = proto; return new BasicAllowance( - Coins.fromData(spend_limit), + spend_limit ? Coins.fromData(spend_limit) : undefined, expiration ? new Date(expiration) : undefined ); } @@ -70,7 +72,7 @@ export class BasicAllowance extends JSONSerializable< const { spend_limit, expiration } = this; return { '@type': '/cosmos.feegrant.v1beta1.BasicAllowance', - spend_limit: spend_limit.toData(), + spend_limit: spend_limit?.toData(), expiration: expiration?.toISOString().replace(/\.000Z$/, 'Z'), }; } @@ -86,7 +88,7 @@ export class BasicAllowance extends JSONSerializable< const { spend_limit, expiration } = this; return BasicAllowance_pb.fromPartial({ expiration, - spendLimit: spend_limit.toProto(), + spendLimit: spend_limit?.toProto(), }); } @@ -106,14 +108,14 @@ export namespace BasicAllowance { export interface Amino { type: 'cosmos-sdk/BasicAllowance'; value: { - spend_limit: Coins.Amino; + spend_limit?: Coins.Amino; expiration?: string; }; } export interface Data { '@type': '/cosmos.feegrant.v1beta1.BasicAllowance'; - spend_limit: Coins.Data; + spend_limit?: Coins.Data; expiration?: string; }