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

Fix PythBalance parser to work with commas #280

Merged
merged 3 commits into from
Nov 20, 2023
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: 2 additions & 0 deletions staking/app/pythBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class PythBalance {
}

static fromString(amount: string) {
amount = amount.split(",").join("");

if (amount.match(INTEGER_REGEXP)) {
return new PythBalance(new BN(amount).mul(new BN(10 ** PYTH_DECIMALS)));
} else if (amount.match(DECIMAL_REGEXP)) {
Expand Down
5 changes: 5 additions & 0 deletions staking/tests/pyth_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe("pyth balance tests", async () => {
assert(amount.eq(new PythBalance(new BN(60_969_430_243))));
assert(!amount.isZero());

amount = PythBalance.fromString("60,969.430243");
assert.equal(amount.toString(), "60,969.430243");
assert(amount.eq(new PythBalance(new BN(60_969_430_243))));
assert(!amount.isZero());

amount = PythBalance.fromString("060969.430243");
assert.equal(amount.toString(), "60,969.430243");
assert(amount.eq(new PythBalance(new BN(60_969_430_243))));
Expand Down