Skip to content

Commit

Permalink
Merge 68117dc into d28c329
Browse files Browse the repository at this point in the history
  • Loading branch information
kamescg committed Jul 7, 2021
2 parents d28c329 + 68117dc commit 54732f3
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .solcover.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
skipFiles: ["test/RNGServiceMock.sol"],
skipFiles: ["test/"],
};
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"not-rely-on-time": "off",
"reason-string": ["warn", {"maxLength": 64}]
}
}
}
17 changes: 0 additions & 17 deletions contracts/ISushi.sol

This file was deleted.

48 changes: 26 additions & 22 deletions contracts/SushiYieldSource.sol
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
// SPDX-License-Identifier: GPL-3.0

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 "./ISushiBar.sol";
import "./ISushi.sol";

/// @title A pooltogether yield source for sushi token
/// @author Steffel Fenix
contract SushiYieldSource is IYieldSource {

using SafeERC20 for IERC20;
using SafeMath for uint256;

ISushiBar public immutable sushiBar;
ISushi public immutable sushiAddr;
IERC20 public immutable sushiAddr;

mapping(address => uint256) public balances;

constructor(ISushiBar _sushiBar, ISushi _sushiAddr) public {
constructor(ISushiBar _sushiBar, IERC20 _sushiAddr) public {
sushiBar = _sushiBar;
sushiAddr = _sushiAddr;
}

/// @notice Approve SUSHI to spend infinite sushiBar (xSUSHI)
function intialize() external {
sushiAddr.safeApprove(address(sushiBar), type(uint256).max);
}

/// @notice Returns the ERC20 asset token used for deposits.
/// @return The ERC20 asset token
function depositToken() public view override returns (address) {
Expand All @@ -38,56 +43,55 @@ contract SushiYieldSource is IYieldSource {
uint256 totalShares = sushiBar.totalSupply();
uint256 barSushiBalance = sushiAddr.balanceOf(address(sushiBar));

return balances[addr].mul(barSushiBalance).div(totalShares);
return balances[addr].mul(barSushiBalance).div(totalShares);
}

/// @notice Allows assets to be supplied on other user's behalf using the `to` param.
/// @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) public override {
sushiAddr.transferFrom(msg.sender, address(this), amount);
sushiAddr.approve(address(sushiBar), amount);
sushiAddr.safeTransferFrom(msg.sender, address(this), amount);

ISushiBar bar = sushiBar;
uint256 beforeBalance = bar.balanceOf(address(this));

bar.enter(amount);

uint256 afterBalance = bar.balanceOf(address(this));
uint256 balanceDiff = afterBalance.sub(beforeBalance);

balances[to] = balances[to].add(balanceDiff);
}

/// @notice Redeems tokens from the yield source to the msg.sender, it burns yield bearing tokens and returns token to the sender.
/// @param amount The amount of `token()` to withdraw. Denominated in `token()` as above.
/// @dev The maxiumum that can be called for token() is calculated by balanceOfToken() above.
/// @return The actual amount of tokens that were redeemed. This may be different from the amount passed due to the fractional math involved.
/// @return The actual amount of tokens that were redeemed. This may be different from the amount passed due to the fractional math involved.
function redeemToken(uint256 amount) public override returns (uint256) {
ISushiBar bar = sushiBar;
ISushi sushi = sushiAddr;
IERC20 sushi = sushiAddr;

uint256 totalShares = bar.totalSupply();
if(totalShares == 0) return 0;
if (totalShares == 0) return 0;

uint256 barSushiBalance = sushi.balanceOf(address(bar));
if(barSushiBalance == 0) return 0;
if (barSushiBalance == 0) return 0;

uint256 sushiBeforeBalance = sushi.balanceOf(address(this));

uint256 requiredShares = ((amount.mul(totalShares) + totalShares)).div(barSushiBalance);
if(requiredShares == 0) return 0;
if (requiredShares == 0) return 0;

uint256 requiredSharesBalance = requiredShares.sub(1);
bar.leave(requiredSharesBalance);

uint256 sushiAfterBalance = sushi.balanceOf(address(this));

uint256 sushiBalanceDiff = sushiAfterBalance.sub(sushiBeforeBalance);

balances[msg.sender] = balances[msg.sender].sub(requiredSharesBalance);
sushi.transfer(msg.sender, sushiBalanceDiff);
sushi.safeTransfer(msg.sender, sushiBalanceDiff);

return (sushiBalanceDiff);
}

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 All @@ -39,10 +40,10 @@
"etherscan-verify": "hardhat etherscan-verify --network",
"test:integration": "FORK_MAINNET=true yarn hardhat test --network hardhat",
"test": "yarn hardhat test --network hardhat test/unit_test.js",
"lint": "yarn solhint 'contracts/SushiYieldSource.sol' && yarn prettier -c './**/*.js'",
"lint": "yarn solhint 'contracts/SushiYieldSource.sol'",
"format": "yarn prettier --write contracts/*.sol && yarn prettier --write test/*.js",
"hint": "solhint \"contracts/SushiYieldSource.sol\"",
"coverage": "FORK_MAINNET=true yarn hardhat coverage --testfiles \"test/*.js\"",
"coverage": "OPTIMIZER_DISABLED=true hardhat coverage --testfiles \"test/unit_test.js\"",
"coverage:file": "yarn hardhat coverage --testfiles",
"start-fork": "FORK_MAINNET=true hardhat node --no-reset",
"fork-run": "hardhat run --network localhost"
Expand Down
2 changes: 2 additions & 0 deletions test/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ describe("SushiYieldSource integration", function () {
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2",
{ gasLimit: 9500000 }
);
await yieldSource.intialize()

const yieldSourcePrizePoolConfig = {
yieldSource: yieldSource.address,
maxExitFeeMantissa: toWei("0.5"),
Expand Down
1 change: 1 addition & 0 deletions test/unit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe("SushiYieldSource", function () {
overrides
);
amount = toWei("100");
await yieldSource.intialize()
await sushi.mint(wallet.address, amount);
await sushi.mint(wallet2.address, amount.mul(99));
await sushi.connect(wallet2).approve(sushiBar.address, amount.mul(99));
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 @@ -8487,6 +8504,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 @@ -8512,7 +8551,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

0 comments on commit 54732f3

Please sign in to comment.