diff --git a/package.json b/package.json index c4a042b..28eceeb 100644 --- a/package.json +++ b/package.json @@ -25,12 +25,10 @@ "pify": "^5.0.0" }, "devDependencies": { - "@ethereumjs/util": "^8.1.0", "@lavamoat/allow-scripts": "^2.5.1", "@metamask/auto-changelog": "^3.3.0", "@metamask/eth-json-rpc-middleware": "^12.0.0", "eth-block-tracker": "^8.0.0", - "ethjs-query": "^0.3.8", "ganache-core": "^2.13.2", "sinon": "^15.2.0", "tape": "^5.7.0" @@ -59,7 +57,8 @@ "ganache-core>websocket>bufferutil": false, "ganache-core>websocket>utf-8-validate": false, "ganache-core>ethereumjs-util>ethereum-cryptography>keccak": false, - "ganache-core>ethereumjs-util>ethereum-cryptography>secp256k1": false + "ganache-core>ethereumjs-util>ethereum-cryptography>secp256k1": false, + "ganache-core>web3-provider-engine>eth-block-tracker>json-rpc-engine>babelify>babel-core>babel-runtime>core-js": false } } } diff --git a/test/ganache.js b/test/ganache.js index 68ed837..54825a1 100644 --- a/test/ganache.js +++ b/test/ganache.js @@ -1,5 +1,4 @@ const test = require('tape') -const ethUtil = require('@ethereumjs/util') const { createTestSetup, asyncTest, @@ -9,25 +8,42 @@ const { test('LogFilter - basic', asyncTest(async (t) => { const tools = createTestSetup() - const eth = tools.query + const { sendAsync } = tools // deploy log-echo contract - const coinbase = await eth.coinbase() + const coinbase = await sendAsync({ method: 'eth_coinbase' }) const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase }) t.ok(contractAddress, 'got deployed contract address') // create filter - const blockNumber = (await eth.blockNumber()).toNumber() + const blockNumber = await sendAsync({ method: 'eth_blockNumber' }) const targetTopic = '0xaabbcce106361d4f6cd9098051596d565c1dbf7bc20b4c3acb3aaa4204aabbcc' - const filterParams = { address: contractAddress, topics: [targetTopic], fromBlock: blockNumber, toBlock: 'latest' } - const filterId = ethUtil.intToHex((await eth.newFilter(filterParams)).toNumber()) + const filterId = await sendAsync({ + method: 'eth_newFilter', + params: { + address: contractAddress, + topics: [targetTopic], + fromBlock: blockNumber, + toBlock: 'latest' + } + }) t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`) // trigger filter - const triggeringTxHash = await eth.sendTransaction({ from: coinbase, to: contractAddress, data: targetTopic }) + const triggeringTxHash = await sendAsync({ + method: 'eth_sendTransaction', + params: { + from: coinbase, + to: contractAddress, + data: targetTopic + } + }) await tools.trackNextBlock() // check filter - const filterChanges = await eth.getFilterChanges(filterId) + const filterChanges = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges.length, 1, 'only one matched filter') const matchingFilter = filterChanges[0] t.equal(matchingFilter.transactionHash, triggeringTxHash, 'tx hash should match') @@ -35,24 +51,31 @@ test('LogFilter - basic', asyncTest(async (t) => { const matchedTopic = matchingFilter.topics[0] t.equal(matchedTopic, targetTopic, 'topic matches expected') - await eth.uninstallFilter(filterId) + await sendAsync({ method: 'eth_uninstallFilter', params: [filterId] }) })) test('LogFilter - multiple blocks', asyncTest(async (t) => { const tools = createTestSetup() - const eth = tools.query + const { sendAsync } = tools // deploy log-echo contract - const coinbase = await eth.coinbase() + const coinbase = await sendAsync({ method: 'eth_coinbase' }) const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase }) t.ok(contractAddress, 'got deployed contract address') // create filter - const blockNumber = (await eth.blockNumber()).toNumber() + const blockNumber = await sendAsync({ method: 'eth_blockNumber' }) const targetTopic = '0x112233e106361d4f6cd9098051596d565c1dbf7bc20b4c3acb3aaa4204112233' - const filterParams = { address: contractAddress, topics: [targetTopic], fromBlock: blockNumber, toBlock: 'latest' } - const filterId = ethUtil.intToHex((await eth.newFilter(filterParams)).toNumber()) + const filterId = await sendAsync({ + method: 'eth_newFilter', + params: { + address: contractAddress, + topics: [targetTopic], + fromBlock: blockNumber, + toBlock: 'latest' + } + }) t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`) // await multiple blocks @@ -64,7 +87,14 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => { await tools.trackNextBlock() // trigger filter - const triggeringTxHash = await eth.sendTransaction({ from: coinbase, to: contractAddress, data: targetTopic }) + const triggeringTxHash = await sendAsync({ + method: 'eth_sendTransaction', + params: { + from: coinbase, + to: contractAddress, + data: targetTopic + } + }) await tools.trackNextBlock() // await multiple blocks @@ -74,7 +104,10 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => { await tools.trackNextBlock() // check filter - const filterChanges = await eth.getFilterChanges(filterId) + const filterChanges = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges.length, 1, 'only one matched filter') const matchingFilter = filterChanges[0] t.equal(matchingFilter.transactionHash, triggeringTxHash, 'tx hash should match') @@ -82,23 +115,26 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => { const matchedTopic = matchingFilter.topics[0] t.equal(matchedTopic, targetTopic, 'topic matches expected') - await eth.uninstallFilter(filterId) + await sendAsync({ method: 'eth_uninstallFilter', params: [filterId] }) })) test('BlockFilter - basic', asyncTest(async (t) => { const tools = createTestSetup() - const eth = tools.query + const { sendAsync } = tools // await first block await tools.trackNextBlock() // create filter - const filterId = ethUtil.intToHex((await eth.newBlockFilter()).toNumber()) + const filterId = await sendAsync({ method: 'eth_newBlockFilter', }) t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`) // check filter - const filterChanges1 = await eth.getFilterChanges(filterId) + const filterChanges1 = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges1.length, 0, 'no matched filters yet') // await one block @@ -106,12 +142,18 @@ test('BlockFilter - basic', asyncTest(async (t) => { await tools.trackNextBlock() // check filter - const filterChanges2 = await eth.getFilterChanges(filterId) + const filterChanges2 = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges2.length, 1, 'only one matched filter') const matchingFilter1 = filterChanges2[0] t.equal(matchingFilter1.length, 2 + 32 * 2, 'result is correct length for block hash') // check filter - const filterChanges3 = await eth.getFilterChanges(filterId) + const filterChanges3 = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges3.length, 0, 'matched filters reset') // await two blocks @@ -121,7 +163,10 @@ test('BlockFilter - basic', asyncTest(async (t) => { await tools.trackNextBlock() // check filter - const filterChanges4 = await eth.getFilterChanges(filterId) + const filterChanges4 = await sendAsync({ + method: 'eth_getFilterChanges', + params: [filterId] + }) t.equal(filterChanges4.length, 2, 'two matched filter') const matchingFilter2 = filterChanges4[0] const matchingFilter3 = filterChanges4[1] @@ -129,5 +174,8 @@ test('BlockFilter - basic', asyncTest(async (t) => { t.equal(matchingFilter3.length, 2 + 32 * 2, 'result is correct length for block hash') t.notEqual(matchingFilter2, matchingFilter3, 'hashes are different') - await eth.uninstallFilter(filterId) + await sendAsync({ + method: 'eth_uninstallFilter', + params: [filterId] + }) })) diff --git a/test/index.js b/test/index.js index 9f3bce6..c1a0131 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,4 @@ -process.on('unhandledRejection', function(err){ +process.on('unhandledRejection', function (err) { throw err }) diff --git a/test/subscriptions.js b/test/subscriptions.js index 108dd42..65e7b4d 100644 --- a/test/subscriptions.js +++ b/test/subscriptions.js @@ -1,7 +1,6 @@ const test = require('tape') const { createTestSetup, - createPayload, asyncTest, timeout, deployLogEchoContract, @@ -10,11 +9,8 @@ const { test('subscriptions - newHeads', asyncTest(async (t) => { const tools = createTestSetup() - const eth = tools.query const subs = tools.subs - const { blockTracker } = tools - // await first block await tools.forceNextBlock() await tools.trackNextBlock() @@ -57,11 +53,10 @@ test('subscriptions - newHeads', asyncTest(async (t) => { test('subscriptions - log', asyncTest(async (t) => { const tools = createTestSetup() - const eth = tools.query - const { query, subs, blockTracker } = tools + const { sendAsync, subs, blockTracker } = tools // deploy log-echo contract - const coinbase = await query.coinbase() + const coinbase = await sendAsync({ method: 'eth_coinbase' }) const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase }) t.ok(contractAddress, 'got deployed contract address') // deploy secondary "wrong" log contract @@ -84,15 +79,28 @@ test('subscriptions - log', asyncTest(async (t) => { t.equal(typeof subId, 'string', `got sub id as hex string (${typeof subId})`) // trigger matching log - const triggeringTxHash = await query.sendTransaction({ from: coinbase, to: contractAddress, data: targetTopic }) + const triggeringTxHash = await sendAsync({ + method: 'eth_sendTransaction', + params: { from: coinbase, to: contractAddress, data: targetTopic } + }) await tools.trackNextBlock() // trigger non-matching log - await query.sendTransaction({ from: coinbase, to: contractAddress, data: wrongTopic }) + await sendAsync({ + method: 'eth_sendTransaction', + params: { + from: coinbase, + to: contractAddress, + data: wrongTopic + } + }) await tools.trackNextBlock() // trigger non-matching contract - await query.sendTransaction({ from: coinbase, to: wrongContractAddress, data: targetTopic }) + await sendAsync({ + method: 'eth_sendTransaction', + params: { from: coinbase, to: wrongContractAddress, data: targetTopic } + }) await tools.trackNextBlock() // wait for subscription results to update diff --git a/test/util.js b/test/util.js index 4b2967c..5ed51cb 100644 --- a/test/util.js +++ b/test/util.js @@ -1,6 +1,6 @@ const EventEmitter = require('events') const { PollingBlockTracker } = require('eth-block-tracker') -const EthQuery = require('ethjs-query') +const EthQuery = require('@metamask/eth-query') const { JsonRpcEngine } = require('@metamask/json-rpc-engine') const { providerAsMiddleware } = require('@metamask/eth-json-rpc-middleware') const { providerFromEngine } = require('@metamask/eth-json-rpc-provider') @@ -19,7 +19,7 @@ module.exports = { deployLogEchoContract, } -function createTestSetup () { +function createTestSetup() { // raw data source const { ganacheProvider, forceNextBlock } = createEngineFromGanacheCore() // create block trackerfilterId @@ -31,6 +31,7 @@ function createTestSetup () { const engine = new JsonRpcEngine() const provider = providerFromEngine(engine) const query = new EthQuery(provider) + const sendAsync = pify(query.sendAsync.bind(query)); // add filter middleware engine.push(createFilterMiddleware({ blockTracker, provider })) // add subscription middleware @@ -43,7 +44,7 @@ function createTestSetup () { // subs helper const subs = createSubsHelper({ provider }) - return { ganacheProvider, forceNextBlock, engine, provider, query, subs, blockTracker, trackNextBlock } + return { ganacheProvider, forceNextBlock, engine, provider, sendAsync, subs, blockTracker, trackNextBlock } async function trackNextBlock() { return new Promise((resolve) => blockTracker.once('latest', resolve)) @@ -59,7 +60,7 @@ function createSubsHelper({ provider }) { } function createSubGenerator({ subType, provider }) { - return pify(function() { + return pify(function () { const args = [].slice.call(arguments) const cb = args.pop() args.unshift(subType) @@ -98,7 +99,7 @@ function createNewSub({ id, provider }) { } } -function createEngineFromGanacheCore () { +function createEngineFromGanacheCore() { const ganacheProvider = GanacheCore.provider() return { ganacheProvider, forceNextBlock } @@ -108,7 +109,7 @@ function createEngineFromGanacheCore () { } } -function createEngineFromTestBlockMiddleware () { +function createEngineFromTestBlockMiddleware() { const engine = new JsonRpcEngine() const testBlockSource = new TestBlockMiddleware() engine.push(testBlockSource.createMiddleware()) @@ -119,8 +120,8 @@ function createPayload(payload) { return Object.assign({ id: 1, jsonrpc: '2.0', params: [] }, payload) } -function asyncTest(asyncTestFn){ - return async function(t) { +function asyncTest(asyncTestFn) { + return async function (t) { try { await asyncTestFn(t) t.end() @@ -135,12 +136,21 @@ function timeout(duration) { } -async function deployLogEchoContract({ tools, from }){ +async function deployLogEchoContract({ tools, from }) { // https://github.com/kumavis/eth-needlepoint/blob/master/examples/emit-log.js - const eth = tools.query - const deployTxHash = await eth.sendTransaction({ from, data: '0x600e600c600039600e6000f336600060003760005160206000a1' }) + const { sendAsync } = tools; + const deployTxHash = await sendAsync({ + method: 'eth_sendTransaction', + params: { + from, + data: '0x600e600c600039600e6000f336600060003760005160206000a1' + } + }) await tools.trackNextBlock() - const deployTxRx = await eth.getTransactionReceipt(deployTxHash) + const deployTxRx = await sendAsync({ + method: 'eth_getTransactionReceipt', + params: [deployTxHash] + }) const contractAddress = deployTxRx.contractAddress return { deployTxHash, diff --git a/yarn.lock b/yarn.lock index 112991f..bc03487 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2841,40 +2841,6 @@ ethereumjs-wallet@0.6.5: utf8 "^3.0.0" uuid "^3.3.2" -ethjs-format@0.2.7: - version "0.2.7" - resolved "https://registry.yarnpkg.com/ethjs-format/-/ethjs-format-0.2.7.tgz#20c92f31c259a381588d069830d838b489774b86" - integrity sha512-uNYAi+r3/mvR3xYu2AfSXx5teP4ovy9z2FrRsblU+h2logsaIKZPi9V3bn3V7wuRcnG0HZ3QydgZuVaRo06C4Q== - dependencies: - bn.js "4.11.6" - ethjs-schema "0.2.1" - ethjs-util "0.1.3" - is-hex-prefixed "1.0.0" - number-to-bn "1.7.0" - strip-hex-prefix "1.0.0" - -ethjs-query@^0.3.8: - version "0.3.8" - resolved "https://registry.yarnpkg.com/ethjs-query/-/ethjs-query-0.3.8.tgz#aa5af02887bdd5f3c78b3256d0f22ffd5d357490" - integrity sha512-/J5JydqrOzU8O7VBOwZKUWXxHDGr46VqNjBCJgBVNNda+tv7Xc8Y2uJc6aMHHVbeN3YOQ7YRElgIc0q1CI02lQ== - dependencies: - babel-runtime "^6.26.0" - ethjs-format "0.2.7" - ethjs-rpc "0.2.0" - promise-to-callback "^1.0.0" - -ethjs-rpc@0.2.0: - version "0.2.0" - resolved "https://registry.yarnpkg.com/ethjs-rpc/-/ethjs-rpc-0.2.0.tgz#3d0011e32cfff156ed6147818c6fb8f801701b4c" - integrity sha512-RINulkNZTKnj4R/cjYYtYMnFFaBcVALzbtEJEONrrka8IeoarNB9Jbzn+2rT00Cv8y/CxAI+GgY1d0/i2iQeOg== - dependencies: - promise-to-callback "^1.0.0" - -ethjs-schema@0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/ethjs-schema/-/ethjs-schema-0.2.1.tgz#47e138920421453617069034684642e26bb310f4" - integrity sha512-DXd8lwNrhT9sjsh/Vd2Z+4pfyGxhc0POVnLBUfwk5udtdoBzADyq+sK39dcb48+ZU+2VgtwHxtGWnLnCfmfW5g== - ethjs-unit@0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/ethjs-unit/-/ethjs-unit-0.1.6.tgz#c665921e476e87bce2a9d588a6fe0405b2c41699" @@ -2883,14 +2849,6 @@ ethjs-unit@0.1.6: bn.js "4.11.6" number-to-bn "1.7.0" -ethjs-util@0.1.3: - version "0.1.3" - resolved "https://registry.yarnpkg.com/ethjs-util/-/ethjs-util-0.1.3.tgz#dfd5ea4a400dc5e421a889caf47e081ada78bb55" - integrity sha1-39XqSkANxeQhqInK9H4IGtp4u1U= - dependencies: - is-hex-prefixed "1.0.0" - strip-hex-prefix "1.0.0" - ethjs-util@0.1.6, ethjs-util@^0.1.3, ethjs-util@^0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/ethjs-util/-/ethjs-util-0.1.6.tgz#f308b62f185f9fe6237132fb2a9818866a5cd536"