Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

GasFwd.sweep fix + missing tests #285

Merged
merged 2 commits into from
Aug 8, 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
3 changes: 3 additions & 0 deletions contracts/core/gasFwd/GasFwd.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ contract GasFwd is IGasFwd, Initializable {

function sweep(address token) external onlyAccounts returns (uint256) {
uint256 balance = IERC20(token).balanceOf(address(this));
if (balance == 0) {
return 0;
}
IERC20(token).safeTransfer(msg.sender, balance);
emit Sweep(token, balance);
return balance;
Expand Down
27 changes: 22 additions & 5 deletions test/core/gasFwd/GasFwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {deployDummyERC20} from "test/utils";

describe("GasFwd", function () {
const {ethers, upgrades} = hre;
const BALANCE = 1000;

let owner: SignerWithAddress;
let admin: SignerWithAddress;
let user: SignerWithAddress;
Expand Down Expand Up @@ -44,25 +46,36 @@ describe("GasFwd", function () {
describe("upon payForGas", async function () {
let token: DummyERC20;
let gasFwd: GasFwd;
const BALANCE = 1000;
beforeEach(async function () {
token = await deployDummyERC20(owner);
gasFwd = await deployGasFwdAsProxy(owner, admin, user);
await token.mint(gasFwd.address, BALANCE);
});
it("reverts if called by non-accounts contract", async function () {
await expect(gasFwd.payForGas(token.address, 1)).to.be.revertedWithCustomError(
gasFwd,
"OnlyAccounts"
);
});
it("nothing happens when requested amount is 0", async function () {
await token.mint(gasFwd.address, BALANCE);
await expect(gasFwd.connect(user).payForGas(token.address, 0)).to.not.emit(gasFwd, "GasPay");
expect(await token.balanceOf(user.address)).to.equal(0);
expect(await token.balanceOf(gasFwd.address)).to.equal(BALANCE);
});
it("nothing happens when current balance is 0", async function () {
await expect(gasFwd.connect(user).payForGas(token.address, 1)).to.not.emit(gasFwd, "GasPay");
let balance = await token.balanceOf(user.address);
expect(balance).to.equal(0);
});
it("transfers tokens which do not exceed the balance", async function () {
await token.mint(gasFwd.address, BALANCE);
await gasFwd.connect(user).payForGas(token.address, 1);
let balance = await token.balanceOf(user.address);
expect(balance).to.equal(1);
});
it("transfers tokens when the call exceeds the balance", async function () {
await gasFwd.connect(user).payForGas(token.address, BALANCE+1);
await token.mint(gasFwd.address, BALANCE);
await gasFwd.connect(user).payForGas(token.address, BALANCE + 1);
let balance = await token.balanceOf(user.address);
expect(balance).to.equal(BALANCE);
});
Expand All @@ -74,18 +87,22 @@ describe("GasFwd", function () {
beforeEach(async function () {
token = await deployDummyERC20(owner);
gasFwd = await deployGasFwdAsProxy(owner, admin, user);
await token.mint(gasFwd.address, 10);
});
it("reverts if called by non-accounts contract", async function () {
await expect(gasFwd.sweep(token.address)).to.be.revertedWithCustomError(
gasFwd,
"OnlyAccounts"
);
});
it("nothing happens if there's no balance to sweep", async () => {
await expect(gasFwd.connect(user).sweep(token.address)).to.not.emit(gasFwd, "Sweep");
expect(await token.balanceOf(user.address)).to.equal(0);
});
it("transfers the token balance", async function () {
await token.mint(gasFwd.address, BALANCE);
await gasFwd.connect(user).sweep(token.address);
let balance = await token.balanceOf(user.address);
expect(balance).to.equal(10);
expect(balance).to.equal(BALANCE);
});
});
});
Loading