diff --git a/packages/evm/test/adapters/AMB/01_AMBAdapter.spec.ts b/packages/evm/test/adapters/AMB/01_AMBAdapter.spec.ts deleted file mode 100644 index 16df4a81..00000000 --- a/packages/evm/test/adapters/AMB/01_AMBAdapter.spec.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { expect } from "chai" -import { ethers, network } from "hardhat" - -const GAS = 10000000 -const DOMAIN_ID = "0x0000000000000000000000000000000000000000000000000000000000007a69" -const ID_ONE = 1 -const ID_TWO = 2 -const HASH_ONE = "0x0000000000000000000000000000000000000000000000000000000000000001" -const HASH_TWO = "0x0000000000000000000000000000000000000000000000000000000000000002" - -const setup = async () => { - await network.provider.request({ method: "hardhat_reset", params: [] }) - const [wallet] = await ethers.getSigners() - const HeaderStorage = await ethers.getContractFactory("HeaderStorage") - const headerStorage = await HeaderStorage.deploy() - const AMB = await ethers.getContractFactory("MockAMB") - const amb = await AMB.deploy() - const AMBAdapter = await ethers.getContractFactory("AMBAdapter") - const ambAdapter = await AMBAdapter.deploy(amb.address, wallet.address, DOMAIN_ID) - return { - wallet, - amb, - headerStorage, - ambAdapter, - } -} - -describe("AMBAdapter", function () { - describe("Constructor", function () { - it("Successfully deploys contract with correct state", async function () { - const { amb, ambAdapter, wallet } = await setup() - expect(await ambAdapter.deployed()) - expect(await ambAdapter.amb()).to.equal(amb.address) - expect(await ambAdapter.reporter()).to.equal(wallet.address) - expect(await ambAdapter.chainId()).to.equal(DOMAIN_ID) - }) - }) - - describe("StoreHashes()", function () { - it("Stores hashes", async function () { - const { amb, ambAdapter } = await setup() - const call = await ambAdapter.populateTransaction.storeHashes([ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO]) - await amb.requireToPassMessage(ambAdapter.address, call.data, GAS) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - }) - it("Overwrites previous hashes", async function () { - const { amb, ambAdapter } = await setup() - let call = await ambAdapter.populateTransaction.storeHashes([ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO]) - await amb.requireToPassMessage(ambAdapter.address, call.data, GAS) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - call = await ambAdapter.populateTransaction.storeHashes([ID_TWO, ID_ONE], [HASH_ONE, HASH_TWO]) - await amb.requireToPassMessage(ambAdapter.address, call.data, GAS) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_TWO) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ONE) - }) - }) - - describe("getHashFromOracle()", function () { - it("Returns 0 if no header is stored", async function () { - const { ambAdapter } = await setup() - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal( - "0x0000000000000000000000000000000000000000000000000000000000000000", - ) - }) - it("Returns stored hash", async function () { - const { amb, ambAdapter } = await setup() - const call = await ambAdapter.populateTransaction.storeHashes([ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO]) - await amb.requireToPassMessage(ambAdapter.address, call.data, GAS) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await ambAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - }) - }) -}) diff --git a/packages/evm/test/adapters/PNetwork/01_PNetworkMessageRelay.spec.ts b/packages/evm/test/adapters/PNetwork/01_PNetworkMessageRelay.spec.ts deleted file mode 100644 index 8bb69bfa..00000000 --- a/packages/evm/test/adapters/PNetwork/01_PNetworkMessageRelay.spec.ts +++ /dev/null @@ -1,185 +0,0 @@ -import { expect } from "chai" -import { ethers } from "hardhat" - -import { ZERO_ADDRESS, deployErc1820Registry, resetNetwork } from "./utils.spec" - -const DOMAIN_ID = "0x0000000000000000000000000000000000000000000000000000000000001" - -describe("PNetworkMessageRelay", function () { - describe("Native Network", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - const Yaho = await ethers.getContractFactory("Yaho") - const yaho = await Yaho.deploy() - await deployErc1820Registry(wallet) - const ERC777Token = await ethers.getContractFactory("ERC777Token") - const erc777Token = await ERC777Token.deploy("ERC777 Token", "E777", []) - const anotherErc777Token = await ERC777Token.deploy("Another ERC777 Token", "A777", []) - const Vault = await ethers.getContractFactory("MockVault") - const vault = await Vault.deploy() - await vault.initialize([erc777Token.address, anotherErc777Token.address], "0x12345678") - - const PNetworkMessageRelay = await ethers.getContractFactory("PNetworkMessageRelay") - const pNetworkMessageRelay = await PNetworkMessageRelay.deploy( - yaho.address, - DOMAIN_ID, - vault.address, - erc777Token.address, - "0x005fe7f9", - ) - - await erc777Token.connect(wallet).send(pNetworkMessageRelay.address, 10000, "0x") - - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - pNetworkMessageRelay.address, - vault.address, - pNetworkMessageRelay.address, - "0x005fe7f9", - ) - - const PingPong = await ethers.getContractFactory("PingPong") - const pingPong = await PingPong.deploy() - const message_1 = { - to: pingPong.address, - toChainId: 1, - data: pingPong.interface.getSighash("ping"), - } - - await yaho.dispatchMessages([message_1, message_1]) - - return { - erc777Token, - anotherErc777Token, - vault, - wallet, - yaho, - pNetworkMessageRelay, - pNetworkAdapter, - message_1, - pingPong, - } - } - - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { pNetworkMessageRelay, erc777Token, vault, yaho } = await setup() - expect(await pNetworkMessageRelay.deployed()) - expect(await pNetworkMessageRelay.TOKEN()).to.equal(erc777Token.address) - expect(await pNetworkMessageRelay.VAULT()).to.equal(vault.address) - expect(await pNetworkMessageRelay.YAHO()).to.equal(yaho.address) - }) - }) - - describe("relayMessages()", function () { - it("Relays message hashes over pNetwork", async function () { - const { pNetworkMessageRelay, pNetworkAdapter, vault, erc777Token, yaho } = await setup() - const ids = [0, 1] - const hashes = await Promise.all(ids.map(async (_id) => await yaho.hashes(_id))) - const expectedUserData = new ethers.utils.AbiCoder().encode(["uint256[]", "bytes32[]"], [ids, hashes]) - await expect(pNetworkMessageRelay.relayMessages(ids, pNetworkAdapter.address)) - .to.emit(pNetworkMessageRelay, "MessageRelayed") - .withArgs(pNetworkMessageRelay.address, 0) - .and.to.emit(vault, "PegIn") - .withArgs( - erc777Token.address, - pNetworkMessageRelay.address, - 1, - pNetworkAdapter.address.replace("0x", "").toLowerCase(), - expectedUserData, - "0x12345678", - "0x005fe7f9", - ) - }) - }) - }) - - describe("Host Network", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - const Yaho = await ethers.getContractFactory("Yaho") - const yaho = await Yaho.deploy() - await deployErc1820Registry(wallet) - const PToken = await ethers.getContractFactory("PToken") - const pToken = await PToken.deploy("pToken", "P", []) - const anotherPToken = await PToken.deploy("Another ERC777 Token", "A777", []) - const Vault = await ethers.getContractFactory("MockVault") - const vault = await Vault.deploy() - await vault.initialize([pToken.address, anotherPToken.address], "0x12345678") - - const PNetworkMessageRelay = await ethers.getContractFactory("PNetworkMessageRelay") - const pNetworkMessageRelay = await PNetworkMessageRelay.deploy( - yaho.address, - DOMAIN_ID, - ZERO_ADDRESS, - pToken.address, - "0x005fe7f9", - ) - - await pToken.connect(wallet).send(pNetworkMessageRelay.address, 10000, "0x") - - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - pNetworkMessageRelay.address, - vault.address, - pNetworkMessageRelay.address, - "0x005fe7f9", - ) - - const PingPong = await ethers.getContractFactory("PingPong") - const pingPong = await PingPong.deploy() - const message_1 = { - to: pingPong.address, - toChainId: 1, - data: pingPong.interface.getSighash("ping"), - } - - await yaho.dispatchMessages([message_1, message_1]) - - return { - pToken, - anotherPToken, - vault, - wallet, - yaho, - pNetworkMessageRelay, - pNetworkAdapter, - } - } - - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { pNetworkMessageRelay, pToken, yaho } = await setup() - expect(await pNetworkMessageRelay.deployed()) - expect(await pNetworkMessageRelay.TOKEN()).to.equal(pToken.address) - expect(await pNetworkMessageRelay.VAULT()).to.equal(ZERO_ADDRESS) - expect(await pNetworkMessageRelay.YAHO()).to.equal(yaho.address) - }) - }) - - describe("relayMessages()", function () { - it("Relays message hashes over pNetwork", async function () { - const { pNetworkMessageRelay, pNetworkAdapter, pToken, yaho } = await setup() - const ids = [0, 1] - const hashes = await Promise.all(ids.map(async (_id) => await yaho.hashes(_id))) - const expectedUserData = new ethers.utils.AbiCoder().encode(["uint256[]", "bytes32[]"], [ids, hashes]) - await expect(pNetworkMessageRelay.relayMessages(ids, pNetworkAdapter.address)) - .to.emit(pNetworkMessageRelay, "MessageRelayed") - .withArgs(pNetworkMessageRelay.address, 0) - .and.to.emit(pToken, "Redeem") - .withArgs( - pNetworkMessageRelay.address, - 1, - pNetworkAdapter.address.replace("0x", "").toLowerCase(), - expectedUserData, - "0x87654321", - "0x005fe7f9", - ) - }) - }) - }) -}) diff --git a/packages/evm/test/adapters/PNetwork/02_PNetworkHeaderReporter.spec.ts b/packages/evm/test/adapters/PNetwork/02_PNetworkHeaderReporter.spec.ts deleted file mode 100644 index af1984cb..00000000 --- a/packages/evm/test/adapters/PNetwork/02_PNetworkHeaderReporter.spec.ts +++ /dev/null @@ -1,173 +0,0 @@ -import { expect } from "chai" -import { ethers } from "hardhat" - -import { ZERO_ADDRESS, deployErc1820Registry, resetNetwork } from "./utils.spec" - -const DOMAIN_ID = "0x0000000000000000000000000000000000000000000000000000000000001" - -describe("PNetworkHeaderReporter", function () { - describe("Native Network", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - const HeaderStorage = await ethers.getContractFactory("HeaderStorage") - const headerStorage = await HeaderStorage.deploy() - await deployErc1820Registry(wallet) - const ERC777Token = await ethers.getContractFactory("ERC777Token") - const erc777Token = await ERC777Token.deploy("ERC777 Token", "E777", []) - const anotherErc777Token = await ERC777Token.deploy("Another ERC777 Token", "A777", []) - const Vault = await ethers.getContractFactory("MockVault") - const vault = await Vault.deploy() - await vault.initialize([erc777Token.address, anotherErc777Token.address], "0x12345678") - - const PNetworkHeaderReporter = await ethers.getContractFactory("PNetworkHeaderReporter") - const pNetworkHeaderReporter = await PNetworkHeaderReporter.deploy( - headerStorage.address, - DOMAIN_ID, - vault.address, - erc777Token.address, - "0x005fe7f9", - ) - - await erc777Token.connect(wallet).send(pNetworkHeaderReporter.address, 10000, "0x") - - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - pNetworkHeaderReporter.address, - vault.address, - pNetworkHeaderReporter.address, - "0x005fe7f9", - ) - - return { - erc777Token, - anotherErc777Token, - vault, - wallet, - headerStorage, - pNetworkHeaderReporter, - pNetworkAdapter, - } - } - - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { pNetworkHeaderReporter, erc777Token, vault, headerStorage } = await setup() - expect(await pNetworkHeaderReporter.deployed()) - expect(await pNetworkHeaderReporter.TOKEN()).to.equal(erc777Token.address) - expect(await pNetworkHeaderReporter.VAULT()).to.equal(vault.address) - expect(await pNetworkHeaderReporter.HEADER_STORAGE()).to.equal(headerStorage.address) - }) - }) - - describe("reportHeaders()", function () { - it("Reports headers over pNetwork", async function () { - const { pNetworkHeaderReporter, pNetworkAdapter, vault, erc777Token, headerStorage } = await setup() - const blockIds = [0, 1] - const blocks = await Promise.all(blockIds.map((_id) => ethers.provider._getBlock(_id))) - const hashes = blocks.map((_block) => _block.hash) - const expectedUserData = new ethers.utils.AbiCoder().encode(["uint256[]", "bytes32[]"], [blockIds, hashes]) - await expect(pNetworkHeaderReporter.reportHeaders(blockIds, pNetworkAdapter.address)) - .to.emit(pNetworkHeaderReporter, "HeaderReported") - .withArgs(pNetworkHeaderReporter.address, blockIds[0], hashes[0]) - .and.to.emit(pNetworkHeaderReporter, "HeaderReported") - .withArgs(pNetworkHeaderReporter.address, blockIds[1], hashes[1]) - .and.to.emit(vault, "PegIn") - .withArgs( - erc777Token.address, - pNetworkHeaderReporter.address, - 1, - pNetworkAdapter.address.replace("0x", "").toLowerCase(), - expectedUserData, - "0x12345678", - "0x005fe7f9", - ) - expect(await headerStorage.headers(blockIds[0])).to.equal(hashes[0]) - expect(await headerStorage.headers(blockIds[1])).to.equal(hashes[1]) - }) - }) - }) - - describe("Host Network", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - const HeaderStorage = await ethers.getContractFactory("HeaderStorage") - const headerStorage = await HeaderStorage.deploy() - await deployErc1820Registry(wallet) - const PToken = await ethers.getContractFactory("PToken") - const pToken = await PToken.deploy("pToken", "P", []) - const anotherPToken = await PToken.deploy("Another ERC777 Token", "A777", []) - const Vault = await ethers.getContractFactory("MockVault") - const vault = await Vault.deploy() - await vault.initialize([pToken.address, anotherPToken.address], "0x12345678") - - const PNetworkHeaderReporter = await ethers.getContractFactory("PNetworkHeaderReporter") - const pNetworkHeaderReporter = await PNetworkHeaderReporter.deploy( - headerStorage.address, - DOMAIN_ID, - ZERO_ADDRESS, - pToken.address, - "0x005fe7f9", - ) - - await pToken.connect(wallet).send(pNetworkHeaderReporter.address, 10000, "0x") - - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - pNetworkHeaderReporter.address, - vault.address, - pNetworkHeaderReporter.address, - "0x005fe7f9", - ) - - return { - pToken, - anotherPToken, - vault, - wallet, - headerStorage, - pNetworkHeaderReporter, - pNetworkAdapter, - } - } - - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { pNetworkHeaderReporter, pToken, headerStorage } = await setup() - expect(await pNetworkHeaderReporter.deployed()) - expect(await pNetworkHeaderReporter.TOKEN()).to.equal(pToken.address) - expect(await pNetworkHeaderReporter.VAULT()).to.equal(ZERO_ADDRESS) - expect(await pNetworkHeaderReporter.HEADER_STORAGE()).to.equal(headerStorage.address) - }) - }) - - describe("reportHeaders()", function () { - it("Reports headers over pNetwork", async function () { - const { pNetworkHeaderReporter, pNetworkAdapter, pToken, headerStorage } = await setup() - const blockIds = [0, 1] - const blocks = await Promise.all(blockIds.map((_id) => ethers.provider._getBlock(_id))) - const hashes = blocks.map((_block) => _block.hash) - const expectedUserData = new ethers.utils.AbiCoder().encode(["uint256[]", "bytes32[]"], [blockIds, hashes]) - await expect(pNetworkHeaderReporter.reportHeaders(blockIds, pNetworkAdapter.address)) - .to.emit(pNetworkHeaderReporter, "HeaderReported") - .withArgs(pNetworkHeaderReporter.address, blockIds[0], hashes[0]) - .and.to.emit(pNetworkHeaderReporter, "HeaderReported") - .withArgs(pNetworkHeaderReporter.address, blockIds[1], hashes[1]) - .and.to.emit(pToken, "Redeem") - .withArgs( - pNetworkHeaderReporter.address, - 1, - pNetworkAdapter.address.replace("0x", "").toLowerCase(), - expectedUserData, - "0x87654321", - "0x005fe7f9", - ) - expect(await headerStorage.headers(blockIds[0])).to.equal(hashes[0]) - expect(await headerStorage.headers(blockIds[1])).to.equal(hashes[1]) - }) - }) - }) -}) diff --git a/packages/evm/test/adapters/PNetwork/03_PNetworkAdapter.spec.ts b/packages/evm/test/adapters/PNetwork/03_PNetworkAdapter.spec.ts deleted file mode 100644 index 75012675..00000000 --- a/packages/evm/test/adapters/PNetwork/03_PNetworkAdapter.spec.ts +++ /dev/null @@ -1,320 +0,0 @@ -import { expect } from "chai" -import { ethers } from "hardhat" - -import { ZERO_ADDRESS, deployErc1820Registry, resetNetwork } from "./utils.spec" - -const REPORTER_ADDRESS = "0xd5e099c71b797516c10ed0f0d895f429c2781142" -const DOMAIN_ID = "0x0000000000000000000000000000000000000000000000000000000000001" -const ID_ONE = 1 -const ID_TWO = 2 -const HASH_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000" -const HASH_ONE = "0x0000000000000000000000000000000000000000000000000000000000000001" -const HASH_TWO = "0x0000000000000000000000000000000000000000000000000000000000000002" - -const encodeToMetadata = (userData: string, originChainId: string, sender: string, version = 3) => - new ethers.utils.AbiCoder().encode( - ["bytes1", "bytes", "bytes4", "address"], - [version, userData, originChainId, sender], - ) - -describe("PNetworkAdapter", function () { - describe("Host Blockchain", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - await deployErc1820Registry(wallet) - const PToken = await ethers.getContractFactory("PToken") - const pToken = await PToken.deploy("pToken", "P", []) - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - REPORTER_ADDRESS, - ZERO_ADDRESS, - pToken.address, - "0x005fe7f9", - ) - return { - wallet, - pNetworkAdapter, - pToken, - } - } - - describe("Constructor", function () { - it("Successfully deploys contract with correct state", async function () { - const { pNetworkAdapter, pToken } = await setup() - - expect(await pNetworkAdapter.deployed()) - expect(await pNetworkAdapter.TOKEN()).to.equal(pToken.address) - expect(await pNetworkAdapter.VAULT()).to.equal(ZERO_ADDRESS) - }) - }) - - describe("StoreHashes()", function () { - it("Should store hashes", async function () { - const { pNetworkAdapter, wallet, pToken } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await pToken.connect(wallet).mint(pNetworkAdapter.address, 1000, data, "0x") - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - }) - - it("Should not store hashes when receiving another token", async function () { - const { pNetworkAdapter, wallet, pToken } = await setup() - - const PToken = await ethers.getContractFactory("PToken") - const fakePToken = await PToken.deploy("pToken", "P", []) - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await expect(fakePToken.connect(wallet).mint(pNetworkAdapter.address, 1000, data, "0x")) - .to.be.revertedWithCustomError(pNetworkAdapter, "InvalidToken") - .withArgs(fakePToken.address, pToken.address) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Should not store hashes when tokens are not minted", async function () { - const { pNetworkAdapter, wallet, pToken } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await expect(pToken.connect(wallet).send(pNetworkAdapter.address, 1000, data)) - .to.be.revertedWithCustomError(pNetworkAdapter, "InvalidSender") - .withArgs(wallet.address, ZERO_ADDRESS) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Overwrites previous hashes", async function () { - const { pNetworkAdapter, wallet, pToken } = await setup() - const coder = new ethers.utils.AbiCoder() - let userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - let data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await pToken.connect(wallet).mint(pNetworkAdapter.address, 1000, data, "0x") - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_TWO, ID_ONE], - [HASH_ONE, HASH_TWO], - ], - ) - data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await pToken.connect(wallet).mint(pNetworkAdapter.address, 1000, data, "0x") - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_TWO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ONE) - }) - - it("Returns 0 if no header is stored", async function () { - const { pNetworkAdapter } = await setup() - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - }) - }) - }) - - describe("Native Blockchain", () => { - const setup = async () => { - await resetNetwork() - const [wallet] = await ethers.getSigners() - await deployErc1820Registry(wallet) - const ERC777Token = await ethers.getContractFactory("ERC777Token") - const erc777Token = await ERC777Token.deploy("ERC777 Token", "E777", []) - const anotherErc777Token = await ERC777Token.deploy("Another ERC777 Token", "A777", []) - const Vault = await ethers.getContractFactory("MockVault") - const vault = await Vault.deploy() - await vault.initialize([erc777Token.address, anotherErc777Token.address], "0x12345678") - - const PNetworkAdapter = await ethers.getContractFactory("PNetworkAdapter") - const pNetworkAdapter = await PNetworkAdapter.deploy( - DOMAIN_ID, - REPORTER_ADDRESS, - vault.address, - erc777Token.address, - "0x005fe7f9", - ) - - const coder = new ethers.utils.AbiCoder() - const data = coder.encode( - ["bytes32", "string", "bytes4"], - [ethers.utils.keccak256(ethers.utils.toUtf8Bytes("ERC777-pegIn")), "destination-address", "0x87654321"], - ) - await expect(erc777Token.connect(wallet).send(vault.address, 100, data)).to.emit(vault, "PegIn") - await expect(anotherErc777Token.connect(wallet).send(vault.address, 100, data)).to.emit(vault, "PegIn") - return { - wallet, - vault, - pNetworkAdapter, - erc777Token, - anotherErc777Token, - } - } - - describe("Constructor", function () { - it("Successfully deploys contract with correct state", async function () { - const { pNetworkAdapter, erc777Token, vault } = await setup() - expect(await pNetworkAdapter.deployed()) - expect(await pNetworkAdapter.TOKEN()).to.equal(erc777Token.address) - expect(await pNetworkAdapter.VAULT()).to.equal(vault.address) - }) - }) - - describe("StoreHashes()", function () { - it("Stores hashes", async function () { - const { pNetworkAdapter, wallet, vault, erc777Token } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await vault.connect(wallet).pegOut(pNetworkAdapter.address, erc777Token.address, 100, data) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - }) - - it("Should not store hashes when receiving another token", async function () { - const { pNetworkAdapter, wallet, vault, erc777Token, anotherErc777Token } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await expect(vault.connect(wallet).pegOut(pNetworkAdapter.address, anotherErc777Token.address, 100, data)) - .to.be.revertedWithCustomError(pNetworkAdapter, "InvalidToken") - .withArgs(anotherErc777Token.address, erc777Token.address) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Should not store hashes when tokens are not sent by the vault", async function () { - const { pNetworkAdapter, wallet, erc777Token, vault } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await expect(erc777Token.connect(wallet).send(pNetworkAdapter.address, 100, data)) - .to.be.revertedWithCustomError(pNetworkAdapter, "InvalidSender") - .withArgs(wallet.address, vault.address) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Should not store hashes when data is received from another chain", async function () { - const { pNetworkAdapter, wallet, vault, erc777Token } = await setup() - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x00e4b170", REPORTER_ADDRESS) - await expect(vault.connect(wallet).pegOut(pNetworkAdapter.address, erc777Token.address, 100, data)) - .to.be.revertedWithCustomError(pNetworkAdapter, "InvalidNetworkId") - .withArgs("0x00e4b170", "0x005fe7f9") - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Should not store hashes when data originated from another address", async function () { - const { pNetworkAdapter, wallet, vault, erc777Token } = await setup() - const WRONG_REPORTER_ADDRESS = "0xa5e099c71b797516c10ed0f0d895f429c2781142" - - const coder = new ethers.utils.AbiCoder() - const userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - const data = encodeToMetadata(userData, "0x005fe7f9", WRONG_REPORTER_ADDRESS) - await expect(vault.connect(wallet).pegOut(pNetworkAdapter.address, erc777Token.address, 100, data)) - .to.be.revertedWithCustomError(pNetworkAdapter, "UnauthorizedPNetworkReceive") - .withArgs() - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ZERO) - }) - - it("Overwrites previous hashes", async function () { - const { pNetworkAdapter, wallet, erc777Token, vault } = await setup() - const coder = new ethers.utils.AbiCoder() - let userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_ONE, ID_TWO], - [HASH_ONE, HASH_TWO], - ], - ) - let data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await vault.connect(wallet).pegOut(pNetworkAdapter.address, erc777Token.address, 50, data) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - userData = coder.encode( - ["uint256[]", "bytes32[]"], - [ - [ID_TWO, ID_ONE], - [HASH_ONE, HASH_TWO], - ], - ) - data = encodeToMetadata(userData, "0x005fe7f9", REPORTER_ADDRESS) - await vault.connect(wallet).pegOut(pNetworkAdapter.address, erc777Token.address, 50, data) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_TWO) - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ONE) - }) - - it("Returns 0 if no header is stored", async function () { - const { pNetworkAdapter } = await setup() - expect(await pNetworkAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ZERO) - }) - }) - }) -}) diff --git a/packages/evm/test/adapters/PNetwork/utils.spec.ts b/packages/evm/test/adapters/PNetwork/utils.spec.ts deleted file mode 100644 index ad1cadfb..00000000 --- a/packages/evm/test/adapters/PNetwork/utils.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers" -import { ethers, network } from "hardhat" - -const ERC1820_DEPLOYER = "0xa990077c3205cbDf861e17Fa532eeB069cE9fF96" -const ERC1820_PAYLOAD = - "0xf90a388085174876e800830c35008080b909e5608060405234801561001057600080fd5b506109c5806100206000396000f3fe608060405234801561001057600080fd5b50600436106100a5576000357c010000000000000000000000000000000000000000000000000000000090048063a41e7d5111610078578063a41e7d51146101d4578063aabbb8ca1461020a578063b705676514610236578063f712f3e814610280576100a5565b806329965a1d146100aa5780633d584063146100e25780635df8122f1461012457806365ba36c114610152575b600080fd5b6100e0600480360360608110156100c057600080fd5b50600160a060020a038135811691602081013591604090910135166102b6565b005b610108600480360360208110156100f857600080fd5b5035600160a060020a0316610570565b60408051600160a060020a039092168252519081900360200190f35b6100e06004803603604081101561013a57600080fd5b50600160a060020a03813581169160200135166105bc565b6101c26004803603602081101561016857600080fd5b81019060208101813564010000000081111561018357600080fd5b82018360208201111561019557600080fd5b803590602001918460018302840111640100000000831117156101b757600080fd5b5090925090506106b3565b60408051918252519081900360200190f35b6100e0600480360360408110156101ea57600080fd5b508035600160a060020a03169060200135600160e060020a0319166106ee565b6101086004803603604081101561022057600080fd5b50600160a060020a038135169060200135610778565b61026c6004803603604081101561024c57600080fd5b508035600160a060020a03169060200135600160e060020a0319166107ef565b604080519115158252519081900360200190f35b61026c6004803603604081101561029657600080fd5b508035600160a060020a03169060200135600160e060020a0319166108aa565b6000600160a060020a038416156102cd57836102cf565b335b9050336102db82610570565b600160a060020a031614610339576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b6103428361092a565b15610397576040805160e560020a62461bcd02815260206004820152601a60248201527f4d757374206e6f7420626520616e204552433136352068617368000000000000604482015290519081900360640190fd5b600160a060020a038216158015906103b85750600160a060020a0382163314155b156104ff5760405160200180807f455243313832305f4143434550545f4d4147494300000000000000000000000081525060140190506040516020818303038152906040528051906020012082600160a060020a031663249cb3fa85846040518363ffffffff167c01000000000000000000000000000000000000000000000000000000000281526004018083815260200182600160a060020a0316600160a060020a031681526020019250505060206040518083038186803b15801561047e57600080fd5b505afa158015610492573d6000803e3d6000fd5b505050506040513d60208110156104a857600080fd5b5051146104ff576040805160e560020a62461bcd02815260206004820181905260248201527f446f6573206e6f7420696d706c656d656e742074686520696e74657266616365604482015290519081900360640190fd5b600160a060020a03818116600081815260208181526040808320888452909152808220805473ffffffffffffffffffffffffffffffffffffffff19169487169485179055518692917f93baa6efbd2244243bfee6ce4cfdd1d04fc4c0e9a786abd3a41313bd352db15391a450505050565b600160a060020a03818116600090815260016020526040812054909116151561059a5750806105b7565b50600160a060020a03808216600090815260016020526040902054165b919050565b336105c683610570565b600160a060020a031614610624576040805160e560020a62461bcd02815260206004820152600f60248201527f4e6f7420746865206d616e616765720000000000000000000000000000000000604482015290519081900360640190fd5b81600160a060020a031681600160a060020a0316146106435780610646565b60005b600160a060020a03838116600081815260016020526040808220805473ffffffffffffffffffffffffffffffffffffffff19169585169590951790945592519184169290917f605c2dbf762e5f7d60a546d42e7205dcb1b011ebc62a61736a57c9089d3a43509190a35050565b600082826040516020018083838082843780830192505050925050506040516020818303038152906040528051906020012090505b92915050565b6106f882826107ef565b610703576000610705565b815b600160a060020a03928316600081815260208181526040808320600160e060020a031996909616808452958252808320805473ffffffffffffffffffffffffffffffffffffffff19169590971694909417909555908152600284528181209281529190925220805460ff19166001179055565b600080600160a060020a038416156107905783610792565b335b905061079d8361092a565b156107c357826107ad82826108aa565b6107b85760006107ba565b815b925050506106e8565b600160a060020a0390811660009081526020818152604080832086845290915290205416905092915050565b6000808061081d857f01ffc9a70000000000000000000000000000000000000000000000000000000061094c565b909250905081158061082d575080155b1561083d576000925050506106e8565b61084f85600160e060020a031961094c565b909250905081158061086057508015155b15610870576000925050506106e8565b61087a858561094c565b909250905060018214801561088f5750806001145b1561089f576001925050506106e8565b506000949350505050565b600160a060020a0382166000908152600260209081526040808320600160e060020a03198516845290915281205460ff1615156108f2576108eb83836107ef565b90506106e8565b50600160a060020a03808316600081815260208181526040808320600160e060020a0319871684529091529020549091161492915050565b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff161590565b6040517f01ffc9a7000000000000000000000000000000000000000000000000000000008082526004820183905260009182919060208160248189617530fa90519096909550935050505056fea165627a7a72305820377f4a2d4301ede9949f163f319021a6e9c687c292a5e2b2c4734c126b524e6c00291ba01820182018201820182018201820182018201820182018201820182018201820a01820182018201820182018201820182018201820182018201820182018201820" - -export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" - -export const resetNetwork = async () => { - await network.provider.request({ method: "hardhat_reset", params: [] }) -} - -export const deployErc1820Registry = async (wallet: SignerWithAddress) => { - // deploy erc1820 registry - await ethers.provider.send("eth_sendTransaction", [ - { - from: wallet.address, - to: ERC1820_DEPLOYER, - value: "0x11c37937e080000", - }, - ]) - await ethers.provider.send("eth_sendRawTransaction", [ERC1820_PAYLOAD]) -} diff --git a/packages/evm/test/adapters/Sygma/01_SygmaAdapter.spec.ts b/packages/evm/test/adapters/Sygma/01_SygmaAdapter.spec.ts deleted file mode 100644 index d5ada782..00000000 --- a/packages/evm/test/adapters/Sygma/01_SygmaAdapter.spec.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { expect } from "chai" -import { ethers, network } from "hardhat" - -const DOMAIN_ID = 5 -const ID_ONE = 1 -const ID_TWO = 2 -const HASH_ONE = "0x0000000000000000000000000000000000000000000000000000000000000001" -const HASH_TWO = "0x0000000000000000000000000000000000000000000000000000000000000002" - -const setup = async () => { - await network.provider.request({ method: "hardhat_reset", params: [] }) - const signers = await ethers.getSigners() - const admin = signers[0] - const reporter = signers[1] - const handler = signers[2] - const otherAddress = signers[3] - const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") - const sygmaAdapter = await SygmaAdapter.deploy(handler.address) - return { - admin, - reporter, - handler, - otherAddress, - sygmaAdapter, - } -} - -describe("SygmaAdapter", function () { - describe("Constructor", function () { - it("Successfully deploys contract with correct state", async function () { - const { handler, sygmaAdapter } = await setup() - expect(await sygmaAdapter.deployed()) - expect(await sygmaAdapter._handler()).to.equal(handler.address) - }) - }) - - describe("setReporter()", function () { - it("Enables the reporter and sets its chainID", async function () { - const { reporter, sygmaAdapter } = await setup() - expect(await sygmaAdapter.deployed()) - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - const result = await sygmaAdapter.reporters(reporter.address) - expect(result[0].toNumber()).to.equal(DOMAIN_ID) - expect(result[1]).to.equal(true) - }) - - it("Disables the reporter", async function () { - const { reporter, sygmaAdapter } = await setup() - expect(await sygmaAdapter.deployed()) - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - let result = await sygmaAdapter.reporters(reporter.address) - expect(result[1]).to.equal(true) - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, false)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, false) - result = await sygmaAdapter.reporters(reporter.address) - expect(result[1]).to.equal(false) - }) - - it("Doesn't set the reporter if the sender is unauthorized", async function () { - const { reporter, sygmaAdapter, otherAddress } = await setup() - expect(await sygmaAdapter.deployed()) - await expect( - sygmaAdapter.connect(otherAddress).setReporter(reporter.address, DOMAIN_ID, true), - ).to.be.revertedWithCustomError(sygmaAdapter, "Unauthorized") - }) - }) - - describe("StoreHashes()", function () { - it("Stores hashes", async function () { - const { handler, reporter, sygmaAdapter } = await setup() - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - await expect(sygmaAdapter.connect(handler).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO])) - .to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_ONE, HASH_ONE) - .and.to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_TWO, HASH_TWO) - expect(await sygmaAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - }) - - it("Reverts if array lengths mismatch", async function () { - const { handler, reporter, sygmaAdapter } = await setup() - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - await expect( - sygmaAdapter.connect(handler).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_ONE]), - ).to.be.revertedWithCustomError(sygmaAdapter, "ArrayLengthMismatch") - }) - - it("Reverts if sender is not the authorized handler", async function () { - const { otherAddress, reporter, sygmaAdapter } = await setup() - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - await expect( - sygmaAdapter.connect(otherAddress).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO]), - ) - .to.be.revertedWithCustomError(sygmaAdapter, "InvalidHandler") - .withArgs(otherAddress.address) - }) - - it("Reverts if the reporter is not enabled", async function () { - const { handler, reporter, sygmaAdapter } = await setup() - await expect(sygmaAdapter.connect(handler).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO])) - .to.be.revertedWithCustomError(sygmaAdapter, "InvalidReporter") - .withArgs(reporter.address) - }) - - it("Overwrites previous hashes", async function () { - const { handler, reporter, sygmaAdapter } = await setup() - await expect(sygmaAdapter.setReporter(reporter.address, DOMAIN_ID, true)) - .to.emit(sygmaAdapter, "ReporterSet") - .withArgs(reporter.address, DOMAIN_ID, true) - await expect(sygmaAdapter.connect(handler).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_ONE, HASH_TWO])) - .to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_ONE, HASH_ONE) - .and.to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_TWO, HASH_TWO) - expect(await sygmaAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_ONE) - expect(await sygmaAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_TWO) - - await expect(sygmaAdapter.connect(handler).storeHashes(reporter.address, [ID_ONE, ID_TWO], [HASH_TWO, HASH_ONE])) - .to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_ONE, HASH_TWO) - .and.to.emit(sygmaAdapter, "HashStored") - .withArgs(ID_TWO, HASH_ONE) - expect(await sygmaAdapter.getHashFromOracle(DOMAIN_ID, ID_ONE)).to.equal(HASH_TWO) - expect(await sygmaAdapter.getHashFromOracle(DOMAIN_ID, ID_TWO)).to.equal(HASH_ONE) - }) - }) -}) diff --git a/packages/evm/test/adapters/Sygma/02_SygmaHeaderReporter.spec.ts b/packages/evm/test/adapters/Sygma/02_SygmaHeaderReporter.spec.ts deleted file mode 100644 index f41de8ce..00000000 --- a/packages/evm/test/adapters/Sygma/02_SygmaHeaderReporter.spec.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { mine } from "@nomicfoundation/hardhat-network-helpers" -import { expect } from "chai" -import { ethers, network } from "hardhat" - -const DOMAIN_ID = 5 -const resourceID = "0x0000000000000000000000000000000000000000000000000000000000000500" - -const setup = async () => { - await network.provider.request({ method: "hardhat_reset", params: [] }) - const signers = await ethers.getSigners() - const adapter = signers[1].address - const otherAddress = signers[2].address - const HeaderStorage = await ethers.getContractFactory("HeaderStorage") - const headerStorage = await HeaderStorage.deploy() - const SygmaBridge = await ethers.getContractFactory("MockSygmaBridge") - const sygmaBridge = await SygmaBridge.deploy() - const SygmaHeaderReporter = await ethers.getContractFactory("SygmaHeaderReporter") - // IBridge bridge, HeaderStorage headerStorage, bytes32 resourceID, uint8 defaultDestinationDomainID, address defaultSygmaAdapter - const sygmaHeaderReporter = await SygmaHeaderReporter.deploy( - sygmaBridge.address, - headerStorage.address, - resourceID, - DOMAIN_ID, - adapter, - ) - await mine(10) - return { - adapter, - otherAddress, - headerStorage, - sygmaBridge, - sygmaHeaderReporter, - } -} - -const prepareDepositData = async (reporterAddress: string, ids: string[], hashes: string[], adapter: string) => { - const abiCoder = ethers.utils.defaultAbiCoder - const executionData = abiCoder - .encode(["address", "uint256[]", "bytes32[]"], [ethers.constants.AddressZero, ids, hashes]) - .substring(66) - - const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") - const functionSig = SygmaAdapter.interface.getSighash("storeHashes") - - // bytes memory depositData = abi.encodePacked( - // uint256(0), - // uint16(4), - // IDepositAdapterTarget(address(0)).execute.selector, - // uint8(20), - // _targetDepositAdapter, - // uint8(20), - // _depositorAddress, - // abi.encode(depositContractCalldata) - // ); - - const depositData = - ethers.utils.hexZeroPad("0xe7ef0", 32) + - "0004" + - functionSig.substring(2) + - "14" + - adapter.toLowerCase().substring(2) + - "14" + - reporterAddress.toLowerCase().substring(2) + - executionData - return depositData -} - -describe("SygmaHeaderReporter", function () { - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { sygmaBridge, headerStorage, adapter, sygmaHeaderReporter } = await setup() - expect(await sygmaHeaderReporter.deployed()) - expect(await sygmaHeaderReporter._bridge()).to.equal(sygmaBridge.address) - expect(await sygmaHeaderReporter._headerStorage()).to.equal(headerStorage.address) - expect(await sygmaHeaderReporter._resourceID()).to.equal(resourceID) - expect(await sygmaHeaderReporter._defaultDestinationDomainID()).to.equal(DOMAIN_ID) - expect(await sygmaHeaderReporter._defaultSygmaAdapter()).to.equal(adapter) - }) - }) - - describe("reportHeaders()", function () { - it("Reports headers to Sygma to default domain", async function () { - const { sygmaHeaderReporter, adapter, sygmaBridge, headerStorage } = await setup() - const block = await ethers.provider._getBlock(9) - const block2 = await ethers.provider._getBlock(8) - const depositData = await prepareDepositData( - sygmaHeaderReporter.address, - [9, 8], - [block.hash, block2.hash], - adapter, - ) - - await expect(sygmaHeaderReporter.reportHeaders([9, 8], "0x00")) - .to.emit(sygmaHeaderReporter, "HeaderReported") - .withArgs(sygmaHeaderReporter.address, 9, block.hash) - .and.to.emit(sygmaHeaderReporter, "HeaderReported") - .withArgs(sygmaHeaderReporter.address, 8, block2.hash) - .and.to.emit(sygmaBridge, "Deposit") - // (destinationDomainID, resourceID, 1, msg.sender, depositData, feeData); - .withArgs(DOMAIN_ID, resourceID, 1, sygmaHeaderReporter.address, depositData, "0x00") - expect(await headerStorage.headers(9)).to.equal(block.hash) - expect(await headerStorage.headers(8)).to.equal(block2.hash) - }) - }) - - describe("reportHeadersToDomain()", function () { - it("Reports headers to Sygma to specified domain", async function () { - const { sygmaHeaderReporter, otherAddress, sygmaBridge, headerStorage } = await setup() - const otherDomain = 4 - const block = await ethers.provider._getBlock(9) - const block2 = await ethers.provider._getBlock(8) - const depositData = await prepareDepositData( - sygmaHeaderReporter.address, - [9, 8], - [block.hash, block2.hash], - otherAddress, - ) - - await expect(sygmaHeaderReporter.reportHeadersToDomain([9, 8], otherAddress, otherDomain, "0x00")) - .to.emit(sygmaHeaderReporter, "HeaderReported") - .withArgs(sygmaHeaderReporter.address, 9, block.hash) - .and.to.emit(sygmaHeaderReporter, "HeaderReported") - .withArgs(sygmaHeaderReporter.address, 8, block2.hash) - .and.to.emit(sygmaBridge, "Deposit") - // (destinationDomainID, resourceID, 1, msg.sender, depositData, feeData); - .withArgs(otherDomain, resourceID, 1, sygmaHeaderReporter.address, depositData, "0x00") - expect(await headerStorage.headers(9)).to.equal(block.hash) - expect(await headerStorage.headers(8)).to.equal(block2.hash) - }) - }) -}) diff --git a/packages/evm/test/adapters/Sygma/03_SygmaMessageRelay.spec.ts b/packages/evm/test/adapters/Sygma/03_SygmaMessageRelay.spec.ts deleted file mode 100644 index bc26eacb..00000000 --- a/packages/evm/test/adapters/Sygma/03_SygmaMessageRelay.spec.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { expect } from "chai" -import { ethers, network } from "hardhat" - -const DOMAIN_ID = 5 -const resourceID = "0x0000000000000000000000000000000000000000000000000000000000000500" - -const setup = async () => { - await network.provider.request({ method: "hardhat_reset", params: [] }) - const signers = await ethers.getSigners() - const sender = signers[0].address - const otherAddress = signers[2].address - const Yaho = await ethers.getContractFactory("Yaho") - const yaho = await Yaho.deploy() - const SygmaBridge = await ethers.getContractFactory("MockSygmaBridge") - const sygmaBridge = await SygmaBridge.deploy() - const SygmaMessageRelay = await ethers.getContractFactory("SygmaMessageRelay") - const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") - const sygmaAdapter = await SygmaAdapter.deploy(sygmaBridge.address) - // IBridge bridge, HeaderStorage headerStorage, bytes32 resourceID, uint8 defaultDestinationDomainID, address defaultSygmaAdapter - const sygmaMessageRelay = await SygmaMessageRelay.deploy( - sygmaBridge.address, - yaho.address, - resourceID, - DOMAIN_ID, - sygmaAdapter.address, - ) - - await sygmaAdapter.setReporter(sygmaMessageRelay.address, DOMAIN_ID, true) - - const PingPong = await ethers.getContractFactory("PingPong") - const pingPong = await PingPong.deploy() - const message_1 = { - to: pingPong.address, - toChainId: 1, - data: pingPong.interface.getSighash("ping"), - } - await yaho.dispatchMessages([message_1, message_1]) - // await mine(10) - return { - sender, - sygmaAdapter, - otherAddress, - yaho, - sygmaBridge, - sygmaMessageRelay, - pingPong, - message_1, - } -} - -const prepareDepositData = async (reporterAddress: string, ids: string[], hashes: string[], adapter: string) => { - const abiCoder = ethers.utils.defaultAbiCoder - const executionData = abiCoder - .encode(["address", "uint256[]", "bytes32[]"], [ethers.constants.AddressZero, ids, hashes]) - .substring(66) - - const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") - const functionSig = SygmaAdapter.interface.getSighash("storeHashes") - - // bytes memory depositData = abi.encodePacked( - // uint256(0), - // uint16(4), - // IDepositAdapterTarget(address(0)).execute.selector, - // uint8(20), - // _targetDepositAdapter, - // uint8(20), - // _depositorAddress, - // abi.encode(depositContractCalldata) - // ); - - const depositData = - ethers.utils.hexZeroPad("0xe7ef0", 32) + - "0004" + - functionSig.substring(2) + - "14" + - adapter.toLowerCase().substring(2) + - "14" + - reporterAddress.toLowerCase().substring(2) + - executionData - return depositData -} - -describe("SygmaMessageRelay", function () { - describe("Deploy", function () { - it("Successfully deploys contract", async function () { - const { sygmaBridge, yaho, sygmaAdapter, sygmaMessageRelay } = await setup() - expect(await sygmaMessageRelay.deployed()) - expect(await sygmaMessageRelay._bridge()).to.equal(sygmaBridge.address) - expect(await sygmaMessageRelay._yaho()).to.equal(yaho.address) - expect(await sygmaMessageRelay._resourceID()).to.equal(resourceID) - expect(await sygmaMessageRelay._defaultDestinationDomainID()).to.equal(DOMAIN_ID) - expect(await sygmaMessageRelay._defaultSygmaAdapter()).to.equal(sygmaAdapter.address) - }) - }) - - describe("relayMessages()", function () { - it("Relays messages to Sygma to default domain", async function () { - const { sender, sygmaMessageRelay, sygmaAdapter, sygmaBridge, yaho, message_1 } = await setup() - const hash0 = await yaho.calculateHash(network.config.chainId, 0, yaho.address, sender, message_1) - const hash1 = await yaho.calculateHash(network.config.chainId, 1, yaho.address, sender, message_1) - const depositData = await prepareDepositData( - sygmaMessageRelay.address, - ["0", "1"], - [hash0, hash1], - sygmaAdapter.address, - ) - expect(await sygmaMessageRelay.callStatic.relayMessages([0, 1], sygmaAdapter.address)).to.equal( - "0x0000000000000000000000000000000000000000000000000000000000000001", - ) - await expect(sygmaMessageRelay.relayMessages([0, 1], sygmaAdapter.address)) - .to.emit(sygmaMessageRelay, "MessageRelayed") - .withArgs(sygmaMessageRelay.address, 0) - .and.to.emit(sygmaMessageRelay, "MessageRelayed") - .withArgs(sygmaMessageRelay.address, 1) - .and.to.emit(sygmaBridge, "Deposit") - // (destinationDomainID, resourceID, 1, msg.sender, depositData, feeData); - .withArgs(DOMAIN_ID, resourceID, 1, sygmaMessageRelay.address, depositData, "0x") - }) - }) -}) diff --git a/packages/evm/test/adapters/Sygma/04_Sygma_E2E.spec.ts b/packages/evm/test/adapters/Sygma/04_Sygma_E2E.spec.ts deleted file mode 100644 index 368ba917..00000000 --- a/packages/evm/test/adapters/Sygma/04_Sygma_E2E.spec.ts +++ /dev/null @@ -1,116 +0,0 @@ -/* -Note that these E2E tests simulate cross-chain interactions but, -for the sake of convenience, use only one network as both the origin and destination chain. - -*/ -import { expect } from "chai" -import { ethers, network } from "hardhat" - -// Source chain ID -const CHAIN_ID = network.config.chainId -// Destination domain ID -const DOMAIN_ID = 5 -const resourceID = "0x0000000000000000000000000000000000000000000000000000000000000500" - -const ID_ZERO = 0 - -const baseSetup = async () => { - const [wallet] = await ethers.getSigners() - - // deploy hashi - const Hashi = await ethers.getContractFactory("Hashi") - const hashi = await Hashi.deploy() - - // deploy ShoyuBashi - const ShoyuBashi = await ethers.getContractFactory("ShoyuBashi") - const shoyuBashi = ShoyuBashi.deploy(wallet.address, hashi.address) - - // deploy Yaho - const Yaho = await ethers.getContractFactory("Yaho") - const yaho = await Yaho.deploy() - - // deploy Mock Sygma Bridge - const SygmaBridge = await ethers.getContractFactory("MockSygmaBridge") - const sygmaBridge = await SygmaBridge.deploy() - - // deploy Sygma Adapter - const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") - const sygmaAdapter = await SygmaAdapter.deploy(sygmaBridge.address) - - // deploy Sygma Message Relayer - const SygmaMessageRelay = await ethers.getContractFactory("SygmaMessageRelay") - const sygmaMessageRelay = await SygmaMessageRelay.deploy( - sygmaBridge.address, - yaho.address, - resourceID, - DOMAIN_ID, - sygmaAdapter.address, - ) - - await sygmaAdapter.setReporter(sygmaMessageRelay.address, CHAIN_ID, true) - - // deploy Yaru - const Yaru = await ethers.getContractFactory("Yaru") - const yaru = await Yaru.deploy(hashi.address, yaho.address, CHAIN_ID) - - // deploy avatar - const Avatar = await ethers.getContractFactory("TestAvatar") - const avatar = await Avatar.deploy() - - // const deploy PingPong test contract - const PingPong = await ethers.getContractFactory("PingPong") - const pingPong = await PingPong.deploy() - - return { - avatar, - sygmaBridge, - sygmaMessageRelay, - sygmaAdapter, - wallet, - hashi, - shoyuBashi, - yaho, - yaru, - pingPong, - } -} - -const setupTestWithTestAvatar = async () => { - const base = await baseSetup() - const Module = await ethers.getContractFactory("HashiModule") - const provider = await ethers.getDefaultProvider() - const network = await provider.getNetwork() - const module = await Module.deploy( - base.avatar.address, - base.avatar.address, - base.avatar.address, - base.yaru.address, - base.wallet.address, - CHAIN_ID, - ) - await base.avatar.setModule(module.address) - return { ...base, Module, module, network } -} - -describe("SygmaMessageRelay End-to-End", function () { - describe("executeTransaction()", function () { - it("executes a transaction", async () => { - const { pingPong, yaho, sygmaMessageRelay, sygmaAdapter, module, wallet, yaru } = await setupTestWithTestAvatar() - const calldata = await pingPong.interface.encodeFunctionData("ping", []) - const tx = await module.interface.encodeFunctionData("executeTransaction", [pingPong.address, 0, calldata, 0]) - const message = { - to: module.address, - toChainId: DOMAIN_ID, - data: tx, - } - const pingCount = await pingPong.count() - - // dispatch message - await yaho.dispatchMessagesToAdapters([message], [sygmaMessageRelay.address], [sygmaAdapter.address]) - // execute messages - await yaru.executeMessages([message], [ID_ZERO], [wallet.address], [sygmaAdapter.address]) - - expect(await pingPong.count()).to.equal(pingCount + 1) - }) - }) -})