Skip to content

Commit

Permalink
better readme (#12)
Browse files Browse the repository at this point in the history
* better readme

* change fake view address argument

* add required dependencies that were found elsewhere in the project when running locally but do not exist on ci

* fix typescript for ci
  • Loading branch information
coburncoburn authored Jun 26, 2019
1 parent 09d35f4 commit e711f02
Show file tree
Hide file tree
Showing 7 changed files with 1,049 additions and 44 deletions.
29 changes: 19 additions & 10 deletions poster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@ yarn add open-oracle-poster

## Running

To run as a standalone:

The poster requires 5 arguments to run, with one optional argument.
--sources, -s sources to pull price messages from, a list of https endpoints created by open oracle reporters serving open oracle payloads as json
--poster_key, -k Private key holding enough gas to post (try: `file:<file> or env:<env>)`
--view_function_name, -f Function signature for the view (e.g. postPrices(bytes[],bytes[],string[]))
--web3_provider Web 3 provider
--view_address address of open oracle view to post through
--timeout, -t how many seconds to wait before retrying with more gas, defaults to 180

To run as standalone from this project's root, simply invoke the start script.
```
TODO: make this more clear, as these are just notes from starting to integrate
# start reporter
yarn run start --private_key=0x5763aa1cb4c9cd141a1b409d92e5c5b967a28e18c70eb4cd965374ad75bff356 --script="examples/fixed.js"
yarn run start --view_address=0xa543d9701bb291E8F75CE2747A8E094bF042009A --poster_key=0x5763aa1cb4c9cd141a1b409d92e5c5b967a28e18c70eb4cd965374ad75bff356 --sources=http://localhost:3000 --view_function_name='postPrices(bytes[],bytes[],string[])' --web3_provider=http://localhost:8545
yarn run start --view_address=0xViewAddress --poster_key=0xWalletWithGas --sources=http://localhost:3000 --view_function_name='postPrices(bytes[],bytes[],string[])' --web3_provider=http://127.0.0.1:8545
```

Otherwise, you can include the DelFi poster in an app for configuration:

```typescript
import poster from 'delfi-poster';

// sources = [list of reporter urls]
poster.main();
import Web3 from 'web3';

// sample arguments, fill these in with real data :)
// let sources = [list of sources];
// let posterKey = ...a key to a wallet holding eth for gas;
// let viewAddress = "0xDelfiPriceView";
// let viewFunctionName = ...view function signature e.g. 'postPrices(bytes[],bytes[],string[])';
// let web3Provider = new Web3("web3Node.com", undefined, {transactionPollingTimeout: 180});
await poster.main(sources, posterKey, viewAddress, viewFunctionName, web3Provider);
```
12 changes: 10 additions & 2 deletions poster/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ module.exports = {
// globalTeardown: null,

// A set of global variables that need to be available in all test environments
// globals: {},
// globals: {
// 'ts-jest': {
// diagnostics: {
// warnOnly: true
// }
// }
// },

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
Expand Down Expand Up @@ -163,7 +169,9 @@ module.exports = {
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: null,
"transform": {
"^.+\\.tsx?$": "ts-jest"
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
Expand Down
8 changes: 7 additions & 1 deletion poster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
"license": "MIT",
"scripts": {
"start": "node ./.tsbuilt/index.js",
"test": "VCR_MODE=cache node ./node_modules/jest-cli/bin/jest.js"
"test": "VCR_MODE=cache jest"
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/node": "^12.0.10",
"jest": "^24.8.0",
"jest-junit": "^6.4.0",
"sepia": "^2.0.2",
"ts-jest": "^24.0.2",
"typescript": "^3.5.1"
},
"dependencies": {
"node-fetch": "^2.6.0",
"web3": "^1.0.0-beta.55",
"yargs": "^13.2.4"
}
}
14 changes: 6 additions & 8 deletions poster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import yargs from 'yargs';

async function run() {
const argv = yargs
.option('sources', {alias: 's', description: 'sources to pull price messages from', type: 'string'})
.option('poster_key', {alias: 'k', description: 'Private key (try: `file:<file> or env:<env>`', type: 'string'})
.option('view_function_name', {alias: 'f', description: 'Function signature for the view', type: 'string'})
.option('sources', {alias: 's', description: 'sources to pull price messages from, a list of https endpoints created by open oracle reporters serving open oracle payloads as json', type: 'string'})
.option('poster_key', {alias: 'k', description: 'Private key holding enough gas to post (try: `file:<file> or env:<env>)`', type: 'string'})
.option('view_function_name', {alias: 'f', description: 'Function signature for the view (e.g. postPrices(bytes[],bytes[],string[]))', type: 'string'})
.option('web3_provider', {description: 'Web 3 provider', type: 'string'})
.option('view_address', {description: 'address of view', type: 'string'})
.option('timeout', {alias: 't', description: 'how many secondsto wait before retrying', type: 'number', default: 180})
.option('view_address', {description: 'address of open oracle view to post through', type: 'string'})
.option('timeout', {alias: 't', description: 'how many seconds to wait before retrying with more gas', type: 'number', default: 180})
.help()
.alias('help', 'h')
.demandOption(['poster_key', 'sources', 'view_function_name', 'web3_provider', 'view_address'], 'Provide all the arguments')
.argv;

const web3 = await new Web3(argv.web3_provider, undefined, {});

// posting promise will reject and retry once with higher gas after this timeout
web3.eth.transactionPollingTimeout = argv.timeout;
const web3 = await new Web3(argv.web3_provider, undefined, {transactionPollingTimeout: argv.timeout});

if (argv.web3_provider === "http://127.0.0.1:8545") {
// confirm immediately in dev
Expand Down
7 changes: 4 additions & 3 deletions poster/src/poster.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {postWithRetries} from './postWithRetries';
import fetch from 'node-fetch';
import AbiCoder from 'web3-eth-abi';
import { AbiCoder } from 'web3-eth-abi';
import Web3 from 'web3';

async function main(sources : string,
Expand Down Expand Up @@ -53,8 +53,9 @@ function buildTrxData(payloads : DelFiReporterPayload[], functionName : string)
let symbols = new Set(payloads.map(x => Object.keys(x.prices)))

// see https://github.com/ethereum/web3.js/blob/2.x/packages/web3-eth-abi/src/AbiCoder.js#L112
return AbiCoder.encodeFunctionSignature(functionName) +
AbiCoder
const coder = new AbiCoder();
return coder.encodeFunctionSignature(functionName) +
coder
.encodeParameters(types, [messages, signatures, ...symbols])
.replace('0x', '');
}
Expand Down
19 changes: 10 additions & 9 deletions poster/tests/poster_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {buildTrxData, findTypes, fetchGasPrice, fetchPayloads} from '../src/poster';
import AbiCoder from 'web3-eth-abi';
import { AbiCoder } from 'web3-eth-abi';
require('sepia');

describe('loading poster arguments from environment and https', () => {
Expand Down Expand Up @@ -34,16 +34,16 @@ describe('loading poster arguments from environment and https', () => {

describe('building a function call', () => {
test('findTypes', () => {
let typeString = "writePrices(bytes[],bytes[],string[])"
expect(findTypes(typeString)).toEqual(["bytes[]", "bytes[]", "string[]"])
})
let typeString = "writePrices(bytes[],bytes[],string[])";
expect(findTypes(typeString)).toEqual(["bytes[]", "bytes[]", "string[]"]);
});

test('buildTrxData', () => {
let encodedMessage = '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10'
let encodedMessage = '0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10';

let signedMessage = '0x04a78a7b3013f6939da19eac6fd1ad5c5a20c41bcc5d828557442aad6f07598d029ae684620bec13e13d018cba0da5096626e83cfd4d5356d808d7437a0a5076000000000000000000000000000000000000000000000000000000000000001c'
let signedMessage = '0x04a78a7b3013f6939da19eac6fd1ad5c5a20c41bcc5d828557442aad6f07598d029ae684620bec13e13d018cba0da5096626e83cfd4d5356d808d7437a0a5076000000000000000000000000000000000000000000000000000000000000001c';

let prices = {"eth": "250", "zrx": "300"}
let prices = {"eth": "250", "zrx": "300"};

let data = buildTrxData(
[{encoded: encodedMessage, signature: signedMessage, prices: prices}],
Expand Down Expand Up @@ -72,8 +72,9 @@ describe('building a function call', () => {
"type": "function"
};

let officialWeb3Encoding =
AbiCoder.encodeFunctionCall(assumedAbi, [[encodedMessage], [signedMessage], Object.keys(prices)]);
// @ts-ignore-start
let officialWeb3Encoding = new AbiCoder().encodeFunctionCall(assumedAbi, [[encodedMessage], [signedMessage], Object.keys(prices)]);
// @ts-ignore-end

expect(data).toEqual(officialWeb3Encoding);
});
Expand Down
Loading

0 comments on commit e711f02

Please sign in to comment.