Skip to content

Commit

Permalink
Merge pull request #3 from pooltogether/fix/46
Browse files Browse the repository at this point in the history
reduce SLOAD calls in _withdrawFromVault
  • Loading branch information
PierrickGT committed Jul 7, 2021
2 parents bff8877 + 9da582b commit 48e224a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ artifacts
cache
node_modules
types
deployments/localhost/

# Solidity Coverage
coverage
Expand Down
29 changes: 15 additions & 14 deletions contracts/yield-source/YearnV2YieldSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol
contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeable, ReentrancyGuardUpgradeable {
using SafeERC20Upgradeable for IERC20Upgradeable;
using SafeMathUpgradeable for uint;

/// @notice Yearn Vault which manages `token` to generate yield
IYVaultV2 public vault;
/// @dev Deposit Token contract address
IERC20Upgradeable internal token;
IERC20Upgradeable internal token;
/// @dev Max % of losses that the Yield Source will accept from the Vault in BPS
uint256 public maxLosses = 0; // 100% would be 10_000

Expand All @@ -33,7 +33,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
address indexed user,
uint256 amount
);

/// @notice Emitted when the yield source is initialized
event YieldSourceYearnV2Initialized(
IYVaultV2 vault,
Expand All @@ -60,14 +60,14 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
uint256 amount
);

