Skip to content

Commit

Permalink
Rollback to make spend limit optional for BasicAllowance
Browse files Browse the repository at this point in the history
  • Loading branch information
joon9823 committed Jun 3, 2024
1 parent efe8a0a commit 04ac0ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 21 additions & 19 deletions src/core/feegrant/allowances/BasicAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand All @@ -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
);
}
Expand All @@ -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'),
},
};
Expand All @@ -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
);
}
Expand All @@ -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'),
};
}
Expand All @@ -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(),
});
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 04ac0ab

Please sign in to comment.