|
1 | 1 | import { ethers } from "hardhat"; |
2 | 2 | import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; |
3 | 3 | import faker from "faker"; |
4 | | -import { ContractTransaction } from "ethers"; |
5 | | -import { TitleEscrow, TitleEscrowFactory } from "@tradetrust/contracts"; |
| 4 | +import { ContractTransaction, Signer } from "ethers"; |
| 5 | +import { DummyContractMock, TitleEscrow, TitleEscrowFactory } from "@tradetrust/contracts"; |
6 | 6 | import { TitleEscrowCreatedEvent } from "@tradetrust/contracts/contracts/TitleEscrowFactory"; |
7 | | -import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; |
8 | 7 | import { expect } from "."; |
9 | 8 | import { deployEscrowFactoryFixture } from "./fixtures"; |
10 | 9 | import { computeTitleEscrowAddress, getEventFromReceipt } from "../src/utils"; |
11 | 10 | import { contractInterfaceId, defaultAddress } from "../src/constants"; |
12 | | -import { createDeployFixtureRunner, getTestUsers, TestUsers } from "./helpers"; |
| 11 | +import { createDeployFixtureRunner, getTestUsers, impersonateAccount, TestUsers } from "./helpers"; |
13 | 12 |
|
14 | 13 | describe("TitleEscrowFactory", async () => { |
15 | 14 | let users: TestUsers; |
@@ -85,35 +84,70 @@ describe("TitleEscrowFactory", async () => { |
85 | 84 | }); |
86 | 85 |
|
87 | 86 | describe("Create Title Escrow Contract", () => { |
88 | | - let fakeRegistrySigner: SignerWithAddress; |
89 | | - let titleEscrowFactoryCreateTx: ContractTransaction; |
90 | | - let titleEscrowContract: TitleEscrow; |
91 | 87 | let tokenId: string; |
92 | 88 |
|
93 | 89 | beforeEach(async () => { |
94 | 90 | tokenId = faker.datatype.hexaDecimal(64); |
95 | | - fakeRegistrySigner = users.others[faker.datatype.number(users.others.length - 1)]; |
96 | | - titleEscrowFactoryCreateTx = await titleEscrowFactory.connect(fakeRegistrySigner).create(tokenId); |
| 91 | + }); |
97 | 92 |
|
98 | | - const receipt = await titleEscrowFactoryCreateTx.wait(); |
99 | | - const titleEscrowAddress = getEventFromReceipt<TitleEscrowCreatedEvent>( |
100 | | - receipt, |
101 | | - titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated") |
102 | | - ).args.titleEscrow; |
| 93 | + describe("Create Caller", () => { |
| 94 | + it("should revert when calls create from an EOA", async () => { |
| 95 | + const eoa = users.others[faker.datatype.number(users.others.length - 1)]; |
103 | 96 |
|
104 | | - titleEscrowContract = (await ethers.getContractFactory("TitleEscrow")).attach(titleEscrowAddress) as TitleEscrow; |
105 | | - }); |
| 97 | + const tx = titleEscrowFactory.connect(eoa).create(tokenId); |
106 | 98 |
|
107 | | - it("should create with the correct token registry address", async () => { |
108 | | - const registryAddress = await titleEscrowContract.registry(); |
| 99 | + await expect(tx).to.be.revertedWithCustomError(titleEscrowFactory, "CreateCallerNotContract"); |
| 100 | + }); |
109 | 101 |
|
110 | | - expect(registryAddress).to.equal(fakeRegistrySigner.address); |
| 102 | + it("should call create successfully from a contract", async () => { |
| 103 | + const dummyContractMock = (await ( |
| 104 | + await ethers.getContractFactory("DummyContractMock") |
| 105 | + ).deploy()) as DummyContractMock; |
| 106 | + const mockContractSigner = await impersonateAccount({ address: dummyContractMock.address }); |
| 107 | + |
| 108 | + const tx = titleEscrowFactory.connect(mockContractSigner).create(tokenId); |
| 109 | + |
| 110 | + await expect(tx).to.not.be.reverted; |
| 111 | + }); |
111 | 112 | }); |
112 | 113 |
|
113 | | - it("should emit TitleEscrowCreated event", async () => { |
114 | | - expect(titleEscrowFactoryCreateTx) |
115 | | - .to.emit(titleEscrowFactory, "TitleEscrowCreated") |
116 | | - .withArgs(titleEscrowContract.address, fakeRegistrySigner.address, tokenId); |
| 114 | + describe("Create Title Escrow Behaviours", () => { |
| 115 | + let mockContractSigner: Signer; |
| 116 | + let titleEscrowFactoryCreateTx: ContractTransaction; |
| 117 | + let titleEscrowContract: TitleEscrow; |
| 118 | + |
| 119 | + beforeEach(async () => { |
| 120 | + const dummyContractMock = (await ( |
| 121 | + await ethers.getContractFactory("DummyContractMock") |
| 122 | + ).deploy()) as DummyContractMock; |
| 123 | + mockContractSigner = await impersonateAccount({ address: dummyContractMock.address }); |
| 124 | + titleEscrowFactoryCreateTx = await titleEscrowFactory.connect(mockContractSigner).create(tokenId); |
| 125 | + |
| 126 | + const receipt = await titleEscrowFactoryCreateTx.wait(); |
| 127 | + const titleEscrowAddress = getEventFromReceipt<TitleEscrowCreatedEvent>( |
| 128 | + receipt, |
| 129 | + titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated") |
| 130 | + ).args.titleEscrow; |
| 131 | + |
| 132 | + titleEscrowContract = (await ethers.getContractFactory("TitleEscrow")).attach( |
| 133 | + titleEscrowAddress |
| 134 | + ) as TitleEscrow; |
| 135 | + }); |
| 136 | + |
| 137 | + it("should create with the correct token registry address", async () => { |
| 138 | + const registryAddress = await titleEscrowContract.registry(); |
| 139 | + const signerAddress = await mockContractSigner.getAddress(); |
| 140 | + |
| 141 | + expect(registryAddress).to.equal(signerAddress); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should emit TitleEscrowCreated event", async () => { |
| 145 | + const signerAddress = await mockContractSigner.getAddress(); |
| 146 | + |
| 147 | + expect(titleEscrowFactoryCreateTx) |
| 148 | + .to.emit(titleEscrowFactory, "TitleEscrowCreated") |
| 149 | + .withArgs(titleEscrowContract.address, signerAddress, tokenId); |
| 150 | + }); |
117 | 151 | }); |
118 | 152 | }); |
119 | 153 |
|
|
0 commit comments