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

update to support SafeERC20 #2

Merged
merged 2 commits into from
Jul 13, 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
47 changes: 30 additions & 17 deletions contracts/BadgerYieldSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
pragma solidity 0.6.12;

import { IYieldSource } from "@pooltogether/yield-source-interface/contracts/IYieldSource.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./IBadgerSett.sol";
import "./IBadger.sol";


/// @title A pooltogether yield source for badger sett
/// @author Steffel Fenix, 0xkarl
contract BadgerYieldSource is IYieldSource, ReentrancyGuard {
using SafeERC20 for IERC20;
using SafeMath for uint256;

IBadgerSett private immutable badgerSett;
IBadger private immutable badger;
IERC20 private immutable badger;
mapping(address => uint256) private balances;

/// @notice Emitted when asset tokens are redeemed from the yield source
Expand All @@ -33,11 +34,26 @@ contract BadgerYieldSource is IYieldSource, ReentrancyGuard {
address indexed to
);

constructor(address badgerSettAddr, address badgerAddr) public ReentrancyGuard() {
require(address(badgerSettAddr) != address(0), "BadgerYieldSource/badgerSettAddr-not-zero-address");
require(address(badgerAddr) != address(0), "BadgerYieldSource/badgerAddr-not-zero-address");
badgerSett = IBadgerSett(badgerSettAddr);
badger = IBadger(badgerAddr);
constructor(IBadgerSett _badgerSettAddr, IERC20 _badgerAddr) public ReentrancyGuard() {
require(address(_badgerSettAddr) != address(0), "BadgerYieldSource/badgerSettAddr-not-zero-address");
require(address(_badgerAddr) != address(0), "BadgerYieldSource/badgerAddr-not-zero-address");
badgerSett = _badgerSettAddr;
badger = _badgerAddr;

_badgerAddr.safeApprove(address(_badgerSettAddr), type(uint256).max);
}

/// @notice Approve Badger Sett vault to spend max uint256 amount
/// @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 _badgerSettAddress = address(badgerSett);
IERC20 _badger = badger;

uint256 allowance = _badger.allowance(address(this), _badgerSettAddress);

_badger.safeIncreaseAllowance(_badgerSettAddress, type(uint256).max.sub(allowance));
return true;
}

/// @notice Returns the ERC20 asset token used for deposits.
Expand All @@ -60,14 +76,11 @@ contract BadgerYieldSource is IYieldSource, ReentrancyGuard {
/// @param amount The amount of `token()` to be supplied
/// @param to The user whose balance will receive the tokens
function supplyTokenTo(uint256 amount, address to) external override nonReentrant {
IBadger _badger = badger;
IBadgerSett _badgerSett = badgerSett;
_badger.transferFrom(msg.sender, address(this), amount);
_badger.approve(address(_badgerSett), amount);

uint256 beforeBalance = _badgerSett.balanceOf(address(this));
_badgerSett.deposit(amount);
uint256 afterBalance = _badgerSett.balanceOf(address(this));
badger.safeTransferFrom(msg.sender, address(this), amount);

uint256 beforeBalance = badgerSett.balanceOf(address(this));
badgerSett.deposit(amount);
uint256 afterBalance = badgerSett.balanceOf(address(this));
uint256 balanceDiff = afterBalance.sub(beforeBalance);
balances[to] = balances[to].add(balanceDiff);

Expand Down Expand Up @@ -100,7 +113,7 @@ contract BadgerYieldSource is IYieldSource, ReentrancyGuard {

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

badger.transfer(msg.sender, badgerBalanceDiff);
badger.safeTransfer(msg.sender, badgerBalanceDiff);
emit RedeemedToken(msg.sender, requiredSharesBalance, amount);

return (badgerBalanceDiff);
Expand Down
17 changes: 0 additions & 17 deletions contracts/IBadger.sol

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"hardhat-typechain": "^0.3.5",
"prettier": "^2.2.1",
"prettier-plugin-solidity": "^1.0.0-beta.6",
"solidity-coverage": "^0.7.16",
"solhint": "3.3.6",
"solidity-coverage": "0.7.16",
"ts-generator": "^0.1.1",
"ts-node": "^9.1.1",
"typechain": "^4.0.3",
Expand Down
17 changes: 17 additions & 0 deletions test/unit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ describe("BadgerYieldSource", function () {
isDeployTest = false;
});

it('should succeed to construct yield source', async () => {
await deployBadgerYieldSource(badgerSett.address, badger.address);

expect(await badger.allowance(yieldSource.address, badgerSett.address)).to.equal(
ethers.constants.MaxUint256,
);
});

it('should fail if badgerSett address is address 0', async () => {
await expect(
deployBadgerYieldSource(ethers.constants.AddressZero, badger.address),
Expand All @@ -138,6 +146,15 @@ describe("BadgerYieldSource", function () {
});
});

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

it("get token address", async function () {
let address = await yieldSource.depositToken();
expect(address == badger);
Expand Down
41 changes: 40 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,13 @@
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.12.0.tgz#18a0fb2a9d2484b23176f63b16093c64794fc323"
integrity sha512-DT3f/Aa4tQysZwUsuqBwvr8YRJzKkvPUKV/9o2/o5EVw3xqlbzmtx4O60lTUcZdCawL+N8bBLNUyOGpHjGlJVQ==

"@solidity-parser/parser@^0.13.2":
version "0.13.2"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.13.2.tgz#b6c71d8ca0b382d90a7bbed241f9bc110af65cbe"
integrity sha512-RwHnpRnfrnD2MSPveYoPh8nhofEvX7fgjHk1Oq+NNvCcLx4r1js91CO9o+F/F3fBzOCyvm8kKRTriFICX/odWw==
dependencies:
antlr4ts "^0.5.0-alpha.4"

"@solidity-parser/parser@^0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.5.2.tgz#4d74670ead39e4f4fdab605a393ba8ea2390a2c4"
Expand Down Expand Up @@ -1644,6 +1651,11 @@ antlr4@4.7.1:
resolved "https://registry.yarnpkg.com/antlr4/-/antlr4-4.7.1.tgz#69984014f096e9e775f53dd9744bf994d8959773"
integrity sha512-haHyTW7Y9joE5MVs37P2lNYfU2RWBLfcRDD8OWldcdZm5TiCE91B5Xl1oWSwiDUSd4rlExpt2pu1fksYQjRBYQ==

antlr4ts@^0.5.0-alpha.4:
version "0.5.0-alpha.4"
resolved "https://registry.yarnpkg.com/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz#71702865a87478ed0b40c0709f422cf14d51652a"
integrity sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ==

anymatch@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142"
Expand Down Expand Up @@ -1747,6 +1759,11 @@ assign-symbols@^1.0.0:
resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367"
integrity sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c=

ast-parents@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/ast-parents/-/ast-parents-0.0.1.tgz#508fd0f05d0c48775d9eccda2e174423261e8dd3"
integrity sha1-UI/Q8F0MSHddnszaLhdEIyYejdM=

astral-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
Expand Down Expand Up @@ -8526,6 +8543,28 @@ solc@^0.6.3:
semver "^5.5.0"
tmp "0.0.33"

solhint@3.3.6:
version "3.3.6"
resolved "https://registry.yarnpkg.com/solhint/-/solhint-3.3.6.tgz#abe9af185a9a7defefba480047b3e42cbe9a1210"
integrity sha512-HWUxTAv2h7hx3s3hAab3ifnlwb02ZWhwFU/wSudUHqteMS3ll9c+m1FlGn9V8ztE2rf3Z82fQZA005Wv7KpcFA==
dependencies:
"@solidity-parser/parser" "^0.13.2"
ajv "^6.6.1"
antlr4 "4.7.1"
ast-parents "0.0.1"
chalk "^2.4.2"
commander "2.18.0"
cosmiconfig "^5.0.7"
eslint "^5.6.0"
fast-diff "^1.1.2"
glob "^7.1.3"
ignore "^4.0.6"
js-yaml "^3.12.0"
lodash "^4.17.11"
semver "^6.3.0"
optionalDependencies:
prettier "^1.14.3"

solhint@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/solhint/-/solhint-2.3.1.tgz#6fee8fc2635112bf5812f7cba8359c14e9d9a491"
Expand All @@ -8551,7 +8590,7 @@ solidity-comments-extractor@^0.0.4:
resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.4.tgz#ce420aef23641ffd0131c7d80ba85b6e1e42147e"
integrity sha512-58glBODwXIKMaQ7rfcJOrWtFQMMOK28tJ0/LcB5Xhu7WtAxk4UX2fpgKPuaL41XjMp/y0gAa1MTLqk018wuSzA==

solidity-coverage@^0.7.16:
solidity-coverage@0.7.16:
version "0.7.16"
resolved "https://registry.yarnpkg.com/solidity-coverage/-/solidity-coverage-0.7.16.tgz#c8c8c46baa361e2817bbf275116ddd2ec90a55fb"
integrity sha512-ttBOStywE6ZOTJmmABSg4b8pwwZfYKG8zxu40Nz+sRF5bQX7JULXWj/XbX0KXps3Fsp8CJXg8P29rH3W54ipxw==
Expand Down