viem 0.0.1-alpha.0 → 0.0.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,15 +9,29 @@ var __publicField = (obj, key, value) => {
9
9
  var package_default = {
10
10
  name: "viem",
11
11
  description: "TypeScript (& JavaScript) Interface for Ethereum",
12
- version: "0.0.1-alpha.0",
12
+ version: "0.0.1-alpha.2",
13
13
  scripts: {
14
+ anvil: "source .env && anvil --fork-url $ANVIL_FORK_URL --fork-block-number $VITE_ANVIL_BLOCK_NUMBER --block-time $VITE_ANVIL_BLOCK_TIME",
15
+ bench: "vitest bench --no-threads",
16
+ "bench:ci": "CI=true vitest bench --no-threads",
14
17
  build: "tsup",
15
- dev: "DEV=true tsup"
16
- },
17
- dependencies: {
18
- "@noble/hashes": "^1.1.2",
19
- "@wagmi/chains": "^0.1.0",
20
- abitype: "^0.2.5"
18
+ changeset: "changeset",
19
+ "changeset:release": "pnpm build && changeset publish",
20
+ "changeset:version": "changeset version && pnpm install --lockfile-only",
21
+ dev: "DEV=true tsup",
22
+ "dev:docs": "pnpm -r --filter site dev",
23
+ format: "rome format src/ test/ --write",
24
+ lint: "rome check .",
25
+ "lint:fix": "pnpm lint --apply-suggested",
26
+ playground: "pnpm --filter playground-dev dev",
27
+ "playground:benchmark": "pnpm --filter playground-benchmark dev",
28
+ postinstall: "pnpm dev",
29
+ preinstall: "npx only-allow pnpm",
30
+ prepare: "npx simple-git-hooks",
31
+ test: "vitest dev --coverage --no-threads",
32
+ "test:ci": "CI=true vitest --coverage --no-threads",
33
+ "test:ui": "vitest dev --ui --no-threads",
34
+ typecheck: "tsc --noEmit"
21
35
  },
22
36
  files: [
23
37
  "/actions",
@@ -59,17 +73,60 @@ var package_default = {
59
73
  main: "dist/index.js",
60
74
  types: "dist/index.d.ts",
61
75
  sideEffects: false,
62
- license: "WAGMIT",
76
+ dependencies: {
77
+ "@noble/hashes": "^1.1.2",
78
+ "@wagmi/chains": "^0.1.0",
79
+ abitype: "^0.2.5"
80
+ },
81
+ devDependencies: {
82
+ "@actions/core": "^1.10.0",
83
+ "@actions/github": "^5.1.1",
84
+ "@changesets/changelog-github": "^0.4.5",
85
+ "@changesets/cli": "^2.23.2",
86
+ "@testing-library/jest-dom": "^5.16.5",
87
+ "@types/dedent": "^0.7.0",
88
+ "@types/fs-extra": "^9.0.13",
89
+ "@types/node": "^17.0.45",
90
+ "@vitest/coverage-c8": "^0.24.3",
91
+ "@vitest/ui": "^0.19.1",
92
+ bundlewatch: "^0.3.3",
93
+ dedent: "^0.7.0",
94
+ esbuild: "^0.16.12",
95
+ "esbuild-register": "^3.4.2",
96
+ "essential-eth": "^0.6.2",
97
+ ethers: "^5.7.2",
98
+ execa: "^6.1.0",
99
+ "fs-extra": "^10.1.0",
100
+ jsdom: "^20.0.0",
101
+ rome: "^11.0.0",
102
+ "simple-git-hooks": "^2.8.1",
103
+ tsup: "^6.5.0",
104
+ typescript: "^4.9.3",
105
+ vite: "^3.0.4",
106
+ vitest: "^0.25.2",
107
+ web3: "^1.8.1"
108
+ },
109
+ license: "MIT",
63
110
  repository: "wagmi-dev/viem",
64
- author: "moxey.eth",
65
- ethereum: "moxey.eth",
111
+ authors: [
112
+ "awkweb.eth",
113
+ "jxom.eth"
114
+ ],
66
115
  keywords: [
67
116
  "eth",
68
117
  "ethereum",
69
118
  "dapps",
70
119
  "wallet",
71
120
  "web3"
72
- ]
121
+ ],
122
+ "simple-git-hooks": {
123
+ "pre-commit": "pnpm format & pnpm lint:fix"
124
+ },
125
+ pnpm: {
126
+ patchedDependencies: {
127
+ "vitepress@1.0.0-alpha.34": "patches/vitepress@1.0.0-alpha.34.patch"
128
+ }
129
+ }
73
130
  };
74
131
 
75
132
  // src/utils/BaseError.ts
@@ -80,11 +137,11 @@ var BaseError = class extends Error {
80
137
  const docsPath = args.cause instanceof BaseError ? args.cause.docsPath || args.docsPath : args.docsPath;
81
138
  const message = [
82
139
  humanMessage,
83
- ...docsPath ? ["", "Docs: https://viem.sh" + docsPath] : [],
140
+ ...docsPath ? ["", `Docs: https://viem.sh${docsPath}`] : [],
84
141
  "",
85
- ...details ? ["Details: " + details] : [],
86
- "Version: viem@" + version,
87
- ...args.cause && !(args.cause instanceof BaseError) && Object.keys(args.cause).length > 0 ? ["Internal Error: " + JSON.stringify(args.cause)] : []
142
+ ...details ? [`Details: ${details}`] : [],
143
+ `Version: viem@${version}`,
144
+ ...args.cause && !(args.cause instanceof BaseError) && Object.keys(args.cause).length > 0 ? [`Internal Error: ${JSON.stringify(args.cause)}`] : []
88
145
  ].join("\n");
89
146
  super(message);
90
147
  __publicField(this, "humanMessage");
@@ -157,7 +214,10 @@ function padHex(hex_, { dir, size: size2 = 32 } = {}) {
157
214
  targetSize: size2,
158
215
  type: "hex"
159
216
  });
160
- return "0x" + hex[dir === "right" ? "padEnd" : "padStart"](size2 * 2, "0");
217
+ return `0x${hex[dir === "right" ? "padEnd" : "padStart"](
218
+ size2 * 2,
219
+ "0"
220
+ )}`;
161
221
  }
162
222
  function padBytes(bytes, { dir, size: size2 = 32 } = {}) {
163
223
  if (bytes.length > size2)
@@ -199,7 +259,7 @@ function trim(hexOrBytes, { dir = "left" } = {}) {
199
259
  data = dir === "left" ? data.slice(sliceLength) : data.slice(0, data.length - sliceLength);
200
260
  if (typeof hexOrBytes === "string") {
201
261
  if (data.length === 1 && dir === "right")
202
- data = data + "0";
262
+ data = `${data}0`;
203
263
  return `0x${data}`;
204
264
  }
205
265
  return data;
@@ -312,7 +372,7 @@ function encodeBytes(value) {
312
372
  function hexToBytes(hex_) {
313
373
  let hex = hex_.slice(2);
314
374
  if (hex.length % 2)
315
- hex = "0" + hex;
375
+ hex = `0${hex}`;
316
376
  const bytes = new Uint8Array(hex.length / 2);
317
377
  for (let index = 0; index < bytes.length; index++) {
318
378
  const start = index * 2;
@@ -372,18 +432,26 @@ function decodeHex(hex, to) {
372
432
  return hexToBool(hex);
373
433
  return hexToBytes(hex);
374
434
  }
375
- function hexToBigInt(hex) {
376
- return BigInt(hex);
435
+ function hexToBigInt(hex, opts = {}) {
436
+ const { signed } = opts;
437
+ const value = BigInt(hex);
438
+ if (!signed)
439
+ return value;
440
+ const size2 = (hex.length - 2) / 2;
441
+ const max = (1n << BigInt(size2) * 8n - 1n) - 1n;
442
+ if (value <= max)
443
+ return value;
444
+ return value - BigInt(`0x${"f".padStart(size2 * 2, "f")}`) - 1n;
377
445
  }
378
446
  function hexToBool(hex) {
379
- if (hex === "0x0")
447
+ if (trim(hex) === "0x0")
380
448
  return false;
381
- if (hex === "0x1")
449
+ if (trim(hex) === "0x1")
382
450
  return true;
383
451
  throw new InvalidHexBooleanError(hex);
384
452
  }
385
- function hexToNumber(hex) {
386
- return Number(BigInt(hex));
453
+ function hexToNumber(hex, opts = {}) {
454
+ return Number(hexToBigInt(hex, opts));
387
455
  }
388
456
  function hexToString(hex) {
389
457
  const bytes = hexToBytes(hex);
@@ -597,7 +665,7 @@ function hashFunction(def) {
597
665
  var getEventSignature = (event) => hashFunction(event);
598
666
 
599
667
  // src/utils/hash/getFunctionSignature.ts
600
- var getFunctionSignature = (fn) => hashFunction(fn).slice(0, 10);
668
+ var getFunctionSignature = (fn) => slice(hashFunction(fn), 0, 4);
601
669
 
602
670
  // src/utils/address/getAddress.ts
603
671
  var addressRegex = /^(0x)?[a-fA-F0-9]{40}$/;
@@ -639,7 +707,7 @@ function getCreateAddress(opts) {
639
707
  if (nonce[0] === 0)
640
708
  nonce = new Uint8Array([]);
641
709
  return getAddress(
642
- "0x" + keccak256(encodeRlp([from, nonce], "bytes")).slice(26)
710
+ `0x${keccak256(encodeRlp([from, nonce], "bytes")).slice(26)}`
643
711
  );
644
712
  }
645
713
  function getCreate2Address(opts) {
@@ -675,6 +743,512 @@ function isAddressEqual(a, b) {
675
743
  return getAddress(a) === getAddress(b);
676
744
  }
677
745
 
746
+ // src/utils/abi/encodeAbi.ts
747
+ function encodeAbi({
748
+ params,
749
+ values
750
+ }) {
751
+ if (params.length !== values.length)
752
+ throw new AbiEncodingLengthMismatchError({
753
+ expectedLength: params.length,
754
+ givenLength: values.length
755
+ });
756
+ const preparedParams = prepareParams({ params, values });
757
+ const data = encodeParams(preparedParams);
758
+ if (data.length === 0)
759
+ return void 0;
760
+ return data;
761
+ }
762
+ function prepareParams({
763
+ params,
764
+ values
765
+ }) {
766
+ let preparedParams = [];
767
+ for (let i = 0; i < params.length; i++) {
768
+ preparedParams.push(prepareParam({ param: params[i], value: values[i] }));
769
+ }
770
+ return preparedParams;
771
+ }
772
+ function prepareParam({
773
+ param,
774
+ value
775
+ }) {
776
+ const arrayComponents = getArrayComponents(param.type);
777
+ if (arrayComponents) {
778
+ const [length, type] = arrayComponents;
779
+ return encodeArray(value, { length, param: { ...param, type } });
780
+ }
781
+ if (param.type === "tuple") {
782
+ return encodeTuple(value, {
783
+ param
784
+ });
785
+ }
786
+ if (param.type === "address") {
787
+ return encodeAddress(value);
788
+ }
789
+ if (param.type === "bool") {
790
+ return encodeBool(value);
791
+ }
792
+ if (param.type.startsWith("uint") || param.type.startsWith("int")) {
793
+ const signed = param.type.startsWith("int");
794
+ return encodeNumber(value, { signed });
795
+ }
796
+ if (param.type.startsWith("bytes")) {
797
+ return encodeBytes2(value, { param });
798
+ }
799
+ if (param.type === "string") {
800
+ return encodeString(value);
801
+ }
802
+ throw new InvalidAbiEncodingTypeError(param.type);
803
+ }
804
+ function encodeParams(preparedParams) {
805
+ let staticSize = 0;
806
+ for (let i = 0; i < preparedParams.length; i++) {
807
+ const { dynamic, encoded } = preparedParams[i];
808
+ if (dynamic)
809
+ staticSize += 32;
810
+ else
811
+ staticSize += size(encoded);
812
+ }
813
+ let staticParams = [];
814
+ let dynamicParams = [];
815
+ let dynamicSize = 0;
816
+ for (let i = 0; i < preparedParams.length; i++) {
817
+ const { dynamic, encoded } = preparedParams[i];
818
+ if (dynamic) {
819
+ staticParams.push(numberToHex(staticSize + dynamicSize, { size: 32 }));
820
+ dynamicParams.push(encoded);
821
+ dynamicSize += size(encoded);
822
+ } else {
823
+ staticParams.push(encoded);
824
+ }
825
+ }
826
+ return concat([...staticParams, ...dynamicParams]);
827
+ }
828
+ function encodeArray(value, {
829
+ length,
830
+ param
831
+ }) {
832
+ let dynamic = length === null;
833
+ if (!Array.isArray(value))
834
+ throw new InvalidArrayError(value);
835
+ if (!dynamic && value.length !== length)
836
+ throw new AbiEncodingArrayLengthMismatchError({
837
+ expectedLength: length,
838
+ givenLength: value.length,
839
+ type: `${param.type}[${length}]`
840
+ });
841
+ let dynamicChild = false;
842
+ let preparedParams = [];
843
+ for (let i = 0; i < value.length; i++) {
844
+ const preparedParam = prepareParam({ param, value: value[i] });
845
+ if (preparedParam.dynamic)
846
+ dynamicChild = true;
847
+ preparedParams.push(preparedParam);
848
+ }
849
+ if (dynamic || dynamicChild) {
850
+ const data = encodeParams(preparedParams);
851
+ if (dynamic) {
852
+ const length2 = numberToHex(preparedParams.length, { size: 32 });
853
+ return {
854
+ dynamic: true,
855
+ encoded: preparedParams.length > 0 ? concat([length2, data]) : length2
856
+ };
857
+ }
858
+ if (dynamicChild)
859
+ return { dynamic: true, encoded: data };
860
+ }
861
+ return {
862
+ dynamic: false,
863
+ encoded: concat(preparedParams.map(({ encoded }) => encoded))
864
+ };
865
+ }
866
+ function encodeTuple(value, { param }) {
867
+ let dynamic = false;
868
+ let preparedParams = [];
869
+ for (let i = 0; i < param.components.length; i++) {
870
+ const param_ = param.components[i];
871
+ const index = Array.isArray(value) ? i : param_.name;
872
+ const preparedParam = prepareParam({
873
+ param: param_,
874
+ value: value[index]
875
+ });
876
+ preparedParams.push(preparedParam);
877
+ dynamic = preparedParam.dynamic;
878
+ }
879
+ return {
880
+ dynamic,
881
+ encoded: dynamic ? encodeParams(preparedParams) : concat(preparedParams.map(({ encoded }) => encoded))
882
+ };
883
+ }
884
+ function encodeAddress(value) {
885
+ return { dynamic: false, encoded: padHex(value.toLowerCase()) };
886
+ }
887
+ function encodeBytes2(value, { param }) {
888
+ const [_, size_] = param.type.split("bytes");
889
+ if (!size_)
890
+ return {
891
+ dynamic: true,
892
+ encoded: concat([
893
+ padHex(numberToHex(size(value), { size: 32 })),
894
+ padHex(value, { dir: "right" })
895
+ ])
896
+ };
897
+ return { dynamic: false, encoded: padHex(value, { dir: "right" }) };
898
+ }
899
+ function encodeBool(value) {
900
+ return { dynamic: false, encoded: padHex(boolToHex(value)) };
901
+ }
902
+ function encodeNumber(value, { signed }) {
903
+ return {
904
+ dynamic: false,
905
+ encoded: numberToHex(value, {
906
+ size: 32,
907
+ signed
908
+ })
909
+ };
910
+ }
911
+ function encodeString(value) {
912
+ return {
913
+ dynamic: true,
914
+ encoded: concat([
915
+ padHex(numberToHex(value.length, { size: 32 })),
916
+ padHex(stringToHex(value), { dir: "right" })
917
+ ])
918
+ };
919
+ }
920
+ function getArrayComponents(type) {
921
+ const matches = type.match(/^(.*)\[(\d+)?\]$/);
922
+ return matches ? [matches[2] ? Number(matches[2]) : null, matches[1]] : void 0;
923
+ }
924
+ var AbiEncodingArrayLengthMismatchError = class extends BaseError {
925
+ constructor({
926
+ expectedLength,
927
+ givenLength,
928
+ type
929
+ }) {
930
+ super(
931
+ [
932
+ `ABI encoding array length mismatch for type ${type}.`,
933
+ `Expected length: ${expectedLength}`,
934
+ `Given length: ${givenLength}`
935
+ ].join("\n")
936
+ );
937
+ __publicField(this, "name", "AbiEncodingArrayLengthMismatchError");
938
+ }
939
+ };
940
+ var AbiEncodingLengthMismatchError = class extends BaseError {
941
+ constructor({
942
+ expectedLength,
943
+ givenLength
944
+ }) {
945
+ super(
946
+ [
947
+ "ABI encoding params/values length mismatch.",
948
+ `Expected length (params): ${expectedLength}`,
949
+ `Given length (values): ${givenLength}`
950
+ ].join("\n")
951
+ );
952
+ __publicField(this, "name", "AbiEncodingLengthMismatchError");
953
+ }
954
+ };
955
+ var InvalidAbiEncodingTypeError = class extends BaseError {
956
+ constructor(type) {
957
+ super(
958
+ [
959
+ `Type "${type}" is not a valid encoding type.`,
960
+ "Please provide a valid ABI type."
961
+ ].join("\n"),
962
+ { docsPath: "/docs/contract/encodeAbi#params" }
963
+ );
964
+ __publicField(this, "name", "InvalidAbiEncodingType");
965
+ }
966
+ };
967
+ var InvalidArrayError = class extends BaseError {
968
+ constructor(value) {
969
+ super([`Value "${value}" is not a valid array.`].join("\n"));
970
+ __publicField(this, "name", "InvalidArrayError");
971
+ }
972
+ };
973
+
974
+ // src/utils/abi/decodeAbi.ts
975
+ function decodeAbi({
976
+ data,
977
+ params
978
+ }) {
979
+ if (size(data) % 32 !== 0)
980
+ throw new AbiDecodingDataSizeInvalidError(size(data));
981
+ return decodeParams({
982
+ data,
983
+ params
984
+ });
985
+ }
986
+ function decodeParams({
987
+ data,
988
+ params
989
+ }) {
990
+ let decodedValues = [];
991
+ let position = 0;
992
+ for (let i = 0; i < params.length; i++) {
993
+ const param = params[i];
994
+ const { consumed, value } = decodeParam({ data, param, position });
995
+ decodedValues.push(value);
996
+ position += consumed;
997
+ }
998
+ return decodedValues;
999
+ }
1000
+ function decodeParam({
1001
+ data,
1002
+ param,
1003
+ position
1004
+ }) {
1005
+ const arrayComponents = getArrayComponents(param.type);
1006
+ if (arrayComponents) {
1007
+ const [length, type] = arrayComponents;
1008
+ return decodeArray(data, {
1009
+ length,
1010
+ param: { ...param, type },
1011
+ position
1012
+ });
1013
+ }
1014
+ if (param.type === "tuple") {
1015
+ return decodeTuple(data, { param, position });
1016
+ }
1017
+ if (param.type === "string") {
1018
+ return decodeString(data, { position });
1019
+ }
1020
+ if (param.type.startsWith("bytes")) {
1021
+ return decodeBytes2(data, { param, position });
1022
+ }
1023
+ let value = slice(data, position, position + 32);
1024
+ if (param.type.startsWith("uint") || param.type.startsWith("int")) {
1025
+ return decodeNumber(value, { param });
1026
+ }
1027
+ if (param.type === "address") {
1028
+ return decodeAddress(value);
1029
+ }
1030
+ if (param.type === "bool") {
1031
+ return decodeBool(value);
1032
+ }
1033
+ throw new InvalidAbiDecodingTypeError(param.type);
1034
+ }
1035
+ function decodeArray(data, {
1036
+ param,
1037
+ length,
1038
+ position
1039
+ }) {
1040
+ if (!length) {
1041
+ const offset = hexToNumber(slice(data, position, position + 32));
1042
+ const length2 = hexToNumber(slice(data, offset, offset + 32));
1043
+ let consumed2 = 0;
1044
+ let value2 = [];
1045
+ for (let i = 0; i < length2; ++i) {
1046
+ const decodedChild = decodeParam({
1047
+ data: slice(data, offset + 32),
1048
+ param,
1049
+ position: consumed2
1050
+ });
1051
+ consumed2 += decodedChild.consumed;
1052
+ value2.push(decodedChild.value);
1053
+ }
1054
+ return { value: value2, consumed: 32 };
1055
+ }
1056
+ if (hasDynamicChild(param)) {
1057
+ const arrayComponents = getArrayComponents(param.type);
1058
+ const dynamicChild = !arrayComponents?.[0];
1059
+ let consumed2 = 0;
1060
+ let value2 = [];
1061
+ for (let i = 0; i < length; ++i) {
1062
+ const offset = hexToNumber(slice(data, position, position + 32));
1063
+ const decodedChild = decodeParam({
1064
+ data: slice(data, offset),
1065
+ param,
1066
+ position: dynamicChild ? consumed2 : i * 32
1067
+ });
1068
+ consumed2 += decodedChild.consumed;
1069
+ value2.push(decodedChild.value);
1070
+ }
1071
+ return { value: value2, consumed: consumed2 };
1072
+ }
1073
+ let consumed = 0;
1074
+ let value = [];
1075
+ for (let i = 0; i < length; ++i) {
1076
+ const decodedChild = decodeParam({
1077
+ data,
1078
+ param,
1079
+ position: position + consumed
1080
+ });
1081
+ consumed += decodedChild.consumed;
1082
+ value.push(decodedChild.value);
1083
+ }
1084
+ return { value, consumed };
1085
+ }
1086
+ function decodeAddress(value) {
1087
+ return { consumed: 32, value: checksumAddress(trim(value)) };
1088
+ }
1089
+ function decodeBool(value) {
1090
+ return { consumed: 32, value: hexToBool(value) };
1091
+ }
1092
+ function decodeBytes2(data, { param, position }) {
1093
+ const [_, size2] = param.type.split("bytes");
1094
+ if (!size2) {
1095
+ const offset = hexToNumber(slice(data, position, position + 32));
1096
+ const length = hexToNumber(slice(data, offset, offset + 32));
1097
+ const value2 = slice(data, offset + 32, offset + 32 + length);
1098
+ return { consumed: 32, value: value2 };
1099
+ }
1100
+ const value = slice(data, position, position + parseInt(size2));
1101
+ return { consumed: 32, value };
1102
+ }
1103
+ function decodeNumber(value, { param }) {
1104
+ const signed = param.type.startsWith("int");
1105
+ const size2 = parseInt(param.type.split("int")[1] || "256");
1106
+ return {
1107
+ consumed: 32,
1108
+ value: size2 > 48 ? hexToBigInt(value, { signed }) : hexToNumber(value, { signed })
1109
+ };
1110
+ }
1111
+ function decodeString(data, { position }) {
1112
+ const offset = hexToNumber(slice(data, position, position + 32));
1113
+ const length = hexToNumber(slice(data, offset, offset + 32));
1114
+ const value = hexToString(
1115
+ trim(slice(data, offset + 32, offset + 32 + length))
1116
+ );
1117
+ return { consumed: 32, value };
1118
+ }
1119
+ function decodeTuple(data, { param, position }) {
1120
+ const hasUnnamedChild = param.components.length === 0 || param.components.some(({ name }) => !name);
1121
+ let value = hasUnnamedChild ? [] : {};
1122
+ let consumed = 0;
1123
+ if (hasDynamicChild(param)) {
1124
+ const offset = hexToNumber(slice(data, position, position + 32));
1125
+ for (let i = 0; i < param.components.length; ++i) {
1126
+ const component = param.components[i];
1127
+ const decodedChild = decodeParam({
1128
+ data: slice(data, offset),
1129
+ param: component,
1130
+ position: consumed
1131
+ });
1132
+ consumed += decodedChild.consumed;
1133
+ value[hasUnnamedChild ? i : component?.name] = decodedChild.value;
1134
+ }
1135
+ return { consumed: 32, value };
1136
+ }
1137
+ for (let i = 0; i < param.components.length; ++i) {
1138
+ const component = param.components[i];
1139
+ const decodedChild = decodeParam({
1140
+ data,
1141
+ param: component,
1142
+ position: position + consumed
1143
+ });
1144
+ consumed += decodedChild.consumed;
1145
+ value[hasUnnamedChild ? i : component?.name] = decodedChild.value;
1146
+ }
1147
+ return { consumed, value };
1148
+ }
1149
+ function hasDynamicChild(param) {
1150
+ const { type } = param;
1151
+ if (type === "string")
1152
+ return true;
1153
+ if (type === "bytes")
1154
+ return true;
1155
+ if (type.endsWith("[]"))
1156
+ return true;
1157
+ if (type === "tuple")
1158
+ return param.components?.some(hasDynamicChild);
1159
+ const arrayComponents = getArrayComponents(param.type);
1160
+ if (arrayComponents && hasDynamicChild({ ...param, type: arrayComponents[1] }))
1161
+ return true;
1162
+ return false;
1163
+ }
1164
+ var AbiDecodingDataSizeInvalidError = class extends BaseError {
1165
+ constructor(size2) {
1166
+ super(
1167
+ [
1168
+ `Data size of ${size2} bytes is invalid.`,
1169
+ "Size must be in increments of 32 bytes (size % 32 === 0)."
1170
+ ].join("\n")
1171
+ );
1172
+ __publicField(this, "name", "AbiDecodingDataSizeInvalidError");
1173
+ }
1174
+ };
1175
+ var InvalidAbiDecodingTypeError = class extends BaseError {
1176
+ constructor(type) {
1177
+ super(
1178
+ [
1179
+ `Type "${type}" is not a valid decoding type.`,
1180
+ "Please provide a valid ABI type."
1181
+ ].join("\n"),
1182
+ { docsPath: "/docs/contract/decodeAbi#params" }
1183
+ );
1184
+ __publicField(this, "name", "InvalidAbiDecodingType");
1185
+ }
1186
+ };
1187
+
1188
+ // src/utils/abi/getDefinition.ts
1189
+ function getDefinition(description) {
1190
+ if (description.type !== "function" && description.type !== "event" && description.type !== "error")
1191
+ throw new InvalidDefinitionTypeError(description.type);
1192
+ return `${description.name}(${getParams(description.inputs)})`;
1193
+ }
1194
+ function getParams(params) {
1195
+ if (!params)
1196
+ return "";
1197
+ return params.map(getParam).join(",");
1198
+ }
1199
+ function getParam(param) {
1200
+ if (param.type.startsWith("tuple")) {
1201
+ return `(${getParams(
1202
+ param.components
1203
+ )})${param.type.slice("tuple".length)}`;
1204
+ }
1205
+ return param.type;
1206
+ }
1207
+ var InvalidDefinitionTypeError = class extends BaseError {
1208
+ constructor(type) {
1209
+ super(
1210
+ [
1211
+ `"${type}" is not a valid definition type.`,
1212
+ 'Valid types: "function", "event", "error"'
1213
+ ].join("\n"),
1214
+ {
1215
+ docsPath: "/docs/contract/getDefinition"
1216
+ }
1217
+ );
1218
+ }
1219
+ };
1220
+
1221
+ // src/utils/abi/encodeFunctionParams.ts
1222
+ function encodeFunctionParams({
1223
+ abi,
1224
+ args,
1225
+ functionName
1226
+ }) {
1227
+ const description = abi.find((x) => "name" in x && x.name === functionName);
1228
+ if (!description)
1229
+ throw new AbiFunctionNotFoundError(functionName);
1230
+ const definition = getDefinition(description);
1231
+ const signature = getFunctionSignature(definition);
1232
+ const data = "inputs" in description && description.inputs ? encodeAbi({
1233
+ params: description.inputs,
1234
+ values: args ?? []
1235
+ }) : void 0;
1236
+ return concatHex([signature, data ?? "0x"]);
1237
+ }
1238
+ var AbiFunctionNotFoundError = class extends BaseError {
1239
+ constructor(functionName) {
1240
+ super(
1241
+ [
1242
+ `Function "${functionName}" not found on ABI.`,
1243
+ "Make sure you are using the correct ABI and that the function exists on it."
1244
+ ].join("\n"),
1245
+ {
1246
+ docsPath: "/docs/contract/encodeFunctionParams"
1247
+ }
1248
+ );
1249
+ }
1250
+ };
1251
+
678
1252
  // src/utils/buildRequest.ts
679
1253
  function buildRequest(request) {
680
1254
  return async (args) => {
@@ -1427,6 +2001,9 @@ export {
1427
2001
  getCreate2Address,
1428
2002
  isAddress,
1429
2003
  isAddressEqual,
2004
+ encodeAbi,
2005
+ decodeAbi,
2006
+ encodeFunctionParams,
1430
2007
  buildRequest,
1431
2008
  RpcRequestError,
1432
2009
  ParseRpcError,