-
Notifications
You must be signed in to change notification settings - Fork 336
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
parseCoins has to work with Big Int arithmetics. Uint64 is not enough #1268
Comments
Actually the only thing this line does is a format check on the string and strip leading zeros. So we can even implement it without using BigInt, which is still an issue for some environments. |
@webmaster128, thanks for reply The easiest case is just to use either the BigInt, or bn.js if you want so (or this is a backward compatibility issue). Like this: /* eslint-disable no-bitwise */
import BN from "bn.js";
import { Coin } from "@cosmjs/amino";
/**
* Takes a coins list like "819966000ucosm,700000000ustake" and parses it.
*
* This is a Stargate ready version of parseCoins from @cosmjs/amino.
* It supports more denoms.
*/
export function parseCoins(input: string): Coin[] {
return input
.replace(/\s/g, "")
.split(",")
.filter(Boolean)
.map((part) => {
// Denom regex from Stargate (https://github.com/cosmos/cosmos-sdk/blob/v0.42.7/types/coin.go#L599-L601)
const match = part.match(/^([0-9]+)([a-zA-Z][a-zA-Z0-9/]{2,127})$/);
if (!match) throw new Error("Got an invalid coin string");
return {
amount: new BN((match[1]), 10, "be").toString(),
denom: match[2],
};
});
} |
The point I am trying to make is that We use bn.js for historic reasons and I'd like to avoid creating more usages of it. I'd like to use native BigInt but the React Native folks make me wonder if we can use it in critical places. Anyways, I'll fix this one way or another. |
Thanks for reply |
Shipped as part of CosmJS 0.29.0 |
parseCoins
can work only with uint64 type which is too small for tokens with decimals like 12 or 18https://github.com/cosmos/cosmjs/blob/main/packages/proto-signing/src/coins.ts#L20
has to be changed to work with BigInt and serialized to a string type after
The text was updated successfully, but these errors were encountered: