Skip to content

Commit

Permalink
No need for passing a newVault in Migrations (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggviana committed Nov 28, 2022
1 parent 8326197 commit 94d2f63
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 81 deletions.
8 changes: 1 addition & 7 deletions abi/BaseVault.json
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,7 @@
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IVault",
"name": "newVault",
"type": "address"
}
],
"inputs": [],
"name": "migrate",
"outputs": [],
"stateMutability": "nonpayable",
Expand Down
11 changes: 3 additions & 8 deletions abi/ConfigurationManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,14 @@
"internalType": "address",
"name": "oldVault",
"type": "address"
},
{
"internalType": "address",
"name": "newVault",
"type": "address"
}
],
"name": "isVaultMigrationAllowed",
"name": "getVaultMigration",
"outputs": [
{
"internalType": "bool",
"internalType": "address",
"name": "",
"type": "bool"
"type": "address"
}
],
"stateMutability": "view",
Expand Down
11 changes: 3 additions & 8 deletions abi/IConfigurationManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,14 @@
"internalType": "address",
"name": "oldVault",
"type": "address"
},
{
"internalType": "address",
"name": "newVault",
"type": "address"
}
],
"name": "isVaultMigrationAllowed",
"name": "getVaultMigration",
"outputs": [
{
"internalType": "bool",
"internalType": "address",
"name": "",
"type": "bool"
"type": "address"
}
],
"stateMutability": "view",
Expand Down
10 changes: 0 additions & 10 deletions abi/Migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
"name": "from",
"type": "address"
},
{
"internalType": "contract IVault",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "shares",
Expand All @@ -64,11 +59,6 @@
"name": "from",
"type": "address"
},
{
"internalType": "contract IVault",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "shares",
Expand Down
8 changes: 1 addition & 7 deletions abi/PrincipalProtectedMock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,13 +1012,7 @@
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IVault",
"name": "newVault",
"type": "address"
}
],
"inputs": [],
"name": "migrate",
"outputs": [],
"stateMutability": "nonpayable",
Expand Down
8 changes: 1 addition & 7 deletions abi/STETHVault.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,13 +1012,7 @@
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IVault",
"name": "newVault",
"type": "address"
}
],
"inputs": [],
"name": "migrate",
"outputs": [],
"stateMutability": "nonpayable",
Expand Down
4 changes: 2 additions & 2 deletions contracts/configuration/ConfigurationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract ConfigurationManager is IConfigurationManager, Ownable {
/**
* @inheritdoc IConfigurationManager
*/
function isVaultMigrationAllowed(address oldVault, address newVault) external view override returns (bool) {
return _allowedVaults[oldVault] == newVault;
function getVaultMigration(address oldVault) external view override returns (address) {
return _allowedVaults[oldVault];
}
}
5 changes: 2 additions & 3 deletions contracts/interfaces/IConfigurationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ interface IConfigurationManager {
function setVaultMigration(address oldVault, address newVault) external;

/**
* @notice Returns if the migration for a vault is allowed.
* @notice Returns the new Vault address.
* @param oldVault The current vault address
* @param newVault The vault where assets are going to be migrated to
*/
function isVaultMigrationAllowed(address oldVault, address newVault) external view returns (bool);
function getVaultMigration(address oldVault) external view returns (address);
}
5 changes: 3 additions & 2 deletions contracts/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ interface IVault is IERC4626, IERC20Permit {
function refund() external returns (uint256 assets);

/**
* @notice Migrate assets from this vault to `newVault`.
* @notice Migrate assets from this vault to the next vault.
* @dev The `newVault` will be assigned by the ConfigurationManager
*/
function migrate(IVault newVault) external;
function migrate() external;

/**
* @notice Handle migrated assets.
Expand Down
17 changes: 7 additions & 10 deletions contracts/proxy/Migration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ contract Migration {
* It will withdraw from the old vault and it will deposit into the new vault
* @dev The new shares only will be available after the process deposit of the new vault
* @param from origin vault (old Vault) from the liquidity will be migrated
* @param to destination vault (new Vault) to deposit
* @param shares amount of shares to withdraw from the origin vault (from)
* @return uint256 shares' amount returned by the new vault contract
*/
function migrate(
IVault from,
IVault to,
uint256 shares
) external returns (uint256) {
if (!configuration.isVaultMigrationAllowed(address(from), address(to))) {
function migrate(IVault from, uint256 shares) external returns (uint256) {
IVault to = IVault(configuration.getVaultMigration(address(from)));

if (to == IVault(address(0))) {
revert Migration__MigrationNotAllowed();
}

Expand All @@ -55,7 +52,6 @@ contract Migration {
* @notice migrateWithPermit liquidity from an old vault to a new vault
* * It will withdraw from the old vault and it will deposit into the new vault
* @param from origin vault (old Vault) from where the liquidity will be migrated
* @param to destination vault (new Vault) to deposit
* @param shares amount of shares to withdraw from the origin vault (from)
* @param deadline deadline that this transaction will be valid
* @param v recovery id
Expand All @@ -65,14 +61,15 @@ contract Migration {
*/
function migrateWithPermit(
IVault from,
IVault to,
uint256 shares,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint256) {
if (!configuration.isVaultMigrationAllowed(address(from), address(to))) {
IVault to = IVault(configuration.getVaultMigration(address(from)));

if (to == IVault(address(0))) {
revert Migration__MigrationNotAllowed();
}

Expand Down
10 changes: 7 additions & 3 deletions contracts/vaults/BaseVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,10 @@ abstract contract BaseVault is IVault, ERC20Permit, ERC4626, Capped {
/**
* @inheritdoc IVault
*/
function migrate(IVault newVault) external override {
if (!configuration.isVaultMigrationAllowed(address(this), address(newVault))) {
function migrate() external override {
IVault newVault = IVault(configuration.getVaultMigration(address(this)));

if (newVault == IVault(address(0))) {
revert IVault__MigrationNotAllowed();
}

Expand All @@ -361,7 +363,9 @@ abstract contract BaseVault is IVault, ERC20Permit, ERC4626, Capped {
* @inheritdoc IVault
*/
function handleMigration(uint256 assets, address receiver) external override returns (uint256) {
if (!configuration.isVaultMigrationAllowed(msg.sender, address(this))) {
IVault newVault = IVault(configuration.getVaultMigration(msg.sender));

if (address(newVault) != address(this)) {
revert IVault__MigrationNotAllowed();
}

Expand Down
5 changes: 1 addition & 4 deletions test/proxy/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Migration', () => {

// Execute migration
await vaultFrom.connect(user1).approve(migration.address, ethers.constants.MaxUint256)
await migration.connect(user1).migrate(vaultFrom.address, vaultTo.address, await vaultFrom.balanceOf(user1.address))
await migration.connect(user1).migrate(vaultFrom.address, await vaultFrom.balanceOf(user1.address))

expect(await vaultFrom.totalAssets()).to.be.closeTo(vaultFromTotalAssets.sub(user1Withdrawable), 1)
expect(await asset.balanceOf(migration.address)).to.be.closeTo(BigNumber.from('0'), 1)
Expand Down Expand Up @@ -160,7 +160,6 @@ describe('Migration', () => {
)
await migration.connect(userPermit).migrateWithPermit(
vaultFrom.address,
vaultTo.address,
shares,
permit.deadline,
permit.v,
Expand Down Expand Up @@ -204,7 +203,6 @@ describe('Migration', () => {
)
migrationTx = migration.connect(userPermit).migrateWithPermit(
vaultFrom.address,
vaultTo.address,
shares,
permit.deadline,
permit.v,
Expand All @@ -217,7 +215,6 @@ describe('Migration', () => {
await vaultFrom.connect(user1).approve(migration.address, ethers.constants.MaxUint256)
migrationTx = migration.connect(user1).migrate(
vaultFrom.address,
vaultTo.address,
await vaultFrom.balanceOf(user1.address)
)

Expand Down
13 changes: 3 additions & 10 deletions test/vaults/BaseVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ describe('BaseVault', () => {
expect(await vault.assetsOf(user0.address)).to.be.equal(assets)
expect(await newVault.idleAssetsOf(user0.address)).to.be.equal(0)

const migrationTx = vault.connect(user0).migrate(newVault.address)
const migrationTx = vault.connect(user0).migrate()
await expect(migrationTx)
.to.emit(vault, 'Migrated')
.withArgs(user0.address, vault.address, newVault.address, feeExcluded(assets), shares)
Expand All @@ -833,7 +833,7 @@ describe('BaseVault', () => {
expect(await newVault.idleAssetsOf(user0.address)).to.be.equal(feeExcluded(assets))
})

it('should not migrate to disallowed vaults', async () => {
it('should not migrate if a vault was not assigned', async () => {
const assets = ethers.utils.parseEther('100')

await asset.connect(user0).mint(assets)
Expand All @@ -842,14 +842,7 @@ describe('BaseVault', () => {
await vault.connect(vaultController).processQueuedDeposits([user0.address])
await vault.connect(vaultController).startRound()

const Vault = await ethers.getContractFactory('YieldVaultMock')
const newVault = await Vault.deploy(
configuration.address,
asset.address,
yieldSource.address
)

const migrationTx = vault.connect(user0).migrate(newVault.address)
const migrationTx = vault.connect(user0).migrate()
await expect(migrationTx)
.to.be.revertedWithCustomError(vault, 'IVault__MigrationNotAllowed')
})
Expand Down

0 comments on commit 94d2f63

Please sign in to comment.