Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit fix 94 one approve #17

Merged
merged 4 commits into from
Jul 12, 2021
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
17 changes: 17 additions & 0 deletions contracts/SushiYieldSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ contract SushiYieldSource is IYieldSource, ReentrancyGuard {
using SafeERC20 for IERC20;
using SafeMath for uint256;

/// @notice Interface of the SushiBar contract
ISushiBar public immutable sushiBar;

/// @notice Interface for the Sushi token
IERC20 public immutable sushiAddr;

mapping(address => uint256) public balances;
Expand Down Expand Up @@ -52,6 +55,19 @@ contract SushiYieldSource is IYieldSource, ReentrancyGuard {
_sushiAddr.safeApprove(address(_sushiBar), type(uint256).max);
}

/// @notice Approve SUSHI to spend infinite sushiBar (xSUSHI)
/// @dev Emergency function to re-approve max amount if approval amount dropped too low
/// @return true if operation is successful
function approveMaxAmount() external returns (bool) {
address _sushiBarAddress = address(sushiBar);
IERC20 sushi = sushiAddr;

uint256 allowance = sushi.allowance(address(this), _sushiBarAddress);

sushi.safeIncreaseAllowance(_sushiBarAddress, type(uint256).max.sub(allowance));
return true;
}

/// @notice Returns the ERC20 asset token used for deposits.
/// @return The ERC20 asset token
function depositToken() external view override returns (address) {
Expand Down Expand Up @@ -116,6 +132,7 @@ contract SushiYieldSource is IYieldSource, ReentrancyGuard {
uint256 sushiBalanceDiff = sushiAfterBalance.sub(sushiBeforeBalance);

balances[msg.sender] = balances[msg.sender].sub(requiredSharesBalance);

sushi.safeTransfer(msg.sender, sushiBalanceDiff);
emit RedeemedToken(msg.sender, requiredSharesBalance, amount);

Expand Down
23 changes: 17 additions & 6 deletions test/unit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ describe("SushiYieldSource", function () {
});

describe("constructor()", () => {
let randomWalletAddress;

before(() => {
isDeployTest = true;
});

beforeEach(() => {
randomWalletAddress = ethers.Wallet.createRandom().address;
});

after(() => {
isDeployTest = false;
});

it('should succeed to construct yield source', async () => {
await deploySushiYieldSource(sushiBar.address, sushi.address);

expect(await yieldSource.sushiBar()).to.equal(sushiBar.address);
expect(await yieldSource.sushiAddr()).to.equal(sushi.address);
expect(await sushi.allowance(yieldSource.address, sushiBar.address)).to.equal(
ethers.constants.MaxUint256,
);
});

it("should fail if sushiBar address is address 0", async () => {
await expect(
deploySushiYieldSource(ethers.constants.AddressZero, sushi.address)
Expand All @@ -90,6 +94,13 @@ describe("SushiYieldSource", function () {
});
});

describe('approveMaxAmount()', () => {
it('should approve Sushi to spend max uint256 amount', async () => {
expect(await yieldSource.callStatic.approveMaxAmount()).to.eq(true);
expect(await sushi.allowance(yieldSource.address, sushiBar.address)).to.eq(ethers.constants.MaxUint256);
});
});

it("get token address", async function () {
let address = await yieldSource.depositToken();
expect(address == sushi);
Expand Down