Skip to content

Commit

Permalink
fix(arweave): resolve BN parse error for extremely large Arweave bala…
Browse files Browse the repository at this point in the history
…nces

Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
  • Loading branch information
james-a-morris committed Feb 17, 2024
1 parent 49aef9b commit 604ba08
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Arweave from "arweave";
import { JWKInterface } from "arweave/node/lib/wallet";
import { ethers } from "ethers";
import winston from "winston";
import { isDefined, jsonReplacerWithBigNumbers, parseWinston } from "../../utils";
import { isDefined, jsonReplacerWithBigNumbers, parseWinston, toBN } from "../../utils";
import { Struct, is } from "superstruct";
import { ARWEAVE_TAG_APP_NAME } from "../../constants";

Expand Down Expand Up @@ -139,6 +139,14 @@ export class ArweaveClient {
async getBalance(): Promise<ethers.BigNumber> {
const address = await this.getAddress();
const balanceInFloat = await this.client.wallets.getBalance(address);
return parseWinston(balanceInFloat);
// Sometimes the balance is returned in scientific notation, so we need to
// convert it to a BigNumber
if (balanceInFloat.includes("e")) {
const [balance, exponent] = balanceInFloat.split("e");
const resultingBN = ethers.BigNumber.from(balance).mul(toBN(10).pow(exponent.replace("+", "")));
return parseWinston(resultingBN.toString());
} else {
return parseWinston(balanceInFloat);
}
}
}

0 comments on commit 604ba08

Please sign in to comment.