-
Notifications
You must be signed in to change notification settings - Fork 0
/
FarcasterRestrictedNft.sol
91 lines (76 loc) · 3.31 KB
/
FarcasterRestrictedNft.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {ERC721} from "../lib/solbase/src/tokens/ERC721/ERC721.sol";
import {FarcasterLikesChainlinkFunction, FunctionsRequest} from "./FarcasterLikesChainlinkFunction.sol";
/**
* @notice - This contract is a restricted NFT that can only be minted by users who have liked the Farcaster cast
* @dev WIP !!! USE FOR TESTING ONLY !!!
* @author James McComish (https://github.com/jamesmccomish)
*/
contract FarcasterRestrictedNft is ERC721, FarcasterLikesChainlinkFunction {
using FunctionsRequest for FunctionsRequest.Request;
uint256 public currentId;
mapping(uint256 => address) public requestIdToMinter;
constructor(
string memory _name,
string memory _symbol,
address _router,
string memory _farcasterApiCallLogic,
uint64 _subscriptionId
) ERC721(_name, _symbol) FarcasterLikesChainlinkFunction(_router, _farcasterApiCallLogic, _subscriptionId) {}
/// -----------------------------------------------------------------------
/// ERC721 FUNCTIONS
/// -----------------------------------------------------------------------
function whitelistMint() external {
// Init our request with the logic needed to call the Farcaster api
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(farcasterApiCallLogic);
string[] memory args = new string[](1);
args[0] = addressToString(msg.sender);
req.setArgs(args);
// Send req to chainlink fn
bytes32 assignedReqID = _sendRequest(req.encodeCBOR(), subscriptionId, 100000, "fun-polygon-mumbai-1"); // TODO fix gasLimit
requestIdToMinter[uint256(assignedReqID)] = msg.sender;
}
function burn(uint256 id) external {
_burn(id);
}
function tokenURI(uint256 id) public view override returns (string memory) {
return "TODO";
}
/// -----------------------------------------------------------------------
/// ERC721 FUNCTIONS
/// -----------------------------------------------------------------------
/**
* @notice Callback that is invoked once the DON has resolved the request or hit an error
*
* @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 {
// finalise the mint given a correct response
uint256 isValidMinter = abi.decode(response, (uint256));
if (isValidMinter == 1) _mint(requestIdToMinter[uint256(requestId)], currentId++);
}
function addressToString(address _pool) public pure returns (string memory _uintAsString) {
uint256 _i = uint256(uint160(_pool));
if (_i == 0) {
return "0";
}
uint256 j = _i;
uint256 len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint256 k = len - 1;
while (_i != 0) {
bstr[k--] = bytes1(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
}