• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple data provider toolkit for Ethereum

TinyOracle

This is a simple example library serving as a guide on how to implement an oracle (aka data provider) with the current Ethereum infrastructure.

What are data providers? Essentially Ethereum contracts run in their own walled garden. They can communicate with each other, can save and retrieve data stored on the blockchain, but external data can only enter the system by interaction from the user (e.g. passing data to a method). Data providers (or oracles) are contracts which have a connection to the outside world. Other contracts can request outside data from them. For example making HTTP GET/POST transactions.

Use case

For example you want to enable your contract to query for external data, you might do the following (in Solidity):

import "api.sol";

contract SampleClient is usingTinyOracle {
  bytes public response;

  function __tinyOracleCallback(uint256 id, bytes _response) onlyFromTinyOracle external {
    response = _response;
  }

  function query() {
    string memory tmp = "hello world";
    query(bytes(tmp));
  }

  function query(bytes query) {
    queryTinyOracle(query);
  }
}

Whenever a query is submitted to queryTinyOracle, it will end up being caught by a server side listener. After processing, the response will be sent back to a specific method (the above __tinyOracleCallback) of the querying contract.

queryTinyOracle will return a uin256 identifier and the same will be available in the __tinyOracleCallback. This is a unique transaction id to enable running multiple calls from a client, because the whole process is asynchronous.

Solution

It is a fairly simple concept:

  • Listen for specific events
  • Transact with the caller

To accomplish this we need the following:

  • A node accessible with RPC
  • A handful of contracts, one of them being our trusted the endpoint for on-chain transactions and emitting an event
  • Listening to that specific event over RPC
  • Sending back a transaction to a pre-agreed method in the caller contract
  • Job done.

Step by step

  1. Have geth fully set up, including an account with ethers. (Shorthand in the following sections for this account is sender.)

  2. Start the RPC server and unlock the account sender used for sending responses:

geth --rpc --rpcaddr "127.0.0.1" --rpcport "8545" --unlock 0

For the account list use:

geth account list
  1. Deploy dispatch.sol. Take note of its address (shorthand is dispatch).

  2. Deploy lookup.sol. Take note of its address (shorthand is lookup). Call setQueryAddress() with dispatch, and setResponseAddress() with sender.

  3. Edit api.sol: replace the address of lookupContact with the value of lookup.

  4. Run the RPC listener & dispatch code:

tinyoracle --rpcport 8545 --rpchost 127.0.0.1 --interval 10 --dispatch <dispatch> --sender <sender>

Interval above is the frequency of polling for incoming requests, where 10 means every 10 seconds. Replace all the parameters with the ones set up earlier.

  1. Deploy sample-client.sol. Transact with query() and call getResponse() to verify the response was received.

  2. Next steps: further improve TinyOracle and send a pull request.

Important notes

As you are sending responses back as a transaction, which costs ether, it would make sense to charge the clients a fee.

This code is not intended for use in production. Any failure to process the event can mean it is lost forever and a response will never be sent. It is suggested to store the received events in a database and process the responses in a separate thread or application.

Offering data through this toolkit to the public will not make it a trusted source. Your user can trust it as long as it trusts you and that the lookup contract is controlled by you.

Future improvements

  1. Received events should be stored in a queue or database and processed asynchronously.

  2. There should be a storage for requests in the dispatcher, so that in case of RPC errors, events can still be recalled. This has a cost on storage.

Under the hood

TinyOracle is running on Node.js and uses json-rpc2 to communicate with the Ethereum RPC endpoint. Data is decoded and encoded using ethereumjs-abi.

Acknowledgements

Thanks goes to Oraclize.it who run an actual useful oracle service available today on Ethereum and have inspired this guide.

License

Copyright (C) 2015 Alex Beregszaszi

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

mango

Mango: Git, completely decentralised.
639
star
2

evm.elf

Assembly
76
star
3

yultsur

Yultsur is a Rust implementation of Yul.
Rust
56
star
4

ethereum-rsa

RSA signature verification in Ethereum
49
star
5

runevm

EVM interpreter compatible with the evm2wasm interface
Rust
42
star
6

weth3074

WETH3074 is a new version of the WETH contract which utilizes EIP-3074 to avoid the need for wrapping/unwrapping. In essence it turns ETH into an ERC-20 compatible token.
Solidity
39
star
7

shapeshiftbot

Simple Ethereum contract to transfer Ether to Bitcoin addresses via Shapeshift.io
38
star
8

ethereum-tokenescrow

A contract for running an escrow service for Ethereum token-contracts
29
star
9

density

Density is a library or collection of useful modifiers and methods for Solidity.
29
star
10

solc-rust

Rust bindings for the Solidity compiler.
Rust
27
star
11

mango-admin

CLI for Mango
JavaScript
22
star
12

tropicssl

