-
Notifications
You must be signed in to change notification settings - Fork 0
/
Issuer.sol
72 lines (54 loc) · 2.5 KB
/
Issuer.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {RealEstateToken} from "./RealEstateToken.sol";
import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.sol";
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 {FunctionsSource} from "./FunctionsSource.sol";
/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract Issuer is FunctionsClient, FunctionsSource, OwnerIsCreator {
using FunctionsRequest for FunctionsRequest.Request;
error LatestIssueInProgress();
struct FractionalizedNft {
address to;
uint256 amount;
}
RealEstateToken internal immutable i_realEstateToken;
bytes32 internal s_lastRequestId;
uint256 private s_nextTokenId;
mapping(bytes32 requestId => FractionalizedNft) internal s_issuesInProgress;
constructor(address realEstateToken, address functionsRouterAddress) FunctionsClient(functionsRouterAddress) {
i_realEstateToken = RealEstateToken(realEstateToken);
}
function issue(address to, uint256 amount, uint64 subscriptionId, uint32 gasLimit, bytes32 donID)
external
onlyOwner
returns (bytes32 requestId)
{
if (s_lastRequestId != bytes32(0)) revert LatestIssueInProgress();
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(this.getNftMetadata());
requestId = _sendRequest(req.encodeCBOR(), subscriptionId, gasLimit, donID);
s_issuesInProgress[requestId] = FractionalizedNft(to, amount);
s_lastRequestId = requestId;
}
function cancelPendingRequest() external onlyOwner {
s_lastRequestId = bytes32(0);
}
function fulfillRequest(bytes32 requestId, bytes memory response, bytes memory err) internal override {
if (err.length != 0) {
revert(string(err));
}
if (s_lastRequestId == requestId) {
string memory tokenURI = string(response);
uint256 tokenId = s_nextTokenId++;
FractionalizedNft memory fractionalizedNft = s_issuesInProgress[requestId];
i_realEstateToken.mint(fractionalizedNft.to, tokenId, fractionalizedNft.amount, "", tokenURI);
s_lastRequestId = bytes32(0);
}
}
}