Skip to content

Instantly share code, notes, and snippets.

@ijonas
Last active April 5, 2022 12:20
Show Gist options
  • Save ijonas/9d6eefd23ace3bce0e636cc42d2ef13b to your computer and use it in GitHub Desktop.
Save ijonas/9d6eefd23ace3bce0e636cc42d2ef13b to your computer and use it in GitHub Desktop.
Fetching a string-value via an Oracle

Testnet oracle & job details

Kovan

Oracle Address/ID: 0x17A8182DBF79427801573A3D75d42eB9B1215DEF
Get > Bytes32 jobId: 1349eaee74fd40cba66f1a466eee80a7

Rinkeby

Oracle Address/ID: 0xD0691a51e3C6c562691D3C44C2944Bd9D368Ec1f
Get > Bytes32 jobId: cc01528b2baf400e9386cf84b6daa9f2
contract MyContract {
string public lastMarket;
function requestEthereumLastMarket(address _oracle, string memory _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), address(this), this.fulfillEthereumLastMarket.selector);
req.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
req.add("path", "RAW,ETH,USD,LASTMARKET"); // Chainlink nodes 1.0.0 and later support this format
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillEthereumLastMarket(bytes32 _requestId, bytes32 _market)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumLastMarket(_requestId, _market);
// _market will contain a bytes32-encoded string value
// e.g. the value of "Coinbase" will be encoded as
// 0x436f696e62617365000000000000000000000000000000000000000000000000
lastMarket = string(abi.encodePacked(_market));
}
}
type = "directrequest"
schemaVersion = 1
name = "Get > Bytes32 v14"
externalJobID = "1349eaee-74fd-40cb-a66f-1a466eee80a7"
maxTaskDuration = "0s"
contractAddress = "0x17A8182DBF79427801573A3D75d42eB9B1215DEF"
minIncomingConfirmations = 0
observationSource = """
decode_log [type="ethabidecodelog"
abi="OracleRequest(bytes32 indexed specId, address requester, bytes32 requestId, uint256 payment, address callbackAddr, bytes4 callbackFunctionId, uint256 cancelExpiration, uint256 dataVersion, bytes data)"
data="$(jobRun.logData)"
topics="$(jobRun.logTopics)"]
decode_cbor [type="cborparse" data="$(decode_log.data)"]
fetch [type="http" method=GET url="$(decode_cbor.get)"]
parse [type="jsonparse" path="$(decode_cbor.path)" data="$(fetch)"]
encode_data [type="ethabiencode" abi="(bytes8 value)" data="{ \\"value\\": $(parse) }"]
encode_tx [type="ethabiencode"
abi="fulfillOracleRequest(bytes32 requestId, uint256 payment, address callbackAddress, bytes4 callbackFunctionId, uint256 expiration, bytes32 data)"
data="{\\"requestId\\": $(decode_log.requestId), \\"payment\\": $(decode_log.payment), \\"callbackAddress\\": $(decode_log.callbackAddr), \\"callbackFunctionId\\": $(decode_log.callbackFunctionId), \\"expiration\\": $(decode_log.cancelExpiration), \\"data\\": $(encode_data)}"
]
submit_tx [type="ethtx" to="0x17A8182DBF79427801573A3D75d42eB9B1215DEF" data="$(encode_tx)"]
decode_log -> decode_cbor -> fetch -> parse -> encode_data -> encode_tx -> submit_tx
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment