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

Method encodeFilterTopics

lib.esm/abi/interface.js:857–915  ·  view source on GitHub ↗
(fragment, values)

Source from the content-addressed store, hash-verified

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

Callers 2

constructorMethod · 0.45
test-abi.jsFile · 0.45

Calls 7

getEventMethod · 0.95
assertArgumentFunction · 0.90
assertFunction · 0.90
formatMethod · 0.45
isArrayMethod · 0.45
mapMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected