Data Feed: Stream a data feed on-chain at regular intervals
Overview
A Data Feed utilizes a cron job, which is a time-based job scheduler in Unix-like operating systems, to automatically trigger the submission of transactions to a smart contract on a blockchain. This means that the program will execute the transaction submission at regular intervals without requiring any manual intervention.
CronJobTriggered Source:
Time-based job scheduler defined in the CronJobTriggered Source definition.
MakeHttpRequest Task:
Makes an API request to the coinbase domain in a secure manner
Extracts the ethPrice from the API response
Sends the price to the Transact To EVM Function task
TransactToEvmFunction task:
Sends back the ethPrice to the contract via function defined by the user in the next block
Midpoint
$ npm install -g midpoint-cli
$ midpoint init blank my-data-feed
1 . Cron Job Triggered
$ midpoint add-source cronJobTriggeredSource cron-job-source
We define the Cron Job Triggered source so that contract can make a midpoint request.
{
"cronJobTriggeredSource": {
"crontab": "* * * * *",
"excludeLogs": [],
"extracts": [
{
"name": "Midpoint_ID",
"from": "Midpoint_ID"
}
]
}
}
2. Make Http Request
$ midpoint add-task makeHttpRequest get-eth-price
We define the Make HTTP Request Task to get the ethPrice from the coinbase endpoint.
{
"makeHttpRequest": {
"urlRaw": "https://api.coinbase.com/v2/prices/ETH-USD/spot",
"method": "GET",
"urlType": "raw",
"extracts": [
{
"name": "ethPrice",
"from": "body.data.amount"
}
]
}
}
3. Shift Decimal
$ midpoint add-task makeHttpRequest shift-eth-price
We define the Shift Decimal Task to return a usable price on-chain.
{
"shiftDecimal": {
"inputValue": "{{ethPrice}}",
"decimalPlaces": "5",
"outputName": "ethPriceScaled",
"round": "false",
"excludeLogs": []
}
}
4. Transact To EVM Function
$ midpoint add-task transactToEvmFunctionTestnet post-eth-price-on-blockchain
We define the Transact to EVM Function Task to send the summary back to the contract.
{
"transactToEvmFunctionTestnet": {
"chainId": "80001",
"contractAddress": "0xC05f817a17bC0c505E48AC9CC52b5cb556F7B296",
"arguments": [
{
"name": "Midpoint_ID",
"datatype": "uint64",
"value": "{{Midpoint_ID}}"
},
{
"name": "EthPrice",
"datatype": "uint64",
"value": "{{ethPriceScaled}}"
}
],
"functionName": "latestEthPrice"
}
}
5. Define Path
We define our path file.
cron-job-source => get-eth-price
get-eth-price => shift-eth-price
shift-eth-price => post-eth-price-to-blockchain
6. Publish Midpoint
$ midpoint publish
Last updated
Was this helpful?