Friendly Ether Bot

Comments

Ethereum is a great system to achieve consensus between an independent peers, but if we want to pump data from the real world into the system , how exactly we do it ?

Let’s write a simple example of server bot to pump real price spot data into a special contract: PriceFeed.sol

The full example for the feed reporter can be clone from feed.reporter.ether.camp, we will just go over central code points :

  1. How to get info from the exchanges, we will use Poloniex exchange to get ETH price spots:
Code examplelink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    // Accessing the rest point
    RestTemplate restTemplate = new RestTemplate();
    String rpcEnd = "https://poloniex.com/public?command=returnTicker";

    HttpHeaders headers = new HttpHeaders();
    HttpEntity entity = new HttpEntity(headers);

    ResponseEntity<Map<String, PoloniexInstrument>> response = null;


    response = restTemplate.exchange(rpcEnd, GET, entity,
            new ParameterizedTypeReference<Map<String, PoloniexInstrument>>(){});

    // parsing retrieved data 
    Map<String, PoloniexInstrument> output = response.getBody();

    for (MarketAsset asset : assets) {
        PoloniexInstrument pi = output.get(asset.getSymbol());
        ret.add(new MarketData(asset, new Date(), Double.parseDouble(pi.getLast())));
    }

The full example can be seen here: PoloniexService.java

Now after we got the updated info from the real world exchange let’s push it into the Ethereum contract:

Code examplelink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Accessing Ethereum managed been 
Ethereum ethereum = ethereumBean.getEthereum();

CallTransaction.Function function = CallTransaction.Function.fromSignature("update",
        "bytes32[]", "uint[]", "uint[]");

byte[] accountAddr = userKey.getAddress();


BigInteger nonce = ethereum.getRepository().getNonce(accountAddr);
log.info("======= Nonce: " + nonce);
long t = System.currentTimeMillis();
String[] symbols = new String[lastData.size()];
int[] prices = new int[lastData.size()];
long[] timestamps = new long[lastData.size()];

for (int i = 0; i < lastData.size(); i++) {
    symbols[i] = lastData.get(i).getAsset().getSymbol();
    prices[i] = (int) (lastData.get(i).getLastPrice() * 1_000_000);
    timestamps[i] = Utils.toUnixTime(lastData.get(i).getTime().getTime());
}


Transaction tx = CallTransaction.createCallTransaction(
        nonce.longValue(),
        70_000_000_000L, // => gas price
        1_000_000,       // => gas limit
        feedAccount,     // => the contract address we actually updating
        1,               // => value,  can be zero
        function,        // => abi definition of the call
        symbols, prices, timestamps // => params to update: for each: symbol~(price, timestamp)
);

tx.sign(userKey.getPrivKeyBytes());

log.info("=> Sending: " + tx);
ethereum.submitTransaction(tx);

The full example can be seen here: Scheduler.java

That is actually all you need to make your own bot to transfer data from the real world to an ethereum network.

After the contract is updated with data any other contract can access it and be built on top of market real prices. As we discussed in this forum post: forum link

The last point we need to take care - is the actuall private key for the account which will fund the ongoing update transactions, here is how you should put it down in pricefeed.conf

Code examplelink
1
2
// if the private key is a seed sha3(value) will be calculated
user.account.privateKey = "my private key"

Now it’s good time to try and clone/run the repository:

Code example
1
2
3
git clone https://github.com/ether-camp/feed.reporter.ether.camp
cd feed.reporter.ether.camp
./gradlew bootRun

To really enjoy this example I am chalenging you to add info from more stock exchanges for assets that you care about and we will merge your code into the git repo.

For any question or comment regarding that example you can ask us here chat room, or by leaving a comment directly.

Comments