Comment on page
Data Feed: Stream a data feed on-chain at regular intervals
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:
- 1.Time-based job scheduler defined in the CronJobTriggered Source definition.
MakeHttpRequest Task:
- 1.Makes an API request to the coinbase domain in a secure manner
- 2.Extracts the ethPrice from the API response
- 3.Sends the price to the Transact To EVM Function task
TransactToEvmFunction task:
- 1.Sends back the ethPrice to the contract via function defined by the user in the next block
$ npm install -g midpoint-cli
$ midpoint init blank my-data-feed
$ midpoint add-source cronJobTriggeredSource cron-job-source
{
"cronJobTriggeredSource": {
"crontab": "* * * * *",
"excludeLogs": [],
"extracts": [
{
"name": "Midpoint_ID",
"from": "Midpoint_ID"
}
]
}
}
2. Make Http Request
$ midpoint add-task makeHttpRequest get-eth-price
{
"makeHttpRequest": {
"urlRaw": "https://api.coinbase.com/v2/prices/ETH-USD/spot",
"method": "GET",
"urlType": "raw",
"extracts": [
{
"name": "ethPrice",
"from": "body.data.amount"
}
]
}
}
$ midpoint add-task makeHttpRequest shift-eth-price
{
"shiftDecimal": {
"inputValue": "{{ethPrice}}",
"decimalPlaces": "5",
"outputName": "ethPriceScaled",
"round": "false",
"excludeLogs": []
}
}
$ midpoint add-task transactToEvmFunctionTestnet post-eth-price-on-blockchain
{
"transactToEvmFunctionTestnet": {
"chainId": "80001",
"contractAddress": "0xC05f817a17bC0c505E48AC9CC52b5cb556F7B296",
"arguments": [
{
"name": "Midpoint_ID",
"datatype": "uint64",
"value": "{{Midpoint_ID}}"
},
{
"name": "EthPrice",
"datatype": "uint64",
"value": "{{ethPriceScaled}}"
}
],
"functionName": "latestEthPrice"
}
}
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
$ midpoint publish