Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dovgopoly committed Oct 17, 2023
1 parent b9f538b commit b1d09a8
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 213 deletions.
6 changes: 6 additions & 0 deletions contracts/mock/spherex/SphereXCalleeMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SphereXCalleeMock {
function protectedMethod() external {}
}
69 changes: 69 additions & 0 deletions test/core/ContractsRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ const { assert } = require("chai");
const { toBN, accounts } = require("../../scripts/utils/utils");
const Reverter = require("../helpers/reverter");
const truffleAssert = require("truffle-assertions");
const { ZERO_ADDR } = require("../../scripts/utils/constants");
const { web3 } = require("hardhat");
const { impersonate } = require("../helpers/impersonator");

const ContractsRegistry = artifacts.require("ContractsRegistry");
const ERC1967Proxy = artifacts.require("ERC1967Proxy");
const ERC20Mock = artifacts.require("ERC20Mock");
const ERC20MockUpgraded = artifacts.require("ERC20MockUpgraded");
const ProtectedTransparentProxy = artifacts.require("ProtectedTransparentProxy");
const SphereXEngineMock = artifacts.require("SphereXEngineMock");
const SphereXCalleeMock = artifacts.require("SphereXCalleeMock");

ContractsRegistry.numberFormat = "BigNumber";

Expand All @@ -18,13 +24,18 @@ describe("ContractsRegistry", () => {

let contractsRegistry;

let sphereXEngine;
let sphereXCallee;

const reverter = new Reverter();

before("setup", async () => {
OWNER = await accounts(0);
SECOND = await accounts(1);

contractsRegistry = await ContractsRegistry.new();
sphereXEngine = await SphereXEngineMock.new();
sphereXCallee = await SphereXCalleeMock.new();

await contractsRegistry.__OwnableContractsRegistry_init();

Expand Down Expand Up @@ -149,4 +160,62 @@ describe("ContractsRegistry", () => {
assert.equal(toBN(await USD.importantVariable()).toFixed(), "42");
});
});

describe("SphereX", () => {
let sphereXCalleeProxy;
let transparentProxy;
let protectedMethodSelector;

beforeEach(async () => {
protectedMethodSelector = web3.eth.abi.encodeFunctionSignature("protectedMethod()");

await contractsRegistry.addProxyContract(await contractsRegistry.USER_REGISTRY_NAME(), sphereXCallee.address);
await contractsRegistry.addProxyContract(await contractsRegistry.POOL_FACTORY_NAME(), sphereXCallee.address);
await contractsRegistry.addProxyContract(await contractsRegistry.POOL_REGISTRY_NAME(), sphereXCallee.address);
await contractsRegistry.addProxyContract(await contractsRegistry.DEXE_EXPERT_NFT_NAME(), sphereXCallee.address);
await contractsRegistry.addProxyContract(await contractsRegistry.PRICE_FEED_NAME(), sphereXCallee.address);
await contractsRegistry.addProxyContract(await contractsRegistry.CORE_PROPERTIES_NAME(), sphereXCallee.address);

sphereXCalleeProxy = await SphereXCalleeMock.at(await contractsRegistry.getUserRegistryContract());
transparentProxy = await ProtectedTransparentProxy.at(sphereXCalleeProxy.address);

await impersonate(contractsRegistry.address);
});

it("should protect when sphereXEngine and selector are on", async () => {
await contractsRegistry.setSphereXEngine(sphereXEngine.address);
await transparentProxy.addProtectedFuncSigs([protectedMethodSelector], { from: contractsRegistry.address });

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());

await sphereXEngine.toggleRevert();

await truffleAssert.reverts(sphereXCalleeProxy.protectedMethod(), "SphereXEngineMock: malicious tx");
});

it("should not protect when selector is off", async () => {
await contractsRegistry.setSphereXEngine(sphereXEngine.address);

await sphereXEngine.toggleRevert();

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());
});

it("should not protect when sphereXEngine is off", async () => {
await contractsRegistry.setSphereXEngine(sphereXEngine.address);
await contractsRegistry.setSphereXEngine(ZERO_ADDR);
await transparentProxy.addProtectedFuncSigs([protectedMethodSelector], { from: contractsRegistry.address });

await sphereXEngine.toggleRevert();

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());
});

it("should not set engine if not an operator", async () => {
await truffleAssert.reverts(
contractsRegistry.setSphereXEngine(sphereXEngine.address, { from: SECOND }),
"Ownable: caller is not the owner"
);
});
});
});
101 changes: 100 additions & 1 deletion test/factory/PoolRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const { accounts } = require("../../scripts/utils/utils");
const Reverter = require("../helpers/reverter");
const truffleAssert = require("truffle-assertions");
const { DEFAULT_CORE_PROPERTIES } = require("../utils/constants");
const { web3 } = require("hardhat");
const { impersonate } = require("../helpers/impersonator");
const { ZERO_ADDR } = require("../../scripts/utils/constants");

