-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from keep-network/approve-and-call
Added `approveAndCall` to TBTC token
- Loading branch information
Showing
5 changed files
with
142 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity <0.9.0; | ||
|
||
import "../token/TBTCToken.sol"; | ||
|
||
contract ReceiveApprovalStub is IReceiveApproval { | ||
bool public shouldRevert; | ||
|
||
event ApprovalReceived( | ||
address from, | ||
uint256 value, | ||
address token, | ||
bytes extraData | ||
); | ||
|
||
function receiveApproval( | ||
address from, | ||
uint256 value, | ||
address token, | ||
bytes calldata extraData | ||
) external override { | ||
if (shouldRevert) { | ||
revert("i am your father luke"); | ||
} | ||
|
||
emit ApprovalReceived(from, value, token, extraData); | ||
} | ||
|
||
function setShouldRevert(bool _shouldRevert) external { | ||
shouldRevert = _shouldRevert; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { expect } = require("chai") | ||
const { to1e18, ZERO_ADDRESS } = require("../helpers/contract-test-helpers") | ||
|
||
describe("TBTCToken", () => { | ||
let approver | ||
|
||
let approvalReceiver | ||
let tbtc | ||
|
||
beforeEach(async () => { | ||
approver = await ethers.getSigner(1) | ||
|
||
const ReceiveApprovalStub = await ethers.getContractFactory( | ||
"ReceiveApprovalStub" | ||
) | ||
approvalReceiver = await ReceiveApprovalStub.deploy() | ||
await approvalReceiver.deployed() | ||
|
||
const TBTCToken = await ethers.getContractFactory("TBTCToken") | ||
tbtc = await TBTCToken.deploy() | ||
await tbtc.deployed() | ||
|
||
tbtc.mint(approver.address, to1e18(1000000)) | ||
}) | ||
|
||
describe("approveAndCall", () => { | ||
const amount = to1e18(200) | ||
|
||
context("when approval fails", () => { | ||
it("should revert", async () => { | ||
await expect( | ||
tbtc.connect(approver).approveAndCall(ZERO_ADDRESS, amount, []) | ||
).to.be.reverted | ||
}) | ||
}) | ||
|
||
context("when receiveApproval fails", () => { | ||
beforeEach(async () => { | ||
await approvalReceiver.setShouldRevert(true) | ||
}) | ||
|
||
it("should revert", async () => { | ||
await expect( | ||
tbtc | ||
.connect(approver) | ||
.approveAndCall(approvalReceiver.address, amount, []) | ||
).to.be.revertedWith("i am your father luke") | ||
}) | ||
}) | ||
|
||
it("approves the provided amount for transfer", async () => { | ||
await tbtc | ||
.connect(approver) | ||
.approveAndCall(approvalReceiver.address, amount, []) | ||
expect( | ||
await tbtc.allowance(approver.address, approvalReceiver.address) | ||
).to.equal(amount) | ||
}) | ||
|
||
it("calls approveAndCall with the provided parameters", async () => { | ||
const tx = await tbtc | ||
.connect(approver) | ||
.approveAndCall(approvalReceiver.address, amount, "0xbeef") | ||
await expect(tx) | ||
.to.emit(approvalReceiver, "ApprovalReceived") | ||
.withArgs(approver.address, amount, tbtc.address, "0xbeef") | ||
}) | ||
}) | ||
}) |