|
| 1 | +const { expect } = require("chai"); |
| 2 | +const { ethers } = require("hardhat"); |
| 3 | +const { |
| 4 | + setupInputSettlerXCMEscrow, |
| 5 | + createOrderFactory, |
| 6 | + DESTINATION_CHAIN_ID, |
| 7 | + STANDARD_AMOUNT, |
| 8 | + DOUBLE_AMOUNT, |
| 9 | + MOCK_XCM_MESSAGE_1 |
| 10 | +} = require("./helpers/inputSettlerXCMEscrowHelper"); |
| 11 | + |
| 12 | +describe("InputSettlerXCMEscrow - Admin Functions", function () { |
| 13 | + let inputSettlerXCMEscrow; |
| 14 | + let token; |
| 15 | + let owner; |
| 16 | + let user; |
| 17 | + let mockXcm; |
| 18 | + let mockLibrary; |
| 19 | + let baseSettler; |
| 20 | + let chainId; |
| 21 | + let createOrder; |
| 22 | + |
| 23 | + beforeEach(async function () { |
| 24 | + const setup = await setupInputSettlerXCMEscrow(); |
| 25 | + owner = setup.owner; |
| 26 | + user = setup.user; |
| 27 | + mockXcm = setup.mockXcm; |
| 28 | + mockLibrary = setup.mockLibrary; |
| 29 | + baseSettler = setup.baseSettler; |
| 30 | + inputSettlerXCMEscrow = setup.inputSettlerXCMEscrow; |
| 31 | + token = setup.token; |
| 32 | + chainId = setup.chainId; |
| 33 | + createOrder = createOrderFactory(user, token, chainId); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("Deployment", function () { |
| 37 | + it("Should set the right owner", async function () { |
| 38 | + expect(await inputSettlerXCMEscrow.owner()).to.equal(owner.address); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("allowTeleport", function () { |
| 43 | + it("Should allow teleport for a destination and token", async function () { |
| 44 | + const tokenAddress = await token.getAddress(); |
| 45 | + |
| 46 | + await expect(inputSettlerXCMEscrow.allowTeleport(DESTINATION_CHAIN_ID, tokenAddress)) |
| 47 | + .to.emit(inputSettlerXCMEscrow, "TeleportAllowed") |
| 48 | + .withArgs(DESTINATION_CHAIN_ID, tokenAddress); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Should revert if not called by owner", async function () { |
| 52 | + const tokenAddress = await token.getAddress(); |
| 53 | + |
| 54 | + await expect( |
| 55 | + inputSettlerXCMEscrow.connect(user).allowTeleport(DESTINATION_CHAIN_ID, tokenAddress) |
| 56 | + ).to.be.revertedWithCustomError(inputSettlerXCMEscrow, "OwnableUnauthorizedAccount") |
| 57 | + .withArgs(user.address); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("forbidTeleport", function () { |
| 62 | + it("Should forbid teleport for a destination and token", async function () { |
| 63 | + const tokenAddress = await token.getAddress(); |
| 64 | + |
| 65 | + await inputSettlerXCMEscrow.allowTeleport(DESTINATION_CHAIN_ID, tokenAddress); |
| 66 | + |
| 67 | + await expect(inputSettlerXCMEscrow.forbidTeleport(DESTINATION_CHAIN_ID, tokenAddress)) |
| 68 | + .to.emit(inputSettlerXCMEscrow, "TeleportForbidden") |
| 69 | + .withArgs(DESTINATION_CHAIN_ID, tokenAddress); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("setXCMEnabled", function () { |
| 74 | + it("Should fall back to baseSettler when XCM is disabled for valid XCM order", async function () { |
| 75 | + await inputSettlerXCMEscrow.allowTeleport(DESTINATION_CHAIN_ID, await token.getAddress()); |
| 76 | + |
| 77 | + const order = createOrder(); |
| 78 | + |
| 79 | + // Verify XCM path would work normally |
| 80 | + await token.connect(user).approve(await inputSettlerXCMEscrow.getAddress(), ethers.parseEther(DOUBLE_AMOUNT)); |
| 81 | + await mockLibrary.setTeleportMessage(MOCK_XCM_MESSAGE_1); |
| 82 | + await mockXcm.setExecutionSuccess(true); |
| 83 | + |
| 84 | + // Disable XCM globally |
| 85 | + await inputSettlerXCMEscrow.setXCMEnabled(false); |
| 86 | + |
| 87 | + // Should fall back to baseSettler despite valid XCM configuration |
| 88 | + await expect(inputSettlerXCMEscrow.connect(user).open(order)) |
| 89 | + .to.emit(baseSettler, "Open"); |
| 90 | + |
| 91 | + // Verify XCM was not called |
| 92 | + const xcmAllowance = await token.allowance( |
| 93 | + await inputSettlerXCMEscrow.getAddress(), |
| 94 | + await mockXcm.getAddress() |
| 95 | + ); |
| 96 | + expect(xcmAllowance).to.equal(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("Should resume XCM path when re-enabled", async function () { |
| 100 | + await inputSettlerXCMEscrow.allowTeleport(DESTINATION_CHAIN_ID, await token.getAddress()); |
| 101 | + |
| 102 | + const order = createOrder(); |
| 103 | + |
| 104 | + await token.connect(user).approve(await inputSettlerXCMEscrow.getAddress(), ethers.parseEther(STANDARD_AMOUNT)); |
| 105 | + await mockLibrary.setTeleportMessage(MOCK_XCM_MESSAGE_1); |
| 106 | + await mockXcm.setExecutionSuccess(true); |
| 107 | + |
| 108 | + // Disable then re-enable XCM |
| 109 | + await inputSettlerXCMEscrow.setXCMEnabled(false); |
| 110 | + await inputSettlerXCMEscrow.setXCMEnabled(true); |
| 111 | + |
| 112 | + // Should use XCM path again |
| 113 | + await expect(inputSettlerXCMEscrow.connect(user).open(order)) |
| 114 | + .to.emit(mockXcm, "Executed") |
| 115 | + .withArgs(MOCK_XCM_MESSAGE_1); |
| 116 | + }); |
| 117 | + |
| 118 | + it("Should revert if not called by owner", async function () { |
| 119 | + await expect( |
| 120 | + inputSettlerXCMEscrow.connect(user).setXCMEnabled(false) |
| 121 | + ).to.be.revertedWithCustomError(inputSettlerXCMEscrow, "OwnableUnauthorizedAccount") |
| 122 | + .withArgs(user.address); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments