diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..e9024cfb --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/poster/fixtures/generated/* binary diff --git a/poster/README.md b/poster/README.md index 6932bb5b..fffdde5e 100644 --- a/poster/README.md +++ b/poster/README.md @@ -22,7 +22,11 @@ yarn add open-oracle-poster To run as a standalone: ``` -open-oracle-poster --poster-key=0x... +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 ``` Otherwise, you can include the DelFi poster in an app for configuration: @@ -30,6 +34,6 @@ Otherwise, you can include the DelFi poster in an app for configuration: ```typescript import poster from 'delfi-poster'; -poster.configure(...); -poster.run(); +// sources = [list of reporter urls] +poster.main(); ``` diff --git a/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7 b/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7 new file mode 100644 index 00000000..3e626dc2 --- /dev/null +++ b/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7 @@ -0,0 +1 @@ +{"id":4,"jsonrpc":"2.0","result":{"number":"0x1","hash":"0x4dd036433dde8a2e04bf49f50f7543de20e8a66566bad42359fae6ec2a816ea3","parentHash":"0x37349fdc743a38f6df50dd9007448d90a33bf3482de842966dbb062c14908038","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x8199b911ba18a09653cf8814eb171fbf100674c382d7470b5de61feaa649d622","stateRoot":"0xdf07db4b5355fa2ed211e86e551dbef36ed9c38b590a4b77eff80870e7cba300","receiptsRoot":"0x113215a2409e6e5ea52204d23f975b8eabf1e358d257783576618ffbec9b98a9","miner":"0x0000000000000000000000000000000000000000","difficulty":"0x0","totalDifficulty":"0x0","extraData":"0x","size":"0x3e8","gasLimit":"0x6691b7","gasUsed":"0x7358","timestamp":"0x5d0ed39c","transactions":["0xc693cb5a9f8bea2fac46d003c5ebe63102704c66658b532e76a3f9a2f1272f8b"],"uncles":[]}} \ No newline at end of file diff --git a/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7.headers b/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7.headers new file mode 100644 index 00000000..7a81b061 --- /dev/null +++ b/poster/fixtures/generated/2ce40754482b0a537231820f6654abe7.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:19:25 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 6, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "79" + } + } +} \ No newline at end of file diff --git a/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2 b/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2 new file mode 100644 index 00000000..c318e9d3 --- /dev/null +++ b/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2 @@ -0,0 +1,5 @@ +{ + "id": 83, + "jsonrpc": "2.0", + "result": "0x3d" +} \ No newline at end of file diff --git a/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2.headers b/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2.headers new file mode 100644 index 00000000..08e1be46 --- /dev/null +++ b/poster/fixtures/generated/9333decc3dccc9ae33e242437f381fc2.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:12:50 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 20, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "59" + } + } +} \ No newline at end of file diff --git a/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678 b/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678 new file mode 100644 index 00000000..2e7523ab --- /dev/null +++ b/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678 @@ -0,0 +1 @@ +{"id":2,"jsonrpc":"2.0","result":"0xc693cb5a9f8bea2fac46d003c5ebe63102704c66658b532e76a3f9a2f1272f8b"} \ No newline at end of file diff --git a/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678.headers b/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678.headers new file mode 100644 index 00000000..43513151 --- /dev/null +++ b/poster/fixtures/generated/a679986b5a9be0e6c78da47f6cf9d678.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:19:24 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 30, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "1060" + } + } +} \ No newline at end of file diff --git a/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8 b/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8 new file mode 100644 index 00000000..67a810bb --- /dev/null +++ b/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8 @@ -0,0 +1 @@ +{"id":1,"jsonrpc":"2.0","result":"0x0"} \ No newline at end of file diff --git a/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8.headers b/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8.headers new file mode 100644 index 00000000..3d769c9a --- /dev/null +++ b/poster/fixtures/generated/a6b51e6cf1af8db558e61ce3b53c17e8.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:19:24 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 16, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "124" + } + } +} \ No newline at end of file diff --git a/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a b/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a new file mode 100644 index 00000000..711651d1 --- /dev/null +++ b/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a @@ -0,0 +1 @@ +{"id":3,"jsonrpc":"2.0","result":{"transactionHash":"0xc693cb5a9f8bea2fac46d003c5ebe63102704c66658b532e76a3f9a2f1272f8b","transactionIndex":"0x0","blockHash":"0x4dd036433dde8a2e04bf49f50f7543de20e8a66566bad42359fae6ec2a816ea3","blockNumber":"0x1","from":"0x864f667f63b8650e10a0e52910f01198dab19d69","to":"0x4fe3dd76d873caf6cbf56e442b2c808d3984df1d","gasUsed":"0x7358","cumulativeGasUsed":"0x7358","contractAddress":null,"logs":[],"status":"0x1","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","v":"0x9e","r":"0x13315249d203595fae8d58dfbad79460c5a24f395e7317b72409f0cb770caee6","s":"0x470d1bb57af3ecbd20ac13fc22dca5f023f7cc5533a17dffd03959c8f585b545"}} \ No newline at end of file diff --git a/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a.headers b/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a.headers new file mode 100644 index 00000000..7a2d6295 --- /dev/null +++ b/poster/fixtures/generated/beccb3def7ec8ebb7a7ba3249a4a745a.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:19:25 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 13, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "141" + } + } +} \ No newline at end of file diff --git a/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea b/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea new file mode 100644 index 00000000..67a810bb --- /dev/null +++ b/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea @@ -0,0 +1 @@ +{"id":1,"jsonrpc":"2.0","result":"0x0"} \ No newline at end of file diff --git a/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea.headers b/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea.headers new file mode 100644 index 00000000..375974bc --- /dev/null +++ b/poster/fixtures/generated/cfc0b88c86c6856dd629d1863ba943ea.headers @@ -0,0 +1,21 @@ +{ + "statusCode": 200, + "headers": { + "content-type": "application/json", + "date": "Sun, 23 Jun 2019 01:17:48 GMT", + "connection": "keep-alive", + "transfer-encoding": "chunked" + }, + "url": "http://localhost:8545/", + "time": 12, + "request": { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Connection": "keep-alive", + "Host": "localhost:8545", + "User-Agent": "Mozilla/5.0 (Darwin x64) node.js/12.4.0 v8/7.4.288.27-node.18", + "Content-Length": "124" + } + } +} \ No newline at end of file diff --git a/poster/package.json b/poster/package.json index 281ea4e7..25fad0a7 100644 --- a/poster/package.json +++ b/poster/package.json @@ -7,8 +7,8 @@ "author": "Compound Labs, Inc.", "license": "MIT", "scripts": { - "start": "node ./.tsbuilt/poster.js", - "test": "VCR_MODE=playback node ./node_modules/jest-cli/bin/jest.js" + "start": "node ./.tsbuilt/index.js", + "test": "VCR_MODE=cache node ./node_modules/jest-cli/bin/jest.js" }, "devDependencies": { "@types/jest": "^24.0.13", diff --git a/poster/src/index.ts b/poster/src/index.ts index 559c5980..fa086789 100644 --- a/poster/src/index.ts +++ b/poster/src/index.ts @@ -1,81 +1,43 @@ -import {postWithRetries} from './postWithRetries'; -import fetch from 'node-fetch'; -import AbiCoder from 'web3-eth-abi'; - -async function main () { - const viewAddress = getEnvVar("view-address"); - const senderKey = getEnvVar("poster-key"); - const sources = getEnvVar("sources").split(","); - const functionName = getEnvVar("view-function-name"); - const web3Provider = getEnvVar("web3-provider"); - - const payloads = await fetchPayloads(sources); - const gasPrice = await fetchGasPrice(); - const trxData = buildTrxData(payloads, functionName); - - const trx = { - data: trxData, - to: viewAddress, - gasPrice: gasPrice, - gas: 1_000_000 +import {main} from './poster'; +import Web3 from 'web3'; +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: or env:`', type: 'string'}) + .option('view_function_name', {alias: 'f', description: 'Function signature for the view', 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}) + .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; + + if (argv.web3_provider === "http://127.0.0.1:8545") { + // confirm immediately in dev + web3.eth.transactionConfirmationBlocks = 1 + + // monkey patch web3 since ganache does not implement eth_chainId + // https://github.com/trufflesuite/ganache-core/issues/339 + // https://github.com/tcichowicz/ganache-core/commit/15d740cc5bdca86c87c3c9fd79bbce5f785b105e + const originalSend = web3.eth.currentProvider.send; + web3.eth.currentProvider.send = async function (method, parameters) { + if (method === "eth_chainId") { + return "0x" + Number(1337).toString(16) + } else { + return originalSend.call(web3.eth.currentProvider, method, parameters); + } + } } - return postWithRetries(trx, senderKey, web3Provider); -} - -async function fetchPayloads(sources : string[]) : Promise { - let sourcePromises = sources.map(async (source) => { - let response = await fetch(source); - return response.json(); - }); - - return await Promise.all(sourcePromises) -} - -async function fetchGasPrice() : Promise { - let source = "https://api.compound.finance/api/gas_prices/get_gas_price"; - let response = await fetch(source); - let prices = await response.json(); - let averagePrice = Number(prices["average"]["value"]); - return averagePrice; + return await main(argv.sources, argv.poster_key, argv.view_address, argv.view_function_name, web3); } -function buildTrxData(payloads : OpenOraclePayload[], functionName : string) : string { - const types = findTypes(functionName); - - let messages = payloads.map(x => x.message); - let signatures = payloads.map(x => x.signature); - 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 - .encodeParameters(types, [messages, signatures, ...symbols]) - .replace('0x', ''); -} - -// e.g. findTypes("postPrices(bytes[],bytes[],string[])")-> ["bytes[]","bytes[]","string[]"] -function findTypes(functionName : string) : string[] { - let start = functionName.indexOf("(") + 1; - let types = functionName - .slice(start, functionName.lastIndexOf(")")) - .split(","); - return types -} - -function getEnvVar(name : string): string { - const result: string | undefined = process.env[name]; - - if (result) { - return result; - } else { - throw `Missing required env var: ${name}`; - } -} - -export { - buildTrxData, - findTypes, - fetchGasPrice, - fetchPayloads -} +run() diff --git a/poster/src/interfaces.ts b/poster/src/interfaces.ts index af28e9ae..2750a235 100644 --- a/poster/src/interfaces.ts +++ b/poster/src/interfaces.ts @@ -10,9 +10,9 @@ // A payload for an open oracle view comprises 2 fields: // 1. Abi encoded values to be written to the open oracle data contract // 2. The attestor's signature on a hash of that message -interface OpenOraclePayload { +interface DelFiReporterPayload { // ABI encoded values to be written to the open oracle data contract. - message: string, + encoded: string, // The signature of the attestor to these values. The values in 'message' // will be stored in a mapping under this signer's public address. signature: string, @@ -28,7 +28,6 @@ interface Trx { gasPrice: number, gas: number, to: string, - chainId: string, data: string } @@ -41,3 +40,7 @@ interface TrxReceipt { [event: string]: Event } } + +interface Event { + returnValues: { [key: string]: any } +} diff --git a/poster/src/postWithRetries.ts b/poster/src/postWithRetries.ts index 3862ca57..2ece27eb 100644 --- a/poster/src/postWithRetries.ts +++ b/poster/src/postWithRetries.ts @@ -1,45 +1,36 @@ -const Web3 = require('web3'); -const pTimeout = require('p-timeout'); - -// The purpose of this program is to successfully submit a transaction by -// calling the provided function on the provided contract with the provided data - -// The core logic is around retries due to gas price -async function postWithRetries(transaction : Trx, signerKey : string, web3Provider: string) { - console.log("Running Open Oracle Poster..."); - - const timeoutMessage = "Transaction receipt not available after 2 minutes" +import Web3 from 'web3'; + +async function postWithRetries(transaction : Trx, signerKey : string, web3 : Web3) { + console.log("Running Open Oracle Poster..."); try { - let transactionPromise = signAndSend(transaction, signerKey, web3Provider); - - // If no receipt is available after 3 minutes, it is not likely to be mined - // at current price in reasonable time frame. - const timeoutMilliseconds = 180_000; - return pTimeout(transactionPromise, timeoutMilliseconds, timeoutMessage); + return signAndSend(transaction, signerKey, web3); } catch (e) { // If higher gas price will help, try again. Otherwise, really throw. - if (["Returned error: replacement transaction underpriced", - timeoutMessage].includes(e.message) - ) { - transaction.gasPrice = Math.floor(transaction.gasPrice * 1.2); - return signAndSend(transaction, signerKey, web3Provider); + if (e.message === "Returned error: replacement transaction underpriced") { + return replaceTransaction(transaction, signerKey, web3); + } else if (/Error: Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!/.test(e.error)){ + return replaceTransaction(transaction, signerKey, web3); } else { throw(e); } } + console.log("Completed run of Open Oracle Poster"); } -async function signAndSend(transaction : Trx, signerKey : string, web3Provider :string ): Promise { - const web3 = new Web3(web3Provider); - // set provider +async function signAndSend(transaction : Trx, signerKey : string, web3 ): Promise { let signedTransaction = await web3 .eth .accounts .signTransaction(transaction, signerKey); - return web3.eth.sendSignedTransaction(signedTransaction.rawTransaction); + return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction); +} + +async function replaceTransaction(transaction : Trx, signerKey : string, web3 ): Promise { + transaction.gasPrice = Math.floor(transaction.gasPrice * 0.2); + return signAndSend(transaction, signerKey, web3); } export { diff --git a/poster/src/poster.ts b/poster/src/poster.ts new file mode 100644 index 00000000..fc6686f2 --- /dev/null +++ b/poster/src/poster.ts @@ -0,0 +1,115 @@ +import {postWithRetries} from './postWithRetries'; +import fetch from 'node-fetch'; +import AbiCoder from 'web3-eth-abi'; +import Web3 from 'web3'; + +async function main(sources : string, + senderKey : string, + viewAddress : string, + functionName : string, + web3 : Web3 ) { + const payloads = await fetchPayloads(sources.split(",")); + const gasPrice = await fetchGasPrice(); + const trxData = buildTrxData(payloads, functionName); + + const trx = { + data: trxData, + to: viewAddress, + gasPrice: gasPrice, + gas: 1_000_000 + } + + return await postWithRetries(trx, senderKey, web3); +} + +async function fetchPayloads(sources : string[]) : Promise { + let sourcePromises = sources.map(async (source) => { + let response = await fetch(source); + return response.json(); + }); + + return await Promise.all(sourcePromises) +} + +async function fetchGasPrice() : Promise { + try { + let source = "https://api.compound.finance/api/gas_prices/get_gas_price"; + let response = await fetch(source); + let prices = await response.json(); + let averagePrice = Number(prices["average"]["value"]); + return averagePrice; + } catch (e) { + // use 3 gwei if api is unreachable for some reason + console.error(e); + return 3_000_000_000; + } +} + +function buildTrxData(payloads : DelFiReporterPayload[], functionName : string) : string { + const types = findTypes(functionName); + + let messages = payloads.map(x => x.encoded); + let signatures = payloads.map(x => x.signature); + 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 + .encodeParameters(types, [messages, signatures, ...symbols]) + .replace('0x', ''); +} + +// e.g. findTypes("postPrices(bytes[],bytes[],string[])")-> ["bytes[]","bytes[]","string[]"] +function findTypes(functionName : string) : string[] { + // this unexported function from ethereumjs-abi is copy pasted from source + // see https://github.com/ethereumjs/ethereumjs-abi/blob/master/lib/index.js#L81 + let parseSignature = function (sig) { + var tmp = /^(\w+)\((.*)\)$/.exec(sig) || []; + + if (tmp.length !== 3) { + throw new Error('Invalid method signature') + } + + var args = /^(.+)\):\((.+)$/.exec(tmp[2]) + + if (args !== null && args.length === 3) { + return { + method: tmp[1], + args: args[1].split(','), + retargs: args[2].split(',') + } + } else { + var params = tmp[2].split(',') + if (params.length === 1 && params[0] === '') { + // Special-case (possibly naive) fixup for functions that take no arguments. + // TODO: special cases are always bad, but this makes the function return + // match what the calling functions expect + params = [] + } + return { + method: tmp[1], + args: params + } + } + } + + return parseSignature(functionName).args; +} + +function getEnvVar(name : string): string { + const result: string | undefined = process.env[name]; + + if (result) { + return result; + } else { + throw `Missing required env var: ${name}`; + } +} + +export { + buildTrxData, + findTypes, + fetchGasPrice, + fetchPayloads, + main +} diff --git a/poster/tests/post_with_retries_test.ts b/poster/tests/post_with_retries_test.ts index b492d3e8..ec26f028 100644 --- a/poster/tests/post_with_retries_test.ts +++ b/poster/tests/post_with_retries_test.ts @@ -1,32 +1,27 @@ import {postWithRetries, signAndSend} from '../src/postWithRetries'; -const Web3 = require('web3'); +import Web3 from 'web3'; require('sepia'); describe('posting', () => { test('signs and sends', async () => { const provider = "http://localhost:8545"; - const web3 = await new Web3(provider); - let senderKey = "0xcf1eebe7f2053fd82c1ee6dff3b7e4a46820049ec5ff1abc036abd439b88167e" + const web3 = await new Web3(provider, undefined, { }); + web3.eth.transactionConfirmationBlocks = 1 + + let senderKey = "0x7f888bbe5b812b1f8439df461d9335b30edc0f24ad65d23334e61bf107f45bb5" // sends the transaction const data = "0xa59d56ad000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf1000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006004a78a7b3013f6939da19eac6fd1ad5c5a20c41bcc5d828557442aad6f07598d029ae684620bec13e13d018cba0da5096626e83cfd4d5356d808d7437a0a5076000000000000000000000000000000000000000000000000000000000000001c" - try { let receipt = await signAndSend({ data: data, - to: "0xbF380fD4776f6107Ae869270d1AEB90164589128", + to: "0x4fe3dd76d873CAF6Cbf56E442B2C808D3984df1D", gasPrice: 10_000_000, - gas: 100_000, - chainId: "0x" + Number(1337).toString(16) - }, senderKey, provider); - + gas: 100_000 + }, senderKey, web3); - expect(receipt).toEqual([]); - } - catch(e) { - console.log(e) - // TODO: post a valid message after we finalize the view - // console.log(e) - } - }); + expect(receipt).toEqual( + {"blockHash": "0x4dd036433dde8a2e04bf49f50f7543de20e8a66566bad42359fae6ec2a816ea3", "blockNumber": 1, "contractAddress": null, "cumulativeGasUsed": 29528, "from": "0x864f667F63B8650e10A0E52910f01198dAb19d69", "gas": undefined, "gasUsed": 29528, "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "nonce": undefined, "r": "0x13315249d203595fae8d58dfbad79460c5a24f395e7317b72409f0cb770caee6", "s": "0x470d1bb57af3ecbd20ac13fc22dca5f023f7cc5533a17dffd03959c8f585b545", "status": true, "to": "0x4fe3dd76d873CAF6Cbf56E442B2C808D3984df1D", "transactionHash": "0xc693cb5a9f8bea2fac46d003c5ebe63102704c66658b532e76a3f9a2f1272f8b", "transactionIndex": 0, "v": "0x9e"} + ); + }); }); diff --git a/poster/tests/poster_test.ts b/poster/tests/poster_test.ts index 89c2a9ea..580df3d2 100644 --- a/poster/tests/poster_test.ts +++ b/poster/tests/poster_test.ts @@ -1,11 +1,11 @@ -import {buildTrxData, findTypes, fetchGasPrice, fetchPayloads} from '../src/index'; +import {buildTrxData, findTypes, fetchGasPrice, fetchPayloads} from '../src/poster'; import AbiCoder from 'web3-eth-abi'; require('sepia'); describe('loading poster arguments from environment and https', () => { test('fetchGasPrice', async () => { let gasPrice = await fetchGasPrice(); - expect(gasPrice).toEqual(10_000_000_000); + expect(gasPrice).toEqual(10000000000); }); test('fetchPayloads', async () => { @@ -46,9 +46,8 @@ describe('building a function call', () => { let prices = {"eth": "250", "zrx": "300"} let data = buildTrxData( - [{message: encodedMessage, signature: signedMessage, prices: prices}], + [{encoded: encodedMessage, signature: signedMessage, prices: prices}], "writePrices(bytes[],bytes[],string[])"); - console.log("built") let assumedAbi = { "constant": false,