Skip to content

Commit

Permalink
Approve->authorize for vaults and functions around it
Browse files Browse the repository at this point in the history
  • Loading branch information
dimpar committed Dec 11, 2023
1 parent d7e8ac6 commit 01086fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
22 changes: 11 additions & 11 deletions core/contracts/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import "@openzeppelin/contracts/access/Ownable.sol";
/// generate yield for Bitcoin holders.
contract Dispatcher is Ownable {
struct VaultInfo {
bool approved;
bool authorized;
}

/// @notice Approved Yield Vaults that implement ERC4626 standard. These
/// @notice Authorized Yield Vaults that implement ERC4626 standard. These
/// vaults deposit assets to yield strategies, e.g. Uniswap V3
/// WBTC/TBTC pool. Vault can be a part of Acre ecosystem or can be
/// implemented externally. As long as it complies with ERC4626
/// standard and is approved by the owner it can be plugged into
/// standard and is authorized by the owner it can be plugged into
/// Acre.
address[] public vaults;
mapping(address => VaultInfo) public vaultsInfo;
Expand All @@ -26,23 +26,23 @@ contract Dispatcher is Ownable {

constructor() Ownable(msg.sender) {}

/// @notice Adds a vault to the list of approved vaults.
/// @notice Adds a vault to the list of authorized vaults.
/// @param vault Address of the vault to add.
function addVault(address vault) external onlyOwner {
require(!vaultsInfo[vault].approved, "Vault already approved");
function authorizeVault(address vault) external onlyOwner {
require(!vaultsInfo[vault].authorized, "Vault already authorized");

vaults.push(vault);
vaultsInfo[vault].approved = true;
vaultsInfo[vault].authorized = true;

emit VaultAdded(vault);
}

/// @notice Removes a vault from the list of approved vaults.
/// @notice Removes a vault from the list of authorized vaults.
/// @param vault Address of the vault to remove.
function removeVault(address vault) external onlyOwner {
require(vaultsInfo[vault].approved, "Not a vault");
function deauthorizeVault(address vault) external onlyOwner {
require(vaultsInfo[vault].authorized, "Not a vault");

vaultsInfo[vault].approved = false;
vaultsInfo[vault].authorized = false;

for (uint256 i = 0; i < vaults.length; i++) {
if (vaults[i] == vault) {
Expand Down
64 changes: 32 additions & 32 deletions core/test/Dispatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ describe("Dispatcher", () => {
await snapshot.restore()
})

describe("addVault", () => {
describe("authorizeVault", () => {
context("when caller is not a governance account", () => {
it("should revert when adding a vault", async () => {
await expect(
dispatcher.connect(thirdParty).addVault(vault1),
dispatcher.connect(thirdParty).authorizeVault(vault1),
).to.be.revertedWithCustomError(
dispatcher,
"OwnableUnauthorizedAccount",
Expand All @@ -53,49 +53,49 @@ describe("Dispatcher", () => {

context("when caller is a governance account", () => {
it("should be able to add vaults", async () => {
await dispatcher.connect(governance).addVault(vault1)
await dispatcher.connect(governance).addVault(vault2)
await dispatcher.connect(governance).addVault(vault3)
await dispatcher.connect(governance).authorizeVault(vault1)
await dispatcher.connect(governance).authorizeVault(vault2)
await dispatcher.connect(governance).authorizeVault(vault3)

expect(await dispatcher.vaults(0)).to.equal(vault1)
const isVault1Approved = await dispatcher.vaultsInfo(vault1)
expect(isVault1Approved).to.equal(true)
const isVault1Authorized = await dispatcher.vaultsInfo(vault1)
expect(isVault1Authorized).to.equal(true)

expect(await dispatcher.vaults(1)).to.equal(vault2)
const isVault2Approved = await dispatcher.vaultsInfo(vault2)
expect(isVault2Approved).to.equal(true)
const isVault2Authorized = await dispatcher.vaultsInfo(vault2)
expect(isVault2Authorized).to.equal(true)

expect(await dispatcher.vaults(2)).to.equal(vault3)
const isVault3Approved = await dispatcher.vaultsInfo(vault3)
expect(isVault3Approved).to.equal(true)
const isVault3Authorized = await dispatcher.vaultsInfo(vault3)
expect(isVault3Authorized).to.equal(true)
})

it("should not be able to add the same vault twice", async () => {
await dispatcher.connect(governance).addVault(vault1)
await dispatcher.connect(governance).authorizeVault(vault1)
await expect(
dispatcher.connect(governance).addVault(vault1),
).to.be.revertedWith("Vault already approved")
dispatcher.connect(governance).authorizeVault(vault1),
).to.be.revertedWith("Vault already authorized")
})

it("should emit an event when adding a vault", async () => {
await expect(dispatcher.connect(governance).addVault(vault1))
await expect(dispatcher.connect(governance).authorizeVault(vault1))
.to.emit(dispatcher, "VaultAdded")
.withArgs(vault1)
})
})
})

describe("removeVault", () => {
describe("deauthorizeVault", () => {
beforeEach(async () => {
await dispatcher.connect(governance).addVault(vault1)
await dispatcher.connect(governance).addVault(vault2)
await dispatcher.connect(governance).addVault(vault3)
await dispatcher.connect(governance).authorizeVault(vault1)
await dispatcher.connect(governance).authorizeVault(vault2)
await dispatcher.connect(governance).authorizeVault(vault3)
})

context("when caller is not a governance account", () => {
it("should revert when adding a vault", async () => {
await expect(
dispatcher.connect(thirdParty).removeVault(vault1),
dispatcher.connect(thirdParty).deauthorizeVault(vault1),
).to.be.revertedWithCustomError(
dispatcher,
"OwnableUnauthorizedAccount",
Expand All @@ -105,36 +105,36 @@ describe("Dispatcher", () => {

context("when caller is a governance account", () => {
it("should be able to remove vaults", async () => {
await dispatcher.connect(governance).removeVault(vault1)
await dispatcher.connect(governance).deauthorizeVault(vault1)

// Last vault replaced the first vault in the 'vaults' array
expect(await dispatcher.vaults(0)).to.equal(vault3)
const isVault1Approved = await dispatcher.vaultsInfo(vault1)
expect(isVault1Approved).to.equal(false)
const isVault1Authorized = await dispatcher.vaultsInfo(vault1)
expect(isVault1Authorized).to.equal(false)
expect(await dispatcher.vaultsLength()).to.equal(2)

await dispatcher.connect(governance).removeVault(vault2)
await dispatcher.connect(governance).deauthorizeVault(vault2)

// Last vault (vault2) was removed from the 'vaults' array
expect(await dispatcher.vaults(0)).to.equal(vault3)
expect(await dispatcher.vaultsLength()).to.equal(1)
const isVault2Approved = await dispatcher.vaultsInfo(vault2)
expect(isVault2Approved).to.equal(false)
const isVault2Authorized = await dispatcher.vaultsInfo(vault2)
expect(isVault2Authorized).to.equal(false)

await dispatcher.connect(governance).removeVault(vault3)
await dispatcher.connect(governance).deauthorizeVault(vault3)
expect(await dispatcher.vaultsLength()).to.equal(0)
const isVault3Approved = await dispatcher.vaultsInfo(vault3)
expect(isVault3Approved).to.equal(false)
const isVault3Authorized = await dispatcher.vaultsInfo(vault3)
expect(isVault3Authorized).to.equal(false)
})

it("should not be able to remove a vault that is not approved", async () => {
it("should not be able to remove a vault that is not authorized", async () => {
await expect(
dispatcher.connect(governance).removeVault(vault4),
dispatcher.connect(governance).deauthorizeVault(vault4),
).to.be.revertedWith("Not a vault")
})

it("should emit an event when removing a vault", async () => {
await expect(dispatcher.connect(governance).removeVault(vault1))
await expect(dispatcher.connect(governance).deauthorizeVault(vault1))
.to.emit(dispatcher, "VaultRemoved")
.withArgs(vault1)
})
Expand Down

0 comments on commit 01086fc

Please sign in to comment.