-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: unbond from pool tx * fix: create pools tx * feat: use decimals patch * feat: add decimal patch to codegen * feat: fix legacy decs txs * fix: build * fix: amino type * feat: fix quote asset * fix: create cl pool tx * improvement: remove console log * fix: tests
- Loading branch information
1 parent
2291cff
commit 44e7356
Showing
63 changed files
with
783 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
preset: "ts-jest", | ||
roots: ["<rootDir>/src/", "<rootDir>/scripts/"], | ||
testMatch: ["**/__tests__/?(*.)+(spec|test).[jt]s?(x)"], | ||
testTimeout: 100000, | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
transform: { | ||
"^.+\\.(js|jsx)?$": [ | ||
"babel-jest", | ||
{ configFile: "../../babel.config.json" }, | ||
], | ||
"^.+\\.(ts|tsx)?$": [ | ||
"ts-jest", | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
watchPlugins: [ | ||
"jest-watch-typeahead/filename", | ||
"jest-watch-typeahead/testname", | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/proto-codecs/scripts/__tests__/decimal-patch.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// decimal.test.ts | ||
import { Decimal as CodegenDecimal } from "../../src/codegen/decimals"; | ||
import { Decimal as DecimalPatch } from "../decimals-patch"; | ||
|
||
/** | ||
* Run the tests for both the codegen and the patch | ||
* to ensure that the patch is correctly applied. | ||
*/ | ||
[DecimalPatch, CodegenDecimal].forEach((Decimal) => { | ||
test("should create a Decimal from user input", () => { | ||
const decimal = Decimal.fromUserInput("123.456", 3); | ||
expect(decimal.atomics).toBe("123456"); | ||
expect(decimal.fractionalDigits).toBe(3); | ||
}); | ||
|
||
test("should throw an error for invalid user input", () => { | ||
expect(() => Decimal.fromUserInput("123.45.6", 3)).toThrow( | ||
"More than one separator found" | ||
); | ||
expect(() => Decimal.fromUserInput("123a.456", 3)).toThrow( | ||
"Invalid character at position 4" | ||
); | ||
expect(() => Decimal.fromUserInput("123.4567", 3)).toThrow( | ||
"Got more fractional digits than supported" | ||
); | ||
}); | ||
|
||
test("should create a Decimal from atomics", () => { | ||
const decimal = Decimal.fromAtomics("123456", 3); | ||
expect(decimal.atomics).toBe("123456"); | ||
expect(decimal.fractionalDigits).toBe(3); | ||
}); | ||
|
||
test("should convert Decimal to string", () => { | ||
const decimal = Decimal.fromUserInput("123.456", 3); | ||
expect(decimal.toString()).toBe("123456"); | ||
|
||
const decimalNoFraction = Decimal.fromUserInput("123", 3); | ||
expect(decimalNoFraction.toString()).toBe("123000"); | ||
}); | ||
|
||
test("should handle edge cases", () => { | ||
const decimal = Decimal.fromUserInput("0.000", 3); | ||
expect(decimal.toString()).toBe("0"); | ||
|
||
const decimalMaxFraction = Decimal.fromUserInput("1.999", 3); | ||
expect(decimalMaxFraction.toString()).toBe("1999"); | ||
}); | ||
|
||
test("toString should return the atomics", () => { | ||
const decimal = Decimal.fromUserInput("0.01", 18); | ||
expect(decimal.toString()).toBe("10000000000000000"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* This class patch is required to override Telescope number handling. | ||
* Following Osmosis v26, which includes the new Cosmos SDK, we need to handle | ||
* numbers in a different way. Specifically, we need to omit periods in the | ||
* number representation to ensure compatibility with the updated SDK. | ||
* This patch ensures that the Decimal class processes numbers correctly | ||
* according to the new requirements. | ||
*/ | ||
|
||
const maxFractionalDigits = 30; | ||
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 { | ||
return this.atomics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/proto-codecs/src/codegen/cosmos/staking/v1beta1/staking.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.