Skip to content
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
47 changes: 3 additions & 44 deletions merge-openrpc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const MetaMaskOpenRPC = require("./openrpc.json");
const fs = require("fs");
const fetch = require("node-fetch");

const mergeOpenRPC = require("./merge-utils");

const getFilteredExecutionAPIs = () => {
return fetch("https://raw.githubusercontent.com/ethereum/execution-apis/assembled-spec/refs-openrpc.json")
Expand All @@ -19,7 +19,7 @@ getFilteredExecutionAPIs().then((EthereumOpenRPC) => {
4
)
);
})
});

const unneeded = [
/eth_signTransaction/,
Expand All @@ -30,49 +30,8 @@ const unneeded = [

const filterExecutionAPIs = (openrpcDocument) => {
openrpcDocument.methods = openrpcDocument.methods.filter((method) => {
const matches = unneeded.some((regex) => regex.test(method.name))
const matches = unneeded.some((regex) => regex.test(method.name));
return !matches;
});
return openrpcDocument;
}

const mergeOpenRPC = (first, second) => {
return {
openrpc: first.openrpc,
info: first.info,
methods: [
...first.methods,
...second.methods
],
components: {
errors: {
...first.components.errors,
...second.components.errors
},
schemas: {
...first.components.schemas,
...second.components.schemas
},
tags: {
...first.components.tags,
...second.components.tags
},
contentDescriptors: {
...first.components.contentDescriptors,
...second.components.contentDescriptors
},
examplePairings: {
...first.components.examplePairings,
...second.components.examplePairings
},
links: {
...first.components.links,
...second.components.links
},
examples: {
...first.components.examples,
...second.components.examples
}
}
}
};
52 changes: 52 additions & 0 deletions merge-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

const dedupe = (methods1, methods2) => {
return methods2.reduce(
(arr, method) => {
if (arr.find((m) => m.name === method.name) === undefined) {
arr.push(method);
}
return arr;
},
methods1
);
};

const mergeOpenRPC = (first, second) => {
return {
openrpc: first.openrpc,
info: first.info,
methods: dedupe(first.methods, second.methods),
components: {
errors: {
...first.components.errors,
...second.components.errors
},
schemas: {
...first.components.schemas,
...second.components.schemas
},
tags: {
...first.components.tags,
...second.components.tags
},
contentDescriptors: {
...first.components.contentDescriptors,
...second.components.contentDescriptors
},
examplePairings: {
...first.components.examplePairings,
...second.components.examplePairings
},
links: {
...first.components.links,
...second.components.links
},
examples: {
...first.components.examples,
...second.components.examples
}
}
};
};

module.exports = mergeOpenRPC;
1 change: 1 addition & 0 deletions openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{
"name": "filterOptions",
"schema": {
"title": "filterOptions",
"type": "object",
"description": "An optional object containing filter options specific to the subscription type. Only applicable for 'logs' subscription type.",
"properties": {
Expand Down
28 changes: 28 additions & 0 deletions tests/merge-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-ignore
import mergeOpenRPC from '../merge-utils';

describe("merge openrpc document", () => {
describe('mergeOpenRPC', () => {
it('result does not contain duplicate methods', () => {
const doc1 = {
info: {},
methods: [
{ name: 'foo', description: 'abc' },
{ name: 'bar' },
{ name: 'baz' },
],
components: {}
};
const doc2 = {
info: {},
methods: [
{ name: 'foo', description: '123' },
{ name: 'bling' },
{ name: 'blang' },
],
components: {}
};
expect(mergeOpenRPC(doc1, doc2).methods[0].description).toBe("abc");
});
});
});