-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRealEstate.sol
255 lines (214 loc) · 9.2 KB
/
RealEstate.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {FunctionsClient} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {FunctionsRequest} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";
import {ConfirmedOwner} from "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import {ERC721Burnable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
/**
* @title Chainlink Functions example consuming Real Estate API
*/
contract RealEstate is
FunctionsClient,
ConfirmedOwner,
ERC721("Tokenized Real Estate", "tRE"),
ERC721URIStorage,
ERC721Burnable
{
using FunctionsRequest for FunctionsRequest.Request;
using SafeERC20 for IERC20;
struct APIResponse {
uint256 index;
string tokenId;
string response;
}
struct House {
string tokenId;
address recipientAddress;
string homeAddress;
string listPrice;
string squareFootage;
uint256 createTime;
uint256 lastUpdate;
}
House[] public houseInfo;
// Chainlink Functions script source code.
string private constant SOURCE_PRICE_INFO = "const id = args[0];"
"const priceResponse = await Functions.makeHttpRequest({" "url: `https://api.chateau.voyage/house/${id}`," "});"
"if (priceResponse.error) {" "throw Error('Housing Price Request Error');" "}"
"const price = priceResponse.data.latestValue;" "return Functions.encodeString(price);";
bytes32 public donId; // DON ID for the Functions DON to which the requests are sent
uint64 private subscriptionId; // Subscription ID for the Chainlink Functions
uint32 private gasLimit; // Gas limit for the Chainlink Functions callbacks
uint256 public epoch; // Time interval for price updates.
uint256 private _totalHouses;
// Mapping of request IDs to API response info
mapping(bytes32 => APIResponse) public requests;
mapping(string => bytes32) public latestRequestId;
mapping(string tokenId => string price) public latestPrice;
event LastPriceRequested(bytes32 indexed requestId, string tokenId);
event LastPriceReceived(bytes32 indexed requestId, string response);
event RequestFailed(bytes error);
constructor(address router, bytes32 _donId, uint64 _subscriptionId, uint32 _gasLimit, uint256 _epoch)
FunctionsClient(router)
ConfirmedOwner(msg.sender)
{
donId = _donId;
subscriptionId = _subscriptionId;
gasLimit = _gasLimit;
epoch = _epoch;
}
/**
* @notice Issues new tokenized real estate NFT asset.
*/
function issueHouse(
address recipientAddress,
string memory homeAddress,
string memory listPrice,
string memory squareFootage
) external onlyOwner {
uint256 index = _totalHouses;
string memory tokenId = string(abi.encode(index));
// increase: _totalHouses.
_totalHouses++;
// create: instance of a House.
houseInfo.push(
House({
tokenId: tokenId,
recipientAddress: recipientAddress,
homeAddress: homeAddress,
listPrice: listPrice,
squareFootage: squareFootage,
createTime: block.timestamp,
lastUpdate: block.timestamp
})
);
setURI(index, homeAddress, listPrice, squareFootage);
_safeMint(recipientAddress, index);
}
/**
* @notice Request `lastPrice` for a given `tokenId`
* @param tokenId id of said token e.g. 0
*/
function requestPrice(string calldata tokenId, uint256 index) external {
string[] memory args = new string[](1);
args[0] = tokenId;
// gets: houseInfo[tokenId]
House storage house = houseInfo[index];
// ensures: price update is not too soon (i.e. not until a full epoch elapsed).
require(block.timestamp - house.lastUpdate >= epoch, "RealEstate: Price update too soon");
bytes32 requestId = _sendRequest(SOURCE_PRICE_INFO, args);
// maps: `tokenId` associated with a given `requestId`.
requests[requestId].tokenId = tokenId;
// maps: `index` associated with a given `requestId`.
requests[requestId].index = index;
latestRequestId[tokenId] = requestId;
emit LastPriceRequested(requestId, tokenId);
}
/**
* @notice Construct and store a URI containing the off-chain data.
* @param tokenId the tokenId associated with the home.
* @param homeAddress the address of the home.
* @param listPrice year the home was built.
* @param squareFootage size of the home (in ft^2)
*/
function setURI(uint256 tokenId, string memory homeAddress, string memory listPrice, string memory squareFootage)
internal
{
// [then] create URI: with property details.
string memory uri = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "Tokenized Real Estate",' '"description": "Tokenized Real Estate",',
'"image": "",' '"attributes": [',
'{"trait_type": "homeAddress",',
'"value": ',
homeAddress,
"}",
',{"trait_type": "listPrice",',
'"value": ',
listPrice,
"}",
',{"trait_type": "squareFootage",',
'"value": ',
squareFootage,
"}",
"]}"
)
)
)
);
// [then] create: finalTokenURI: with metadata.
string memory finalTokenURI = string(abi.encodePacked("data:application/json;base64,", uri));
// [then] set: tokenURI for a given `tokenId`, containing metadata.
_setTokenURI(tokenId, finalTokenURI);
}
/**
* @notice Process the response from the executed Chainlink Functions script
* @param requestId The request ID
* @param response The response from the Chainlink Functions script
*/
function _processResponse(bytes32 requestId, bytes memory response) private {
requests[requestId].response = string(response);
uint256 index = requests[requestId].index;
string memory tokenId = requests[requestId].tokenId;
// store: latest price for a given `tokenId`.
latestPrice[tokenId] = string(response);
// gets: houseInfo[tokenId]
House storage house = houseInfo[index];
// updates: listPrice for a given `tokenId`.
house.listPrice = string(response);
// updates: lastUpdate for a given `tokenId`.
house.lastUpdate = block.timestamp;
emit LastPriceReceived(requestId, string(response));
}
// CHAINLINK FUNCTIONS //
/**
* @notice Triggers an on-demand Functions request
* @param args String arguments passed into the source code and accessible via the global variable `args`
*/
function _sendRequest(string memory source, string[] memory args) internal returns (bytes32 requestId) {
FunctionsRequest.Request memory req;
req.initializeRequest(FunctionsRequest.Location.Inline, FunctionsRequest.CodeLanguage.JavaScript, source);
if (args.length > 0) {
req.setArgs(args);
}
requestId = _sendRequest(req.encodeCBOR(), subscriptionId, gasLimit, donId);
}
/**
* @notice Fulfillment callback function
* @param requestId The request ID, returned by sendRequest()
* @param response Aggregated response from the user code
* @param err Aggregated error from the user code or from the execution pipeline
* Either response or error parameter will be set, but never both
*/
function fulfillRequest(bytes32 requestId, bytes memory response, bytes memory err) internal override {
if (err.length > 0) {
emit RequestFailed(err);
return;
}
_processResponse(requestId, response);
}
// ERC721 SETTINGS //
// gets: tokenURI for a given `tokenId`.
function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
return super.tokenURI(tokenId);
}
// checks: interface is supported by this contract.
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721URIStorage) returns (bool) {
return super.supportsInterface(interfaceId);
}
function totalHouses() public view returns (uint256) {
return _totalHouses;
}
// OWNER SETTING //
// prevents excessive calls from UI.
function setEpoch(uint256 _epoch) public onlyOwner {
epoch = _epoch;
}
}