/// @notice Initializes the yield source with
/// @notice Initializes the yield source with
/// @param _vault YearnV2 Vault in which the Yield Source will deposit `token` to generate Yield
/// @param _token Underlying Token / Deposit Token
function initialize(
IYVaultV2 _vault,
IERC20Upgradeable _token
)
public
)
public
initializer
{
require(address(vault) == address(0), "YearnV2YieldSource:: already initialized");
Expand Down Expand Up @@ -168,7 +168,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
/// @return The actual amount of shares that were received for the deposited tokens
function _depositInVault() internal returns (uint256) {
IYVaultV2 v = vault; // NOTE: for gas usage
if(token.allowance(address(this), address(v)) < token.balanceOf(address(this))) {
if (token.allowance(address(this), address(v)) < token.balanceOf(address(this))) {
token.safeApprove(address(v), type(uint256).max);
}
// this will deposit full balance (for cases like not enough room in Vault)
Expand All @@ -184,8 +184,9 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
uint256 yShares = _tokenToYShares(amount);
uint256 previousBalance = token.balanceOf(address(this));
// we accept losses to avoid being locked in the Vault (if losses happened for some reason)
if(maxLosses != 0) {
vault.withdraw(yShares, address(this), maxLosses);
uint256 _maxLosses = maxLosses;
if (_maxLosses != 0) {
vault.withdraw(yShares, address(this), _maxLosses);
} else {
vault.withdraw(yShares);
}
Expand Down Expand Up @@ -222,7 +223,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
}

/// @notice Support function to retrieve used by Vault
/// @dev used to correctly scale prices
/// @dev used to correctly scale prices
/// @return decimals of vault's shares (and underlying token)
function _vaultDecimals() internal view returns (uint256) {
return vault.decimals();
Expand All @@ -237,7 +238,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
return tokens.mul(10 ** _vaultDecimals()).div(_pricePerYShare());
}

/// @notice Converter from deposit yShares (yearn vault's shares) to token
/// @notice Converter from deposit yShares (yearn vault's shares) to token
/// @param yShares Vault's shares to be converted
/// @return tokens that will be received if yShares shares are redeemed
function _ySharesToToken(uint256 yShares) internal view returns (uint256) {
Expand All @@ -248,7 +249,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
/// @param tokens amount of tokens to be converted
/// @return shares number of shares equivalent to the amount of tokens
function _tokenToShares(uint256 tokens) internal view returns (uint256 shares) {
if(totalSupply() == 0) {
if (totalSupply() == 0) {
shares = tokens;
} else {
uint256 _totalTokens = _totalAssetsInToken();
Expand All @@ -261,7 +262,7 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
/// @dev used to calculate how many shares to mint / burn when depositing / withdrawing
/// @return tokens number of tokens equivalent (in value) to the amount of Yield Source shares
function _sharesToToken(uint256 shares) internal view returns (uint256 tokens) {
if(totalSupply() == 0) {
if (totalSupply() == 0) {
tokens = shares;
} else {
uint256 _totalTokens = _totalAssetsInToken();
Expand All @@ -276,4 +277,4 @@ contract YearnV2YieldSource is IYieldSource, ERC20Upgradeable, OwnableUpgradeabl
function areEqualStrings(string memory a, string memory b) internal pure returns (bool) {
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"hardhat-deploy-ethers": "0.3.0-beta.7",
"hardhat-gas-reporter": "1.0.4",
"prettier": "2.2.1",
"solhint": "3.3.3",
"solhint": "3.3.6",
"solidity-coverage": "0.7.16",
"ts-generator": "0.1.1",
"ts-node": "9.1.1",
Expand Down
32 changes: 25 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ __metadata:
hardhat-deploy-ethers: 0.3.0-beta.7
hardhat-gas-reporter: 1.0.4
prettier: 2.2.1
solhint: 3.3.3
solhint: 3.3.6
solidity-coverage: 0.7.16
ts-generator: 0.1.1
ts-node: 9.1.1
Expand Down Expand Up @@ -1066,6 +1066,15 @@ __metadata:
languageName: node
linkType: hard

"@solidity-parser/parser@npm:^0.13.2":
version: 0.13.2
resolution: "@solidity-parser/parser@npm:0.13.2"
dependencies:
antlr4ts: ^0.5.0-alpha.4
checksum: ad2f8ca981bf7564711e5d9e709c79d3a44116419b21391692b356387c9528fc8056178378a5118d08389d2c73b93ca30cca9df2bd06de17bb0092b433077b33
languageName: node
linkType: hard

"@solidity-parser/parser@npm:^0.5.2":
version: 0.5.2
resolution: "@solidity-parser/parser@npm:0.5.2"
Expand Down Expand Up @@ -1645,6 +1654,15 @@ __metadata:
languageName: node
linkType: hard

"antlr4ts@npm:^0.5.0-alpha.4":
version: 0.5.0-dev
resolution: "antlr4ts@npm:0.5.0-dev"
dependencies:
source-map-support: ^0.5.16
checksum: 0f206ef5ef5793456de66545a1bf6d8d52343bc6c6689d941cc16ea3f0b6b18a7710b4cc3946a61d021fda51f93b89adcd7c5d7fe249ea1739b0459e7a85b42c
languageName: node
linkType: hard

"anymatch@npm:~3.1.1":
version: 3.1.1
resolution: "anymatch@npm:3.1.1"
Expand Down Expand Up @@ -10939,11 +10957,11 @@ resolve@1.1.x:
languageName: node
linkType: hard

"solhint@npm:3.3.3":
version: 3.3.3
resolution: "solhint@npm:3.3.3"
"solhint@npm:3.3.6":
version: 3.3.6
resolution: "solhint@npm:3.3.6"
dependencies:
"@solidity-parser/parser": ^0.12.0
"@solidity-parser/parser": ^0.13.2
ajv: ^6.6.1
antlr4: 4.7.1
ast-parents: 0.0.1
Expand All @@ -10963,7 +10981,7 @@ resolve@1.1.x:
optional: true
bin:
solhint: solhint.js
checksum: 671c02462d97865b19d3058ceb6159c4afa86795390c08280b4a137c6022a434911cb2418befcfaf5262585e9ba13eb7b3010b6f06eef246e3bf1501dac7c414
checksum: 3e8320a03932913fb45de1964563f1b8f4f92e3e8cc086d21dd0763fb17a035a1ae1665983e15c1fe57517e9b3b0820c5ec4210288fa7240b06806918d831e55
languageName: node
linkType: hard

Expand Down Expand Up @@ -11082,7 +11100,7 @@ resolve@1.1.x:
languageName: node
linkType: hard

"source-map-support@npm:^0.5.13, source-map-support@npm:^0.5.17":
"source-map-support@npm:^0.5.13, source-map-support@npm:^0.5.16, source-map-support@npm:^0.5.17":
version: 0.5.19
resolution: "source-map-support@npm:0.5.19"
dependencies:
Expand Down

0 comments on commit 48e224a

Please sign in to comment.