Blockchain oracles are third-party services that provide smart contracts with external information from outside the blockchain. They serve as a bridge between blockchains and the external world. An example use case demonstrated connecting a smart contract deployed on the Ropsten testnet to a Raspberry Pi device and safe via an oracle. When the "Unlock" event was detected from the smart contract, the oracle triggered the Raspberry Pi to blink an LED, and when the "Lock" event occurred it stopped the blinking. The full document provided details on oracle architecture and functions, different types of oracles, and code samples for interacting with the smart contract and Raspberry Pi.
3. DEFINITION
Blockchain Oracles are third-party services that
provide smart contracts with external information.
They serve as bridges between blockchains and
the outside world.
4. WHAT ORACLES DO?
Provide information to the smart contracts on the blockchain
Translate information from outside platforms
Trigger smart contracts
Deliver information from smart contracts to outside world
13. SMART CONTRACT
pragma solidity >=0.4.22 <0.6.0;
contract OpenSesame{
enum State { Locked, Unlocked }
State public state;
uint public value; //value to unlock
address payable public seller;
event Unlock();
event Lock();
…
14. SMART CONTRACT (UNLOCK AND
LOCK FUNCTIONS)
function unlock()
public
inState(State.Locked)
condition(msg.value >
value)
payable
{
state = State.Unlocked;
emit Unlock();
}
function lock(uint _value)
public
onlySeller
inState(State.Unlocked)
{
value = _value;
state = State.Locked;
emit Lock();
}
36. STAY IN TOUCH
Gene Leybzon https://www.linkedin.com/in/leybzon/
https://www.meetup.com/members/90744
20/
https://www.leybzon.com
37. CODE FOR THE EVENT LISTENER
1/3const Web3 = require("web3");
const fs = require('fs');
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(23, 'out'); //use GPIO pin 18, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms
const contractAddress = "0x032D472C05ff870cf800dbaD4B14A50c031432aB";
function blinkLED() { //function to start blinking
if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
LED.writeSync(1); //set pin state to 1 (turn LED on)
} else {
LED.writeSync(0); //set pin state to 0 (turn LED off)
}
}
38. CODE FOR THE EVENT LISTENER
2/3function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
LED.writeSync(0); // Turn LED off
//LED.unexport(); // Unexport GPIO to free resources
}
endBlink();
var abi = fs.readFileSync('OpenSesame_sol_OpenSesame.abi', 'utf8');
//console.log("ABI", abi);
const web3 = new Web3(new
Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws/v3/edb1d45f792244bfa97c13d84
a809090"))
var myContract = new web3.eth.Contract(JSON.parse(abi), contractAddress);
console.log("Listening for events on", contractAddress);
39. CODE FOR THE EVENT LISTENER
3/3myContract.events.allEvents()
.on('data', (event) => {
console.log("Event:", event);
console.log("Event Type:", event.event);
if (event.event == "Unlock") {
console.log("Will unlock the safe");
blinkLED();
}
if (event.event == "Lock") {
console.log("Will lock the safe");
endBlink();
}
})
.on('error', console.error);