Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Get some of the older tests up to date #208

Merged
merged 6 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 56 additions & 52 deletions test/core/accounts/AccountsAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import {FakeContract, smock} from "@defi-wonderland/smock";
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {expect} from "chai";
import hre from "hardhat";
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {deployDummyERC20, DEFAULT_CHARITY_ENDOWMENT, DEFAULT_PERMISSIONS_STRUCT} from "test/utils";
import {deployFacetAsProxy} from "test/core/accounts/utils/deployTestFacet";
import {DEFAULT_CHARITY_ENDOWMENT} from "test/utils";
import {
TestFacetProxyContract,
AccountsAllowance__factory,
AccountsAllowance,
AccountsAllowance__factory,
DummyERC20,
DummyERC20__factory,
TestFacetProxyContract,
} from "typechain-types";
import {deployFacetAsProxy} from "test/core/accounts/utils/deployTestFacet";
import {getSigners} from "utils";
import {AccountStorage} from "typechain-types/contracts/test/accounts/TestFacetProxyContract";
import {genWallet, getSigners} from "utils";

describe("AccountsAllowance", function () {
const {ethers} = hre;

let owner: SignerWithAddress;
let proxyAdmin: SignerWithAddress;
let user: SignerWithAddress;
let token: DummyERC20;
let token2: DummyERC20;

let tokenFake: FakeContract<DummyERC20>;

before(async function () {
const signers = await getSigners(hre);
owner = signers.deployer;
proxyAdmin = signers.proxyAdmin;
user = signers.apTeam1;
token = await deployDummyERC20(owner);
token2 = await deployDummyERC20(owner);
});

beforeEach(async () => {
tokenFake = await smock.fake<DummyERC20>(new DummyERC20__factory());
});

describe("Test cases for `manageAllowances`", async function () {
Expand All @@ -44,9 +48,9 @@ describe("AccountsAllowance", function () {
enumData: 0,
});
// add an accepted token to endowment #42
await state.setTokenAccepted(42, token.address, true);
await state.setTokenAccepted(42, tokenFake.address, true);
// set a starting balance for Endowment: 100 qty of tokens in liquid
await state.setEndowmentTokenBalance(42, token.address, 0, 100);
await state.setEndowmentTokenBalance(42, tokenFake.address, 0, 100);
// setup endowment 42 with minimum needed for testing
// Allowlists Beneficiaries set for a user
let endowment = DEFAULT_CHARITY_ENDOWMENT;
Expand All @@ -61,15 +65,15 @@ describe("AccountsAllowance", function () {
data: {endowId: 0, fundId: 0, addr: ethers.constants.AddressZero},
enumData: 0,
});
await expect(facet.manageAllowances(42, user.address, token.address, 10)).to.be.revertedWith(
"Endowment is closed"
);
await expect(
facet.manageAllowances(42, user.address, tokenFake.address, 10)
).to.be.revertedWith("Endowment is closed");
});

it("reverts when the sender is not the endowment owner or a valid delegate who can control allowlists", async function () {
// not the endowment owner sending the message and user is not a delegate
await expect(
facet.connect(user).manageAllowances(42, user.address, token.address, 10)
facet.connect(user).manageAllowances(42, user.address, tokenFake.address, 10)
).to.be.revertedWith("Unauthorized");
});

Expand All @@ -78,59 +82,59 @@ describe("AccountsAllowance", function () {
facet.manageAllowances(42, user.address, ethers.constants.AddressZero, 10)
).to.be.revertedWith("Invalid Token");

await expect(facet.manageAllowances(42, user.address, token2.address, 10)).to.be.revertedWith(
"Invalid Token"
);
await expect(
facet.manageAllowances(42, user.address, genWallet().address, 10)
).to.be.revertedWith("Invalid Token");
});

it("reverts when the spender is not in allowlist", async function () {
await expect(
facet.manageAllowances(42, proxyAdmin.address, token.address, 10)
facet.manageAllowances(42, proxyAdmin.address, tokenFake.address, 10)
).to.be.revertedWith("Spender is not in allowlists");
});

it("reverts when there are no adjustments needed (ie. proposed amount == spender balance amount)", async function () {
// set allowance for user to 10 tokens of total 10 tokens outstanding
await state.setTokenAllowance(42, user.address, token.address, 10, 10);
await state.setTokenAllowance(42, user.address, tokenFake.address, 10, 10);

await expect(facet.manageAllowances(42, user.address, token.address, 10)).to.be.revertedWith(
"Spender balance equal to amount. No changes needed"
);
await expect(
facet.manageAllowances(42, user.address, tokenFake.address, 10)
).to.be.revertedWith("Spender balance equal to amount. No changes needed");
});

it("reverts when try to increase a valid token's allowance beyond liquid balance available", async function () {
await expect(
facet.manageAllowances(42, user.address, token.address, 100000)
facet.manageAllowances(42, user.address, tokenFake.address, 100000)
).to.be.revertedWith("Insufficient liquid balance to allocate");
});

it("passes when try to increase a valid token's allowance within range of liquid balance available", async function () {
expect(await facet.manageAllowances(42, user.address, token.address, 10))
expect(await facet.manageAllowances(42, user.address, tokenFake.address, 10))
.to.emit(facet, "AllowanceUpdated")
.withArgs(42, user.address, token.address, 10, 10, 0);
.withArgs(42, user.address, tokenFake.address, 10, 10, 0);

// endowment liquid balance should be 90 now (100 - 10)
const endowBal = await state.getEndowmentTokenBalance(42, token.address);
const endowBal = await state.getEndowmentTokenBalance(42, tokenFake.address);
expect(endowBal[1]).to.equal(90);
// user allowance should be 10 now
const allowance = await state.getTokenAllowance(42, user.address, token.address);
const allowance = await state.getTokenAllowance(42, user.address, tokenFake.address);
expect(allowance).to.equal(10);
});

it("passes when try to decrease an existing spender's allowance", async function () {
// now we allocate some token allowance to the user address to spend from
await state.setTokenAllowance(42, user.address, token.address, 10, 10);
await state.setTokenAllowance(42, user.address, tokenFake.address, 10, 10);

// set a lower total token allowance for the user, returning the delta to liquid balance
expect(await facet.manageAllowances(42, user.address, token.address, 3))
expect(await facet.manageAllowances(42, user.address, tokenFake.address, 3))
.to.emit(facet, "AllowanceUpdated")
.withArgs(42, user.address, token.address, 3, 0, 7);
.withArgs(42, user.address, tokenFake.address, 3, 0, 7);

// endowment liquid balance should be 107 now (100 + 7)
const endowBal = await state.getEndowmentTokenBalance(42, token.address);
const endowBal = await state.getEndowmentTokenBalance(42, tokenFake.address);
expect(endowBal[1]).to.equal(107);
// user allowance should be 3 now
const allowance = await state.getTokenAllowance(42, user.address, token.address);
const allowance = await state.getTokenAllowance(42, user.address, tokenFake.address);
expect(allowance).to.equal(3);
});
});
Expand All @@ -150,9 +154,9 @@ describe("AccountsAllowance", function () {
enumData: 0,
});
// add an accepted token to endowment #42
await state.setTokenAccepted(42, token.address, true);
await state.setTokenAccepted(42, tokenFake.address, true);
// set a starting balance for Endowment: 100 qty of tokens in liquid
await state.setEndowmentTokenBalance(42, token.address, 0, 100);
await state.setEndowmentTokenBalance(42, tokenFake.address, 0, 100);
});

