Skip to content

Commit

Permalink
Merge pull request #11 from LooksRare/legacy-tests
Browse files Browse the repository at this point in the history
test: Added legacy tests
  • Loading branch information
0xJurassicPunk committed Mar 24, 2022
2 parents 4e166b1 + a9c43b9 commit afbf02c
Show file tree
Hide file tree
Showing 11 changed files with 1,861 additions and 168 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"hardhat-abi-exporter": "^2.8.0",
"hardhat-gas-reporter": "^1.0.4",
"husky": "^7.0.4",
"merkletreejs": "^0.2.31",
"prettier": "^2.3.2",
"prettier-plugin-solidity": "^1.0.0-beta.13",
"solhint": "^3.3.6",
Expand Down
92 changes: 24 additions & 68 deletions test/aggregatorFeeSharingWithUniswapV3.test.ts

Large diffs are not rendered by default.

124 changes: 43 additions & 81 deletions test/feeSharingSystem.test.ts

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions test/helpers/block-traveller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ export async function advanceBlockTo(targetBlock: BigNumber): Promise<void> {
await network.provider.send("hardhat_mine", [numberBlocks.toHexString()]);
}
}

/**
* Advance the block time to target time
* @param targetTime target time (epoch)
* @dev If target time is lower/equal to current time, it throws an error
*/
export async function increaseTo(targetTime: BigNumber): Promise<void> {
const currentTime = BigNumber.from(await latest());
if (targetTime.lt(currentTime)) {
throw Error(`Target·time·(${targetTime})·is·lower·than·current·time·#(${currentTime})`);
}

await network.provider.send("evm_setNextBlockTimestamp", [targetTime.toHexString()]);
}

/**
* Fetch the current block number
*/
export async function latest(): Promise<number> {
return (await ethers.provider.getBlock(await ethers.provider.getBlockNumber())).timestamp;
}
651 changes: 651 additions & 0 deletions test/privateSaleWithFeeSharing.test.ts

Large diffs are not rendered by default.

