|
| 1 | +const { parseEther, id, ZeroAddress } = require("ethers") |
| 2 | +const { expect } = require("chai") |
| 3 | +const { ethers } = require("hardhat") |
| 4 | + |
| 5 | +// "err" as bytes, induces a simulated error in MockRecipient.sol and MockRecipientReturnBool.sol |
| 6 | +const errData = "0x657272" |
| 7 | + |
| 8 | +describe("CrosschainERC677", () => { |
| 9 | + it("transferAndCall triggers ERC677 callback", async () => { |
| 10 | + const [signer, minter] = await ethers.getSigners() |
| 11 | + |
| 12 | + const MockRecipient = await ethers.getContractFactory("MockRecipient") |
| 13 | + const recipient = await MockRecipient.deploy() |
| 14 | + await recipient.waitForDeployment() |
| 15 | + const recipientAddress = await recipient.getAddress() |
| 16 | + |
| 17 | + const MockRecipientNotERC677Receiver = await ethers.getContractFactory("MockRecipientNotERC677Receiver") |
| 18 | + const nonReceiverRecipient = await MockRecipientNotERC677Receiver.deploy() |
| 19 | + await nonReceiverRecipient.waitForDeployment() |
| 20 | + const nonReceiverRecipientAddress = await nonReceiverRecipient.getAddress() |
| 21 | + |
| 22 | + const MockRecipientReturnBool = await ethers.getContractFactory("MockRecipientReturnBool") |
| 23 | + const returnBoolRecipient = await MockRecipientReturnBool.deploy() |
| 24 | + await returnBoolRecipient.waitForDeployment() |
| 25 | + const returnBoolRecipientAddress = await returnBoolRecipient.getAddress() |
| 26 | + |
| 27 | + // (ERC20 _coToken, address _minter, string memory _name, string memory _symbol, uint8 _decimals) |
| 28 | + const CrosschainERC677 = await ethers.getContractFactory("CrosschainERC677") |
| 29 | + const token = await CrosschainERC677.deploy(ZeroAddress, ZeroAddress, minter.address, "TestToken", "TEST", 18) |
| 30 | + await token.waitForDeployment() |
| 31 | + |
| 32 | + await expect(token.connect(minter).mint(signer.address, parseEther("10"))).to.emit(token, "Transfer(address,address,uint256)") |
| 33 | + |
| 34 | + // revert in callback => should revert transferAndCall |
| 35 | + await expect(token.transferAndCall(recipientAddress, parseEther("1"), errData)).to.be.reverted |
| 36 | + |
| 37 | + // no callback => should revert transferAndCall |
| 38 | + await expect(token.transferAndCall(nonReceiverRecipientAddress, parseEther("1"), "0x")).to.be.reverted |
| 39 | + |
| 40 | + // contract that implements ERC677Receiver executes the callback |
| 41 | + const txsBefore = await recipient.txCount() |
| 42 | + await token.transferAndCall(recipientAddress, parseEther("1"), "0x6c6f6c") |
| 43 | + const txsAfter = await recipient.txCount() |
| 44 | + |
| 45 | + // callback returns true or false but doesn't revert => should NOT revert |
| 46 | + const txsBeforeBool = await returnBoolRecipient.txCount() |
| 47 | + await token.transferAndCall(returnBoolRecipientAddress, parseEther("1"), errData) |
| 48 | + await token.transferAndCall(returnBoolRecipientAddress, parseEther("1"), "0x") |
| 49 | + const txsAfterBool = await returnBoolRecipient.txCount() |
| 50 | + |
| 51 | + expect(txsAfter).to.equal(txsBefore + 1n) |
| 52 | + expect(txsAfterBool).to.equal(txsBeforeBool + 2n) |
| 53 | + }) |
| 54 | + |
| 55 | + it("transferAndCall just does normal transfer for non-contract accounts", async () => { |
| 56 | + const [signer, minter] = await ethers.getSigners() |
| 57 | + const targetAddress = "0x0000000000000000000000000000000000000001" |
| 58 | + |
| 59 | + const DATAv2 = await ethers.getContractFactory("DATAv2") |
| 60 | + const token = await DATAv2.deploy() |
| 61 | + await token.waitForDeployment() |
| 62 | + |
| 63 | + await expect(token.grantRole(id("MINTER_ROLE"), minter.address)).to.emit(token, "RoleGranted") |
| 64 | + await expect(token.connect(minter).mint(signer.address, parseEther("1"))).to.emit(token, "Transfer(address,address,uint256)") |
| 65 | + |
| 66 | + const balanceBefore = await token.balanceOf(targetAddress) |
| 67 | + await token.transferAndCall(targetAddress, parseEther("1"), "0x6c6f6c") |
| 68 | + const balanceAfter = await token.balanceOf(targetAddress) |
| 69 | + |
| 70 | + expect(balanceAfter - balanceBefore).to.equal(parseEther("1")) |
| 71 | + }) |
| 72 | +}) |
0 commit comments