Skip to content

Commit

Permalink
BN serialization fix (#347)
Browse files Browse the repository at this point in the history
* BN serialization fix
  • Loading branch information
IndiaJonathan authored Aug 29, 2024
1 parent 88d2c96 commit 9d23aca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions chain-api/src/ethers/utils/utf8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*
* @_subsection api/utils:Strings and UTF-8 [about-strings]
*/
import BigNumber from "bignumber.js";

import { assertArgument, assertNormalize } from "../errors.js";
import { BytesLike, getBytes } from "./data.js";

Expand Down Expand Up @@ -278,8 +280,17 @@ function getUtf8CodePoints(_bytes: BytesLike, onError?: Utf8ErrorFunc): Array<nu
*
* If %%form%% is specified, the string is normalized.
*/
export function toUtf8Bytes(str: string, form?: UnicodeNormalizationForm): Uint8Array {
assertArgument(typeof str === "string", "invalid string value", "str", str);
export function toUtf8Bytes(str: string | BigNumber, form?: UnicodeNormalizationForm): Uint8Array {
assertArgument(
typeof str === "string" || BigNumber.isBigNumber(str),
`invalid string or bignumber value`,
"str or BN",
str
);

if (BigNumber.isBigNumber(str)) {
str = str.toFixed();
}

if (form != null) {
assertNormalize(form);
Expand Down
6 changes: 6 additions & 0 deletions chain-api/src/utils/serialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import BigNumber from "bignumber.js";
import { ArrayMinSize, IsString } from "class-validator";

import { toUtf8Bytes } from "../ethers/utils/utf8";
import { ChainCallDTO, ChainObject } from "../types";
import { BigNumberProperty } from "../validators";
import serialize from "./serialize";
Expand Down Expand Up @@ -122,3 +123,8 @@ it("should handle very large numbers with no decimals", () => {
expect(chainObjectS).toEqual(expectedClassS);
expect(plainS).toEqual(expectedPlainS);
});

it("Bignumber toUtf8Bytes", () => {
const bigNumber = BigNumber("300");
expect(toUtf8Bytes(bigNumber)).toEqual(toUtf8Bytes("300"));
});

0 comments on commit 9d23aca

Please sign in to comment.