Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply useCosmosSDKDec and useEnhancedDecimal #92

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .telescope.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"useDeepPartial": true,
"num64": "bigint",
"customTypes": {
"useCosmosSDKDec": false
"useCosmosSDKDec": true
},
"useTelescopeGeneratedType": true,
"autoFixUndefinedEnumDefault": true
Expand Down
9 changes: 5 additions & 4 deletions libs/cosmos-types/src/cosmos/base/v1beta1/coin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BinaryReader, BinaryWriter } from "../../../binary";
import { DeepPartial } from "../../../helpers";
import { Decimal } from "../../../decimals";
/**
* Coin defines a token with a denomination and an amount.
*
Expand Down Expand Up @@ -93,7 +94,7 @@ export const DecCoin = {
writer.uint32(10).string(message.denom);
}
if (message.amount !== "") {
writer.uint32(18).string(message.amount);
writer.uint32(18).string(Decimal.fromUserInput(message.amount, 18).atomics);
}
return writer;
},
Expand All @@ -108,7 +109,7 @@ export const DecCoin = {
message.denom = reader.string();
break;
case 2:
message.amount = reader.string();
message.amount = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down Expand Up @@ -171,7 +172,7 @@ export const DecProto = {
aminoType: "cosmos-sdk/DecProto",
encode(message: DecProto, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
if (message.dec !== "") {
writer.uint32(10).string(message.dec);
writer.uint32(10).string(Decimal.fromUserInput(message.dec, 18).atomics);
}
return writer;
},
Expand All @@ -183,7 +184,7 @@ export const DecProto = {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.dec = reader.string();
message.dec = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down
5 changes: 3 additions & 2 deletions libs/cosmos-types/src/cosmos/gov/v1beta1/gov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Any } from "../../../google/protobuf/any";
import { Timestamp } from "../../../google/protobuf/timestamp";
import { Duration } from "../../../google/protobuf/duration";
import { BinaryReader, BinaryWriter } from "../../../binary";
import { Decimal } from "../../../decimals";
import { DeepPartial, toTimestamp, fromTimestamp } from "../../../helpers";
/** VoteOption enumerates the valid vote options for a given governance proposal. */
export enum VoteOption {
Expand Down Expand Up @@ -270,7 +271,7 @@ export const WeightedVoteOption = {
writer.uint32(8).int32(message.option);
}
if (message.weight !== "") {
writer.uint32(18).string(message.weight);
writer.uint32(18).string(Decimal.fromUserInput(message.weight, 18).atomics);
}
return writer;
},
Expand All @@ -285,7 +286,7 @@ export const WeightedVoteOption = {
message.option = reader.int32() as any;
break;
case 2:
message.weight = reader.string();
message.weight = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down
29 changes: 15 additions & 14 deletions libs/cosmos-types/src/cosmos/staking/v1beta1/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Coin } from "../../base/v1beta1/coin";
import { ValidatorUpdate } from "../../../tendermint/abci/types";
import { BinaryReader, BinaryWriter } from "../../../binary";
import { DeepPartial, toTimestamp, fromTimestamp } from "../../../helpers";
import { Decimal } from "../../../decimals";
/** BondStatus is the status of a validator. */
export enum BondStatus {
/** BOND_STATUS_UNSPECIFIED - UNSPECIFIED defines an invalid validator status. */
Expand Down Expand Up @@ -391,13 +392,13 @@ export const CommissionRates = {
aminoType: "cosmos-sdk/CommissionRates",
encode(message: CommissionRates, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
if (message.rate !== "") {
writer.uint32(10).string(message.rate);
writer.uint32(10).string(Decimal.fromUserInput(message.rate, 18).atomics);
}
if (message.maxRate !== "") {
writer.uint32(18).string(message.maxRate);
writer.uint32(18).string(Decimal.fromUserInput(message.maxRate, 18).atomics);
}
if (message.maxChangeRate !== "") {
writer.uint32(26).string(message.maxChangeRate);
writer.uint32(26).string(Decimal.fromUserInput(message.maxChangeRate, 18).atomics);
}
return writer;
},
Expand All @@ -409,13 +410,13 @@ export const CommissionRates = {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.rate = reader.string();
message.rate = Decimal.fromAtomics(reader.string(), 18).toString();
break;
case 2:
message.maxRate = reader.string();
message.maxRate = Decimal.fromAtomics(reader.string(), 18).toString();
break;
case 3:
message.maxChangeRate = reader.string();
message.maxChangeRate = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down Expand Up @@ -583,7 +584,7 @@ export const Validator = {
writer.uint32(42).string(message.tokens);
}
if (message.delegatorShares !== "") {
writer.uint32(50).string(message.delegatorShares);
writer.uint32(50).string(Decimal.fromUserInput(message.delegatorShares, 18).atomics);
}
if (message.description !== undefined) {
Description.encode(message.description, writer.uint32(58).fork()).ldelim();
Expand Down Expand Up @@ -633,7 +634,7 @@ export const Validator = {
message.tokens = reader.string();
break;
case 6:
message.delegatorShares = reader.string();
message.delegatorShares = Decimal.fromAtomics(reader.string(), 18).toString();
break;
case 7:
message.description = Description.decode(reader, reader.uint32());
Expand Down Expand Up @@ -915,7 +916,7 @@ export const Delegation = {
writer.uint32(18).string(message.validatorAddress);
}
if (message.shares !== "") {
writer.uint32(26).string(message.shares);
writer.uint32(26).string(Decimal.fromUserInput(message.shares, 18).atomics);
}
return writer;
},
Expand All @@ -933,7 +934,7 @@ export const Delegation = {
message.validatorAddress = reader.string();
break;
case 3:
message.shares = reader.string();
message.shares = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down Expand Up @@ -1104,7 +1105,7 @@ export const RedelegationEntry = {
writer.uint32(26).string(message.initialBalance);
}
if (message.sharesDst !== "") {
writer.uint32(34).string(message.sharesDst);
writer.uint32(34).string(Decimal.fromUserInput(message.sharesDst, 18).atomics);
}
if (message.unbondingId !== BigInt(0)) {
writer.uint32(40).uint64(message.unbondingId);
Expand All @@ -1131,7 +1132,7 @@ export const RedelegationEntry = {
message.initialBalance = reader.string();
break;
case 4:
message.sharesDst = reader.string();
message.sharesDst = Decimal.fromAtomics(reader.string(), 18).toString();
break;
case 5:
message.unbondingId = reader.uint64();
Expand Down Expand Up @@ -1248,7 +1249,7 @@ export const Params = {
writer.uint32(42).string(message.bondDenom);
}
if (message.minCommissionRate !== "") {
writer.uint32(50).string(message.minCommissionRate);
writer.uint32(50).string(Decimal.fromUserInput(message.minCommissionRate, 18).atomics);
}
return writer;
},
Expand All @@ -1275,7 +1276,7 @@ export const Params = {
message.bondDenom = reader.string();
break;
case 6:
message.minCommissionRate = reader.string();
message.minCommissionRate = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down
113 changes: 113 additions & 0 deletions libs/cosmos-types/src/decimals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* This file and any referenced files were automatically generated by @cosmology/[email protected]
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/


// The largest value we need is 18 (Ether).
const maxFractionalDigits = 30;
/**
* A type for arbitrary precision, non-negative decimals.
*
* Instances of this class are immutable.
*/
export class Decimal {
public static fromUserInput(
input: string,
fractionalDigits: number
): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);
const badCharacter = input.match(/[^0-9.]/);
if (badCharacter) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw new Error(
`Invalid character at position ${badCharacter.index! + 1}`
);
}
let whole: string;
let fractional: string;
if (input === "") {
whole = "0";
fractional = "";
} else if (input.search(/\./) === -1) {
// integer format, no separator
whole = input;
fractional = "";
} else {
const parts = input.split(".");
switch (parts.length) {
case 0:
case 1:
throw new Error(
"Fewer than two elements in split result. This must not happen here."
);
case 2:
if (!parts[1]) throw new Error("Fractional part missing");
whole = parts[0];
fractional = parts[1].replace(/0+$/, "");
break;
default:
throw new Error("More than one separator found");
}
}
if (fractional.length > fractionalDigits) {
throw new Error("Got more fractional digits than supported");
}
const quantity = `${whole}${fractional.padEnd(fractionalDigits, "0")}`;
return new Decimal(quantity, fractionalDigits);
}
public static fromAtomics(
atomics: string,
fractionalDigits: number
): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);
return new Decimal(atomics, fractionalDigits);
}
private static verifyFractionalDigits(fractionalDigits: number): void {
if (!Number.isInteger(fractionalDigits))
throw new Error("Fractional digits is not an integer");
if (fractionalDigits < 0)
throw new Error("Fractional digits must not be negative");
if (fractionalDigits > maxFractionalDigits) {
throw new Error(
`Fractional digits must not exceed ${maxFractionalDigits}`
);
}
}
public get atomics(): string {
return this.data.atomics.toString();
}
public get fractionalDigits(): number {
return this.data.fractionalDigits;
}
private readonly data: {
readonly atomics: bigint;
readonly fractionalDigits: number;
};
private constructor(atomics: string, fractionalDigits: number) {
if (!atomics.match(/^[0-9]+$/)) {
throw new Error(
"Invalid string format. Only non-negative integers in decimal representation supported."
);
}
this.data = {
atomics: BigInt(atomics),
fractionalDigits: fractionalDigits,
};
}
public toString(): string {
const factor = BigInt(10) ** BigInt(this.data.fractionalDigits);
const whole = this.data.atomics / factor;
const fractional = this.data.atomics % factor;
if (fractional === 0n) {
return whole.toString();
} else {
const fullFractionalPart = fractional
.toString()
.padStart(this.data.fractionalDigits, "0");
const trimmedFractionalPart = fullFractionalPart.replace(/0+$/, "");
return `${whole.toString()}.${trimmedFractionalPart}`;
}
}
}
9 changes: 5 additions & 4 deletions libs/injective-react/src/cosmos/base/v1beta1/coin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BinaryReader, BinaryWriter } from "../../../binary";
import { DeepPartial } from "../../../helpers";
import { Decimal } from "../../../decimals";
/**
* Coin defines a token with a denomination and an amount.
*
Expand Down Expand Up @@ -209,7 +210,7 @@ export const DecCoin = {
writer.uint32(10).string(message.denom);
}
if (message.amount !== "") {
writer.uint32(18).string(message.amount);
writer.uint32(18).string(Decimal.fromUserInput(message.amount, 18).atomics);
}
return writer;
},
Expand All @@ -224,7 +225,7 @@ export const DecCoin = {
message.denom = reader.string();
break;
case 2:
message.amount = reader.string();
message.amount = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down Expand Up @@ -371,7 +372,7 @@ export const DecProto = {
},
encode(message: DecProto, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
if (message.dec !== "") {
writer.uint32(10).string(message.dec);
writer.uint32(10).string(Decimal.fromUserInput(message.dec, 18).atomics);
}
return writer;
},
Expand All @@ -383,7 +384,7 @@ export const DecProto = {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.dec = reader.string();
message.dec = Decimal.fromAtomics(reader.string(), 18).toString();
break;
default:
reader.skipType(tag & 7);
Expand Down
Loading
Loading