Skip to content

Commit

Permalink
Add ReceiptInformation event check
Browse files Browse the repository at this point in the history
  • Loading branch information
ninokeldishvili committed Jul 11, 2024
1 parent 56cb932 commit 54116ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 111 deletions.
26 changes: 22 additions & 4 deletions test/foundry/src/concrete/receipt/Receipt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import {TestReceipt} from "../../../../../contracts/test/TestReceipt.sol";
import {TestReceiptOwner} from "../../../../../contracts/test/TestReceiptOwner.sol";

contract ReceiptTest is Test {
event ReceiptInformation(address sender, uint256 id, bytes information);

function generateNonEmptyBytes(uint256 maxLength, uint256 seed) internal pure returns (bytes memory) {
uint256 length = (seed % (maxLength - 1)) + 1; // Ensure length is at least 1
bytes memory data = new bytes(length);
for (uint256 i = 0; i < length; i++) {
data[i] = bytes1(uint8(seed % 256)); // Random data
}
return data;
}

function testInitialize() public {
TestReceipt receipt = new TestReceipt();
TestReceiptOwner mockOwner = new TestReceiptOwner();
Expand All @@ -31,7 +42,7 @@ contract ReceiptTest is Test {
}

/// test receipt OwnerMint function
function testOwnerBurn(uint256 fuzzedKeyAlice, uint256 id, uint256 amount, bytes memory data) public {
function testOwnerMint(uint256 fuzzedKeyAlice, uint256 id, uint256 amount, bytes memory data) public {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
amount = bound(amount, 1, type(uint256).max);
Expand All @@ -54,10 +65,13 @@ contract ReceiptTest is Test {
}

/// test receipt OwnerBurn function
function testOwnerMint(uint256 fuzzedKeyAlice, uint256 id, uint256 amount, bytes memory data) public {
function testOwnerBurn(uint256 fuzzedKeyAlice, uint256 id, uint256 amount, uint256 fuzzedReceiptSeed) public {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
amount = bound(amount, 1, type(uint256).max);
id = bound(id, 0, type(uint256).max);

bytes memory fuzzedReceiptInformation = generateNonEmptyBytes(100, fuzzedReceiptSeed); // Generate non-empty bytes

TestReceipt receipt = new TestReceipt();
TestReceiptOwner receiptOwner = new TestReceiptOwner();
Expand All @@ -70,13 +84,17 @@ contract ReceiptTest is Test {
receiptOwner.setTo(alice);

vm.startPrank(alice);
receiptOwner.ownerMint(receipt, alice, id, amount, data);
receiptOwner.ownerMint(receipt, alice, id, amount, fuzzedReceiptInformation);
uint256 receiptBalance = receipt.balanceOf(alice, id);

receiptOwner.setFrom(alice);
receiptOwner.setTo(address(0));

receiptOwner.ownerBurn(receipt, alice, id, receiptBalance, data);
// Set up the event expectation for ReceiptInformation
vm.expectEmit(false, false, false, true);
emit ReceiptInformation(alice, id, fuzzedReceiptInformation);

receiptOwner.ownerBurn(receipt, alice, id, receiptBalance, fuzzedReceiptInformation);

// Check the balance of the minted tokens
assertEq(receipt.balanceOf(alice, id), 0);
Expand Down
107 changes: 0 additions & 107 deletions test/receipt/Receipt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,113 +13,6 @@ import { ReceiptInformationEvent } from "../../typechain-types/contracts/vault/r
const assert = require("assert");

describe("Receipt vault", async function () {
it("Check OwnerBurn burns correct amount", async function () {
const signers = await ethers.getSigners();
const alice = signers[0];

const testErc20 = await ethers.getContractFactory("TestErc20");
const asset = (await testErc20.deploy()) as TestErc20;
await asset.deployed();

const testReceipt = await ethers.getContractFactory("TestReceipt");
const receipt = (await testReceipt.deploy()) as TestReceipt;
await receipt.deployed();

const testReceiptOwner = await ethers.getContractFactory(
"TestReceiptOwner"
);
const receiptOwner = (await testReceiptOwner.deploy()) as TestReceiptOwner;
await receiptOwner.deployed();

await receipt.setOwner(receiptOwner.address);

await receiptOwner.setFrom(ADDRESS_ZERO);
await receiptOwner.setTo(alice.address);

const assets = ethers.BigNumber.from(30);
await asset.transfer(alice.address, assets);
await asset.connect(alice).increaseAllowance(receiptOwner.address, assets);

const receiptId = ethers.BigNumber.from(1);
const toMint = ethers.BigNumber.from(10);
await receiptOwner
.connect(alice)
.ownerMint(receipt.address, alice.address, receiptId, toMint, []);

const toBurn = ethers.BigNumber.from(5);
await receiptOwner.setFrom(alice.address);
await receiptOwner.setTo(ADDRESS_ZERO);

const balanceBefore = await receipt.balanceOf(alice.address, receiptId);

await receiptOwner
.connect(alice)
.ownerBurn(receipt.address, alice.address, receiptId, toBurn, []);

const balanceAfter = await receipt.balanceOf(alice.address, receiptId);

assert(
balanceAfter.eq(balanceBefore.sub(toBurn)),
`Wrong balance. Expected ${balanceBefore.add(
toBurn
)}, got ${balanceAfter}`
);
});
it("Checks ReceiptInformation event is emitted with correct data", async function () {
const signers = await ethers.getSigners();
const alice = signers[0];

const testErc20 = await ethers.getContractFactory("TestErc20");
const asset = (await testErc20.deploy()) as TestErc20;
await asset.deployed();

const testReceipt = await ethers.getContractFactory("TestReceipt");
const receipt = (await testReceipt.deploy()) as TestReceipt;
await receipt.deployed();

const testReceiptOwner = await ethers.getContractFactory(
"TestReceiptOwner"
);
const receiptOwner = (await testReceiptOwner.deploy()) as TestReceiptOwner;
await receiptOwner.deployed();

await receipt.setOwner(receiptOwner.address);

await receiptOwner.setFrom(ADDRESS_ZERO);
await receiptOwner.setTo(alice.address);

const assets = ethers.BigNumber.from(30);
await asset.transfer(alice.address, assets);
await asset.connect(alice).increaseAllowance(receiptOwner.address, assets);

const receiptId = ethers.BigNumber.from(1);
const toMint = ethers.BigNumber.from(10);
await receiptOwner
.connect(alice)
.ownerMint(receipt.address, alice.address, receiptId, toMint, []);

const toBurn = ethers.BigNumber.from(5);
await receiptOwner.setFrom(alice.address);
await receiptOwner.setTo(ADDRESS_ZERO);

const { sender, id, information } = (await getEventArgs(
await receiptOwner
.connect(alice)
.ownerBurn(receipt.address, alice.address, receiptId, toBurn, [1]),
"ReceiptInformation",
receipt
)) as ReceiptInformationEvent["args"];

assert(
sender === alice.address,
`Wrong sender. Expected ${alice.address}, got ${sender}`
);
assert(
information === "0x01",
`Wrong information. Expected 0x01, got ${information}`
);
assert(id.eq(receiptId), `Wrong id. Expected ${receiptId}, got ${id}`);
});
it("OwnerBurn fails while not enough balance to burn", async function () {
const signers = await ethers.getSigners();
const alice = signers[0];
Expand Down

0 comments on commit 54116ca

Please sign in to comment.