irongit
79 lines2.3 KB
1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.20;
3
4import "./HomeTransaction.sol";
5
6contract Factory {
7 HomeTransaction[] public contracts;
8
9 mapping(address => HomeTransaction[]) public contractsByRealtor;
10 mapping(address => HomeTransaction[]) public contractsBySeller;
11 mapping(address => HomeTransaction[]) public contractsByBuyer;
12
13 event ContractCreated(
14 address indexed contractAddress,
15 address indexed realtor,
16 address indexed seller,
17 address buyer,
18 uint price
19 );
20
21 function create(
22 string memory _address,
23 string memory _zip,
24 string memory _city,
25 uint _realtorFee,
26 uint _price,
27 address payable _seller,
28 address payable _buyer
29 ) external returns (HomeTransaction homeTransaction) {
30 homeTransaction = new HomeTransaction(
31 _address,
32 _zip,
33 _city,
34 _realtorFee,
35 _price,
36 payable(msg.sender), // realtor is the caller
37 _seller,
38 _buyer
39 );
40
41 contracts.push(homeTransaction);
42 contractsByRealtor[msg.sender].push(homeTransaction);
43 contractsBySeller[_seller].push(homeTransaction);
44 contractsByBuyer[_buyer].push(homeTransaction);
45
46 emit ContractCreated(
47 address(homeTransaction),
48 msg.sender,
49 _seller,
50 _buyer,
51 _price
52 );
53 }
54
55 function getInstance(uint index) external view returns (HomeTransaction) {
56 require(index < contracts.length, "Index out of range");
57 return contracts[index];
58 }
59
60 function getInstances() external view returns (HomeTransaction[] memory) {
61 return contracts;
62 }
63
64 function getInstanceCount() external view returns (uint) {
65 return contracts.length;
66 }
67
68 function getContractsByRealtor(address realtor) external view returns (HomeTransaction[] memory) {
69 return contractsByRealtor[realtor];
70 }
71
72 function getContractsBySeller(address seller) external view returns (HomeTransaction[] memory) {
73 return contractsBySeller[seller];
74 }
75
76 function getContractsByBuyer(address buyer) external view returns (HomeTransaction[] memory) {
77 return contractsByBuyer[buyer];
78 }
79}