Skip to content

Commit

Permalink
fix: deposit everything to AffiliateToken (yearn#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazaletskiy authored and sambacha committed Sep 7, 2021
1 parent a0a7729 commit e0f1235
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion contracts/BaseWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ abstract contract BaseWrapper {
VaultAPI _bestVault = bestVault();

if (pullFunds) {
token.safeTransferFrom(depositor, address(this), amount);
if (amount != DEPOSIT_EVERYTHING) {
token.safeTransferFrom(depositor, address(this), amount);
} else {
token.safeTransferFrom(depositor, address(this), token.balanceOf(depositor));
}
}

if (token.allowance(address(this), address(_bestVault)) < amount) {
Expand Down
7 changes: 4 additions & 3 deletions contracts/test/AffiliateToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ contract AffiliateToken is ERC20, BaseWrapper {
}

function pricePerShare() external view returns (uint256) {
return 10**uint256(decimals());
return totalVaultBalance(address(this)).mul(10**uint256(decimals())).div(totalSupply());
}

function _sharesForValue(uint256 amount) internal view returns (uint256) {
uint256 totalWrapperAssets = totalVaultBalance(address(this));
// total wrapper assets before deposit (assumes deposit already occured)
uint256 totalWrapperAssets = totalVaultBalance(address(this)).sub(amount);

if (totalWrapperAssets > 0) {
return totalSupply().mul(amount).div(totalWrapperAssets);
Expand All @@ -82,8 +83,8 @@ contract AffiliateToken is ERC20, BaseWrapper {
}

function deposit(uint256 amount) public returns (uint256 deposited) {
uint256 shares = _sharesForValue(amount); // NOTE: Must be calculated before deposit is handled
deposited = _deposit(msg.sender, address(this), amount, true); // `true` = pull from `msg.sender`
uint256 shares = _sharesForValue(deposited); // NOTE: Must be calculated after deposit is handled
_mint(msg.sender, shares);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/functional/wrappers/test_affliate.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def test_deposit(token, registry, vault, affiliate_token, gov, rando):
assert vault.balanceOf(rando) == 0


def test_deposit_max_uint256(token, registry, vault, affiliate_token, gov, rando):
registry.newRelease(vault, {"from": gov})
registry.endorseVault(vault, {"from": gov})
token.transfer(rando, 10000, {"from": gov})
assert affiliate_token.balanceOf(rando) == vault.balanceOf(rando) == 0

# NOTE: Must approve affiliate_token to deposit
token.approve(affiliate_token, 2 ** 256 - 1, {"from": rando})

affiliate_token.deposit({"from": rando})
assert affiliate_token.balanceOf(rando) == 10000
assert vault.balanceOf(rando) == 0


def test_migrate(token, registry, create_vault, affiliate_token, gov, rando, affiliate):
vault1 = create_vault(version="1.0.0", token=token)
registry.newRelease(vault1, {"from": gov})
Expand Down

0 comments on commit e0f1235

Please sign in to comment.