-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mUSD-migration' into master-v2
- Loading branch information
Showing
18 changed files
with
5,122 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity 0.8.0; | ||
pragma abicoder v2; | ||
|
||
import { MassetStructs } from "../MassetStructs.sol"; | ||
|
||
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol"; | ||
|
||
import { IBasketManager } from "../../z_mocks/masset/migrate3/IBasketManager.sol"; | ||
import { Basket, Basset } from "../../z_mocks/masset/migrate3/MassetStructsV2.sol"; | ||
|
||
|
||
library Migrator { | ||
|
||
function upgrade( | ||
IBasketManager basketManager, | ||
MassetStructs.BassetPersonal[] storage bAssetPersonal, | ||
MassetStructs.BassetData[] storage bAssetData, | ||
mapping(address => uint8) storage bAssetIndexes | ||
) external { | ||
Basket memory importedBasket = basketManager.getBasket(); | ||
|
||
uint256 len = importedBasket.bassets.length; | ||
uint256[] memory scaledVaultBalances = new uint[](len); | ||
uint256 maxScaledVaultBalance; | ||
for (uint8 i = 0; i < len; i++) { | ||
Basset memory bAsset = importedBasket.bassets[i]; | ||
address bAssetAddress = bAsset.addr; | ||
bAssetIndexes[bAssetAddress] = i; | ||
|
||
address integratorAddress = basketManager.getBassetIntegrator(bAssetAddress); | ||
bAssetPersonal.push( | ||
MassetStructs.BassetPersonal({ | ||
addr: bAssetAddress, | ||
integrator: integratorAddress, | ||
hasTxFee: false, | ||
status: MassetStructs.BassetStatus.Normal | ||
}) | ||
); | ||
|
||
uint128 ratio = SafeCast.toUint128(bAsset.ratio); | ||
uint128 vaultBalance = SafeCast.toUint128(bAsset.vaultBalance); | ||
bAssetData.push( | ||
MassetStructs.BassetData({ ratio: ratio, vaultBalance: vaultBalance }) | ||
); | ||
|
||
// caclulate scaled vault bAsset balance and totoal vault balance | ||
uint128 scaledVaultBalance = (vaultBalance * ratio) / 1e8; | ||
scaledVaultBalances[i] = scaledVaultBalance; | ||
maxScaledVaultBalance += scaledVaultBalance; | ||
} | ||
|
||
// Check each bAsset is under 25.01% weight | ||
uint256 maxWeight = 2501; | ||
if(len == 3){ | ||
maxWeight = 3334; | ||
} else if (len != 4){ | ||
revert("Invalid length"); | ||
} | ||
maxScaledVaultBalance = maxScaledVaultBalance * 2501 / 10000; | ||
for (uint8 i = 0; i < len; i++) { | ||
require(scaledVaultBalances[i] < maxScaledVaultBalance, "imbalanced"); | ||
} | ||
} | ||
} |
Oops, something went wrong.