MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / encodeFilterTopics

Method encodeFilterTopics

lib.commonjs/abi/interface.js:865–923  ·  view source on GitHub ↗
(fragment, values)

Source from the content-addressed store, hash-verified

863 */
864 // Create the filter for the event with search criteria (e.g. for eth_filterLog)
865 encodeFilterTopics(fragment, values) {
866 if (typeof (fragment) === "string") {
867 const f = this.getEvent(fragment);
868 (0, index_js_3.assertArgument)(f, "unknown event", "eventFragment", fragment);
869 fragment = f;
870 }
871 (0, index_js_3.assert)(values.length <= fragment.inputs.length, `too many arguments for ${fragment.format()}`, "UNEXPECTED_ARGUMENT", { count: values.length, expectedCount: fragment.inputs.length });
872 const topics = [];
873 if (!fragment.anonymous) {
874 topics.push(fragment.topicHash);
875 }
876 // @TODO: Use the coders for this; to properly support tuples, etc.
877 const encodeTopic = (param, value) => {
878 if (param.type === "string") {
879 return (0, index_js_2.id)(value);
880 }
881 else if (param.type === "bytes") {
882 return (0, index_js_1.keccak256)((0, index_js_3.hexlify)(value));
883 }
884 if (param.type === "bool" && typeof (value) === "boolean") {
885 value = (value ? "0x01" : "0x00");
886 }
887 else if (param.type.match(/^u?int/)) {
888 value = (0, index_js_3.toBeHex)(value); // @TODO: Should this toTwos??
889 }
890 else if (param.type.match(/^bytes/)) {
891 value = (0, index_js_3.zeroPadBytes)(value, 32);
892 }
893 else if (param.type === "address") {
894 // Check addresses are valid
895 this.#abiCoder.encode(["address"], [value]);
896 }
897 return (0, index_js_3.zeroPadValue)((0, index_js_3.hexlify)(value), 32);
898 };
899 values.forEach((value, index) => {
900 const param = fragment.inputs[index];
901 if (!param.indexed) {
902 (0, index_js_3.assertArgument)(value == null, "cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
903 return;
904 }
905 if (value == null) {
906 topics.push(null);
907 }
908 else if (param.baseType === "array" || param.baseType === "tuple") {
909 (0, index_js_3.assertArgument)(false, "filtering with tuples or arrays not supported", ("contract." + param.name), value);
910 }
911 else if (Array.isArray(value)) {
912 topics.push(value.map((value) => encodeTopic(param, value)));
913 }
914 else {
915 topics.push(encodeTopic(param, value));
916 }
917 });
918 // Trim off trailing nulls
919 while (topics.length && topics[topics.length - 1] === null) {
920 topics.pop();
921 }
922 return topics;

Callers 2

constructorMethod · 0.45
test-abi.jsFile · 0.45

Calls 5

getEventMethod · 0.95
formatMethod · 0.45
isArrayMethod · 0.45
mapMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected