Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt to eip-1193 provider changes #170

Merged
merged 20 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions getBlocksForRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,18 @@ function intToHex(int) {
return '0x' + hexString
}

function sendAsync(provider, request) {
return new Promise((resolve, reject) => {
provider.sendAsync(request, (error, response) => {
if (error) {
reject(error);
} else if (response.error) {
reject(response.error);
} else if (response.result) {
resolve(response.result);
} else {
reject(new Error("Result was empty"));
}
});
});
}

async function query(provider, method, params) {
for (let i = 0; i < 3; i++) {
try {
return await sendAsync(provider, {
return provider.request({
id: 1,
jsonrpc: "2.0",
method,
params,
});
} catch (error) {
console.error(
`provider.sendAsync failed: ${error.stack || error.message || error}`
`provider.request failed: ${error.stack || error.message || error}`
);
}
}
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@metamask/auto-changelog": "^3.3.0",
"@metamask/eth-json-rpc-middleware": "^13.0.0",
"@metamask/eth-block-tracker": "~10.0.0 || ^10.1.1",
"ganache-cli": "^6.12.2",
"ganache": "^7.9.2",
"sinon": "^15.2.0",
"tape": "^5.7.0"
},
Expand All @@ -52,8 +52,14 @@
"ethereumjs-util>ethereum-cryptography>keccak": false,
"ethereumjs-util>ethereum-cryptography>secp256k1": false,
"ethjs-query>babel-runtime>core-js": false,
"ganache-cli>ethereumjs-util>ethereum-cryptography>keccak": false,
"ganache-cli>ethereumjs-util>ethereum-cryptography>secp256k1": false
"ganache>keccak": false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New ganache config has been added to fix.

@lavamoat/allow-scripts has detected dependencies without configuration. explicit configuration required.
run "allow-scripts auto" to automatically populate the configuration.

packages missing configuration:
- ganache>@trufflesuite/bigint-buffer [1 location(s)]
- ganache>@trufflesuite/uws-js-unofficial>bufferutil [1 location(s)]
- ganache>@trufflesuite/uws-js-unofficial>utf-8-validate [1 location(s)]
- ganache>bufferutil [1 location(s)]
- ganache>leveldown [1 location(s)]
- ganache>utf-8-validate [1 location(s)]
error Command failed with exit code 1.

"ganache>secp256k1": false,
"ganache>@trufflesuite/bigint-buffer": false,
"ganache>@trufflesuite/uws-js-unofficial>bufferutil": false,
"ganache>@trufflesuite/uws-js-unofficial>utf-8-validate": false,
"ganache>bufferutil": false,
"ganache>leveldown": false,
"ganache>utf-8-validate": false
}
},
"packageManager": "[email protected]"
Expand Down
71 changes: 39 additions & 32 deletions test/ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ const {
test('LogFilter - basic', asyncTest(async (t) => {

const tools = createTestSetup()
const { sendAsync } = tools
const { request } = tools

// deploy log-echo contract
const coinbase = await sendAsync({ method: 'eth_coinbase' })
const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase })
const accounts = await request({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was throwing Error: Returned error: sender account not recognized, when coinbase (0x000....) supplied as a from account. So coinbase account has been replaced with first account from the accounts array.

method: "eth_accounts",
params: []
});
const fromAccount = accounts[0];
const { contractAddress } = await deployLogEchoContract({ tools, from: fromAccount })
t.ok(contractAddress, 'got deployed contract address')

// create filter
const blockNumber = await sendAsync({ method: 'eth_blockNumber' })
const blockNumber = await request({ method: 'eth_blockNumber' })
const targetTopic = '0xaabbcce106361d4f6cd9098051596d565c1dbf7bc20b4c3acb3aaa4204aabbcc'
const filterId = await sendAsync({
const filterId = await request({
method: 'eth_newFilter',
params: {
address: contractAddress,
Expand All @@ -30,17 +34,17 @@ test('LogFilter - basic', asyncTest(async (t) => {
t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`)

// trigger filter
const triggeringTxHash = await sendAsync({
const triggeringTxHash = await request({
method: 'eth_sendTransaction',
params: {
from: coinbase,
params: [{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was throwing Error: Incorrect number of arguments. 'eth_sendTransaction' requires exactly 1 argument error without the [].

from: fromAccount,
to: contractAddress,
data: targetTopic
}
}]
})
await tools.trackNextBlock()
// check filter
const filterChanges = await sendAsync({
const filterChanges = await request({
method: 'eth_getFilterChanges',
params: [filterId]
})
Expand All @@ -51,30 +55,34 @@ test('LogFilter - basic', asyncTest(async (t) => {
const matchedTopic = matchingFilter.topics[0]
t.equal(matchedTopic, targetTopic, 'topic matches expected')

await sendAsync({ method: 'eth_uninstallFilter', params: [filterId] })
await request({ method: 'eth_uninstallFilter', params: [filterId] })
}))

test('LogFilter - multiple blocks', asyncTest(async (t) => {

const tools = createTestSetup()
const { sendAsync } = tools
const { request } = tools

// deploy log-echo contract
const coinbase = await sendAsync({ method: 'eth_coinbase' })
const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase })
const accounts = await request({
method: "eth_accounts",
params: []
});
const fromAcccount = accounts[0];
const { contractAddress } = await deployLogEchoContract({ tools, from: fromAcccount })
t.ok(contractAddress, 'got deployed contract address')

// create filter
const blockNumber = await sendAsync({ method: 'eth_blockNumber' })
const blockNumber = await request({ method: 'eth_blockNumber' })
const targetTopic = '0x112233e106361d4f6cd9098051596d565c1dbf7bc20b4c3acb3aaa4204112233'
const filterId = await sendAsync({
const filterId = await request({
method: 'eth_newFilter',
params: {
params: [{
address: contractAddress,
topics: [targetTopic],
fromBlock: blockNumber,
toBlock: 'latest'
}
}]
})
t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`)

Expand All @@ -87,13 +95,13 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => {
await tools.trackNextBlock()

// trigger filter
const triggeringTxHash = await sendAsync({
const triggeringTxHash = await request({
method: 'eth_sendTransaction',
params: {
from: coinbase,
params: [{
from: fromAcccount,
to: contractAddress,
data: targetTopic
}
}]
})
await tools.trackNextBlock()

Expand All @@ -104,7 +112,7 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => {
await tools.trackNextBlock()

// check filter
const filterChanges = await sendAsync({
const filterChanges = await request({
method: 'eth_getFilterChanges',
params: [filterId]
})
Expand All @@ -115,23 +123,23 @@ test('LogFilter - multiple blocks', asyncTest(async (t) => {
const matchedTopic = matchingFilter.topics[0]
t.equal(matchedTopic, targetTopic, 'topic matches expected')

await sendAsync({ method: 'eth_uninstallFilter', params: [filterId] })
await request({ method: 'eth_uninstallFilter', params: [filterId] })
}))

test('BlockFilter - basic', asyncTest(async (t) => {

const tools = createTestSetup()
const { sendAsync } = tools
const { request } = tools

// await first block
await tools.trackNextBlock()

// create filter
const filterId = await sendAsync({ method: 'eth_newBlockFilter', })
const filterId = await request({ method: 'eth_newBlockFilter', })
t.ok(filterId, `got filter id: ${filterId} (${typeof filterId})`)

// check filter
const filterChanges1 = await sendAsync({
const filterChanges1 = await request({
method: 'eth_getFilterChanges',
params: [filterId]
})
Expand All @@ -140,17 +148,16 @@ test('BlockFilter - basic', asyncTest(async (t) => {
// await one block
await tools.forceNextBlock()
await tools.trackNextBlock()

// check filter
const filterChanges2 = await sendAsync({
const filterChanges2 = await request({
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 sendAsync({
const filterChanges3 = await request({
method: 'eth_getFilterChanges',
params: [filterId]
})
Expand All @@ -163,7 +170,7 @@ test('BlockFilter - basic', asyncTest(async (t) => {
await tools.trackNextBlock()

// check filter
const filterChanges4 = await sendAsync({
const filterChanges4 = await request({
method: 'eth_getFilterChanges',
params: [filterId]
})
Expand All @@ -174,7 +181,7 @@ 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 sendAsync({
await request({
method: 'eth_uninstallFilter',
params: [filterId]
})
Expand Down
16 changes: 7 additions & 9 deletions test/getBlocksForRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const test = require("tape");
const getBlocksForRange = require("../getBlocksForRange");

test("return block number if there is no error", async (t) => {
const sendAsync = sinon
const request = sinon
.stub()
.withArgs({
id: 1,
Expand All @@ -13,8 +13,8 @@ test("return block number if there is no error", async (t) => {
params: ["0x1", false],
})
.onCall(0)
.callsArgWith(1, null, { result: "0x5c377193f05cb9aa0f44bcc77d05492f" });
const provider = { sendAsync };
.returns("0x5c377193f05cb9aa0f44bcc77d05492f");
const provider = { request };
try {
const result = await getBlocksForRange({
provider,
Expand All @@ -29,7 +29,7 @@ test("return block number if there is no error", async (t) => {
});

test("not throw error even if it is not found and filter out null", async (t) => {
const sendAsync = sinon
const request = sinon
.stub()
.withArgs({
id: 1,
Expand All @@ -38,12 +38,10 @@ test("not throw error even if it is not found and filter out null", async (t) =>
params: ["0x1", false],
})
.onCall(0)
.callsArgWith(1, new Error("some error"), null)
.throws(new Error("some error"))
.onCall(1)
.callsArgWith(1, null, { error: "some error" })
.onCall(2)
.callsArgWith(1, null, { result: null });
const provider = { sendAsync };
.returns(null);
const provider = { request };
try {
const result = await getBlocksForRange({ provider, fromBlock: "0x1", toBlock: "0x1" });
t.deepEquals(result, []);
Expand Down
28 changes: 16 additions & 12 deletions test/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ test('subscriptions - newHeads', asyncTest(async (t) => {
test('subscriptions - log', asyncTest(async (t) => {

const tools = createTestSetup()
const { sendAsync, subs, blockTracker } = tools
const { request, subs, blockTracker } = tools

// deploy log-echo contract
const coinbase = await sendAsync({ method: 'eth_coinbase' })
const { contractAddress } = await deployLogEchoContract({ tools, from: coinbase })
const accounts = await request({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was throwing Error: Returned error: sender account not recognized, when coinbase (0x000....) supplied as a from account. So coinbase account has been replaced with first account from the accounts array.

method: "eth_accounts",
params: []
});
const fromAcccount = accounts[0];
const { contractAddress } = await deployLogEchoContract({ tools, from: fromAcccount })
t.ok(contractAddress, 'got deployed contract address')
// deploy secondary "wrong" log contract
const wrongContractAddress = (await deployLogEchoContract({ tools, from: coinbase })).contractAddress
const wrongContractAddress = (await deployLogEchoContract({ tools, from: fromAcccount })).contractAddress

// create subscription
const subResults = []
Expand All @@ -79,27 +83,27 @@ 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 sendAsync({
const triggeringTxHash = await request({
method: 'eth_sendTransaction',
params: { from: coinbase, to: contractAddress, data: targetTopic }
params: [{ from: fromAcccount, to: contractAddress, data: targetTopic }]
})
await tools.trackNextBlock()

// trigger non-matching log
await sendAsync({
await request({
method: 'eth_sendTransaction',
params: {
from: coinbase,
params: [{
from: fromAcccount,
to: contractAddress,
data: wrongTopic
}
}]
})
await tools.trackNextBlock()

// trigger non-matching contract
await sendAsync({
await request({
method: 'eth_sendTransaction',
params: { from: coinbase, to: wrongContractAddress, data: targetTopic }
params: [{ from: fromAcccount, to: wrongContractAddress, data: targetTopic }]
})
await tools.trackNextBlock()

Expand Down
Loading
Loading