const ContractsRegistry = artifacts.require("ContractsRegistry");
const PoolRegistry = artifacts.require("PoolRegistry");
Expand All @@ -11,6 +14,10 @@ const BABTMock = artifacts.require("BABTMock");
const CoreProperties = artifacts.require("CoreProperties");
const PriceFeedMock = artifacts.require("PriceFeedMock");
const UniswapV2RouterMock = artifacts.require("UniswapV2RouterMock");
const SphereXEngineMock = artifacts.require("SphereXEngineMock");
const SphereXCalleeMock = artifacts.require("SphereXCalleeMock");
const PoolBeacon = artifacts.require("PoolBeacon");
const ProtectedPublicBeaconProxy = artifacts.require("ProtectedPublicBeaconProxy");

ContractsRegistry.numberFormat = "BigNumber";
PoolRegistry.numberFormat = "BigNumber";
Expand All @@ -22,6 +29,7 @@ UniswapV2RouterMock.numberFormat = "BigNumber";

describe("PoolRegistry", () => {
let OWNER;
let SECOND;
let FACTORY;
let NOTHING;

Expand All @@ -30,11 +38,15 @@ describe("PoolRegistry", () => {
let DEXE;
let poolRegistry;

let sphereXEngine;
let sphereXCallee;

const reverter = new Reverter();

before("setup", async () => {
OWNER = await accounts(0);
FACTORY = await accounts(1);
SECOND = await accounts(1);
FACTORY = await accounts(2);
NOTHING = await accounts(9);

const contractsRegistry = await ContractsRegistry.new();
Expand All @@ -45,6 +57,8 @@ describe("PoolRegistry", () => {
const _coreProperties = await CoreProperties.new();
const _priceFeed = await PriceFeedMock.new();
const uniswapV2Router = await UniswapV2RouterMock.new();
sphereXEngine = await SphereXEngineMock.new();
sphereXCallee = await SphereXCalleeMock.new();

await contractsRegistry.__OwnableContractsRegistry_init();
await contractsRegistry.addProxyContract(await contractsRegistry.CORE_PROPERTIES_NAME(), _coreProperties.address);
Expand Down Expand Up @@ -102,4 +116,89 @@ describe("PoolRegistry", () => {
assert.isTrue(await poolRegistry.isGovPool(POOL_1));
});
});

describe("SphereX", () => {
let sphereXCalleeProxy;
let poolBeaconProxy;
let protectedPublicBeaconProxy;
let protectedMethodSelector;

beforeEach(async () => {
protectedMethodSelector = web3.eth.abi.encodeFunctionSignature("protectedMethod()");

await poolRegistry.setNewImplementations(
[
GOV_NAME,
await poolRegistry.SETTINGS_NAME(),
await poolRegistry.VALIDATORS_NAME(),
await poolRegistry.USER_KEEPER_NAME(),
await poolRegistry.DISTRIBUTION_PROPOSAL_NAME(),
await poolRegistry.TOKEN_SALE_PROPOSAL_NAME(),
await poolRegistry.EXPERT_NFT_NAME(),
await poolRegistry.NFT_MULTIPLIER_NAME(),
await poolRegistry.LINEAR_POWER_NAME(),
await poolRegistry.POLYNOMIAL_POWER_NAME(),
],
[
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
sphereXCallee.address,
]
);

poolBeaconProxy = await PoolBeacon.at(await poolRegistry.getProxyBeacon(GOV_NAME));

protectedPublicBeaconProxy = await ProtectedPublicBeaconProxy.new(poolBeaconProxy.address, "0x");
sphereXCalleeProxy = await SphereXCalleeMock.at(protectedPublicBeaconProxy.address);

await impersonate(poolRegistry.address);
});

it("should protect when sphereXEngine and selector are on", async () => {
await poolRegistry.setSphereXEngine(sphereXEngine.address);
await poolBeaconProxy.addProtectedFuncSigs([protectedMethodSelector], { from: poolRegistry.address });

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());

await sphereXEngine.toggleRevert();

await truffleAssert.reverts(sphereXCalleeProxy.protectedMethod(), "SphereXEngineMock: malicious tx");
});

it("should not protect when selector is off", async () => {
await poolRegistry.setSphereXEngine(sphereXEngine.address);

await sphereXEngine.toggleRevert();

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());
});

it("should not protect when sphereXEngine is off", async () => {
await poolRegistry.setSphereXEngine(sphereXEngine.address);
await poolRegistry.setSphereXEngine(ZERO_ADDR);
await poolBeaconProxy.addProtectedFuncSigs([protectedMethodSelector], { from: poolRegistry.address });

await sphereXEngine.toggleRevert();

await truffleAssert.passes(sphereXCalleeProxy.protectedMethod());
});

it("should not set engine if not an operator", async () => {
await truffleAssert.reverts(
poolRegistry.setSphereXEngine(sphereXEngine.address, { from: SECOND }),
"Ownable: caller is not the owner"
);
});

it("should return correct implementation", async () => {
assert.equal(await protectedPublicBeaconProxy.implementation(), await sphereXCallee.address);
});
});
});
Loading

0 comments on commit b1d09a8

Please sign in to comment.