Last active
November 29, 2022 00:24
-
-
Save andrewxhill/ea3f82bc9fc07fe90818307a2b1c8052 to your computer and use it in GitHub Desktop.
Uses midpointapi.com to return a single cell value from any table or sql request on Tableland
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.17; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "@openzeppelin/contracts/utils/structs/EnumerableMap.sol"; | |
interface IMidpoint { | |
function callMidpoint(uint64 midpointId, bytes calldata _data) external returns(uint256 requestId); | |
} | |
interface IStringCallback { | |
function stringResponse(uint256 requestId, string memory data) external; | |
} | |
contract QueryController { | |
// will be used for recalling callbacks | |
using EnumerableMap for EnumerableMap.UintToAddressMap; | |
// emits when requests come in | |
event RequestMade(uint256 requestId, string network, string query); | |
// emits when midpoint responses come in | |
event ResponseReceived(uint256 requestId, string response); | |
// the midpoint contract | |
address constant midpoint = 0x9BEa2A4C2d84334287D60D6c36Ab45CB453821eB; | |
// the addr of the midpoint responses | |
address constant midpointOrigin = 0xC0FFEE4a3A2D488B138d090b8112875B90b5e6D9; | |
// the id of our query type | |
uint64 midpointID = 389; | |
// recalling callbacks | |
// mapping(uint256 => address) public callbacks; | |
EnumerableMap.UintToAddressMap private callbacks; | |
// just the basic call, no callback recorded | |
function _queryString(string memory column, string memory query) private returns(uint256 requestId) { | |
string memory wrapped = string.concat( | |
"SELECT ", column, " as response FROM (", query,") LIMIT 1" | |
); | |
bytes memory args = abi.encodePacked(wrapped, bytes1(0x00)); | |
requestId = IMidpoint(midpoint).callMidpoint(midpointID, args); | |
emit RequestMade(requestId, wrapped, query); | |
return requestId; | |
} | |
// call midpoint w/ a callback to your own contract. contract must conform to IStringCallback | |
function queryString(string memory column, string memory query, address endpoint) public returns(uint256 requestId) { | |
// create a requestid from midpoint | |
requestId = _queryString(column, query); | |
EnumerableMap.set(callbacks, requestId, endpoint); | |
return requestId; | |
} | |
function callback(uint256 _requestId, uint64 _midpointId, string memory response) public { | |
// Only allow the verified callback address to submit information for your midpoint. | |
require(tx.origin == midpointOrigin, "Invalid callback address"); | |
require(midpointID == _midpointId, "Invalid Midpoint ID"); | |
if (EnumerableMap.contains(callbacks, _requestId)) { | |
address to = EnumerableMap.get(callbacks, _requestId); | |
EnumerableMap.remove(callbacks, _requestId); | |
IStringCallback(to).stringResponse(_requestId, response); | |
} | |
// For Demonstration Purposes Only | |
emit ResponseReceived(_requestId, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment