|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | +const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
| 4 | + |
| 5 | +const { getLocalChain } = require('../helpers/chains'); |
| 6 | +const { impersonate } = require('../helpers/account'); |
| 7 | +const { generators } = require('../helpers/random'); |
| 8 | + |
| 9 | +const value = 42n; |
| 10 | +const payload = generators.hexBytes(128); |
| 11 | +const attributes = []; |
| 12 | + |
| 13 | +async function fixture() { |
| 14 | + const [sender, notAGateway] = await ethers.getSigners(); |
| 15 | + const { toErc7930 } = await getLocalChain(); |
| 16 | + |
| 17 | + const gateway = await ethers.deployContract('$ERC7786GatewayMock'); |
| 18 | + const receiver = await ethers.deployContract('$ERC7786RecipientMock', [gateway]); |
| 19 | + |
| 20 | + return { sender, notAGateway, gateway, receiver, toErc7930 }; |
| 21 | +} |
| 22 | + |
| 23 | +// NOTE: here we are only testing the receiver. Failures of the gateway itself (invalid attributes, ...) are out of scope. |
| 24 | +describe('ERC7786Recipient', function () { |
| 25 | + beforeEach(async function () { |
| 26 | + Object.assign(this, await loadFixture(fixture)); |
| 27 | + }); |
| 28 | + |
| 29 | + it('receives gateway relayed messages', async function () { |
| 30 | + await expect( |
| 31 | + this.gateway.connect(this.sender).sendMessage(this.toErc7930(this.receiver), payload, attributes, { value }), |
| 32 | + ) |
| 33 | + .to.emit(this.gateway, 'MessageSent') |
| 34 | + .withArgs(ethers.ZeroHash, this.toErc7930(this.sender), this.toErc7930(this.receiver), payload, value, attributes) |
| 35 | + .to.emit(this.receiver, 'MessageReceived') |
| 36 | + .withArgs(this.gateway, ethers.toBeHex(1n, 32n), this.toErc7930(this.sender), payload, value); |
| 37 | + }); |
| 38 | + |
| 39 | + it('receive multiple similar messages (with different receiveIds)', async function () { |
| 40 | + for (let i = 1n; i < 5n; ++i) { |
| 41 | + await expect( |
| 42 | + this.gateway.connect(this.sender).sendMessage(this.toErc7930(this.receiver), payload, attributes, { value }), |
| 43 | + ) |
| 44 | + .to.emit(this.receiver, 'MessageReceived') |
| 45 | + .withArgs(this.gateway, ethers.toBeHex(i, 32n), this.toErc7930(this.sender), payload, value); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it('multiple use of the same receiveId', async function () { |
| 50 | + const gatewayAsEOA = await impersonate(this.gateway.target); |
| 51 | + const receiveId = ethers.toBeHex(1n, 32n); |
| 52 | + |
| 53 | + await expect( |
| 54 | + this.receiver.connect(gatewayAsEOA).receiveMessage(receiveId, this.toErc7930(this.sender), payload, { value }), |
| 55 | + ) |
| 56 | + .to.emit(this.receiver, 'MessageReceived') |
| 57 | + .withArgs(this.gateway, receiveId, this.toErc7930(this.sender), payload, value); |
| 58 | + |
| 59 | + await expect( |
| 60 | + this.receiver.connect(gatewayAsEOA).receiveMessage(receiveId, this.toErc7930(this.sender), payload, { value }), |
| 61 | + ) |
| 62 | + .to.be.revertedWithCustomError(this.receiver, 'ERC7786RecipientMessageAlreadyProcessed') |
| 63 | + .withArgs(this.gateway, receiveId); |
| 64 | + }); |
| 65 | + |
| 66 | + it('unauthorized call', async function () { |
| 67 | + await expect( |
| 68 | + this.receiver.connect(this.notAGateway).receiveMessage(ethers.ZeroHash, this.toErc7930(this.sender), payload), |
| 69 | + ) |
| 70 | + .to.be.revertedWithCustomError(this.receiver, 'ERC7786RecipientUnauthorizedGateway') |
| 71 | + .withArgs(this.notAGateway, this.toErc7930(this.sender)); |
| 72 | + }); |
| 73 | +}); |
0 commit comments