diff --git a/contracts/DiamondInit.sol b/contracts/DiamondInit.sol index 30136704..dcff16e2 100644 --- a/contracts/DiamondInit.sol +++ b/contracts/DiamondInit.sol @@ -9,7 +9,6 @@ import {IDiamondCut} from "./interfaces/IDiamondCut.sol"; import {IDiamondLoupe} from "./interfaces/IDiamondLoupe.sol"; import {LibDiamond} from "./libs/LibDiamond.sol"; import "./libs/Details.sol"; -import "hardhat/console.sol"; contract DiamondInit { struct Args { @@ -32,11 +31,6 @@ contract DiamondInit { s.foundry = _args.foundry; s.vaultRegistry = _args.vaultRegistry; s.curveRegistry = _args.curveRegistry; - console.log( - "## DiamondInit s.curveRegistry:%s vaultRegistry:%s", - address(s.curveRegistry), - address(s.vaultRegistry) - ); s.migrationRegistry = _args.migrationRegistry; // s.mintFee = _args.mintFee; // s.burnBuyerFee = _args.burnBuyerFee; diff --git a/contracts/curves/BancorPower.sol b/contracts/curves/BancorPower.sol index 0791c6f6..5c34b74e 100644 --- a/contracts/curves/BancorPower.sol +++ b/contracts/curves/BancorPower.sol @@ -29,14 +29,12 @@ contract BancorPower is Power, ICurve { bytes16 private immutable _maxWeight = uint256(MAX_WEIGHT).fromUInt(); // gas savings bytes16 private immutable _one = (uint256(1)).fromUInt(); address public hub; - address public foundry; // NOTE: keys are the respective hubId mapping(uint256 => Bancor) private _bancors; - constructor(address _hub, address _foundry) { + constructor(address _hub) { hub = _hub; - foundry = _foundry; } function register(uint256 _hubId, bytes calldata _encodedDetails) diff --git a/contracts/curves/StepwiseCurve.sol b/contracts/curves/StepwiseCurve.sol index 59333cdf..bb5b2b2c 100644 --- a/contracts/curves/StepwiseCurve.sol +++ b/contracts/curves/StepwiseCurve.sol @@ -21,14 +21,11 @@ contract StepwiseCurve is ICurve { uint256 public constant PRECISION = 10**18; address public hub; - address public foundry; - // NOTE: keys are their respective hubId mapping(uint256 => Stepwise) private _stepwises; - constructor(address _hub, address _foundry) { + constructor(address _hub) { hub = _hub; - foundry = _foundry; } function register(uint256 _hubId, bytes calldata _encodedDetails) diff --git a/contracts/curves/StepwiseCurveABDK.sol b/contracts/curves/StepwiseCurveABDK.sol index e4f325d1..535d094e 100644 --- a/contracts/curves/StepwiseCurveABDK.sol +++ b/contracts/curves/StepwiseCurveABDK.sol @@ -26,9 +26,8 @@ contract StepwiseCurve is ICurve { // NOTE: keys are their respective hubId mapping(uint256 => Stepwise) private _stepwises; - constructor(address _hub, address _foundry) { + constructor(address _hub) { hub = _hub; - foundry = _foundry; } function register(uint256 _hubId, bytes calldata _encodedDetails) diff --git a/contracts/facets/HubFacet.sol b/contracts/facets/HubFacet.sol index f4aa2d95..aa710d64 100644 --- a/contracts/facets/HubFacet.sol +++ b/contracts/facets/HubFacet.sol @@ -8,7 +8,6 @@ import "../interfaces/IVault.sol"; import "../interfaces/IRegistry.sol"; import "../interfaces/ICurve.sol"; import "../interfaces/IFoundry.sol"; -import "hardhat/console.sol"; contract HubFacet { event Register( @@ -52,11 +51,6 @@ contract HubFacet { bytes memory _encodedVaultArgs ) external { // TODO: access control - console.log( - "## s.curveRegistry:%s vaultRegistry:%s", - address(s.curveRegistry), - address(s.vaultRegistry) - ); require( s.curveRegistry.isApproved(address(_curve)), "_curve !approved" @@ -114,6 +108,7 @@ contract HubFacet { this.finishUpdate(_id); } require(!hub_.updating, "already updating"); + require(block.timestamp >= hub_.endCooldown, "Still cooling down"); // Make sure at least one of the values is different require( @@ -132,6 +127,7 @@ contract HubFacet { ); hub_.targetRefundRatio = _targetRefundRatio; } + bool reconfigure; if (_encodedCurveDetails.length > 0) { if (_targetCurve == address(0)) { diff --git a/test/contracts/curves/BancorBancorCurve.ts b/test/contracts/curves/BancorBancorCurve.ts index 17e2a27d..f9ed72a4 100644 --- a/test/contracts/curves/BancorBancorCurve.ts +++ b/test/contracts/curves/BancorBancorCurve.ts @@ -56,17 +56,14 @@ const setup = async () => { bancorPower = await deploy( "BancorPower", undefined, - hub.address, - foundry.address + hub.address ); ({ token } = await hubSetup( encodedCurveDetails, encodedVaultArgs, 5000, - hub, - foundry, - bancorPower + "BancorPower" )); dai = token; }); @@ -383,8 +380,7 @@ const setup = async () => { newbancorPower = await deploy( "BancorPower", undefined, - hub.address, - foundry.address + hub.address ); ({ token } = await hubSetup( diff --git a/test/contracts/curves/allCurves.ts b/test/contracts/curves/allCurves.ts index 09dfd8b4..a20be824 100644 --- a/test/contracts/curves/allCurves.ts +++ b/test/contracts/curves/allCurves.ts @@ -21,6 +21,7 @@ import { BancorABDK } from "../../../artifacts/types/BancorABDK"; import { curvesTestsHelper } from "./helper/curvesTestsHelper"; import { BancorPower } from "../../../artifacts/types/BancorPower"; import { ICurve } from "../../../artifacts/types/ICurve"; +import { Diamond } from "../../../artifacts/types"; describe("All curves", () => { before("setup curves instance", async () => {}); @@ -34,6 +35,7 @@ const setup = async () => { let migrationRegistry: MigrationRegistry; let foundry: Foundry; let hub: HubFacet; + let diamond: Diamond; let dai: ERC20; let account0: SignerWithAddress; let account1: SignerWithAddress; @@ -50,32 +52,6 @@ const setup = async () => { [DAI] ); - const weightedAverage = await deploy("WeightedAverage"); - foundry = await deploy("Foundry", { - WeightedAverage: weightedAverage.address, - }); - hub = await deploy("HubFacet"); - const bancorABDK = await deploy( - "BancorABDK", - undefined, - hub.address, - foundry.address - ); - - const newBancorABDK = await deploy( - "BancorABDK", - undefined, - hub.address, - foundry.address - ); - - const bancorPower = await deploy( - "BancorPower", - undefined, - hub.address, - foundry.address - ); - // Setting up curve info to test let baseY1 = one.mul(1000); @@ -125,12 +101,16 @@ const setup = async () => { ["uint256", "uint32"], [baseY6, reserveWeight6] ); - - // Create hub and register first hub + // Create and register first hub we also link the hubCurve of type "bancorABDK" to this hub (hubID = 1) + let hubCurve: ICurve; ({ token, + hubCurve, curveRegistry, tokenAddr, + hub, + foundry, + diamond, migrationRegistry, vaultRegistry, account0, @@ -141,44 +121,48 @@ const setup = async () => { encodedCurveDetails1, encodedVaultArgs, 5000, - hub, - foundry, - bancorABDK + "bancorABDK" )); - - let hubArgs: [ + let addArgs: [ + string, HubFacet, + Diamond, Foundry, + string, MeTokenRegistry, CurveRegistry, - string, MigrationRegistry, VaultRegistry, string, string, number, - ICurve, - string + string, + ICurve | undefined ] = [ + tokenAddr, hub, + diamond, foundry, + "bancorABDK", meTokenRegistry, curveRegistry, - tokenAddr, migrationRegistry, vaultRegistry, encodedCurveDetails1, encodedVaultArgs, 5000, - bancorABDK, account0.address, + undefined, ]; - let hubDetails = await addHubSetup(...hubArgs); - await curveRegistry.approve(newBancorABDK.address); + + // we create a new curve of type "bancorABDK" and register it to a new hub (hubID = 2) + // along with encoded details for the curve and the vault + let hubDetails = await addHubSetup(...addArgs); + let curve = { signers: [account0, account1, account2], - curve: bancorABDK, - newCurve: newBancorABDK, + curve: hubDetails.hubCurve, + newCurve: hubCurve, baseY: toETHNumber(baseY1), reserveWeight: reserveWeight1, MAX_WEIGHT: MAX_WEIGHT, @@ -194,8 +178,11 @@ const setup = async () => { curves.push(curve); // Second ABDK Curve - hubArgs[7] = encodedCurveDetails2; - hubDetails = await addHubSetup(...hubArgs); + + addArgs[13] = hubDetails.hubCurve; + addArgs[9] = encodedCurveDetails2; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -205,8 +192,9 @@ const setup = async () => { }); // Third ABDK curve - hubArgs[7] = encodedCurveDetails3; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails3; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -216,8 +204,9 @@ const setup = async () => { }); // Fourth ABDK curve - hubArgs[7] = encodedCurveDetails4; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails4; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -227,8 +216,9 @@ const setup = async () => { }); // fifth ABDK curve - hubArgs[7] = encodedCurveDetails5; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails5; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -238,8 +228,9 @@ const setup = async () => { }); // sixth ABDK curve - hubArgs[7] = encodedCurveDetails6; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails6; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -249,11 +240,15 @@ const setup = async () => { }); // Bancor Power - hubArgs[-2] = bancorPower; + addArgs[4] = "BancorPower"; // First Power curve - hubArgs[7] = encodedCurveDetails1; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails1; + // we create a new curve of type "BancorPower" and register it to the hub + // along with encoded details for this curve + hubDetails = await addHubSetup(...addArgs); + // we set this new curve as the default curve + curve = { ...curve, curve: hubDetails.hubCurve }; curves.push({ ...curve, hubId: hubDetails.hubId, @@ -263,8 +258,10 @@ const setup = async () => { }); // Second Power curve - hubArgs[7] = encodedCurveDetails2; - hubDetails = await addHubSetup(...hubArgs); + addArgs[13] = curve.curve; + addArgs[9] = encodedCurveDetails2; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -274,8 +271,9 @@ const setup = async () => { }); // third power curve - hubArgs[7] = encodedCurveDetails3; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails3; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -285,8 +283,9 @@ const setup = async () => { }); // fourth power curve - hubArgs[7] = encodedCurveDetails4; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails4; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -296,8 +295,9 @@ const setup = async () => { }); // fifth power curve - hubArgs[7] = encodedCurveDetails5; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails5; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, @@ -307,8 +307,9 @@ const setup = async () => { }); // sixth power curve - hubArgs[7] = encodedCurveDetails6; - hubDetails = await addHubSetup(...hubArgs); + addArgs[9] = encodedCurveDetails6; + // we register a new hub with the same curve deployed before but with new encoded curve details + hubDetails = await addHubSetup(...addArgs); curves.push({ ...curve, hubId: hubDetails.hubId, diff --git a/test/utils/hubSetup.ts b/test/utils/hubSetup.ts index 37b7d0ba..8d044aa7 100644 --- a/test/utils/hubSetup.ts +++ b/test/utils/hubSetup.ts @@ -24,7 +24,9 @@ import { Signer } from "ethers"; import { ICurve } from "../../artifacts/types/ICurve"; import { Fees } from "../../artifacts/types/Fees"; import { expect } from "chai"; + import { text } from "stream/consumers"; +import { BancorPower, StepwiseCurve } from "../../artifacts/types"; export async function hubSetup( encodedCurveDetails: string, @@ -41,9 +43,10 @@ export async function hubSetup( tokenAddr: string; foundry: Foundry; hub: HubFacet; + diamond: Diamond; meTokenFactory: MeTokenFactory; singleAssetVault: SingleAssetVault; - curve: BancorABDK; + hubCurve: ICurve; meTokenRegistry: MeTokenRegistry; curveRegistry: CurveRegistry; vaultRegistry: VaultRegistry; @@ -61,9 +64,10 @@ export async function hubSetup( tokenAddr, foundry, hub, + diamond, meTokenFactory, singleAssetVault, - curve, + hubCurve, meTokenRegistry, curveRegistry, vaultRegistry, @@ -84,12 +88,11 @@ export async function hubSetup( erc20Address, erc20Whale ); - await hub.register( account0.address, tokenAddr, singleAssetVault.address, - curve.address, + hubCurve.address, refundRatio, //refund ratio encodedCurveDetails, encodedVaultArgs @@ -98,9 +101,10 @@ export async function hubSetup( tokenAddr, foundry, hub, + diamond, meTokenFactory, singleAssetVault, - curve, + hubCurve, meTokenRegistry, curveRegistry, vaultRegistry, @@ -115,7 +119,34 @@ export async function hubSetup( tokenWhale, }; } - +async function getCurve(curveType: string, diamond: string): Promise { + switch (curveType) { + case "BancorABDK": + return (await deploy( + "BancorABDK", + undefined, + diamond + )) as unknown as ICurve; + case "BancorPower": + return (await deploy( + "BancorPower", + undefined, + diamond + )) as unknown as ICurve; + case "StepwiseCurve": + return (await deploy( + "StepwiseCurve", + undefined, + diamond + )) as unknown as ICurve; + default: + return (await deploy( + "BancorABDK", + undefined, + diamond + )) as unknown as ICurve; + } +} export async function hubSetupWithoutRegister( // hub: HubFacet, // foundry: Foundry, @@ -127,9 +158,10 @@ export async function hubSetupWithoutRegister( tokenAddr: string; foundry: Foundry; hub: HubFacet; + diamond: Diamond; meTokenFactory: MeTokenFactory; singleAssetVault: SingleAssetVault; - curve: BancorABDK; + hubCurve: ICurve; meTokenRegistry: MeTokenRegistry; vaultRegistry: VaultRegistry; curveRegistry: CurveRegistry; @@ -148,7 +180,7 @@ export async function hubSetupWithoutRegister( let hub: HubFacet; let meTokenFactory: MeTokenFactory; let singleAssetVault: SingleAssetVault; - let curve: BancorABDK; + let hubCurve: ICurve; let meTokenRegistry: MeTokenRegistry; let vaultRegistry: VaultRegistry; let curveRegistry: CurveRegistry; @@ -175,11 +207,10 @@ export async function hubSetupWithoutRegister( [account0, account1, account2, account3] = await ethers.getSigners(); token = await getContractAt("ERC20", tokenAddr); tokenHolder = await impersonate(tokenWhale); - - token + const yolo = await token.connect(tokenHolder).balanceOf(account1.address); + await token .connect(tokenHolder) .transfer(account1.address, ethers.utils.parseEther("1000")); - curveRegistry = await deploy("CurveRegistry"); vaultRegistry = await deploy("VaultRegistry"); migrationRegistry = await deploy("MigrationRegistry"); @@ -187,15 +218,13 @@ export async function hubSetupWithoutRegister( foundry = await deploy("Foundry", { WeightedAverage: weightedAverage.address, }); - meTokenFactory = await deploy("MeTokenFactory"); - fee = await deploy("Fees"); let feeInitialization = fees; if (!feeInitialization) { feeInitialization = [0, 0, 0, 0, 0, 0]; } - await fee.initialize( + const txa = await fee.initialize( feeInitialization[0], feeInitialization[1], feeInitialization[2], @@ -203,7 +232,6 @@ export async function hubSetupWithoutRegister( feeInitialization[4], feeInitialization[5] ); - // // NOTE: start diamond deploy // @@ -215,7 +243,6 @@ export async function hubSetupWithoutRegister( account0.address, diamondCutFacet.address ); - // Deploy contracts depending on hubFacet address, // which is actually the address of the diamond meTokenRegistry = await deploy( @@ -236,8 +263,12 @@ export async function hubSetupWithoutRegister( migrationRegistry.address //IMigrationRegistry ); - curve = await deploy("BancorABDK", undefined, diamond.address); - foundry.initialize(diamond.address, fee.address, meTokenRegistry.address); + hubCurve = await getCurve(curveStr, diamond.address); + await foundry.initialize( + diamond.address, + fee.address, + meTokenRegistry.address + ); // Deploying facets const hubFacet = await deploy("HubFacet"); @@ -274,24 +305,22 @@ export async function hubSetupWithoutRegister( diamondInit.address, functionCall ); - await tx.wait(); - + const receipt = await tx.wait(); hub = (await ethers.getContractAt("HubFacet", diamond.address)) as HubFacet; // // NOTE: end diamond deploy // - - await curveRegistry.approve(curve.address); + await curveRegistry.approve(hubCurve.address); await vaultRegistry.approve(singleAssetVault.address); - return { tokenAddr, foundry, hub, + diamond, meTokenFactory, singleAssetVault, - curve, + hubCurve, meTokenRegistry, vaultRegistry, curveRegistry, @@ -306,12 +335,12 @@ export async function hubSetupWithoutRegister( tokenWhale, }; } - export async function addHubSetup( tokenAddr: string, hub: HubFacet, + diamond: Diamond, foundry: Foundry, - curve: ICurve, + curveType: string, meTokenRegistry: MeTokenRegistry, curveRegistry: CurveRegistry, migrationRegistry: MigrationRegistry, @@ -319,17 +348,26 @@ export async function addHubSetup( encodedCurveDetails: string, encodedVaultArgs: string, refundRatio: number, - daoAddress?: string + daoAddress?: string, + curve?: ICurve ): Promise<{ hubId: number; + hubCurve: ICurve; }> { let singleAssetVault: SingleAssetVault; let account0: SignerWithAddress; - const isCurveApproved = await curveRegistry.isApproved(curve.address); + let hubCurve: ICurve; + if (curve) { + hubCurve = curve; + } else { + hubCurve = await getCurve(curveType, diamond.address); + } + + const isCurveApproved = await curveRegistry.isApproved(hubCurve.address); if (!isCurveApproved) { - await curveRegistry.approve(curve.address); + await curveRegistry.approve(hubCurve.address); } - const isCurveApprovedAfter = await curveRegistry.isApproved(curve.address); + const isCurveApprovedAfter = await curveRegistry.isApproved(hubCurve.address); expect(isCurveApprovedAfter).to.be.true; let dao = daoAddress; [account0] = await ethers.getSigners(); @@ -353,7 +391,7 @@ export async function addHubSetup( account0.address, tokenAddr, singleAssetVault.address, - curve.address, + hubCurve.address, refundRatio, //refund ratio encodedCurveDetails, encodedVaultArgs @@ -361,5 +399,66 @@ export async function addHubSetup( const hubId = (await hub.count()).toNumber(); return { hubId, + hubCurve, }; } + +/* export async function addHubSetupWithExistingCurve( + tokenAddr: string, + hub: HubFacet, + foundry: Foundry, + hubCurve: ICurve, + meTokenRegistry: MeTokenRegistry, + curveRegistry: CurveRegistry, + migrationRegistry: MigrationRegistry, + vaultRegistry: VaultRegistry, + encodedCurveDetails: string, + encodedVaultArgs: string, + refundRatio: number, + daoAddress?: string +): Promise<{ + hubId: number; + hubCurve: ICurve; +}> { + let singleAssetVault: SingleAssetVault; + let account0: SignerWithAddress; + //const hubCurve = await getCurve(curveType, diamond.address); + const isCurveApproved = await curveRegistry.isApproved(hubCurve.address); + if (!isCurveApproved) { + await curveRegistry.approve(hubCurve.address); + } + const isCurveApprovedAfter = await curveRegistry.isApproved(hubCurve.address); + expect(isCurveApprovedAfter).to.be.true; + let dao = daoAddress; + [account0] = await ethers.getSigners(); + if (!dao) { + dao = account0.address; + } + + singleAssetVault = await deploy( + "SingleAssetVault", + undefined, //no libs + dao, // DAO + foundry.address, // foundry + hub.address, // hub + meTokenRegistry.address, //IMeTokenRegistry + migrationRegistry.address //IMigrationRegistry + ); + + await vaultRegistry.approve(singleAssetVault.address); + + await hub.register( + account0.address, + tokenAddr, + singleAssetVault.address, + hubCurve.address, + refundRatio, //refund ratio + encodedCurveDetails, + encodedVaultArgs + ); + const hubId = (await hub.count()).toNumber(); + return { + hubId, + hubCurve, + }; +} */ diff --git a/testOld/contracts/curves/allCurves.ts b/testOld/contracts/curves/allCurves.ts index 9da3697f..63712477 100644 --- a/testOld/contracts/curves/allCurves.ts +++ b/testOld/contracts/curves/allCurves.ts @@ -52,8 +52,8 @@ const setup = async () => { ["address"], [DAI] ); - const bancorABDK = await deploy("BancorABDK"); - const bancorPower = await deploy("BancorPower"); + /* const bancorABDK = await deploy("BancorABDK"); + const bancorPower = await deploy("BancorPower"); */ // Setting up curve info to test @@ -118,7 +118,7 @@ const setup = async () => { account1, account2, meTokenRegistry, - } = await hubSetup(encodedCurveDetails1, encodedVaultArgs, 5000, bancorABDK)); + } = await hubSetup(encodedCurveDetails1, encodedVaultArgs, 5000)); let hubArgs: [ Hub, @@ -165,7 +165,7 @@ const setup = async () => { curves.push(curve); - // Second ABDK Curve + /* // Second ABDK Curve hubArgs[7] = encodedCurveDetails2; hubDetails = await addHubSetup(...hubArgs); @@ -277,7 +277,7 @@ const setup = async () => { curve.reserveWeight = reserveWeight6; curve.targetReserveWeight = targetReserveWeight6; curves.push(curve); - + */ return curves; }; setup().then((tests) => {