it("reverts when try to spend token that is invalid(zero address) or dne in allowances", async function () {
Expand All @@ -162,50 +166,50 @@ describe("AccountsAllowance", function () {
).to.be.revertedWith("Invalid Token");

// try to spend an allowance for a token that dne
await expect(facet.spendAllowance(42, token2.address, 10, user.address)).to.be.revertedWith(
"Invalid Token"
);
await expect(
facet.spendAllowance(42, genWallet().address, 10, user.address)
).to.be.revertedWith("Invalid Token");
});

it("reverts when try to spend zero amount of allowance", async function () {
// now we allocate some token allowance to the user address to spend from
await state.setTokenAllowance(42, user.address, token.address, 10, 10);
await state.setTokenAllowance(42, user.address, tokenFake.address, 10, 10);

// try to spend zero allowance
await expect(facet.spendAllowance(42, token.address, 0, user.address)).to.be.revertedWith(
await expect(facet.spendAllowance(42, tokenFake.address, 0, user.address)).to.be.revertedWith(
"Zero Amount"
);
});

it("reverts when try to spend more allowance than is available for token", async function () {
// now we allocate some token allowance to the user address to spend from
await state.setTokenAllowance(42, user.address, token.address, 10, 10);
await state.setTokenAllowance(42, user.address, tokenFake.address, 10, 10);

// try to spend more allowance than user was allocated
await expect(facet.spendAllowance(42, token.address, 1000, user.address)).to.be.revertedWith(
"Amount requested exceeds Allowance balance"
);
await expect(
facet.spendAllowance(42, tokenFake.address, 1000, user.address)
).to.be.revertedWith("Amount requested exceeds Allowance balance");

// try to spend more allowance than user was allocated
await expect(
facet.spendAllowance(42, token.address, 1, proxyAdmin.address)
facet.spendAllowance(42, tokenFake.address, 1, proxyAdmin.address)
).to.be.revertedWith("Amount requested exceeds Allowance balance");
});

it("passes when spend less than or equal to the allowance available for token", async function () {
// now we allocate some token allowance to the user address to spend from
await state.setTokenAllowance(42, user.address, token.address, 10, 10);
await state.setTokenAllowance(42, user.address, tokenFake.address, 10, 10);

// mint tokens so that the contract can transfer them to recipient
await token.mint(facet.address, 10);
tokenFake.transfer.returns(true);

// user spends less than what was allocated to them (ie. 5 out of 10 available)
expect(await facet.connect(user).spendAllowance(42, token.address, 5, user.address))
expect(await facet.connect(user).spendAllowance(42, tokenFake.address, 5, user.address))
.to.emit(facet, "AllowanceSpent")
.withArgs(42, user.address, token.address, 5);
.withArgs(42, user.address, tokenFake.address, 5);

// user allowance should be 5 now (10 - 5)
let allowance = await state.getTokenAllowance(42, user.address, token.address);
let allowance = await state.getTokenAllowance(42, user.address, tokenFake.address);
expect(allowance).to.equal(5);
});
});
Expand Down
45 changes: 4 additions & 41 deletions test/core/accounts/AccountsCreateEndowment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {FakeContract, smock} from "@defi-wonderland/smock";
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {expect, use} from "chai";
import {deployRegistrar} from "contracts/core/registrar/scripts/deploy";
import {deployEndowmentMultiSig} from "contracts/normalized_endowment/endowment-multisig/scripts/deploy";
import {BigNumber} from "ethers";
import hre from "hardhat";
import {deployFacetAsProxy} from "./utils/deployTestFacet";
import {DEFAULT_REGISTRAR_CONFIG} from "test/utils";
import {
AccountsCreateEndowment,
AccountsCreateEndowment__factory,
Expand All @@ -21,8 +19,7 @@ import {AccountMessages} from "typechain-types/contracts/core/accounts/facets/Ac
import {LocalRegistrarLib} from "typechain-types/contracts/core/registrar/LocalRegistrar";
import {RegistrarStorage} from "typechain-types/contracts/core/registrar/Registrar";
import {genWallet, getSigners} from "utils";
import "../../utils/setup";
import {DEFAULT_REGISTRAR_CONFIG} from "test/utils";
import {deployFacetAsProxy} from "./utils/deployTestFacet";

use(smock.matchers);

Expand All @@ -31,19 +28,14 @@ describe("AccountsCreateEndowment", function () {

const donationMatchCharitesAddress = genWallet().address;
const endowmentOwner = genWallet().address;
const treasuryAddress = genWallet().address;

const expectedNextAccountId = 1;

let owner: SignerWithAddress;
let proxyAdmin: SignerWithAddress;
let charityApplications: SignerWithAddress;
let axelarGateway: SignerWithAddress;
let axelarGasService: SignerWithAddress;
let deployer: SignerWithAddress;
let facet: AccountsCreateEndowment;
let state: TestFacetProxyContract;
let Registrar: Registrar;
let createEndowmentRequest: AccountMessages.CreateEndowmentRequestStruct;
let registrarFake: FakeContract<Registrar>;
let gasFwdFactoryFake: FakeContract<GasFwdFactory>;
Expand All @@ -53,9 +45,6 @@ describe("AccountsCreateEndowment", function () {
owner = signers.apTeam1;
proxyAdmin = signers.proxyAdmin;
charityApplications = signers.deployer;
axelarGateway = signers.apTeam2;
axelarGasService = signers.apTeam3;
deployer = signers.deployer;

const defaultSettingsPermissionsStruct = {
locked: false,
Expand Down Expand Up @@ -153,32 +142,6 @@ describe("AccountsCreateEndowment", function () {
});

beforeEach(async function () {
const registrarDeployment = await deployRegistrar(
{
axelarGateway: axelarGateway.address,
axelarGasService: axelarGasService.address,
router: ethers.constants.AddressZero,
owner: owner.address,
deployer,
proxyAdmin,
treasuryAddress: treasuryAddress,
},
hre
);
const endowDeployments = await deployEndowmentMultiSig(hre);
Registrar = Registrar__factory.connect(registrarDeployment!.address, owner);
const {splitToLiquid, ...curConfig} = await Registrar.queryConfig();
await Registrar.updateConfig({
...curConfig,
splitDefault: splitToLiquid.defaultSplit,
splitMax: splitToLiquid.max,
splitMin: splitToLiquid.min,
charityApplications: charityApplications.address,
multisigFactory: endowDeployments!.factory.address,
multisigEmitter: endowDeployments!.emitter.address,
donationMatchCharitesContract: donationMatchCharitesAddress,
});

let Facet = new AccountsCreateEndowment__factory(owner);
let facetImpl = await Facet.deploy();
state = await deployFacetAsProxy(hre, owner, proxyAdmin, facetImpl.address);
Expand Down Expand Up @@ -387,7 +350,7 @@ describe("AccountsCreateEndowment", function () {
expect(result.name).to.equal(request.name);
expect(result.parent).to.equal(request.parent);
expect(result.proposalLink).to.equal(request.proposalLink);
expect(result.rebalance).to.equalRebalance(await Registrar.getRebalanceParams());
expect(result.rebalance).to.equalRebalance(await registrarFake.getRebalanceParams());
expect(result.referralId).to.equal(request.referralId);
expect(result.sdgs).to.have.same.deep.members(request.sdgs.map((x) => BigNumber.from(x)));
expect(result.settingsController.acceptedTokens).to.equalSettingsPermission(
Expand Down Expand Up @@ -490,7 +453,7 @@ describe("AccountsCreateEndowment", function () {
expect(result.name).to.equal(request.name);
expect(result.parent).to.equal(request.parent);
expect(result.proposalLink).to.equal(request.proposalLink);
expect(result.rebalance).to.equalRebalance(await Registrar.getRebalanceParams());
expect(result.rebalance).to.equalRebalance(await registrarFake.getRebalanceParams());
expect(result.referralId).to.equal(request.referralId);
expect(result.sdgs).to.have.same.deep.members(request.sdgs.map((x) => BigNumber.from(x)));
expect(result.settingsController.acceptedTokens).to.equalSettingsPermission(
Expand Down
2 changes: 1 addition & 1 deletion test/core/accounts/AccountsQueryEndowments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {expect} from "chai";
import {BigNumber} from "ethers";
import hre from "hardhat";
import {deployFacetAsProxy} from "./utils/deployTestFacet";
import {DEFAULT_ACCOUNTS_CONFIG, DEFAULT_CHARITY_ENDOWMENT} from "test/utils";
import {
AccountsQueryEndowments,
Expand All @@ -12,6 +11,7 @@ import {
import {AccountMessages} from "typechain-types/contracts/core/accounts/facets/AccountsQueryEndowments";
import {AccountStorage} from "typechain-types/contracts/test/accounts/TestFacetProxyContract";
import {getSigners} from "utils";
import {deployFacetAsProxy} from "./utils/deployTestFacet";

describe("AccountsQueryEndowments", function () {
const {ethers} = hre;
Expand Down
Loading