Skip to content

Commit

Permalink
Fix/broken mint (#195)
Browse files Browse the repository at this point in the history
* fixed flawed mint case

* coverage

* removed commented lines
  • Loading branch information
todesstille authored May 6, 2024
1 parent 91e9218 commit 4acf524
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
18 changes: 12 additions & 6 deletions contracts/libs/utils/TokenBalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ library TokenBalance {

if (token == ETHEREUM_ADDRESS) {
amount = amount.min(balance);
(bool status, ) = payable(receiver).call{value: amount}("");

require(status, "Failed to send eth");
if (amount > 0) {
(bool status, ) = payable(receiver).call{value: amount}("");
require(status, "Failed to send eth");
}
} else {
if (balance < amount) {
try
IERC20Gov(token).mint(address(this), (amount - balance).from18(token))
{} catch {
amount = balance;
}
{} catch {}

amount = normThisBalance(token).min(amount);
}

IERC20(token).safeTransfer(receiver, amount.from18(token));
uint256 amountWithDecimals = amount.from18(token);

if (amountWithDecimals > 0) {
IERC20(token).safeTransfer(receiver, amountWithDecimals);
}
}

return amount;
Expand Down
2 changes: 1 addition & 1 deletion contracts/mock/tokens/ERC20Mock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract ERC20Mock is ERC20 {
return _decimals;
}

function mint(address to, uint256 _amount) external {
function mint(address to, uint256 _amount) external virtual {
require(_allowMint, "ERC20Mock: minting is off");

_mint(to, _amount);
Expand Down
16 changes: 16 additions & 0 deletions contracts/mock/tokens/ERC20MockWithBadMint.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./ERC20Mock.sol";

contract ERC20MockWithBadMint is ERC20Mock {
constructor(
string memory name,
string memory symbol,
uint8 decimalPlaces
) ERC20Mock(name, symbol, decimalPlaces) {}

function mint(address to, uint256 _amount) external override {
return;
}
}
48 changes: 46 additions & 2 deletions test/gov/GovPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const VotePowerMock = artifacts.require("VotePowerMock");
const ERC721RawPower = artifacts.require("ERC721RawPower");
const ERC721Expert = artifacts.require("ERC721Expert");
const ERC20Mock = artifacts.require("ERC20Mock");
const ERC20MockWithBadMint = artifacts.require("ERC20MockWithBadMint");
const WethMock = artifacts.require("WETHMock");
const BscProperties = artifacts.require("NetworkPropertiesMock");
const ERC20 = artifacts.require("ERC20");
Expand Down Expand Up @@ -99,6 +100,7 @@ GovUserKeeper.numberFormat = "BigNumber";
ERC721EnumMock.numberFormat = "BigNumber";
ERC721Expert.numberFormat = "BigNumber";
ERC20Mock.numberFormat = "BigNumber";
ERC20MockWithBadMint.numberFormat = "BigNumber";
ERC20.numberFormat = "BigNumber";
BABTMock.numberFormat = "BigNumber";
WethMock.numberFormat = "BigNumber";
Expand Down Expand Up @@ -4323,10 +4325,10 @@ describe("GovPool", () => {
await govPool.moveProposalToValidators(1);
await validators.voteExternalProposal(1, wei("1000000000000"), true, { from: SECOND });

await network.provider.send("hardhat_setBalance", [govPool.address, "0x" + wei("100")]);

await govPool.execute(1);

await network.provider.send("hardhat_setBalance", [govPool.address, "0x" + toBN(wei("3.2")).toString(16)]);

await govPool.createProposal("example.com", [[settings.address, 0, getBytesAddSettings([NEW_SETTINGS])]], []);
await govPool.vote(2, true, wei("1"), []);

Expand All @@ -4349,6 +4351,16 @@ describe("GovPool", () => {

let tx = await govPool.claimRewards([2], OWNER);

assert.equal(
await web3.eth.getBalance(OWNER),
balance.minus(toBN(tx.receipt.gasUsed).times(tx.receipt.effectiveGasPrice)).toFixed(),
);

await network.provider.send("hardhat_setBalance", [govPool.address, "0x" + wei("100")]);
balance = toBN(await web3.eth.getBalance(OWNER));

tx = await govPool.claimRewards([2], OWNER);

assert.equal(
await web3.eth.getBalance(OWNER),
balance.plus(wei("16")).minus(toBN(tx.receipt.gasUsed).times(tx.receipt.effectiveGasPrice)).toFixed(),
Expand Down Expand Up @@ -4508,6 +4520,38 @@ describe("GovPool", () => {
assert.equal((await newToken.balanceOf(OWNER)).toFixed(), wei("16"));
});

it("should send correct reward if mint neither revert nor mint", async () => {
newToken = await ERC20MockWithBadMint.new("NT", "NT", 18);

NEW_SETTINGS.rewardsInfo.rewardToken = newToken.address;

const bytes = getBytesEditSettings([1], [NEW_SETTINGS]);

await govPool.createProposal("example.com", [[settings.address, 0, bytes]], []);
await govPool.vote(1, true, wei("100000000000000000000"), [], { from: SECOND });

await govPool.moveProposalToValidators(1);
await validators.voteExternalProposal(1, wei("1000000000000"), true, { from: SECOND });

await govPool.execute(1);

await govPool.createProposal(
"example.com",

[[settings.address, 0, getBytesAddSettings([NEW_SETTINGS])]],
[],
);
await govPool.vote(2, true, wei("1"), []);

assert.equal((await newToken.balanceOf(treasury)).toFixed(), "0");
assert.equal((await newToken.balanceOf(OWNER)).toFixed(), wei("0"));

await executeAndClaim(2, OWNER);

assert.equal((await newToken.balanceOf(treasury)).toFixed(), wei("0"));
assert.equal((await newToken.balanceOf(OWNER)).toFixed(), wei("0"));
});

describe("rewards with low pool balance", () => {
async function comparePendingRewards(
user,
Expand Down

0 comments on commit 4acf524

Please sign in to comment.