MCPcopy Create free account
hub / github.com/ccxt/ccxt / encodeFilterTopics

Method encodeFilterTopics

js/src/static_dependencies/ethers/interface.js:735–793  ·  view source on GitHub ↗
(fragment, values)

Source from the content-addressed store, hash-verified

733 */
734 // Create the filter for the event with search criteria (e.g. for eth_filterLog)
735 encodeFilterTopics(fragment, values) {
736 if (typeof (fragment) === "string") {
737 const f = this.getEvent(fragment);
738 assertArgument(f, "unknown event", "eventFragment", fragment);
739 fragment = f;
740 }
741 assert(values.length <= fragment.inputs.length, `too many arguments for ${fragment.format()}`, "UNEXPECTED_ARGUMENT", { count: values.length, expectedCount: fragment.inputs.length });
742 const topics = [];
743 if (!fragment.anonymous) {
744 topics.push(fragment.topicHash);
745 }
746 // @TODO: Use the coders for this; to properly support tuples, etc.
747 const encodeTopic = (param, value) => {
748 if (param.type === "string") {
749 return id(value);
750 }
751 else if (param.type === "bytes") {
752 return keccak256(value);
753 }
754 if (param.type === "bool" && typeof (value) === "boolean") {
755 value = (value ? "0x01" : "0x00");
756 }
757 else if (param.type.match(/^u?int/)) {
758 value = toBeHex(value); // @TODO: Should this toTwos??
759 }
760 else if (param.type.match(/^bytes/)) {
761 value = zeroPadBytes(value, 32);
762 }
763 else if (param.type === "address") {
764 // Check addresses are valid
765 this.#abiCoder.encode(["address"], [value]);
766 }
767 return zeroPadValue(hexlify(value), 32);
768 };
769 values.forEach((value, index) => {
770 const param = fragment.inputs[index];
771 if (!param.indexed) {
772 assertArgument(value == null, "cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
773 return;
774 }
775 if (value == null) {
776 topics.push(null);
777 }
778 else if (param.baseType === "array" || param.baseType === "tuple") {
779 assertArgument(false, "filtering with tuples or arrays not supported", ("contract." + param.name), value);
780 }
781 else if (Array.isArray(value)) {
782 topics.push(value.map((value) => encodeTopic(param, value)));
783 }
784 else {
785 topics.push(encodeTopic(param, value));
786 }
787 });
788 // Trim off trailing nulls
789 while (topics.length && topics[topics.length - 1] === null) {
790 topics.pop();
791 }
792 return topics;

Callers

nothing calls this directly

Calls 9

getEventMethod · 0.95
assertArgumentFunction · 0.90
assertFunction · 0.90
pushMethod · 0.80
formatMethod · 0.45
forEachMethod · 0.45
isArrayMethod · 0.45
mapMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected