Skip to content

Commit

Permalink
Tweaks to ensure smooth migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alsco77 committed Mar 19, 2021
1 parent 36b601c commit e76178f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
15 changes: 13 additions & 2 deletions contracts/masset/peripheral/AaveV2Integration.sol
Expand Up @@ -26,6 +26,7 @@ contract AaveV2Integration is AbstractIntegration {

// Core address for the given platform */
address public immutable platformAddress;
address public immutable basketManager;

event RewardTokenApproved(address rewardToken, address account);

Expand All @@ -37,10 +38,12 @@ contract AaveV2Integration is AbstractIntegration {
constructor(
address _nexus,
address _mAsset,
address _platformAddress
address _platformAddress,
address _basketManager
) AbstractIntegration(_nexus, _mAsset) {
require(_platformAddress != address(0), "Invalid platform address");
platformAddress = _platformAddress;
basketManager = _basketManager;
}

/***************************************
Expand Down Expand Up @@ -71,6 +74,14 @@ contract AaveV2Integration is AbstractIntegration {
CORE
****************************************/

/**
* @dev Modifier to allow function calls only from the Governor.
*/
modifier massetOrManager() {
require(msg.sender == mAssetAddress || msg.sender == basketManager, "Only mAsset or basketManager can execute");
_;
}

/**
* @dev Deposit a quantity of bAsset into the platform. Credited aTokens
* remain here in the vault. Can only be called by whitelisted addresses
Expand All @@ -87,7 +98,7 @@ contract AaveV2Integration is AbstractIntegration {
)
external
override
onlyMasset
massetOrManager
nonReentrant
returns (uint256 quantityDeposited)
{
Expand Down
9 changes: 8 additions & 1 deletion tasks/deployAaveIntegration.ts
Expand Up @@ -8,6 +8,7 @@ import { AaveV2Integration__factory } from "types/generated"
interface CommonAddresses {
nexus: string
mAsset: string
basketManager: string
aave: string
}

Expand All @@ -19,12 +20,18 @@ task("deployAaveIntegration", "Deploys an instance of AaveV2Integration contract

const addresses: CommonAddresses = {
mAsset: "0xe2f2a5C287993345a840Db3B0845fbC70f5935a5",
basketManager: "0x66126B4aA2a1C07536Ef8E5e8bD4EfDA1FdEA96D",
nexus: "0xAFcE80b19A8cE13DEc0739a1aaB7A028d6845Eb3",
aave: "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5",
}

// Deploy
const impl = await new AaveV2Integration__factory(deployer).deploy(addresses.nexus, addresses.mAsset, addresses.aave)
const impl = await new AaveV2Integration__factory(deployer).deploy(
addresses.nexus,
addresses.mAsset,
addresses.aave,
addresses.basketManager,
)
const reciept = await impl.deployTransaction.wait()
console.log(`Deployed Integration to ${impl.address}. gas used ${reciept.gasUsed}`)

Expand Down
53 changes: 39 additions & 14 deletions test-fork/mUSD/mUSD-migrate.spec.ts
Expand Up @@ -9,7 +9,16 @@ import { ethers, network } from "hardhat"

import { ONE_WEEK, ONE_DAY, ZERO_ADDRESS, DEAD_ADDRESS } from "@utils/constants"
import { applyDecimals, applyRatio, applyRatioMassetToBasset, BN, simpleToExactAmount } from "@utils/math"
import { DelayedProxyAdmin, DelayedProxyAdmin__factory, ERC20__factory, Masset, MusdV3, MusdV3__factory } from "types/generated"
import {
DelayedProxyAdmin,
DelayedProxyAdmin__factory,
ERC20__factory,
Masset,
MusdV3,
MusdV3__factory,
AaveV2Integration__factory,
AaveV2Integration,
} from "types/generated"
import { MusdV3LibraryAddresses } from "types/generated/factories/MusdV3__factory"
import { increaseTime } from "@utils/time"
import { BassetStatus } from "@utils/mstable-objects"
Expand All @@ -35,6 +44,9 @@ const nexusAddress = "0xAFcE80b19A8cE13DEc0739a1aaB7A028d6845Eb3"
const delayedProxyAdminAddress = "0x5C8eb57b44C1c6391fC7a8A0cf44d26896f92386"
const governorAddress = "0xF6FF1F7FCEB2cE6d26687EaaB5988b445d0b94a2"

// Aave V2
const aaveAddress = "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5"

const forkBlockNumber = 12043106

const defaultConfig = {
Expand Down Expand Up @@ -247,18 +259,23 @@ const validateBasset = (bAssets, i: number, expectToken: Token, expectVaultBalan
}

// Test the new Masset V3 storage variables
const validateNewMassetStorage = async (mUsd: MusdV3 | Masset, validator: string, expectVaultBalances?: BN[]) => {
const validateNewMassetStorage = async (mUsd: MusdV3 | Masset, validator: string, newPlatform: string, expectVaultBalances?: BN[]) => {
expect(await mUsd.forgeValidator(), "forge validator").to.eq(validator)
expect(await mUsd.maxBassets(), "maxBassets").to.eq(10)

// bAsset personal data
const bAssets = await mUsd.getBassets()
finalBassets.forEach(async (token, i) => {
validateBasset(bAssets, i, token, expectVaultBalances)
expect(await mUsd.bAssetIndexes(token.address)).eq(i)
const bAsset = await mUsd.getBasset(token.address)
expect(bAsset[0][0]).eq(token.address)
})
await Promise.all(
finalBassets.map(async (token, i) => {
if (token.symbol === "sUSD") {
token.integrator = newPlatform
}
validateBasset(bAssets, i, token, expectVaultBalances)
expect(await mUsd.bAssetIndexes(token.address)).eq(i)
const bAsset = await mUsd.getBasset(token.address)
expect(bAsset[0][0]).eq(token.address)
}),
)

// Get basket state
const basketState = await mUsd.basket()
Expand Down Expand Up @@ -361,6 +378,7 @@ describe("mUSD V2.0 to V3.0", () => {
let deployer: Signer
let governor: Signer
const balancedVaultBalances: BN[] = []
let aaveV2: AaveV2Integration
before("Set-up globals", async () => {
accounts = await impersonateAccounts()
deployer = accounts.deployer
Expand Down Expand Up @@ -399,7 +417,7 @@ describe("mUSD V2.0 to V3.0", () => {
it("gets deployed mUSD impl", async () => {
mUsdV3 = await getMusdv3(deployer)
})
it("proposes mUSD upgrade to proxyadmin", async () => {
it("checks the proposed upgrade data", async () => {
const data = await mUsdV3.impl.interface.encodeFunctionData("upgrade", [validatorAddress, defaultConfig])

const request = await delayedProxyAdmin.requests(mUsdProxyAddress)
Expand Down Expand Up @@ -437,6 +455,17 @@ describe("mUSD V2.0 to V3.0", () => {
const basketManagerFactory = new ContractFactory(BasketManagerV2Abi, BasketManagerV2Bytecode, governor)
basketManager = basketManagerFactory.attach(basketManagerAddress)
})
it("migrates sUSD to AaveV2", async () => {
aaveV2 = await new AaveV2Integration__factory(deployer).deploy(
nexusAddress,
mUsdProxyAddress,
aaveAddress,
basketManagerAddress,
)
await aaveV2.deployTransaction.wait()
await aaveV2.connect(governor).setPTokenAddress(sUSD.address, "0x6c5024cd4f8a59110119c56f8933403a539555eb")
await basketManager.migrateBassets([sUSD.address], aaveV2.address)
})
it("adds DAI to the basket", async () => {
// 2. Add DAI to basket
await basketManager.addBasset(DAI.address, "0xD55684f4369040C12262949Ff78299f2BC9dB735", false)
Expand Down Expand Up @@ -544,10 +573,6 @@ describe("mUSD V2.0 to V3.0", () => {
before("elapse the time", async () => {
await increaseTime(ONE_DAY.mul(2))
})
// TODO - ensure all libs are tested:
// - migrator
// - manager
// - validator
describe("accept proposal and verify storage", () => {
it("Should upgrade balanced mUSD", async () => {
// Approve and execute call to upgradeToAndCall on mUSD proxy which then calls migrate on the new mUSD V3 implementation
Expand All @@ -556,7 +581,7 @@ describe("mUSD V2.0 to V3.0", () => {
// validate after the upgrade
await validateTokenStorage(mUsdV3.proxy, "45324893805990774527261941")
await validateUnchangedMassetStorage(mUsdV3.proxy, "1")
await validateNewMassetStorage(mUsdV3.proxy, validatorAddress, balancedVaultBalances)
await validateNewMassetStorage(mUsdV3.proxy, validatorAddress, aaveV2.address, balancedVaultBalances)
})
it("blocks mint/swap/redeem", async () => {
// mint/swap = Unhealthy
Expand Down
17 changes: 14 additions & 3 deletions test/masset/peripheral/aavev2.spec.ts
Expand Up @@ -45,6 +45,7 @@ describe("AaveIntegration", async () => {
nexus.address,
mAsset.address,
integrationDetails.aavePlatformAddress,
sa.mockSavingsManager.address,
)
await Promise.all(
integrationDetails.aTokens.map((a) => aaveIntegration.connect(sa.governor.signer).setPTokenAddress(a.bAsset, a.aToken)),
Expand Down Expand Up @@ -98,7 +99,12 @@ describe("AaveIntegration", async () => {

it("should fail when mAsset address invalid", async () => {
await expect(
new AaveV2Integration__factory(sa.default.signer).deploy(nexus.address, ZERO_ADDRESS, sa.other.address),
new AaveV2Integration__factory(sa.default.signer).deploy(
nexus.address,
ZERO_ADDRESS,
sa.other.address,
sa.mockSavingsManager.address,
),
).to.be.revertedWith("Invalid mAsset address")
})

Expand All @@ -115,7 +121,12 @@ describe("AaveIntegration", async () => {

it("should fail if passed incorrect data", async () => {
await expect(
new AaveV2Integration__factory(sa.default.signer).deploy(nexus.address, mAsset.address, ZERO_ADDRESS),
new AaveV2Integration__factory(sa.default.signer).deploy(
nexus.address,
mAsset.address,
ZERO_ADDRESS,
sa.mockSavingsManager.address,
),
).to.be.revertedWith("Invalid platform address")
})
})
Expand Down Expand Up @@ -268,7 +279,7 @@ describe("AaveIntegration", async () => {

// Step 1. call deposit
await expect(aaveIntegration.connect(sa.dummy1.signer).deposit(bAsset.address, amount.toString(), false)).to.be.revertedWith(
"Only the mAsset can execute",
"Only mAsset or basketManager can execute",
)
})
it("should fail if the bAsset is not supported", async () => {
Expand Down

0 comments on commit e76178f

Please sign in to comment.