-
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.
Merge pull request #7 from compound-finance/jflatow/more-reporter
Start solidifying reporter and improve saddle slightly
- Loading branch information
Showing
10 changed files
with
129 additions
and
93 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
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,20 +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) => { | ||
let data = await getter(); | ||
let encoded = encode(keyType, valueType, +new Date(), getter()) | ||
let signature = sign(encoded, privateKey); | ||
res.json({ | ||
encoded, | ||
signature, | ||
[keyName]: data | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import {encode, sign} from '../src/reporter'; | ||
import {decode, encode, sign} from '../src/reporter'; | ||
|
||
test('encode', async () => { | ||
let encoded = encode('string', 'decimal', 12345678, {"eth": 5.0, "zrx": 10.0}); | ||
let decoded = decode('string', 'decimal', encoded); | ||
let [timestamp, pairs] = decoded; | ||
|
||
expect(encoded).toEqual('0x0000000000000000000000000000000000000000000000000000000000bc614e00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003657468000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000004563918244f400000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000037a7278000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000008ac7230489e80000'); | ||
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'); | ||
}); |
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,14 +1,9 @@ | ||
|
||
describe('View', () => { | ||
it('is a valid oracle', async () => { | ||
let oracle = await saddle.deploy('View', [], {from: saddle.account}); | ||
it('is a valid view', async () => { | ||
const oracle = await saddle.deploy('Oracle', []); | ||
const view = await saddle.deploy('View', [oracle.address, []]); | ||
|
||
expect(await oracle.methods.name.call()).toEqual('55'); | ||
}); | ||
|
||
it('is still a valid oracle', async () => { | ||
let oracle = await saddle.deploy('View', [], {from: saddle.account}); | ||
|
||
expect(await oracle.methods.name.call()).toEqual('66'); | ||
expect(await view.methods.oracle.call()).toEqual(oracle.address); | ||
}); | ||
}); |
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