Skip to content

Commit

Permalink
inverse approve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Dec 1, 2022
1 parent 63dbd28 commit 8d84c3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/bridge-ui/src/erc20/bridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("bridge tests", () => {

it("approve throws when amount is already greater than whats set", async () => {
mockContract.allowance.mockImplementationOnce(() =>
opts.amountInWei.sub(1)
opts.amountInWei.add(1)
);

mockSigner.getAddress.mockImplementationOnce(() => "0xfake");
Expand All @@ -119,7 +119,7 @@ describe("bridge tests", () => {

expect(mockContract.allowance).not.toHaveBeenCalled();
await expect(bridge.Approve(approveOpts)).rejects.toThrowError(
"token vault does not have required allowance"
"token vault already has required allowance"
);

expect(mockSigner.getAddress).toHaveBeenCalled();
Expand All @@ -131,7 +131,7 @@ describe("bridge tests", () => {

it("approve succeeds when allowance is less than what is being requested", async () => {
mockContract.allowance.mockImplementationOnce(() =>
opts.amountInWei.add(1)
opts.amountInWei.sub(1)
);

mockSigner.getAddress.mockImplementationOnce(() => "0xfake");
Expand Down
12 changes: 5 additions & 7 deletions packages/bridge-ui/src/erc20/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class ERC20Bridge implements Bridge {
bridgeAddress: string
): Promise<boolean> {
const contract: Contract = new Contract(tokenAddress, ERC20, signer);
const allowance: BigNumber = await contract.allowance(
await signer.getAddress(),
bridgeAddress
);
const owner = await signer.getAddress();
const allowance: BigNumber = await contract.allowance(owner, bridgeAddress);

return allowance.lt(amount);
}
Expand All @@ -31,14 +29,14 @@ class ERC20Bridge implements Bridge {

async Approve(opts: ApproveOpts): Promise<Transaction> {
if (
await this.spenderRequiresAllowance(
!(await this.spenderRequiresAllowance(
opts.contractAddress,
opts.signer,
opts.amountInWei,
opts.spenderAddress
)
))
) {
throw Error("token vault does not have required allowance");
throw Error("token vault already has required allowance");
}

const contract: Contract = new Contract(
Expand Down

0 comments on commit 8d84c3d

Please sign in to comment.