Skip to content

Commit

Permalink
fix bug with findHandler(), add test, update readme, shutdown() alway…
Browse files Browse the repository at this point in the history
…s returns promise
  • Loading branch information
adraffy committed Oct 20, 2024
1 parent 8ce18bc commit 9c4196f
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ ezccip.enableENSIP10(async (name, context) => {

// more complicated example
let abi = new ethers.Interface([
'function f(bytes32 x) return (string)',
'function g(uint256 a, uint256 b) return (uint256)',
'function f(bytes32 x) returns (string)',
'function g(uint256 a, uint256 b) returns (uint256)',
]);
ezccip.register(abi, { // register multiple functions at once using existing ABI
async ['f()']([x], context, history) { // match function by signature
async ['f(bytes32)']([x], context, history) { // match function by signature
history.show = [context.sender]; // replace arguments of f(...) in logger
history.name = 'Chonk'; // rename f() to Chonk() in logger
return [context.calldata]; // echo incoming calldata
Expand Down
24 changes: 12 additions & 12 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var History = class _History {
if (typeof show === "function") show = show();
if (!Array.isArray(show)) show = [show];
if (show.length) {
desc += show.map((x2) => typeof x2 === "string" ? asciiize(x2) : x2).join(",");
desc += show.map((x) => typeof x === "string" ? asciiize(x) : x).join(",");
}
}
desc += ")";
Expand All @@ -120,13 +120,13 @@ var RESOLVE_ABI = new import_abi.Interface([
"function ABI(bytes32 node, uint256 types) external view returns (uint256 type, bytes memory data)",
"function multicall(bytes[] calls) external view returns (bytes[])"
]);
RESOLVE_ABI.forEachFunction((x2) => x2.__name = x2.format());
RESOLVE_ABI.forEachFunction((x) => x.__name = x.format());
var EZCCIP = class {
constructor() {
this.impls = /* @__PURE__ */ new Map();
this.register("multicall(bytes[]) external view returns (bytes[])", async ([calls], context, history) => {
history.show = false;
return [await Promise.all(calls.map((x2) => this.handleCall(x2, context, history.enter()).catch(encode_error)))];
return [await Promise.all(calls.map((x) => this.handleCall(x, context, history.enter()).catch(encode_error)))];
});
}
enableENSIP10(get, { multicall = true } = {}) {
Expand All @@ -142,12 +142,12 @@ var EZCCIP = class {
findHandler(key) {
if (/^0x[0-9a-f]{8}$/.test(key)) {
return this.impls.get(key.toLowerCase());
} else if (x instanceof import_abi.FunctionFragment) {
return this.impls.get(x.selector);
} else if (key instanceof import_abi.FunctionFragment) {
return this.impls.get(key.selector);
} else {
for (let x2 of this.impls.values()) {
if (x2.frag.name === key || x2.frag.format() === key) {
return x2;
for (let x of this.impls.values()) {
if (x.frag.name === key || x.frag.format() === key) {
return x;
}
}
}
Expand All @@ -159,14 +159,14 @@ var EZCCIP = class {
abi = [abi];
}
abi = import_abi.Interface.from(abi);
let frags = abi.fragments.filter((x2) => x2 instanceof import_abi.FunctionFragment);
let frags = abi.fragments.filter((x) => x instanceof import_abi.FunctionFragment);
if (impl instanceof Function) {
if (frags.length != 1) throw error_with("expected 1 implementation", { abi, impl, names: frags.map((x2) => x2.format()) });
if (frags.length != 1) throw error_with("expected 1 implementation", { abi, impl, names: frags.map((x) => x.format()) });
let frag = frags[0];
impl = { [frag.name]: impl };
}
return Object.entries(impl).map(([key, fn]) => {
let frag = frags.find((x2) => x2.name === key || x2.format() === key || x2.selector === key);
let frag = frags.find((x) => x.name === key || x.format() === key || x.selector === key);
if (!frag) {
throw error_with(`expected interface function: ${key}`, { abi, impl, key });
}
Expand Down Expand Up @@ -263,7 +263,7 @@ async function processENSIP10(record, calldata, multicall = true, history) {
switch (frag.__name) {
case "multicall(bytes[])": {
if (history) history.show = false;
res = [await Promise.all(args.calls.map((x2) => processENSIP10(record, x2, true, history?.enter()).catch(encode_error)))];
res = [await Promise.all(args.calls.map((x) => processENSIP10(record, x, true, history?.enter()).catch(encode_error)))];
break;
}
case "addr(bytes32)": {
Expand Down
24 changes: 12 additions & 12 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var History = class _History {
if (typeof show === "function") show = show();
if (!Array.isArray(show)) show = [show];
if (show.length) {
desc += show.map((x2) => typeof x2 === "string" ? asciiize(x2) : x2).join(",");
desc += show.map((x) => typeof x === "string" ? asciiize(x) : x).join(",");
}
}
desc += ")";
Expand All @@ -89,13 +89,13 @@ var RESOLVE_ABI = new Interface([
"function ABI(bytes32 node, uint256 types) external view returns (uint256 type, bytes memory data)",
"function multicall(bytes[] calls) external view returns (bytes[])"
]);
RESOLVE_ABI.forEachFunction((x2) => x2.__name = x2.format());
RESOLVE_ABI.forEachFunction((x) => x.__name = x.format());
var EZCCIP = class {
constructor() {
this.impls = /* @__PURE__ */ new Map();
this.register("multicall(bytes[]) external view returns (bytes[])", async ([calls], context, history) => {
history.show = false;
return [await Promise.all(calls.map((x2) => this.handleCall(x2, context, history.enter()).catch(encode_error)))];
return [await Promise.all(calls.map((x) => this.handleCall(x, context, history.enter()).catch(encode_error)))];
});
}
enableENSIP10(get, { multicall = true } = {}) {
Expand All @@ -111,12 +111,12 @@ var EZCCIP = class {
findHandler(key) {
if (/^0x[0-9a-f]{8}$/.test(key)) {
return this.impls.get(key.toLowerCase());
} else if (x instanceof FunctionFragment) {
return this.impls.get(x.selector);
} else if (key instanceof FunctionFragment) {
return this.impls.get(key.selector);
} else {
for (let x2 of this.impls.values()) {
if (x2.frag.name === key || x2.frag.format() === key) {
return x2;
for (let x of this.impls.values()) {
if (x.frag.name === key || x.frag.format() === key) {
return x;
}
}
}
Expand All @@ -128,14 +128,14 @@ var EZCCIP = class {
abi = [abi];
}
abi = Interface.from(abi);
let frags = abi.fragments.filter((x2) => x2 instanceof FunctionFragment);
let frags = abi.fragments.filter((x) => x instanceof FunctionFragment);
if (impl instanceof Function) {
if (frags.length != 1) throw error_with("expected 1 implementation", { abi, impl, names: frags.map((x2) => x2.format()) });
if (frags.length != 1) throw error_with("expected 1 implementation", { abi, impl, names: frags.map((x) => x.format()) });
let frag = frags[0];
impl = { [frag.name]: impl };
}
return Object.entries(impl).map(([key, fn]) => {
let frag = frags.find((x2) => x2.name === key || x2.format() === key || x2.selector === key);
let frag = frags.find((x) => x.name === key || x.format() === key || x.selector === key);
if (!frag) {
throw error_with(`expected interface function: ${key}`, { abi, impl, key });
}
Expand Down Expand Up @@ -232,7 +232,7 @@ async function processENSIP10(record, calldata, multicall = true, history) {
switch (frag.__name) {
case "multicall(bytes[])": {
if (history) history.show = false;
res = [await Promise.all(args.calls.map((x2) => processENSIP10(record, x2, true, history?.enter()).catch(encode_error)))];
res = [await Promise.all(args.calls.map((x) => processENSIP10(record, x, true, history?.enter()).catch(encode_error)))];
break;
}
case "addr(bytes32)": {
Expand Down
3 changes: 2 additions & 1 deletion dist/serve.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function serve(ezccip, {
}
});
function shutdown() {
if (!killer && http.listening) {
if (!killer) {
if (!http.listening) return Promise.resolve();
killer = new Promise((ful2) => http.close(() => {
killer = null;
ful2();
Expand Down
3 changes: 2 additions & 1 deletion dist/serve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function serve(ezccip, {
}
});
function shutdown() {
if (!killer && http.listening) {
if (!killer) {
if (!http.listening) return Promise.resolve();
killer = new Promise((ful2) => http.close(() => {
killer = null;
ful2();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@resolverworks/ezccip",
"version": "0.0.21",
"version": "0.0.22",
"type": "module",
"scripts": {
"test": "node test/all.js",
Expand Down
4 changes: 2 additions & 2 deletions src/ezccip.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export class EZCCIP {
findHandler(key) {
if (/^0x[0-9a-f]{8}$/.test(key)) {
return this.impls.get(key.toLowerCase());
} else if (x instanceof FunctionFragment) {
return this.impls.get(x.selector);
} else if (key instanceof FunctionFragment) {
return this.impls.get(key.selector);
} else {
for (let x of this.impls.values()) {
if (x.frag.name === key || x.frag.format() === key) {
Expand Down
3 changes: 2 additions & 1 deletion src/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export function serve(ezccip, {
}
});
function shutdown() {
if (!killer && http.listening) {
if (!killer) {
if (!http.listening) return Promise.resolve();
killer = new Promise(ful => http.close(() => {
killer = null;
ful();
Expand Down
13 changes: 13 additions & 0 deletions test/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import {serve} from '../src/serve.js';
import {test, after} from 'node:test';
import assert from 'node:assert/strict';

test('findHandler', async () => {
let ezccip = new EZCCIP();
ezccip.register('f(bytes32 x) returns (string)', () => []);
assert(ezccip.findHandler('f'));
assert(ezccip.findHandler('f(bytes32)'));
let abi = new ethers.Interface([
'function g(uint256 a, uint256 b) view returns (uint256)',
])
ezccip.register(abi, () => []);
assert(ezccip.findHandler('g'));
assert(ezccip.findHandler(ethers.id('g(uint256,uint256)').slice(0, 10)));
});

test('serve w/custom function', async () => {
let args = [69n, 420n];
let fn = ([a, b]) => [a * 1000n + b];
Expand Down
1 change: 1 addition & 0 deletions test/shutdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ test('multi shutdown', async () => {
test('stacked shutdown', async () => {
const {shutdown} = await serve(() => {});
await shutdown();
assert.notEqual(shutdown(), shutdown());
await shutdown();
});

0 comments on commit 9c4196f

Please sign in to comment.