|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | +const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
| 4 | + |
| 5 | +const { shouldBehaveLikeERC20 } = require('../ERC20.behavior.js'); |
| 6 | +const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior'); |
| 7 | + |
| 8 | +const name = 'My Token'; |
| 9 | +const symbol = 'MTKN'; |
| 10 | +const initialSupply = 100n; |
| 11 | + |
| 12 | +async function fixture() { |
| 13 | + const [other, bridge, ...accounts] = await ethers.getSigners(); |
| 14 | + |
| 15 | + const token = await ethers.deployContract('$ERC20BridgeableMock', [name, symbol, bridge]); |
| 16 | + await token.$_mint(accounts[0], initialSupply); |
| 17 | + |
| 18 | + return { bridge, other, accounts, token }; |
| 19 | +} |
| 20 | + |
| 21 | +describe('ERC20Bridgeable', function () { |
| 22 | + beforeEach(async function () { |
| 23 | + Object.assign(this, await loadFixture(fixture)); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('onlyTokenBridgeFn', function () { |
| 27 | + it('reverts when called by non-bridge', async function () { |
| 28 | + await expect(this.token.onlyTokenBridgeFn()).to.be.revertedWithCustomError(this.token, 'OnlyTokenBridge'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not revert when called by bridge', async function () { |
| 32 | + await expect(this.token.connect(this.bridge).onlyTokenBridgeFn()) |
| 33 | + .to.emit(this.token, 'OnlyTokenBridgeFnCalled') |
| 34 | + .withArgs(this.bridge); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('crosschainMint', function () { |
| 39 | + it('reverts when called by non-bridge', async function () { |
| 40 | + await expect(this.token.crosschainMint(this.other, 100n)).to.be.revertedWithCustomError( |
| 41 | + this.token, |
| 42 | + 'OnlyTokenBridge', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('mints amount provided by the bridge when calling crosschainMint', async function () { |
| 47 | + const amount = 100n; |
| 48 | + await expect(this.token.connect(this.bridge).crosschainMint(this.other, amount)) |
| 49 | + .to.emit(this.token, 'CrosschainMint') |
| 50 | + .withArgs(this.other, amount, this.bridge) |
| 51 | + .to.emit(this.token, 'Transfer') |
| 52 | + .withArgs(ethers.ZeroAddress, this.other, amount); |
| 53 | + |
| 54 | + await expect(this.token.balanceOf(this.other)).to.eventually.equal(amount); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('crosschainBurn', function () { |
| 59 | + it('reverts when called by non-bridge', async function () { |
| 60 | + await expect(this.token.crosschainBurn(this.other, 100n)).to.be.revertedWithCustomError( |
| 61 | + this.token, |
| 62 | + 'OnlyTokenBridge', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('burns amount provided by the bridge when calling crosschainBurn', async function () { |
| 67 | + const amount = 100n; |
| 68 | + await this.token.$_mint(this.other, amount); |
| 69 | + |
| 70 | + await expect(this.token.connect(this.bridge).crosschainBurn(this.other, amount)) |
| 71 | + .to.emit(this.token, 'CrosschainBurn') |
| 72 | + .withArgs(this.other, amount, this.bridge) |
| 73 | + .to.emit(this.token, 'Transfer') |
| 74 | + .withArgs(this.other, ethers.ZeroAddress, amount); |
| 75 | + |
| 76 | + await expect(this.token.balanceOf(this.other)).to.eventually.equal(0); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('ERC165', function () { |
| 81 | + shouldSupportInterfaces({ |
| 82 | + ERC7802: ['crosschainMint(address,uint256)', 'crosschainBurn(address,uint256)'], |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('ERC20 behavior', function () { |
| 87 | + shouldBehaveLikeERC20(initialSupply); |
| 88 | + }); |
| 89 | +}); |
0 commit comments