Skip to content

Commit

Permalink
Attempt to clean up reporter API a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jflatow committed Jun 20, 2019
1 parent 09512c0 commit 797721f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions sdk/javascript/examples/fixed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = function fetchPrices() {
return Promise.resolve({'eth': 260.0, 'zrx': 0.58});
module.exports = async function fetchPrices() {
return [+new Date, {'eth': 260.0, 'zrx': 0.58}];
}
37 changes: 20 additions & 17 deletions sdk/javascript/src/express_endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import express from 'express';
import {encode, sign} from './reporter';

export function endpoint(path: string, privateKey: string, keyName: string, keyType: string, valueType: string, getter: () => object): express.Application {
// Create a new express application instance
const app: express.Application = express();

app.get(path, async (req, res) => {
const pairs = await getter();
const {
message,
signature
} = sign(encode(keyType, valueType, +new Date(), pairs), privateKey);
res.json({
message,
signature,
[keyName]: pairs
export function endpoint(
privateKey: string,
getter: () => Promise<[number, object]>,
name: string = 'prices',
path: string = `/${name}.json`,
keyType: string = 'string',
valueType: string = 'decimal'
): express.Application {
return express()
.get(path, async (req, res) => {
const [timestamp, pairs] = await getter();
const {
message,
signature
} = sign(encode(keyType, valueType, timestamp, pairs), privateKey);
res.json({
message,
signature,
[name]: pairs
});
});
});

return app;
}
45 changes: 26 additions & 19 deletions sdk/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import {endpoint} from './express_endpoint';
import yargs from 'yargs';
import {loadKey} from './key';
import * as fs from 'fs';
import * as path from 'path';
import * as Path from 'path';

const argv = yargs
.option('port', {alias: 'p', description: 'Port to listen on', type: 'number', default: 3000})
.option('private_key', {alias: 'k', description: 'Private key (try: `file:<file> or env:<env>`', type: 'string'})
.option('script', {alias: 's', description: 'Script for data', type: 'string'})
.option('path', {alias: 'u', description: 'Path for endpoint', type: 'string', default: '/'})
.option('key_type', {description: 'Key type to encode', type: 'string', default: 'string'})
.option('value_type', {description: 'Value type to encode', type: 'string', default: 'decimal'})
.help()
.alias('help', 'h')
.demandOption(['private_key', 'script'], 'Please provide both run and path arguments to work with this tool')
.argv;
.option('port', {alias: 'p', description: 'Port to listen on', type: 'number', default: 3000})
.option('private_key', {alias: 'k', description: 'Private key (try: `file:<file> or env:<env>`', type: 'string'})
.option('script', {alias: 's', description: 'Script for data', type: 'string'})
.option('name', {alias: 'n', description: 'Name for data feed', type: 'string', default: 'prices'})
.option('path', {alias: 'u', description: 'Path for endpoint', type: 'string', default: '/prices.json'})
.option('key_type', {description: 'Key type to encode', type: 'string', default: 'string'})
.option('value_type', {description: 'Value type to encode', type: 'string', default: 'decimal'})
.help()
.alias('help', 'h')
.demandOption(['private_key', 'script'], 'Please provide both run and path arguments to work with this tool')
.argv;

// Create a new express application instance
const app: express.Application = express();
Expand All @@ -25,17 +26,23 @@ function fetchEnv(name: string): string {
if (res) {
return res;
}
throw `Cannot find env var \`${name}\``;
throw `Cannot find env var "${name}"`;
}

async function start(port: number, privateKey: string, script: string, keyType: string, valueType: string) {
const fn: any = await import(path.join(process.cwd(), script));

app.use(endpoint(argv.path, privateKey, 'prices', keyType, valueType, fn.default));

async function start(
port: number,
privateKey: string,
script: string,
name: string,
path: string,
keyType: string,
valueType: string
) {
const fn: any = await import(Path.join(process.cwd(), script));
app.use(endpoint(privateKey, fn.default, name, path, keyType, valueType));
app.listen(port, function () {
console.log(`Reporter listening on port ${port}. Try running \`curl http://localhost:${port}${argv.path}\``);
console.log(`Reporter listening on port ${port}. Try running "curl http://localhost:${port}${path}"`);
});
}

start(argv.port, argv.private_key, argv.script, argv.key_type, argv.value_type);
start(argv.port, argv.private_key, argv.script, argv.name, argv.path, argv.key_type, argv.value_type);
23 changes: 9 additions & 14 deletions sdk/javascript/tests/express_endpoint_test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import express from 'express';
import fetch from 'node-fetch';
import {endpoint} from '../src/express_endpoint';

test('integration test', async () => {
let privateKey = '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10';
const privateKey = '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10';
const timestamp = +new Date(2019, 6, 20);

// Create a new express application instance
const app: express.Application = express();

async function fetchPrices() {
return {'eth': 260.0, 'zrx': 0.58};
async function fetchPrices(): Promise<[number, object]> {
return [timestamp, {'eth': 260.0, 'zrx': 0.58}];
}

app.use(endpoint('/prices.json', privateKey, 'prices', 'string', 'decimal', fetchPrices));

app.listen(10123, function () {});

let response = await fetch(`http://localhost:${10123}/prices.json`);
const port = 10123;
const app = endpoint(privateKey, fetchPrices).listen(port);
const response = await fetch(`http://localhost:${port}/prices.json`);

expect(response.ok).toBe(true);
expect(await response.json()).toEqual({
encoded: "0x0000000000000000000000000000000000000000000000000000016b3eabcf0e00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000",
message: "0x0000000000000000000000000000000000000000000000000000016c0e2e218000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000036574680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000e18398e76019000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000037a727800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000080069f78df770a3",
prices: {
eth: 260,
zrx: 0.58,
},
signature: "0x3c022277153248f28d96d6f0bbcde30789d7bef96e9f7ef2d0a93130bc4531dd2a0eff595fa3294556fbdb800ff81e359cb15e57df509ecd6a96eee30def6e12000000000000000000000000000000000000000000000000000000000000001b"
signature: "0xafb2aeb4bdf9d1fca04858d0db0f6023a94d1f6b6ce637641020044249f079002a680b038c6b0c6fe9d89bb6b7a4b1c74de9792db342d0223d6ba944f1d54361000000000000000000000000000000000000000000000000000000000000001c"
});
});
6 changes: 3 additions & 3 deletions sdk/javascript/tests/reporter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ test('encode', async () => {
let decoded = decode('string', 'decimal', encoded);
let [timestamp, pairs] = decoded;

expect(timestamp).numEquals(12345678); // XXX saddle not in this module
expect(timestamp.toString()).toEqual("12345678"); // XXX saddle not in this module: use numEquals
expect(pairs).toEqual([['eth', 5.0], ['zrx', 10.0]]);
});

test('sign', async () => {
let signed = sign('some data', '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10');
let {signature} = sign('some data', '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10');

expect(signed).toEqual('0x04a78a7b3013f6939da19eac6fd1ad5c5a20c41bcc5d828557442aad6f07598d029ae684620bec13e13d018cba0da5096626e83cfd4d5356d808d7437a0a5076000000000000000000000000000000000000000000000000000000000000001c');
expect(signature).toEqual('0x04a78a7b3013f6939da19eac6fd1ad5c5a20c41bcc5d828557442aad6f07598d029ae684620bec13e13d018cba0da5096626e83cfd4d5356d808d7437a0a5076000000000000000000000000000000000000000000000000000000000000001c');
});
2 changes: 1 addition & 1 deletion tsrc/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function deployContract(web3: Web3, network: string, from: string,
}

const contractAbi = JSON.parse(contractBuild.abi);
const contract = new web3.eth.Contract(contractAbi, undefined, {from, gasPrice: '1', gas: 1e6, data: ''});
const contract = new web3.eth.Contract(contractAbi, undefined, {from, gasPrice: '3000000000', gas: 1e6, data: ''});
return await contract.deploy({data: '0x' + contractBuild.bin, arguments: args}).send({from, gas: 2000000});
}

Expand Down

0 comments on commit 797721f

Please sign in to comment.