Skip to content

Commit

Permalink
WIP AtomicQueue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ungaro committed Dec 27, 2024
1 parent cab020b commit 40d719d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
1 change: 0 additions & 1 deletion nest/src/vault/NestAtomicQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { FixedPointMathLib } from "@solmate/utils/FixedPointMathLib.sol";
import { AtomicQueue } from "@boringvault/src/atomic-queue/AtomicQueue.sol";

import { IAtomicQueue } from "../interfaces/IAtomicQueue.sol";
import { IComponentToken } from "../interfaces/IComponentToken.sol";
import { NestBoringVaultModule } from "./NestBoringVaultModule.sol";

/**
Expand Down
136 changes: 136 additions & 0 deletions nest/test/NestAtomicQueue.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { NestAtomicQueue } from "../src/vault/NestAtomicQueue.sol";
import { NestBoringVaultModule } from "../src/vault/NestBoringVaultModule.sol";
import { NestBoringVaultModuleTest } from "./NestBoringVaultModuleTest.t.sol";
import { AtomicQueue } from "@boringvault/src/atomic-queue/AtomicQueue.sol";
import { ERC20 } from "@solmate/tokens/ERC20.sol";

import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";

import { FixedPointMathLib } from "@solmate/utils/FixedPointMathLib.sol";

// Add event definitions at the top of the contract
event AtomicRequestUpdated(
address user,
address offerToken,
address wantToken,
uint256 amount,
uint256 deadline,
uint256 minPrice,
uint256 timestamp
);

contract NestAtomicQueueTest is NestBoringVaultModuleTest {

NestAtomicQueue public queue;

uint256 public constant DEADLINE_PERIOD = 1 days;
uint256 public constant PRICE_PERCENTAGE = 9900; // 99.00%

using SafeCast for uint256;
using FixedPointMathLib for uint256;

function setUp() public override {
super.setUp();

// Deploy NestAtomicQueue
queue = new NestAtomicQueue(
address(this), address(vault), address(accountant), asset, DEADLINE_PERIOD, PRICE_PERCENTAGE
);
}

function testInitialization() public override {
assertEq(queue.owner(), address(this));
assertEq(address(queue.vaultContract()), address(vault));
assertEq(address(queue.accountantContract()), address(accountant));
assertEq(address(queue.assetToken()), address(asset));
assertEq(queue.deadlinePeriod(), DEADLINE_PERIOD);
assertEq(queue.pricePercentage(), PRICE_PERCENTAGE);
}

function testRequestRedeem(
uint256 shares
) public {
// Bound shares to reasonable values
shares = bound(shares, 1e6, 1_000_000e6);

// Give user some shares in the vault
deal(address(vault), user, shares);

// Start as user
vm.startPrank(user);

// Approve queue to spend shares
vault.approve(address(queue), shares);

// Calculate expected deadline and atomic price
uint256 expectedDeadline = block.timestamp + DEADLINE_PERIOD;
uint256 expectedAtomicPrice =
accountant.getRateInQuote(ERC20(address(asset))).mulDivDown(PRICE_PERCENTAGE, 10_000);

// Expect AtomicRequestUpdated event
vm.expectEmit(true, true, true, true);
emit AtomicRequestUpdated(
user, // user
address(vault), // offerToken
address(asset), // wantToken
shares, // amount
expectedDeadline, // deadline
expectedAtomicPrice, // minPrice
block.timestamp // timestamp
);

// Make redeem request
uint256 requestId = queue.requestRedeem(shares, user, user);

// Get request details using getUserAtomicRequest
AtomicQueue.AtomicRequest memory request =
queue.getUserAtomicRequest(user, ERC20(address(vault)), ERC20(address(asset)));

// Verify request details
assertEq(request.offerAmount, shares);
assertEq(request.deadline, expectedDeadline);
assertEq(request.atomicPrice, expectedAtomicPrice);
assertEq(request.inSolve, false);

vm.stopPrank();
}

function testRequestRedeemZeroShares() public {
vm.expectRevert(NestBoringVaultModule.InvalidAmount.selector);
queue.requestRedeem(0, user, user);
}

function testRequestRedeemInsufficientBalance(
uint256 shares
) public {
shares = bound(shares, 1e6, 1_000_000e6);

vm.startPrank(user);
vm.expectRevert("TRANSFER_FROM_FAILED");
queue.requestRedeem(shares, user, user);
vm.stopPrank();
}

function testRequestRedeemInsufficientAllowance(
uint256 shares
) public {
shares = bound(shares, 1e6, 1_000_000e6);

// Give user shares but don't approve queue
deal(address(vault), user, shares);

vm.startPrank(user);
vm.expectRevert("TRANSFER_FROM_FAILED");
queue.requestRedeem(shares, user, user);
vm.stopPrank();
}

function testDepositReverts() public {
vm.expectRevert(NestBoringVaultModule.Unimplemented.selector);
queue.deposit(1e6, address(this), address(this));
}

}

0 comments on commit 40d719d

Please sign in to comment.