Fork of the last BSD licensed release of PolarSSL / XySSL. This repository is an import from http://gitorious.org/tropicssl
C
21
star
13

eth2-deposit-contract

This is a port of the Vyper Eth 2.0 deposit contract to Solidity.
Solidity
18
star
14

puretea

Solidity
17
star
15

skyscanner

A Ruby wrapper for the Skyscanner API.
Ruby
17
star
16

bip32-path

Bitcoin BIP32 ('HD Wallet') path helpers.
JavaScript
15
star
17

rre2

RRE2 - Ruby RE2 binding
Ruby
14
star
18

blake2-solidity

This is a Solidity library aiming to implement BLAKE2 (using EIP-152).
Solidity
13
star
19

zkoisc

Implementing a Turing-complete computer (OISC) within a zk-SNARKS circuit.
12
star
20

git-remote-mango

Git remote helper for Mango
JavaScript
12
star
21

yevm

"Why EVM?" is an EVM compiler infrastructure.
11
star
22

mergeoracle

Solidity
10
star
23

dtiic

An Arduino project implementing an I2C-UART bridge compatible with Devantech's USB-ISS and USB-I2C.
Arduino
10
star
24

swarmgw

Simple Swarm interface to swarm-gateways.net (or other gateways)
JavaScript
10
star
25

compcard

Solidity
9
star
26

ethereum-bsm

Bitcoin Signed Message in Ethereum
JavaScript
7
star
27

rome2rio

A Ruby wrapper for the Rome2rio API.
Ruby
7
star
28

ruby-quota

Ruby-quota is a Ruby extension providing access to filesystem quota under Linux, FreeBSD, NetBSD, Dragonfly BSD, Solaris and Mac OS X.
C
7
star
29

ethereumjs-codesim

[OBSOLETE] A simple CLI app for running EVM simulations.
JavaScript
6
star
30

yamabi

Tool for translating YAML to Contract ABI and Solidity interfaces as per EIP-2069.
JavaScript
6
star
31

keccakjs

Keccak hash (SHA3) in Node.js and in the browser. Fast & simple.
JavaScript
6
star
32

rsnippets

Ruby (and other) Snippets
Ruby
5
star
33

solidity-rsa

5
star
34

web3-complete.js

This library tries to support all RPC methods not supported by by web3.js.
JavaScript
5
star
35

keccak-wasm

SHA-3 (Keccak padding) in WebAssembly WAST
4
star
36

swarmhash

Calculate the Swarm hash of data blobs.
JavaScript
4
star
37

offline-ipfs

JavaScript
4
star
38

scrypt.js

Scrypt in Node.js and in the browser. Fast & simple.
JavaScript
4
star
39

e3vm

EVM aka. "Embedded EVM-C-compatible Ethereum VM" (UNFINISHED)
C
3
star
40

lll-grammar

This is an attempt at specifying the grammar and semantics of LLL.
3
star
41

blockhash

Implementation of https://eips.ethereum.org/EIPS/eip-210 in Yul
3
star
42

daytona

Daytona is an EVMC compatible Ethereum VM kit.
Rust
3
star
43

sexpr-wasm-codegen

S-expressions to WebAssembly binary format converter
JavaScript
2
star
44

tb-dkim-headers

Thunderbird DKIM headers extension
JavaScript
2
star
45

julia-wasm-arithmetics

1
star
46

smartkard

Smartkard is a tiny Java library for easing the communication with ISO 7816 compatible smart cards.
1
star
47

console-web3

1
star
48

uleb128

LEB128 library designed for embedded devices.
C
1
star
49

eip1962-evmc

EIP-1962 implementation as an EVMC precompile module
Rust
1
star
50

arduino-xindajoy

This is a simple library for Arduino to support Joysticks.
C++
1
star
51

evm-bswap-golfing

Solidity
1
star
52

LuaReleases

Tracked Lua releases from version 1.0
C
1
star
53

afe

1
star
54

notes

JavaScript
1
star
55

lamed

1
star
56

soliditylang

HTML
1
star
57

safe-deployer

Solidity
1
star
58

standalone-parity-evm

This is Parity-Ethereum's EVM interpreter pulled out into a crate mapped to a stable upstream version
Rust
1
star
59

ewasm-assemblyscript-demo

JavaScript
1
star
60

etherboard

A contract inspired by Etherboard.io, combining a Ponzi-scheme with the MillionDollarHomepage.com
1
star
61

droidqr

Simple QRcode generator wrapper for Android
Java
1
star
62

trezor-react-pinpad

React component for displaying a pinpad for the Trezor hardware wallet.
JavaScript
1
star
63

evm-c-api

Deprecated. See: https://github.com/ethereum/evmc
1
star
64

ipaddr-buffer

A tiny shim over ipaddr.js providing convenience features for Node.js
JavaScript
1
star