391 changes: 391 additions & 0 deletions test/stakingPoolForUniswapV2Tokens.test.ts

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions test/tokenDistributor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,11 @@ describe("TokenDistributor", () => {
assert.deepEqual(await tokenDistributor.endBlock(), startBlock.add("400"));

tx = await tokenDistributor.connect(user3).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user3.address, parseEther("1356.1926990348"), parseEther("1256.1926990348"));

tx = await tokenDistributor.connect(user4).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user4.address, parseEther("1356.1926990348"), parseEther("1256.1926990348"));
Expand Down Expand Up @@ -175,11 +173,9 @@ describe("TokenDistributor", () => {
BigNumber.from("100"),
BigNumber.from("100"),
];

const numberPeriods = "4";

const TokenDistributor = await ethers.getContractFactory("TokenDistributor");

tokenDistributor = await TokenDistributor.deploy(
looksRareToken.address,
tokenSplitter.address,
Expand All @@ -191,7 +187,6 @@ describe("TokenDistributor", () => {
);

await tokenDistributor.deployed();

await looksRareToken.connect(admin).transferOwnership(tokenDistributor.address);

// Each user receives 500 LOOKS tokens
Expand All @@ -200,7 +195,6 @@ describe("TokenDistributor", () => {
await looksRareToken.connect(user).approve(tokenDistributor.address, constants.MaxUint256);

const tx = await tokenDistributor.connect(user).deposit(parseEther("100"));

await expect(tx)
.to.emit(tokenDistributor, "Deposit")
.withArgs(user.address, parseEther("100"), parseEther("0"));
Expand All @@ -214,7 +208,6 @@ describe("TokenDistributor", () => {

// 120 * 25 / 4 + 25 * 30/4 = 937.5
let tx = await tokenDistributor.connect(user1).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user1.address, parseEther("1037.5"), parseEther("937.5"));
Expand All @@ -234,7 +227,6 @@ describe("TokenDistributor", () => {

// 937.5 + 25 * 30/3 + 100 * 7.5/3 + 25 * 3.75/3 = 1468.75
tx = await tokenDistributor.connect(user2).withdraw(parseEther("50"));

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user2.address, parseEther("50"), parseEther("1468.75"));
Expand All @@ -250,7 +242,6 @@ describe("TokenDistributor", () => {

// 1518.75 / (1518.75 + 200) * 75 * 3.75 = 248.522727272175
tx = await tokenDistributor.connect(user2).harvestAndCompound();

await expect(tx).to.emit(tokenDistributor, "Compound").withArgs(user2.address, parseEther("248.522727272175"));

// Equal to the sum of the compounded amount + previously harvestedAmount + half of the original deposit (50)
Expand All @@ -260,19 +251,16 @@ describe("TokenDistributor", () => {
assert.deepEqual(await tokenDistributor.endBlock(), startBlock.add("275"));

tx = await tokenDistributor.connect(user2).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user2.address, parseEther("1767.272727272175"), parseEther("0"));

tx = await tokenDistributor.connect(user3).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user3.address, parseEther("1585.1136363636"), parseEther("1485.1136363636"));

tx = await tokenDistributor.connect(user4).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user4.address, parseEther("1585.1136363636"), parseEther("1485.1136363636"));
Expand All @@ -292,7 +280,6 @@ describe("TokenDistributor", () => {

// (10 * 30) / 4 = 75 tokens pending (the new tx moves by 1 block)
const tx = await tokenDistributor.connect(user1).withdrawAll();

await expect(tx)
.to.emit(tokenDistributor, "Withdraw")
.withArgs(user1.address, parseEther("175"), parseEther("75"));
Expand All @@ -309,7 +296,6 @@ describe("TokenDistributor", () => {

// (10 * 30) / 4 = 75 tokens pending (the new tx moves by 1 block)
const tx = await tokenDistributor.connect(user1).harvestAndCompound();

await expect(tx).to.emit(tokenDistributor, "Compound").withArgs(user1.address, parseEther("75"));

// 10 * 70 = 700 tokens for token splitter (EOA for this test)
Expand All @@ -328,7 +314,6 @@ describe("TokenDistributor", () => {

// 30 * 1 block = 30
tx = await tokenDistributor.connect(admin).updatePool();

await expect(tx)
.to.emit(looksRareToken, "Transfer")
.withArgs(constants.AddressZero, tokenDistributor.address, parseEther("30"));
Expand All @@ -353,13 +338,11 @@ describe("TokenDistributor", () => {
await advanceBlockTo(startBlock);

tx = await tokenDistributor.connect(user1).deposit(parseEther("100"));

await expect(tx).to.emit(tokenDistributor, "Deposit").withArgs(user1.address, parseEther("100"), parseEther("0"));
});

it("User can deposit twice before start and harvest remains 0", async () => {
const tx = await tokenDistributor.connect(user1).deposit(parseEther("100"));

await expect(tx).to.emit(tokenDistributor, "Deposit").withArgs(user1.address, parseEther("100"), parseEther("0"));
});
});
Expand All @@ -369,7 +352,6 @@ describe("TokenDistributor", () => {
let rewardsPerBlockForStaking = [parseEther("30"), parseEther("15"), parseEther("7.5"), parseEther("3.75")];
let rewardsPerBlockForOthers = [parseEther("70"), parseEther("35"), parseEther("17.5"), parseEther("8.75")];
let periodLengthesInBlocks = [BigNumber.from("100"), BigNumber.from("100"), BigNumber.from("100")];

const numberPeriods = "4";

// 30 * 100 + 15 * 100 + 7.5 * 100 + 3.75 * 100 = 5625 tokens to be distributed to stakers
Expand Down
151 changes: 151 additions & 0 deletions test/tokenSplitter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { assert, expect } from "chai";
import { ethers } from "hardhat";
import { BigNumber, constants, Contract, utils } from "ethers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

const { parseEther } = utils;

describe("TokenSplitter", () => {
let looksRareToken: Contract;
let tokenSplitter: Contract;

let admin: SignerWithAddress;
let team: SignerWithAddress;
let treasury: SignerWithAddress;
let tradingRewards: SignerWithAddress;
let newTreasury: SignerWithAddress;
let randomUser: SignerWithAddress;

beforeEach(async () => {
const accounts = await ethers.getSigners();
admin = accounts[0];
team = accounts[1];
treasury = accounts[2];
tradingRewards = accounts[3];
newTreasury = accounts[4];
randomUser = accounts[10];

const MockERC20 = await ethers.getContractFactory("MockERC20");
looksRareToken = await MockERC20.deploy("LOOKS", "Mock LOOKS");
await looksRareToken.deployed();
await looksRareToken.connect(admin).mint(admin.address, parseEther("1000000"));
const TokenSplitter = await ethers.getContractFactory("TokenSplitter");
tokenSplitter = await TokenSplitter.deploy(
[team.address, treasury.address, tradingRewards.address],
["20", "10", "70"],
looksRareToken.address
);
await tokenSplitter.deployed();
});

describe("#1 - System works as expected", async () => {
it("Release tokens work", async () => {
assert.deepEqual(await tokenSplitter.calculatePendingRewards(team.address), constants.Zero);

// Admin adds 1000 LOOKS
await looksRareToken.connect(admin).transfer(tokenSplitter.address, parseEther("1000"));
assert.deepEqual(await tokenSplitter.calculatePendingRewards(team.address), parseEther("200"));

let tx = await tokenSplitter.connect(team).releaseTokens(team.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(team.address, parseEther("200"));
assert.deepEqual((await tokenSplitter.accountInfo(team.address))[1], parseEther("200"));

// Admin adds 3000 LOOKS
await looksRareToken.connect(admin).transfer(tokenSplitter.address, parseEther("3000"));

tx = await tokenSplitter.connect(team).releaseTokens(team.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(team.address, parseEther("600"));
assert.deepEqual((await tokenSplitter.accountInfo(team.address))[1], parseEther("800"));

tx = await tokenSplitter.connect(treasury).releaseTokens(treasury.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(treasury.address, parseEther("400"));

tx = await tokenSplitter.connect(tradingRewards).releaseTokens(tradingRewards.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(tradingRewards.address, parseEther("2800"));

assert.deepEqual(await looksRareToken.balanceOf(tokenSplitter.address), constants.Zero);
});

it("Cannot claim if no share, nothing to claim, or already claimed everything", async () => {
assert.deepEqual(await tokenSplitter.calculatePendingRewards(randomUser.address), constants.Zero);

await expect(tokenSplitter.connect(randomUser).releaseTokens(randomUser.address)).to.be.revertedWith(
"Splitter: Account has no share"
);

await expect(tokenSplitter.connect(treasury).releaseTokens(treasury.address)).to.be.revertedWith(
"Splitter: Nothing to transfer"
);

// Admin adds 3000 tokens
await looksRareToken.connect(admin).transfer(tokenSplitter.address, parseEther("3000"));

const tx = await tokenSplitter.connect(team).releaseTokens(team.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(team.address, parseEther("600"));

// Cannot transfer again (if no more rewards)
await expect(tokenSplitter.connect(team).releaseTokens(team.address)).to.be.revertedWith(
"Splitter: Nothing to transfer"
);
});

it("Random user with no shares can release tokens on someone's behalf and receives nothing", async () => {
// Admin adds 3000 tokens
await looksRareToken.connect(admin).transfer(tokenSplitter.address, parseEther("3000"));

const tx = await tokenSplitter.connect(randomUser).releaseTokens(team.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(team.address, parseEther("600"));
assert.deepEqual(await looksRareToken.balanceOf(randomUser.address), constants.Zero);
});

it("Admin can port over the shares of someone", async () => {
// Admin adds 3000 tokens
await looksRareToken.connect(admin).transfer(tokenSplitter.address, parseEther("3000"));

let tx = await tokenSplitter.connect(admin).updateSharesOwner(newTreasury.address, treasury.address);
await expect(tx).to.emit(tokenSplitter, "NewSharesOwner").withArgs(treasury.address, newTreasury.address);

tx = await tokenSplitter.connect(newTreasury).releaseTokens(newTreasury.address);
await expect(tx).to.emit(tokenSplitter, "TokensTransferred").withArgs(newTreasury.address, parseEther("300"));
assert.deepEqual(await looksRareToken.balanceOf(newTreasury.address), parseEther("300"));

await expect(tokenSplitter.connect(treasury).releaseTokens(treasury.address)).to.be.revertedWith(
"Splitter: Account has no share"
);
});
});

describe("#2 - Revertions and admin functions", async () => {
it("Cannot deploy with wrong parameters", async () => {
const TokenSplitter = await ethers.getContractFactory("TokenSplitter");

await expect(
TokenSplitter.deploy([team.address, treasury.address], ["20", "10", "70"], looksRareToken.address)
).to.be.revertedWith("Splitter: Length differ");

await expect(TokenSplitter.deploy([], [], looksRareToken.address)).to.be.revertedWith(
"Splitter: Length must be > 0"
);

await expect(TokenSplitter.deploy([team.address], ["0"], looksRareToken.address)).to.be.revertedWith(
"Splitter: Shares are 0"
);
});

it("Reversions for shares transfer work as expected", async () => {
await expect(
tokenSplitter.connect(admin).updateSharesOwner(treasury.address, treasury.address)
).to.be.revertedWith("Owner: New recipient has existing shares");

await expect(
tokenSplitter.connect(admin).updateSharesOwner(newTreasury.address, randomUser.address)
).to.be.revertedWith("Owner: Current recipient has no shares");
});

it("Owner functions are only callable by owner", async () => {
await expect(
tokenSplitter.connect(randomUser).updateSharesOwner(randomUser.address, team.address)
).to.be.revertedWith("Ownable: caller is not the owner");
});
});
});
Loading

0 comments on commit afbf02c

Please sign in to comment.