Skip to content

Commit

Permalink
fix: title escrow factory create call (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
superical committed Dec 23, 2022
1 parent aba5cca commit 7493fdd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
6 changes: 5 additions & 1 deletion contracts/TitleEscrowFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/proxy/Clones.sol";
import "./TitleEscrow.sol";
import "./interfaces/ITitleEscrowFactory.sol";
import "./interfaces/TitleEscrowFactoryErrors.sol";

contract TitleEscrowFactory is ITitleEscrowFactory {
contract TitleEscrowFactory is ITitleEscrowFactory, TitleEscrowFactoryErrors {
address public override implementation;

constructor() {
implementation = address(new TitleEscrow());
}

function create(uint256 tokenId) external override returns (address) {
if (msg.sender.code.length == 0) {
revert CreateCallerNotContract();
}
bytes32 salt = keccak256(abi.encodePacked(msg.sender, tokenId));
address titleEscrow = Clones.cloneDeterministic(implementation, salt);
TitleEscrow(titleEscrow).initialize(msg.sender, tokenId);
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/TitleEscrowFactoryErrors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

interface TitleEscrowFactoryErrors {
error CreateCallerNotContract();
}
9 changes: 9 additions & 0 deletions contracts/mocks/DummyContractMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

contract DummyContractMock {

string public name = "Dummy";

constructor() {}
}
80 changes: 57 additions & 23 deletions test/TitleEscrowFactory.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ethers } from "hardhat";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import faker from "faker";
import { ContractTransaction } from "ethers";
import { TitleEscrow, TitleEscrowFactory } from "@tradetrust/contracts";
import { ContractTransaction, Signer } from "ethers";
import { DummyContractMock, TitleEscrow, TitleEscrowFactory } from "@tradetrust/contracts";
import { TitleEscrowCreatedEvent } from "@tradetrust/contracts/contracts/TitleEscrowFactory";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from ".";
import { deployEscrowFactoryFixture } from "./fixtures";
import { computeTitleEscrowAddress, getEventFromReceipt } from "../src/utils";
import { contractInterfaceId, defaultAddress } from "../src/constants";
import { createDeployFixtureRunner, getTestUsers, TestUsers } from "./helpers";
import { createDeployFixtureRunner, getTestUsers, impersonateAccount, TestUsers } from "./helpers";

describe("TitleEscrowFactory", async () => {
let users: TestUsers;
Expand Down Expand Up @@ -85,35 +84,70 @@ describe("TitleEscrowFactory", async () => {
});

describe("Create Title Escrow Contract", () => {
let fakeRegistrySigner: SignerWithAddress;
let titleEscrowFactoryCreateTx: ContractTransaction;
let titleEscrowContract: TitleEscrow;
let tokenId: string;

beforeEach(async () => {
tokenId = faker.datatype.hexaDecimal(64);
fakeRegistrySigner = users.others[faker.datatype.number(users.others.length - 1)];
titleEscrowFactoryCreateTx = await titleEscrowFactory.connect(fakeRegistrySigner).create(tokenId);
});

const receipt = await titleEscrowFactoryCreateTx.wait();
const titleEscrowAddress = getEventFromReceipt<TitleEscrowCreatedEvent>(
receipt,
titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated")
).args.titleEscrow;
describe("Create Caller", () => {
it("should revert when calls create from an EOA", async () => {
const eoa = users.others[faker.datatype.number(users.others.length - 1)];

titleEscrowContract = (await ethers.getContractFactory("TitleEscrow")).attach(titleEscrowAddress) as TitleEscrow;
});
const tx = titleEscrowFactory.connect(eoa).create(tokenId);

it("should create with the correct token registry address", async () => {
const registryAddress = await titleEscrowContract.registry();
await expect(tx).to.be.revertedWithCustomError(titleEscrowFactory, "CreateCallerNotContract");
});

expect(registryAddress).to.equal(fakeRegistrySigner.address);
it("should call create successfully from a contract", async () => {
const dummyContractMock = (await (
await ethers.getContractFactory("DummyContractMock")
).deploy()) as DummyContractMock;
const mockContractSigner = await impersonateAccount({ address: dummyContractMock.address });

const tx = titleEscrowFactory.connect(mockContractSigner).create(tokenId);

await expect(tx).to.not.be.reverted;
});
});

it("should emit TitleEscrowCreated event", async () => {
expect(titleEscrowFactoryCreateTx)
.to.emit(titleEscrowFactory, "TitleEscrowCreated")
.withArgs(titleEscrowContract.address, fakeRegistrySigner.address, tokenId);
describe("Create Title Escrow Behaviours", () => {
let mockContractSigner: Signer;
let titleEscrowFactoryCreateTx: ContractTransaction;
let titleEscrowContract: TitleEscrow;

beforeEach(async () => {
const dummyContractMock = (await (
await ethers.getContractFactory("DummyContractMock")
).deploy()) as DummyContractMock;
mockContractSigner = await impersonateAccount({ address: dummyContractMock.address });
titleEscrowFactoryCreateTx = await titleEscrowFactory.connect(mockContractSigner).create(tokenId);

const receipt = await titleEscrowFactoryCreateTx.wait();
const titleEscrowAddress = getEventFromReceipt<TitleEscrowCreatedEvent>(
receipt,
titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated")
).args.titleEscrow;

titleEscrowContract = (await ethers.getContractFactory("TitleEscrow")).attach(
titleEscrowAddress
) as TitleEscrow;
});

it("should create with the correct token registry address", async () => {
const registryAddress = await titleEscrowContract.registry();
const signerAddress = await mockContractSigner.getAddress();

expect(registryAddress).to.equal(signerAddress);
});

it("should emit TitleEscrowCreated event", async () => {
const signerAddress = await mockContractSigner.getAddress();

expect(titleEscrowFactoryCreateTx)
.to.emit(titleEscrowFactory, "TitleEscrowCreated")
.withArgs(titleEscrowContract.address, signerAddress, tokenId);
});
});
});

Expand Down

0 comments on commit 7493fdd

Please sign in to comment.