From 9c54a891a152579defab2408962f9a0d4d19be3f Mon Sep 17 00:00:00 2001 From: Peter <105609241+peterMangrove@users.noreply.github.com> Date: Wed, 4 Oct 2023 11:59:52 +0200 Subject: [PATCH] fix: infinite approval checks for 2^200 (#1531) --- CHANGELOG.md | 1 + src/mgvtoken.ts | 4 ++-- test/integration/mgvtoken.integration.test.ts | 23 ++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1afb97f52..bea45f712 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/mgvtoken.ts b/src/mgvtoken.ts index 2f92db366..bf2e4ac97 100644 --- a/src/mgvtoken.ts +++ b/src/mgvtoken.ts @@ -184,7 +184,7 @@ 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. */ @@ -192,7 +192,7 @@ class MgvToken { 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( diff --git a/test/integration/mgvtoken.integration.test.ts b/test/integration/mgvtoken.integration.test.ts index e82e2a7c0..47ac659e3 100644 --- a/test/integration/mgvtoken.integration.test.ts +++ b/test/integration/mgvtoken.integration.test.ts @@ -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"; @@ -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));