Skip to content

Commit

Permalink
Specify deploy tags for each test (#705)
Browse files Browse the repository at this point in the history
* Use chai's closeTo comparison to approximate

* Add appropriate fixture deploy tags to all tests

* Increase line coverage

* Add tests for collecting fees on PermissionlessMetaSwapFlashLoan
instance
  • Loading branch information
penandlim committed Sep 13, 2022
1 parent 05065ed commit 77b182b
Show file tree
Hide file tree
Showing 23 changed files with 194 additions and 87 deletions.
2 changes: 1 addition & 1 deletion deploy/hardhat/006_deploy_Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
}
export default func
func.tags = ["Swap"]
func.dependencies = ["AmplificationUtils", "SwapUtils"]
func.dependencies = ["AmplificationUtils", "SwapUtils", "LPToken"]
9 changes: 9 additions & 0 deletions deploy/hardhat/450_deploy_PermissionlessSwaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
}
export default func
func.tags = ["PermissionlessSwaps"]
func.dependencies = [
"SUSDMetaPoolDeposit",
"LPToken",
"SwapUtils",
"MetaSwapUtils",
"MasterRegistry",
"PoolRegistry",
"AmplificationUtils",
]
// func.skip = async (env) => (await env.getChainId()) == CHAIN_ID.MAINNET
2 changes: 1 addition & 1 deletion test/allowlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Allowlist", () => {

const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture([]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
malActor = signers[10]
Expand Down
14 changes: 8 additions & 6 deletions test/flashloan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deployments } from "hardhat"
import {
FlashLoanBorrowerExample,
GenericERC20,
GenericERC20__factory,
LPToken,
SwapFlashLoan,
} from "../build/typechain/"
Expand Down Expand Up @@ -48,7 +49,7 @@ describe("Swap Flashloan", () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["SUSDMetaPoolTokens"]) // ensure you start from a fresh deployments

