Skip to content

Commit 10ed5e1

Browse files
authored
fix: caller for create in title escrow factory (#162)
1 parent 70ff40e commit 10ed5e1

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

contracts/TitleEscrowFactory.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ contract TitleEscrowFactory is ITitleEscrowFactory, TitleEscrowFactoryErrors {
1414
}
1515

1616
function create(uint256 tokenId) external override returns (address) {
17-
if (msg.sender.code.length == 0) {
17+
if (tx.origin == msg.sender) {
1818
revert CreateCallerNotContract();
1919
}
2020
bytes32 salt = keccak256(abi.encodePacked(msg.sender, tokenId));

contracts/mocks/DummyContractMock.sol

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
import "../interfaces/ITitleEscrowFactory.sol";
5+
6+
contract TitleEscrowFactoryCallerMock {
7+
function callCreate(address titleEscrowFactory, uint256 tokenId) public {
8+
ITitleEscrowFactory(titleEscrowFactory).create(tokenId);
9+
}
10+
}

test/TitleEscrowFactory.test.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { ethers } from "hardhat";
22
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
33
import faker from "faker";
4-
import { ContractTransaction, Signer } from "ethers";
5-
import { DummyContractMock, TitleEscrow, TitleEscrowFactory } from "@tradetrust/contracts";
4+
import { ContractTransaction } from "ethers";
5+
import { TitleEscrow, TitleEscrowFactory, TitleEscrowFactoryCallerMock } from "@tradetrust/contracts";
66
import { TitleEscrowCreatedEvent } from "@tradetrust/contracts/contracts/TitleEscrowFactory";
77
import { expect } from ".";
88
import { deployEscrowFactoryFixture } from "./fixtures";
99
import { computeTitleEscrowAddress, getEventFromReceipt } from "../src/utils";
1010
import { contractInterfaceId, defaultAddress } from "../src/constants";
11-
import { createDeployFixtureRunner, getTestUsers, impersonateAccount, TestUsers } from "./helpers";
11+
import { createDeployFixtureRunner, getTestUsers, TestUsers } from "./helpers";
1212

1313
describe("TitleEscrowFactory", async () => {
1414
let users: TestUsers;
@@ -85,9 +85,14 @@ describe("TitleEscrowFactory", async () => {
8585

8686
describe("Create Title Escrow Contract", () => {
8787
let tokenId: string;
88+
let titleEscrowFactoryCallerMock: TitleEscrowFactoryCallerMock;
8889

8990
beforeEach(async () => {
9091
tokenId = faker.datatype.hexaDecimal(64);
92+
93+
titleEscrowFactoryCallerMock = (await (
94+
await ethers.getContractFactory("TitleEscrowFactoryCallerMock")
95+
).deploy()) as TitleEscrowFactoryCallerMock;
9196
});
9297

9398
describe("Create Caller", () => {
@@ -100,33 +105,29 @@ describe("TitleEscrowFactory", async () => {
100105
});
101106

102107
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);
108+
const tx = titleEscrowFactoryCallerMock.connect(users.carrier).callCreate(titleEscrowFactory.address, tokenId);
109109

110110
await expect(tx).to.not.be.reverted;
111111
});
112112
});
113113

114114
describe("Create Title Escrow Behaviours", () => {
115-
let mockContractSigner: Signer;
116115
let titleEscrowFactoryCreateTx: ContractTransaction;
117116
let titleEscrowContract: TitleEscrow;
118117

119118
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);
119+
const signer = users.others[faker.datatype.number(users.others.length - 1)];
120+
titleEscrowFactoryCreateTx = await titleEscrowFactoryCallerMock
121+
.connect(signer)
122+
.callCreate(titleEscrowFactory.address, tokenId);
125123

126124
const receipt = await titleEscrowFactoryCreateTx.wait();
125+
126+
const titleEscrowFactoryInterface = (await ethers.getContractFactory("TitleEscrowFactory")).interface;
127127
const titleEscrowAddress = getEventFromReceipt<TitleEscrowCreatedEvent>(
128128
receipt,
129-
titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated")
129+
titleEscrowFactory.interface.getEventTopic("TitleEscrowCreated"),
130+
titleEscrowFactoryInterface
130131
).args.titleEscrow;
131132

132133
titleEscrowContract = (await ethers.getContractFactory("TitleEscrow")).attach(
@@ -136,17 +137,17 @@ describe("TitleEscrowFactory", async () => {
136137

137138
it("should create with the correct token registry address", async () => {
138139
const registryAddress = await titleEscrowContract.registry();
139-
const signerAddress = await mockContractSigner.getAddress();
140+
const createCallerAddress = titleEscrowFactoryCallerMock.address;
140141

141-
expect(registryAddress).to.equal(signerAddress);
142+
expect(registryAddress).to.equal(createCallerAddress);
142143
});
143144

144145
it("should emit TitleEscrowCreated event", async () => {
145-
const signerAddress = await mockContractSigner.getAddress();
146+
const createCallerAddress = titleEscrowFactoryCallerMock.address;
146147

147148
expect(titleEscrowFactoryCreateTx)
148149
.to.emit(titleEscrowFactory, "TitleEscrowCreated")
149-
.withArgs(titleEscrowContract.address, signerAddress, tokenId);
150+
.withArgs(titleEscrowContract.address, createCallerAddress, tokenId);
150151
});
151152
});
152153
});

0 commit comments

Comments
 (0)