Skip to content

Commit

Permalink
fix: infinite approval checks for 2^200 (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMangrove authored Oct 4, 2023
1 parent 5a8e6d0 commit 9c54a89
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Next version

- Bump: mangrove-core to v1.5.10
- fix: infiniteApproval checks for larger than 2^200, instead of 2^256

# 1.4.24

Expand Down
4 changes: 2 additions & 2 deletions src/mgvtoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ class MgvToken {
}

/**
* Returns whether allowance of `owner` given to `spender` is infinite.
* Returns whether allowance of `owner` given to `spender` is more than 2^200.
* If `owner` is not specified, defaults to current signer.
* If `spender` is not specified, defaults to Mangrove instance.
*/
async allowanceInfinite(params: { owner?: string; spender?: string } = {}) {
const rawAllowance = await this.getRawAllowance({
spender: params.spender,
});
return rawAllowance.eq(ethers.constants.MaxUint256);
return rawAllowance.gt(ethers.BigNumber.from(2).pow(200));
}

private async getRawAllowance(
Expand Down
23 changes: 22 additions & 1 deletion test/integration/mgvtoken.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, it } from "mocha";
import assert from "assert";
import { afterEach, beforeEach, describe, it } from "mocha";
import { Mangrove, ethers } from "../../src";

import { Big } from "big.js";
Expand Down Expand Up @@ -48,6 +48,27 @@ describe("MGV Token integration tests suite", () => {
assert.ok(await usdc.allowanceInfinite());
});

it("allowanceInfinite is true if allowance is 2^200 + 1 ", async function () {
const usdc = await mgv.token("USDC");
assert.ok(!(await usdc.allowanceInfinite()));
await waitForTransaction(
usdc.approve(
mgv.address,
Big(2).pow(200).add(1).div(Big(10).pow(usdc.decimals))
)
);
assert.ok(await usdc.allowanceInfinite());
});

it("allowanceInfinite is false if allowance is 2^200 ", async function () {
const usdc = await mgv.token("USDC");
assert.ok(!(await usdc.allowanceInfinite()));
await waitForTransaction(
usdc.approve(mgv.address, Big(2).pow(200).div(Big(10).pow(usdc.decimals)))
);
assert.ok(!(await usdc.allowanceInfinite()));
});

it("approve sets approved amount", async function () {
const usdc = await mgv.token("USDC");
await waitForTransaction(await usdc.approve(mgv.address, 100));
Expand Down

0 comments on commit 9c54a89

Please sign in to comment.