-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to clean up reporter API a bit
- Loading branch information
Showing
6 changed files
with
61 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters