-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.sol
450 lines (405 loc) · 15.1 KB
/
contract.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {FunctionsClient} from "@chainlink/contracts@1.2.0/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts@1.2.0/src/v0.8/shared/access/ConfirmedOwner.sol";
import {FunctionsRequest} from "@chainlink/contracts@1.2.0/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";
contract SepoliaGettingStartedFunctionsConsumer is
FunctionsClient,
ConfirmedOwner
{
using FunctionsRequest for FunctionsRequest.Request;
string public baseUrl;
uint64 public subscriptionId;
address[] public verifiedUsers;
mapping(address => address[]) public supportedDelegates;
// Event to log responses
event Response(bytes32 indexed requestId, bytes data, bytes error);
event RequestSend(bytes32 requestId);
event Stringified(string methodName, string result);
// Router address - Hardcoded for Sepolia
// Check to get the router address for your supported network https://docs.chain.link/chainlink-functions/supported-networks
address router = 0xb83E47C2bC239B3bf370bc41e1459A34b41238D0;
// JavaScript source code
string source =
"const baseUrl = args[0]"
"const endPoint = args[1];"
"const apiResponse = await Functions.makeHttpRequest({"
"url: `${baseUrl}/${endPoint}`,"
"headers: {\"X-Wallet-Address\":args[2]},"
"});"
"if (apiResponse.error) {"
"throw Error('Request failed');"
"}"
"const { data } = apiResponse;"
"return Functions.encodeString(data.data);";
string src_with_body =
"const baseUrl = args[0]"
"const endPoint = args[1];"
"const body = args[2]"
"const apiResponse = await Functions.makeHttpRequest({"
"url: `${baseUrl}/${endPoint}/`,"
"method:\"POST\","
"headers: {\"Content-Type\": \"application/json\", \"X-Wallet-Address\":args[3]},"
"data: `${body}`"
"});"
"if (apiResponse.error) {"
"throw Error('Request failed');"
"}"
"const { data } = apiResponse;"
"return Functions.encodeString(data.data);";
//Callback gas limit
uint32 gasLimit = 300000;
// donID - Hardcoded for Sepolia
// Check to get the donID for your supported network https://docs.chain.link/chainlink-functions/supported-networks
bytes32 donID =
0x66756e2d657468657265756d2d7365706f6c69612d3100000000000000000000;
/**
* @notice Initializes the contract with the Chainlink router address and sets the contract owner
*/
constructor(string memory _baseUrl, uint64 _subId)
FunctionsClient(router)
ConfirmedOwner(msg.sender)
{
baseUrl = _baseUrl;
subscriptionId = _subId;
}
function changeBaseUrl(string memory _url) external onlyOwner{
baseUrl = _url;
}
// modifier so that only verified contracts can access the DBs
modifier onlyVerifiedUser() {
bool verified = false;
uint256 l = verifiedUsers.length;
for (uint256 idx = 0; idx < l; idx++) {
if (verifiedUsers[idx] == msg.sender) {
verified = true;
break;
}
}
require(verified, "Unauthorised access");
_;
}
function registerSelf() external payable {
require(msg.value >= 100000000);
verifiedUsers.push(msg.sender);
supportedDelegates[msg.sender] = [msg.sender];
// now this contract/address can also use our SDK
}
function unregisterSelf() external payable onlyVerifiedUser {
uint256 l = verifiedUsers.length;
uint256 idx;
for (idx = 0; idx < l; idx++) {
if (verifiedUsers[idx] == msg.sender) {
break;
}
}
verifiedUsers[idx] = verifiedUsers[l - 1];
verifiedUsers.pop();
payable(msg.sender).transfer(90000000);
}
function addDelegates(address addr) external onlyVerifiedUser {
supportedDelegates[msg.sender].push(addr);
}
modifier onlyDelegates(address verifiedUser) {
uint256 l = supportedDelegates[verifiedUser].length;
bool delegate = false;
for (uint256 idx = 0; idx < l; idx++) {
if (msg.sender == supportedDelegates[verifiedUser][idx]) {
delegate = true;
}
}
require(delegate, "Unauthorized");
_;
}
function sendGetRequest(string[] memory args)
internal
returns (bytes32 requestId)
{
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS code
req.setArgs(args); // Set the arguments for the request
// Send the request and store the request ID
return _sendRequest(
req.encodeCBOR(),
subscriptionId,
gasLimit,
donID
);
}
function sendPostRequest(string[] memory args)
internal
returns (bytes32 requestId)
{
// require(args.length == 3, "Bad args");
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(src_with_body); // Initialize the request with JS code
req.setArgs(args); // Set the arguments for the request
// Send the request and store the request ID
s_lastRequestId = _sendRequest(
req.encodeCBOR(),
subscriptionId,
gasLimit,
donID
);
return s_lastRequestId;
}
function stringifyArray(string[] memory _array)
public
pure
returns (string memory)
{
string memory output = "[";
for (uint256 i = 0; i < _array.length; i++) {
output = strConcat(output, _array[i]);
if (i < _array.length - 1) {
output = strConcat(output, ", ");
}
}
output = strConcat(output, "]");
return output;
}
function strConcat(string memory _a, string memory _b)
internal
pure
returns (string memory)
{
return string(abi.encodePacked(_a, _b));
}
function char(bytes1 b) internal pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
function addrToStr(address x) internal pure returns (string memory) {
bytes memory s = new bytes(40);
for (uint256 i = 0; i < 20; i++) {
bytes1 b = bytes1(
uint8(uint256(uint160(x)) / (2**(8 * (19 - i))))
);
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2 * i] = char(hi);
s[2 * i + 1] = char(lo);
}
return string(s);
}
function createDatabase(string memory dbType, string memory dbName)
external
onlyVerifiedUser
returns (bytes32 requestId)
{
string[] memory args = new string[](4);
args[0] = baseUrl;
args[1] = "db";
string memory p1 = "{\"type\":\"";
string memory p2 = "\",\"dbName\":\"";
string memory p3 = "\"}";
// string memory p6 = string.concat(
// p4,
// string.concat(stringifyArray(fieldTypes), p5)
// );
// string memory p7 = string.concat(
// tableName,
// string.concat(p3, string.concat(stringifyArray(fieldNames), p6))
// );
args[2] = string.concat(p1, string.concat(dbType, string.concat(p2, string.concat(dbName, p3))));
emit Stringified("createDatabase", args[2]);
args[3] = addrToStr(msg.sender);
return sendPostRequest(args);
}
function createTable(
address owner,
string memory dbId,
string memory tableName,
string[] memory fieldNames,
string[] memory fieldTypes
) external onlyDelegates(owner) returns (bytes32 requestId) {
string[] memory args = new string[](3);
args[0] = baseUrl;
args[1] = string.concat(dbId, string.concat("/", "tables"));
string memory p1 = "{\"dbName\":\"";
p1 = string.concat(p1, dbId);
string memory p2 = "\",\"tableName\":\"";
p1 = string.concat(p1, p2);
p1 = string.concat(p1, tableName);
p2 = "\",\"columnNames\":\"";
p1 = string.concat(p1, p2);
p1 = string.concat(p1, stringifyArray(fieldNames));
p2 = "\",\"columnTypes\":\"";
p1 = string.concat(p1, p2);
p1 = string.concat(p1, stringifyArray(fieldTypes));
p2 = "\"}";
p1 = string.concat(p1, p2);
// string memory p6 = string.concat(
// p4,
// string.concat(stringifyArray(fieldTypes), p5)
// );
// string memory p7a = string.concat(
// tableName,
// p3
// );
// string memory p7b = string.concat(stringifyArray(fieldNames), p6);
// string memory p7 = string.concat(p7a, p7b);
// args[2] = string.concat(p1, dbId);
// args[2] = string.concat(args[2], string.concat(p2, p7));
args[2] = p1;
emit Stringified("createTable", args[2]);
args[3] = addrToStr(msg.sender);
return sendPostRequest(args);
}
function getAllRowsOfTable(address owner, string memory dbId, string memory tableId) external onlyDelegates(owner) returns (bytes32 requestId) {
string[] memory args = new string[](2);
args[0] = baseUrl;
args[1] = string.concat(dbId, string.concat("/", string.concat(tableId, "/data")));
args[2] = addrToStr(owner);
return sendGetRequest(args);
}
function insertSingleRow(
address owner,
string memory dbId,
string memory tableId,
string[] memory data
) external onlyDelegates(owner) returns (bytes32 requestId) {
string[] memory args = new string[](3);
args[0] = baseUrl;
args[1] = "insertSingleRow";
string memory p1 = "{\"dbName\":\"";
string memory p2 = "\",\"tableName\":\"";
string memory p3 = "\",\"rowData\":\"";
string memory p4 = "\"}";
string memory stringified_data = stringifyArray(data);
args[2] = string.concat(p1, string.concat(dbId, p2));
args[2] = string.concat(args[2], string.concat(tableId, p3));
args[2] = string.concat(args[2], string.concat(stringified_data, p4));
emit Stringified("insertSingleRow", args[2]);
args[3] = addrToStr(msg.sender);
return sendPostRequest(args);
}
function innerStringify(string[] memory _array) public pure returns (string memory) {
string memory output = "";
for (uint256 i = 0; i < _array.length; i++) {
output = strConcat(output, "'");
output = strConcat(output, _array[i]);
output = strConcat(output, "'");
if (i < _array.length - 1) {
output = strConcat(output, ",");
}
}
return output;
}
function stringifyNested(string[][] memory _arr)
public
pure
returns (string memory)
{
uint l = _arr.length;
string[] memory arr = new string[](l);
for (uint idx = 0; idx < l; idx++){
arr[idx] = innerStringify(_arr[idx]);
}
string memory output = "[";
for (uint idx = 0;idx<l-1;idx++){
output=string.concat(output, string.concat(arr[idx], ","));
}
return string.concat(output, string.concat(arr[l-1],"]"));
}
function insertMultipleRows(
address owner,
string memory dbId,
string memory tableId,
string[][] memory rows
) external onlyDelegates(owner) returns (bytes32 requestId) {
string[] memory args = new string[](3);
args[0] = baseUrl;
args[1] = "insertMultipleRows";
string memory p1 = "{\"dbId\":\"";
string memory p2 = "\",\"tableId\":\"";
string memory p3 = "\",\"data\":\"";
string memory p4 = "\"}";
string memory stringified_data = stringifyNested(rows);
args[2] = string.concat(p1, string.concat(dbId, p2));
args[2] = string.concat(args[2], string.concat(tableId, p3));
args[2] = string.concat(args[2], string.concat(stringified_data, p4));
emit Stringified("insertMultipleRows", args[2]);
args[3] = addrToStr(msg.sender);
return sendPostRequest(args);
}
// function createCollection(string memory dbId, string memory collectionName)
// external
// onlyVerifiedUser
// returns (bytes32 requestId)
// {
// string[] memory args = new string[](3);
// args[0] = baseUrl;
// args[1] = "createCollection";
// args[2] = string.concat("{\"collectionName\":\"", string.concat(collectionName, "\"}"));
// return sendPostRequest(args);
// }
function insertSingleDocument(
address owner,
string memory dbId,
string[] memory keys,
string[] memory values,
string[] memory types
) external onlyDelegates(owner) returns (bytes32 requestId) {}
function insertMultipleDocuments(
address owner,
string memory dbId,
string[][] memory keys,
string[][] memory values,
string[][] memory types
) external onlyDelegates(owner) returns (bytes32 requestId) {}
/**
* @notice Sends an HTTP request for character information
* @param args The arguments to pass to the HTTP request
* @return requestId The ID of the request
*/
function sendGetRequest(string[] calldata args)
internal
returns (bytes32 requestId)
{
// require(args.length == 2, "Bad args");
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS code
req.setArgs(args); // Set the arguments for the request
// Send the request and store the request ID
s_lastRequestId = _sendRequest(
req.encodeCBOR(),
subscriptionId,
gasLimit,
donID
);
return s_lastRequestId;
}
function sendRequestTest(string[] calldata args)
external
onlyOwner
returns (bytes32 requestId)
{
// require(args.length == 2, "Bad args");
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS code
req.setArgs(args); // Set the arguments for the request
// Send the request and store the request ID
s_lastRequestId = _sendRequest(
req.encodeCBOR(),
subscriptionId,
gasLimit,
donID
);
return s_lastRequestId;
}
/**
* @notice Callback function for fulfilling a request
* @param requestId The ID of the request to fulfill
* @param response The HTTP response data
* @param err Any errors from the Functions request
*/
function fulfillRequest(
bytes32 requestId,
bytes memory response,
bytes memory err
) internal override {
// just emit a response
emit Response(requestId, response, err);
}
}