TOKENS.length = 0
signers = await ethers.getSigners()
Expand All @@ -60,13 +61,14 @@ describe("Swap Flashloan", () => {
user1Address = await user1.getAddress()
user2Address = await user2.getAddress()

const erc20Factory = await ethers.getContractFactory("GenericERC20")
const erc20Factory: GenericERC20__factory =
await ethers.getContractFactory("GenericERC20")

// Deploy dummy tokens
DAI = await ethers.getContract("DAI")
USDC = await ethers.getContract("USDC")
USDT = await ethers.getContract("USDT")
SUSD = await ethers.getContract("SUSD")
DAI = await erc20Factory.deploy("DAI", "DAI", "18")
USDC = await erc20Factory.deploy("USDC", "USDC", "6")
USDT = await erc20Factory.deploy("USDT", "USDT", "6")
SUSD = await erc20Factory.deploy("SUSD", "SUSD", "18")

TOKENS.push(DAI, USDC, USDT, SUSD)

Expand Down
3 changes: 1 addition & 2 deletions test/metaPoolAdminFees_Inflated_BP_VP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ describe("Meta-Swap with inflated baseVirtualPrice and 50% admin fees", async ()
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture(["Swap", "USDPool"]) // ensure you start from a fresh deployments

await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"])
signers = await ethers.getSigners()
owner = signers[0]
user1 = signers[1]
Expand Down
2 changes: 1 addition & 1 deletion test/metaPoolHighAdminFees_Inflated_BP_VP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Meta-Swap with inflated baseVirtualPrice and 99% admin fees", async ()
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"])

signers = await ethers.getSigners()
owner = signers[0]
Expand Down
3 changes: 1 addition & 2 deletions test/metaPool_Inflated_BP_VP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ describe("Meta-Swap with inflated baseVirtualPrice", async () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture(["Swap", "USDPool"]) // ensure you start from a fresh deployments

await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"])
signers = await ethers.getSigners()
owner = signers[0]
user1 = signers[1]
Expand Down
17 changes: 16 additions & 1 deletion test/metaSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("Meta-Swap", async () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"])

signers = await ethers.getSigners()
owner = signers[0]
Expand Down Expand Up @@ -129,6 +129,21 @@ describe("Meta-Swap", async () => {
)
})

await expect(
metaSwap.initialize(
[susd.address, baseLPToken.address],
[18, 18],
LP_TOKEN_NAME,
LP_TOKEN_SYMBOL,
INITIAL_A_VALUE,
SWAP_FEE,
0,
(
await get("LPToken")
).address,
),
).to.be.revertedWith("use initializeMetaSwap() instead")

// Initialize meta swap pool
// Manually overload the signature
await metaSwap.initializeMetaSwap(
Expand Down
2 changes: 1 addition & 1 deletion test/metaSwapDecimals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Meta-Swap", async () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
owner = signers[0]
Expand Down
2 changes: 1 addition & 1 deletion test/metaSwapDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("Meta-Swap Deposit Contract", async () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["Swap", "USDPool", "MetaSwapUtils"]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
owner = signers[0]
Expand Down
2 changes: 1 addition & 1 deletion test/permissionless/permissionlessDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("PermissionlessDeployer", async () => {

const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["D4PoolTokens", "PermissionlessSwaps"]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
deployer = signers[0]
Expand Down
65 changes: 64 additions & 1 deletion test/permissionless/permissionlessMetaSwapFlashLoan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import LPTokenArtifact from "../../build/artifacts/contracts/LPToken.sol/LPToken
import {
GenericERC20,
LPToken,
MasterRegistry,
MetaSwap,
MetaSwapDeposit,
MetaSwapUtils,
Expand Down Expand Up @@ -48,11 +49,12 @@ describe("PermissionlessMetaSwapFlashLoan with inflated baseVirtualPrice and 50%

const AMOUNT = 1e17
const INFLATED_VP = "1013335719282759415"
const TOKENS: string[] = []

const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["USDPool", "MetaSwapUtils", "MasterRegistry"]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
owner = signers[0]
Expand Down Expand Up @@ -112,6 +114,15 @@ describe("PermissionlessMetaSwapFlashLoan with inflated baseVirtualPrice and 50%
},
)

// Register user1 as fee collector
const masterRegistry: MasterRegistry = await ethers.getContract(
"MasterRegistry",
)
await masterRegistry.addRegistry(
ethers.utils.formatBytes32String("FeeCollector"),
user1Address,
)

metaSwap = (await (
await ethers.getContractFactory("PermissionlessMetaSwapFlashLoan", {
libraries: {
Expand Down Expand Up @@ -157,6 +168,8 @@ describe("PermissionlessMetaSwapFlashLoan with inflated baseVirtualPrice and 50%

// Initialize meta swap pool
// Manually overload the signature
TOKENS.length = 0
TOKENS.push(susd.address, baseLPToken.address)
await metaSwap.initializeMetaSwap(
[susd.address, baseLPToken.address],
[18, 18],
Expand Down Expand Up @@ -600,4 +613,54 @@ describe("PermissionlessMetaSwapFlashLoan with inflated baseVirtualPrice and 50%
expect(lossBP).to.lt(15)
})
})

describe("withdrawAdminFees", () => {
it("Reverts when called by non-owners", async () => {
await expect(metaSwap.connect(user2).withdrawAdminFees()).to.be.reverted
})

it("Succeeds when there are no fees withdrawn", async () => {
// Sets adminFee to 1% of the swap fees
const balancesBefore = await getUserTokenBalances(owner, TOKENS)
await metaSwap.withdrawAdminFees()
const balancesAfter = await getUserTokenBalances(owner, TOKENS)

expect(balancesBefore).to.eql(balancesAfter)
})

it("Successfully gets called by both owner and the fee collector", async () => {
await metaSwap.withdrawAdminFees()
await metaSwap.connect(user1).withdrawAdminFees()
})

it("Succeeds with expected amount of fees withdrawn", async () => {
// Sets adminFee to 1% of the swap fees
await metaSwap.setAdminFee(BigNumber.from(10 ** 8))
await metaSwap.connect(user1).swap(0, 1, String(1e18), 0, MAX_UINT256)
await metaSwap.connect(user1).swap(1, 0, String(1e18), 0, MAX_UINT256)

expect(await metaSwap.getAdminBalance(0)).to.eq("4094214930477")
expect(await metaSwap.getAdminBalance(1)).to.eq("3906359586183")

const balancesBefore = await getUserTokenBalances(owner, TOKENS)
const collectorBalancesBefore = await getUserTokenBalances(user1, TOKENS)

await metaSwap.withdrawAdminFees()

const balancesAfter = await getUserTokenBalances(owner, TOKENS)
const collectorBalancesAfter = await getUserTokenBalances(user1, TOKENS)
const balancesDelta = balancesAfter.map((balance, i) =>
balance.sub(balancesBefore[i]),
)
const collectorBalancesDelta = collectorBalancesAfter.map((balance, i) =>
balance.sub(collectorBalancesBefore[i]),
)

expect(balancesDelta).to.eql(collectorBalancesDelta)
expect(balancesDelta).to.eql([
BigNumber.from("2047107465238"),
BigNumber.from("1953179793091"),
])
})
})
})
19 changes: 8 additions & 11 deletions test/permissionless/permissionlessSwapFlashLoan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumber, Signer } from "ethers"
import { deployments } from "hardhat"
import {
GenericERC20,
GenericERC20__factory,
LPToken,
MasterRegistry,
Swap,
Expand Down Expand Up @@ -56,7 +57,7 @@ describe("PermissionlessSwapFlashLoan with 4 tokens", () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { get, deploy } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture(["Swap", "MasterRegistry"]) // ensure you start from a fresh deployments

TOKENS.length = 0
signers = await ethers.getSigners()
Expand All @@ -68,17 +69,13 @@ describe("PermissionlessSwapFlashLoan with 4 tokens", () => {
user1Address = await user1.getAddress()
user2Address = await user2.getAddress()

await deploy("SUSD", {
from: ownerAddress,
contract: "GenericERC20",
args: ["SUSD", "Synthetix USD", "18"],
skipIfAlreadyDeployed: true,
})
const erc20Factory: GenericERC20__factory =
await ethers.getContractFactory("GenericERC20")

DAI = await ethers.getContract("DAI")
USDC = await ethers.getContract("USDC")
USDT = await ethers.getContract("USDT")
SUSD = await ethers.getContract("SUSD")
DAI = await erc20Factory.deploy("DAI", "DAI", "18")
USDC = await erc20Factory.deploy("USDC", "USDC", "6")
USDT = await erc20Factory.deploy("USDT", "USDT", "6")
SUSD = await erc20Factory.deploy("SUSD", "SUSD", "18")

TOKENS.push(DAI, USDC, USDT, SUSD)

Expand Down
2 changes: 1 addition & 1 deletion test/retroactiveVesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Retroactive Vesting", () => {
const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
const { deploy } = deployments
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture([]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
deployer = signers[0]
Expand Down
16 changes: 9 additions & 7 deletions test/rewards/simpleRewarder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BIG_NUMBER_1E18,
BIG_NUMBER_ZERO,
getCurrentBlockTimestamp,
increaseTimestamp,
MAX_UINT256,
setTimestamp,
ZERO_ADDRESS,
Expand Down Expand Up @@ -372,25 +373,26 @@ describe("SimpleRewarder", async () => {
await miniChef.set(0, 1, simpleRewarder.address, true)

await miniChef.connect(farmer).harvest(0, farmerAddress)
await setTimestamp((await getCurrentBlockTimestamp()) + 1000)

expect(await usdv2LpToken.balanceOf(farmerAddress)).to.eq(
BIG_NUMBER_1E18.mul(5),
)
expect(await rewardToken1.balanceOf(farmerAddress)).to.eq(
expect(await rewardToken1.balanceOf(farmerAddress)).to.closeTo(
BIG_NUMBER_1E18.mul(5),
BIG_NUMBER_1E18,
)
expect(await rewardToken2.balanceOf(farmerAddress)).to.eq(0)

await increaseTimestamp(1000)

await miniChef
.connect(farmer)
.withdrawAndHarvest(0, BIG_NUMBER_1E18, farmerAddress)
expect(await usdv2LpToken.balanceOf(farmerAddress)).to.eq(
BIG_NUMBER_1E18.mul(6),
)
expect(await rewardToken1.balanceOf(farmerAddress)).to.satisfy(
(balance: BigNumber) =>
balance.eq(BIG_NUMBER_1E18.mul(1006)) ||
balance.eq(BIG_NUMBER_1E18.mul(1007)),
expect(await rewardToken1.balanceOf(farmerAddress)).to.closeTo(
BIG_NUMBER_1E18.mul(1006),
BIG_NUMBER_1E18,
)
expect(await rewardToken2.balanceOf(farmerAddress)).to.eq(
BIG_NUMBER_1E18.mul(2002),
Expand Down
2 changes: 1 addition & 1 deletion test/stakeableTokenWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("StakeableTokenWrapper", () => {

const setupTest = deployments.createFixture(
async ({ deployments, ethers }) => {
await deployments.fixture() // ensure you start from a fresh deployments
await deployments.fixture([]) // ensure you start from a fresh deployments

signers = await ethers.getSigners()
const erc20Factory = await ethers.getContractFactory("GenericERC20")
Expand Down
Loading

0 comments on commit 77b182b

Please sign in to comment.