@thruputchain/client 0.0.0-stage → 0.25.0
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.
- package/LICENSE +201 -0
- package/README.md +935 -2
- package/dist/actions-CdFSUTO2.js +298 -0
- package/dist/actions-CdFSUTO2.js.map +1 -0
- package/dist/actions-DxJZ3yIv.cjs +357 -0
- package/dist/actions-DxJZ3yIv.cjs.map +1 -0
- package/dist/actions.cjs +19 -0
- package/dist/actions.d.cts +4 -0
- package/dist/actions.d.ts +4 -0
- package/dist/actions.js +3 -0
- package/dist/client-BxB6ktQd.d.cts +690 -0
- package/dist/client-BxB6ktQd.d.cts.map +1 -0
- package/dist/client-BxB6ktQd.d.ts +690 -0
- package/dist/client-BxB6ktQd.d.ts.map +1 -0
- package/dist/codec-B7HJjMzI.cjs +8190 -0
- package/dist/codec-B7HJjMzI.cjs.map +1 -0
- package/dist/codec-CX-19jac.js +8130 -0
- package/dist/codec-CX-19jac.js.map +1 -0
- package/dist/contract-BpxjDMHw.js +1105 -0
- package/dist/contract-BpxjDMHw.js.map +1 -0
- package/dist/contract-D0wCFcAN.cjs +1219 -0
- package/dist/contract-D0wCFcAN.cjs.map +1 -0
- package/dist/contract.cjs +58 -0
- package/dist/contract.d.cts +3 -0
- package/dist/contract.d.ts +3 -0
- package/dist/contract.js +2 -0
- package/dist/contracts.cjs +1050 -0
- package/dist/contracts.cjs.map +1 -0
- package/dist/contracts.d.cts +397 -0
- package/dist/contracts.d.cts.map +1 -0
- package/dist/contracts.d.ts +397 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +1030 -0
- package/dist/contracts.js.map +1 -0
- package/dist/dev_wallet-BXWK5KR6.cjs +530 -0
- package/dist/dev_wallet-BXWK5KR6.cjs.map +1 -0
- package/dist/dev_wallet-C9eMQROk.js +497 -0
- package/dist/dev_wallet-C9eMQROk.js.map +1 -0
- package/dist/domainSeparator-B_vU_zUD.cjs +102 -0
- package/dist/domainSeparator-B_vU_zUD.cjs.map +1 -0
- package/dist/domainSeparator-CBCvxIj-.js +79 -0
- package/dist/domainSeparator-CBCvxIj-.js.map +1 -0
- package/dist/execution-CRDyKu45.d.ts +227 -0
- package/dist/execution-CRDyKu45.d.ts.map +1 -0
- package/dist/execution-DyxaIG4v.d.cts +227 -0
- package/dist/execution-DyxaIG4v.d.cts.map +1 -0
- package/dist/hex-DIcaALXL.js +112 -0
- package/dist/hex-DIcaALXL.js.map +1 -0
- package/dist/hex-DkGpiZ5e.cjs +189 -0
- package/dist/hex-DkGpiZ5e.cjs.map +1 -0
- package/dist/index--5LsCotr.d.ts +192 -0
- package/dist/index--5LsCotr.d.ts.map +1 -0
- package/dist/index-B5KqSHeL.d.cts +192 -0
- package/dist/index-B5KqSHeL.d.cts.map +1 -0
- package/dist/index-BZ0O5jZG.d.cts +163 -0
- package/dist/index-BZ0O5jZG.d.cts.map +1 -0
- package/dist/index-BuPQcAK2.d.ts +23 -0
- package/dist/index-BuPQcAK2.d.ts.map +1 -0
- package/dist/index-ByNNqY7S.d.cts +83 -0
- package/dist/index-ByNNqY7S.d.cts.map +1 -0
- package/dist/index-C-M-4jMR.d.cts +263 -0
- package/dist/index-C-M-4jMR.d.cts.map +1 -0
- package/dist/index-C97zw2rk.d.ts +137 -0
- package/dist/index-C97zw2rk.d.ts.map +1 -0
- package/dist/index-CIHK7Ew9.d.cts +303 -0
- package/dist/index-CIHK7Ew9.d.cts.map +1 -0
- package/dist/index-CIHK7Ew9.d.ts +303 -0
- package/dist/index-CIHK7Ew9.d.ts.map +1 -0
- package/dist/index-CXuO2Zjx.d.ts +263 -0
- package/dist/index-CXuO2Zjx.d.ts.map +1 -0
- package/dist/index-DOEBWXY2.d.cts +137 -0
- package/dist/index-DOEBWXY2.d.cts.map +1 -0
- package/dist/index-DzrLv3EA.d.cts +82 -0
- package/dist/index-DzrLv3EA.d.cts.map +1 -0
- package/dist/index-DzrLv3EA.d.ts +82 -0
- package/dist/index-DzrLv3EA.d.ts.map +1 -0
- package/dist/index-fdTURpBk.d.ts +83 -0
- package/dist/index-fdTURpBk.d.ts.map +1 -0
- package/dist/index-sAzHHayu.d.ts +163 -0
- package/dist/index-sAzHHayu.d.ts.map +1 -0
- package/dist/index-uQqRk9Ww.d.cts +23 -0
- package/dist/index-uQqRk9Ww.d.cts.map +1 -0
- package/dist/index.cjs +478 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +173 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +231 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.cjs +101 -0
- package/dist/provider.cjs.map +1 -0
- package/dist/provider.d.cts +2 -0
- package/dist/provider.d.ts +2 -0
- package/dist/provider.js +76 -0
- package/dist/provider.js.map +1 -0
- package/dist/readError-BVbfTMLv.cjs +604 -0
- package/dist/readError-BVbfTMLv.cjs.map +1 -0
- package/dist/readError-Dn_XN98x.js +551 -0
- package/dist/readError-Dn_XN98x.js.map +1 -0
- package/dist/readViews-BKJtE5J3.d.cts +594 -0
- package/dist/readViews-BKJtE5J3.d.cts.map +1 -0
- package/dist/readViews-C_FX8wby.d.ts +594 -0
- package/dist/readViews-C_FX8wby.d.ts.map +1 -0
- package/dist/rolldown-runtime-D6vf50IK.cjs +28 -0
- package/dist/sendInvocation-2EZanZZr.cjs +661 -0
- package/dist/sendInvocation-2EZanZZr.cjs.map +1 -0
- package/dist/sendInvocation-C4ruTC-9.js +505 -0
- package/dist/sendInvocation-C4ruTC-9.js.map +1 -0
- package/dist/sign-BewmV_EL.cjs +139 -0
- package/dist/sign-BewmV_EL.cjs.map +1 -0
- package/dist/sign-DVIRyW8Y.js +122 -0
- package/dist/sign-DVIRyW8Y.js.map +1 -0
- package/dist/sign-Ve0eetRO.d.ts +48 -0
- package/dist/sign-Ve0eetRO.d.ts.map +1 -0
- package/dist/sign-eUT0iGQG.d.cts +48 -0
- package/dist/sign-eUT0iGQG.d.cts.map +1 -0
- package/dist/signed-op.cjs +9 -0
- package/dist/signed-op.d.cts +3 -0
- package/dist/signed-op.d.ts +3 -0
- package/dist/signed-op.js +3 -0
- package/dist/signedOp-Bluo0OAd.cjs +183 -0
- package/dist/signedOp-Bluo0OAd.cjs.map +1 -0
- package/dist/signedOp-x4fakrr4.js +166 -0
- package/dist/signedOp-x4fakrr4.js.map +1 -0
- package/dist/signed_op-BzVbBgv4.js +154 -0
- package/dist/signed_op-BzVbBgv4.js.map +1 -0
- package/dist/signed_op-DFgQ2yFj.d.ts +58 -0
- package/dist/signed_op-DFgQ2yFj.d.ts.map +1 -0
- package/dist/signed_op-LwxrDud8.d.cts +58 -0
- package/dist/signed_op-LwxrDud8.d.cts.map +1 -0
- package/dist/signed_op-z_8fqAi1.cjs +175 -0
- package/dist/signed_op-z_8fqAi1.cjs.map +1 -0
- package/dist/signers-BFP84wJP.cjs +137 -0
- package/dist/signers-BFP84wJP.cjs.map +1 -0
- package/dist/signers-CzOSedwJ.js +120 -0
- package/dist/signers-CzOSedwJ.js.map +1 -0
- package/dist/signers.cjs +5 -0
- package/dist/signers.d.cts +3 -0
- package/dist/signers.d.ts +3 -0
- package/dist/signers.js +2 -0
- package/dist/size-506oTE28.js +91 -0
- package/dist/size-506oTE28.js.map +1 -0
- package/dist/size-OAFfOthl.cjs +108 -0
- package/dist/size-OAFfOthl.cjs.map +1 -0
- package/dist/transaction-CwkQxQOx.js +24 -0
- package/dist/transaction-CwkQxQOx.js.map +1 -0
- package/dist/transaction-DrSrCJ5y.cjs +47 -0
- package/dist/transaction-DrSrCJ5y.cjs.map +1 -0
- package/dist/transaction.cjs +13 -0
- package/dist/transaction.d.cts +3 -0
- package/dist/transaction.d.ts +3 -0
- package/dist/transaction.js +4 -0
- package/dist/transactionHash-CO9ajbvU.js +24 -0
- package/dist/transactionHash-CO9ajbvU.js.map +1 -0
- package/dist/transactionHash-D5yf4KoL.cjs +29 -0
- package/dist/transactionHash-D5yf4KoL.cjs.map +1 -0
- package/dist/transport-B-P16rs6.cjs +252 -0
- package/dist/transport-B-P16rs6.cjs.map +1 -0
- package/dist/transport-DqyoNtUy.js +234 -0
- package/dist/transport-DqyoNtUy.js.map +1 -0
- package/dist/transport.cjs +19 -0
- package/dist/transport.d.cts +3 -0
- package/dist/transport.d.ts +3 -0
- package/dist/transport.js +4 -0
- package/dist/types-BE40oD_Q.d.cts +18 -0
- package/dist/types-BE40oD_Q.d.cts.map +1 -0
- package/dist/types-BE40oD_Q.d.ts +18 -0
- package/dist/types-BE40oD_Q.d.ts.map +1 -0
- package/dist/validation-4w-vDGAi.cjs +543 -0
- package/dist/validation-4w-vDGAi.cjs.map +1 -0
- package/dist/validation-eXATmgO0.js +411 -0
- package/dist/validation-eXATmgO0.js.map +1 -0
- package/dist/wallet-Bthapk8i.cjs +709 -0
- package/dist/wallet-Bthapk8i.cjs.map +1 -0
- package/dist/wallet-CwWPYoHF.js +667 -0
- package/dist/wallet-CwWPYoHF.js.map +1 -0
- package/dist/wallet.cjs +27 -0
- package/dist/wallet.d.cts +3 -0
- package/dist/wallet.d.ts +3 -0
- package/dist/wallet.js +3 -0
- package/package.json +122 -4
|
@@ -0,0 +1,1105 @@
|
|
|
1
|
+
import { l as serializeHeight, r as UnsupportedRpcValue, u as serializeU64 } from "./codec-CX-19jac.js";
|
|
2
|
+
import { g as toBase58, i as toHex0x } from "./hex-DIcaALXL.js";
|
|
3
|
+
import { AuthClaimType, BoolCodec, CxsReader, CxsWriter, FixedArrayCodec, Int128Codec, Int256Codec, Int32Codec, Int64Codec, OptionCodec, Uint128Codec, Uint256Codec, Uint32Codec, Uint64Codec, VarArrayCodec, VoidCodec, calldata, codec, contractAddress, contractEntrypointID, kMaxEventLength, serialize, sha256Hash } from "@thruputchain/protocol";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { CxsReader as CxsReader$1, CxsWriter as CxsWriter$1, FixedOpaqueCodec, Uint8Codec, VarOpaqueCodec, fixedArray, fixedArray as fixedArray$1, fixedBytes, fixedBytes as fixedBytes$1 } from "@thruputchain/xdr";
|
|
6
|
+
import { ContractMethodEffect, ContractMethodEffect as ContractMethodEffect$1, ContractTypeDefKind as ContractTypeDefKind$1, ContractTypeKind as ContractTypeKind$1, ContractUnionCaseKind } from "@thruputchain/contract-spec";
|
|
7
|
+
//#region src/contract/spec.ts
|
|
8
|
+
/**
|
|
9
|
+
* Lookup key for a named type: every segment of its spec id.
|
|
10
|
+
*
|
|
11
|
+
* Two types with the same declared name in different crates or modules are
|
|
12
|
+
* distinct, so named-codec maps from {@link buildNamedCodecs} are keyed by
|
|
13
|
+
* this rather than by `declared` alone. The encoding is injective; use
|
|
14
|
+
* {@link formatSpecId} for anything a person reads.
|
|
15
|
+
*/
|
|
16
|
+
function specIdKey(id) {
|
|
17
|
+
return JSON.stringify([
|
|
18
|
+
id.crate_name,
|
|
19
|
+
id.major,
|
|
20
|
+
id.module_path,
|
|
21
|
+
id.declared
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Render a spec id for diagnostics as `module_path::declared (crate@major)`,
|
|
26
|
+
* the shape the Thruput CLI and spec tool print.
|
|
27
|
+
*/
|
|
28
|
+
function formatSpecId(id) {
|
|
29
|
+
return `${id.module_path}::${id.declared} (${id.crate_name}@${id.major})`;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/contract/errors.ts
|
|
33
|
+
/** Error raised when contract bytes do not match their declared ABI type. */
|
|
34
|
+
var ContractDecodeError = class extends Error {
|
|
35
|
+
constructor(message, options) {
|
|
36
|
+
super(message, options);
|
|
37
|
+
this.name = "ContractDecodeError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/contract/signedToken.ts
|
|
42
|
+
const SIGNED_TOKEN = Object.freeze({ kind: "signedToken" });
|
|
43
|
+
/** The placeholder standing in for a signed auth token. */
|
|
44
|
+
function signedToken() {
|
|
45
|
+
return SIGNED_TOKEN;
|
|
46
|
+
}
|
|
47
|
+
/** Whether `value` is a signed-token placeholder. */
|
|
48
|
+
function isSignedToken(value) {
|
|
49
|
+
return typeof value === "object" && value !== null && value.kind === "signedToken";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Codec for a signed-token position: writes and reads nothing, since the
|
|
53
|
+
* token's handle never travels in calldata or return data.
|
|
54
|
+
*/
|
|
55
|
+
const SignedTokenCodec = {
|
|
56
|
+
encode(_writer, value) {
|
|
57
|
+
if (!isSignedToken(value)) throw new TypeError("SignedToken expected a signed-token placeholder");
|
|
58
|
+
},
|
|
59
|
+
decode() {
|
|
60
|
+
return SIGNED_TOKEN;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/contract/encode.ts
|
|
65
|
+
const AddressCodec = requireByteArrayCodec(codec.ContractAddress, "Address");
|
|
66
|
+
const BytesCodec = requireByteArrayCodec(VarOpaqueCodec(4294967295), "Bytes");
|
|
67
|
+
const ContractBoolCodec = {
|
|
68
|
+
encode(writer, value) {
|
|
69
|
+
if (typeof value !== "boolean") throw new TypeError("Bool expected boolean");
|
|
70
|
+
BoolCodec.encode(writer, value);
|
|
71
|
+
},
|
|
72
|
+
decode: BoolCodec.decode
|
|
73
|
+
};
|
|
74
|
+
const ContractVoidCodec = {
|
|
75
|
+
encode(writer, value) {
|
|
76
|
+
if (value !== void 0) throw new TypeError("Void expected undefined");
|
|
77
|
+
VoidCodec.encode(writer, value);
|
|
78
|
+
},
|
|
79
|
+
decode: VoidCodec.decode
|
|
80
|
+
};
|
|
81
|
+
const ContractU128Codec = requireBigIntCodec("U128", Uint128Codec);
|
|
82
|
+
const ContractI128Codec = requireBigIntCodec("I128", Int128Codec);
|
|
83
|
+
const ContractU256Codec = requireBigIntCodec("U256", Uint256Codec);
|
|
84
|
+
const ContractI256Codec = requireBigIntCodec("I256", Int256Codec);
|
|
85
|
+
const ContractU64Codec = requireBigIntCodec("U64", Uint64Codec);
|
|
86
|
+
const ContractI64Codec = requireBigIntCodec("I64", Int64Codec);
|
|
87
|
+
const ContractU32Codec = requireBigIntCodec("U32", Uint32Codec);
|
|
88
|
+
const ContractI32Codec = requireBigIntCodec("I32", Int32Codec);
|
|
89
|
+
const StringCodec = {
|
|
90
|
+
encode(w, val) {
|
|
91
|
+
assertWellFormedString(val);
|
|
92
|
+
w.writeString(val, 4294967295);
|
|
93
|
+
},
|
|
94
|
+
decode(r) {
|
|
95
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(r.readVarOpaque(4294967295));
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const resolvedCodecCaches = /* @__PURE__ */ new WeakMap();
|
|
99
|
+
/** @internal Enable identity caching for an immutable named-codec map. */
|
|
100
|
+
function enableResolvedCodecCache(namedCodecs, cacheableTypes) {
|
|
101
|
+
resolvedCodecCaches.set(namedCodecs, {
|
|
102
|
+
cacheableTypes: new WeakSet(cacheableTypes),
|
|
103
|
+
codecs: /* @__PURE__ */ new WeakMap()
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function assertWellFormedString(value) {
|
|
107
|
+
if (typeof value !== "string") throw new TypeError("Expected string");
|
|
108
|
+
for (let index = 0; index < value.length; index++) {
|
|
109
|
+
const codeUnit = value.charCodeAt(index);
|
|
110
|
+
if (codeUnit >= 55296 && codeUnit <= 56319) {
|
|
111
|
+
const next = value.charCodeAt(index + 1);
|
|
112
|
+
if (!Number.isInteger(next) || next < 56320 || next > 57343) throw new TypeError("String contains an unpaired UTF-16 surrogate");
|
|
113
|
+
index++;
|
|
114
|
+
} else if (codeUnit >= 56320 && codeUnit <= 57343) throw new TypeError("String contains an unpaired UTF-16 surrogate");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function requireByteArrayCodec(codec, label = "U8 container") {
|
|
118
|
+
return {
|
|
119
|
+
encode(writer, value) {
|
|
120
|
+
if (!(value instanceof Uint8Array)) throw new TypeError(`Expected Uint8Array for ${label}`);
|
|
121
|
+
codec.encode(writer, value);
|
|
122
|
+
},
|
|
123
|
+
decode: codec.decode
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function requireBigIntCodec(label, codec) {
|
|
127
|
+
return {
|
|
128
|
+
encode(writer, value) {
|
|
129
|
+
if (typeof value !== "bigint") throw new TypeError(`${label} expected bigint`);
|
|
130
|
+
codec.encode(writer, value);
|
|
131
|
+
},
|
|
132
|
+
decode: codec.decode
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function requireArrayCodec(codec) {
|
|
136
|
+
return {
|
|
137
|
+
encode(writer, value) {
|
|
138
|
+
if (!Array.isArray(value)) throw new TypeError("Expected array");
|
|
139
|
+
assertOwnArrayElements(value);
|
|
140
|
+
codec.encode(writer, value);
|
|
141
|
+
},
|
|
142
|
+
decode: codec.decode
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const MAX_ZERO_WIDTH_VECTOR_LENGTH = 1024;
|
|
146
|
+
function safeVarArrayCodec(inner, maxLength) {
|
|
147
|
+
return {
|
|
148
|
+
encode: VarArrayCodec(inner, maxLength).encode,
|
|
149
|
+
decode(reader) {
|
|
150
|
+
const length = Number(reader.readUint32());
|
|
151
|
+
if (length > maxLength) throw new Error(`VarArray length ${length} exceeds max ${maxLength}`);
|
|
152
|
+
if (length > reader.remaining && length > MAX_ZERO_WIDTH_VECTOR_LENGTH) throw new Error(`VarArray length ${length} cannot be safely materialized from ${reader.remaining} remaining bytes`);
|
|
153
|
+
const values = [];
|
|
154
|
+
for (let index = 0; index < length; index++) values.push(inner.decode(reader));
|
|
155
|
+
return values;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Resolve a contract-spec type into the XDR/CXS codec used to encode values.
|
|
161
|
+
*
|
|
162
|
+
* Pass `namedCodecs` when resolving named structs, enums, or unions from a
|
|
163
|
+
* contract spec. Build that map with {@link buildNamedCodecs}.
|
|
164
|
+
*/
|
|
165
|
+
function resolveCodec(ty, namedCodecs) {
|
|
166
|
+
if (!namedCodecs) return resolveUncachedCodec(ty);
|
|
167
|
+
const resolvedCache = resolvedCodecCaches.get(namedCodecs);
|
|
168
|
+
if (!resolvedCache?.cacheableTypes.has(ty)) return resolveUncachedCodec(ty, namedCodecs);
|
|
169
|
+
const cached = resolvedCache.codecs.get(ty);
|
|
170
|
+
if (cached) return cached;
|
|
171
|
+
const codec = resolveUncachedCodec(ty, namedCodecs);
|
|
172
|
+
resolvedCache.codecs.set(ty, codec);
|
|
173
|
+
return codec;
|
|
174
|
+
}
|
|
175
|
+
function resolveUncachedCodec(ty, namedCodecs) {
|
|
176
|
+
switch (ty.v) {
|
|
177
|
+
case ContractTypeKind$1.Address: return AddressCodec;
|
|
178
|
+
case ContractTypeKind$1.U128: return ContractU128Codec;
|
|
179
|
+
case ContractTypeKind$1.I128: return ContractI128Codec;
|
|
180
|
+
case ContractTypeKind$1.U256: return ContractU256Codec;
|
|
181
|
+
case ContractTypeKind$1.I256: return ContractI256Codec;
|
|
182
|
+
case ContractTypeKind$1.U64: return ContractU64Codec;
|
|
183
|
+
case ContractTypeKind$1.I64: return ContractI64Codec;
|
|
184
|
+
case ContractTypeKind$1.U32: return ContractU32Codec;
|
|
185
|
+
case ContractTypeKind$1.I32: return ContractI32Codec;
|
|
186
|
+
case ContractTypeKind$1.U8: return Uint8Codec;
|
|
187
|
+
case ContractTypeKind$1.Bool: return ContractBoolCodec;
|
|
188
|
+
case ContractTypeKind$1.Void: return ContractVoidCodec;
|
|
189
|
+
case ContractTypeKind$1.Bytes: return BytesCodec;
|
|
190
|
+
case ContractTypeKind$1.String: return StringCodec;
|
|
191
|
+
case ContractTypeKind$1.Option: return OptionCodec(resolveCodec(ty.option_inner, namedCodecs));
|
|
192
|
+
case ContractTypeKind$1.Vector:
|
|
193
|
+
if (ty.vector.element?.v === ContractTypeKind$1.U8) return requireByteArrayCodec(VarOpaqueCodec(Number(ty.vector.max_len)));
|
|
194
|
+
return requireArrayCodec(safeVarArrayCodec(resolveCodec(ty.vector.element, namedCodecs), Number(ty.vector.max_len)));
|
|
195
|
+
case ContractTypeKind$1.Array:
|
|
196
|
+
if (ty.array.element?.v === ContractTypeKind$1.U8) return requireByteArrayCodec(FixedOpaqueCodec(Number(ty.array.len)));
|
|
197
|
+
const arrayLength = Number(ty.array.len);
|
|
198
|
+
const arrayCodec = requireArrayCodec(FixedArrayCodec(resolveCodec(ty.array.element, namedCodecs), arrayLength));
|
|
199
|
+
return {
|
|
200
|
+
encode: arrayCodec.encode,
|
|
201
|
+
decode(reader) {
|
|
202
|
+
return fixedArray(arrayCodec.decode(reader), arrayLength);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
case ContractTypeKind$1.Tuple: {
|
|
206
|
+
const innerCodecs = ty.tuple.map((t) => resolveCodec(t, namedCodecs));
|
|
207
|
+
return {
|
|
208
|
+
encode(w, val) {
|
|
209
|
+
if (!Array.isArray(val) || val.length !== innerCodecs.length) throw new TypeError(`Tuple expected ${innerCodecs.length} values, got ${Array.isArray(val) ? val.length : "non-array"}`);
|
|
210
|
+
const arr = val;
|
|
211
|
+
assertOwnArrayElements(arr);
|
|
212
|
+
for (const [i, codec] of innerCodecs.entries()) codec.encode(w, arr[i]);
|
|
213
|
+
},
|
|
214
|
+
decode(r) {
|
|
215
|
+
return innerCodecs.map((c) => c.decode(r));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
case ContractTypeKind$1.Named: {
|
|
220
|
+
const key = specIdKey(ty.named);
|
|
221
|
+
const codec = namedCodecs && Object.hasOwn(namedCodecs, key) ? namedCodecs[key] : void 0;
|
|
222
|
+
if (codec === void 0) throw new Error(`No codec for named type "${formatSpecId(ty.named)}".`);
|
|
223
|
+
return codec;
|
|
224
|
+
}
|
|
225
|
+
case ContractTypeKind$1.SignedToken: return SignedTokenCodec;
|
|
226
|
+
default: throw new Error(`Unsupported ContractType kind: ${ty.v}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function lookupMethod(spec, interfaceName, methodName) {
|
|
230
|
+
const iface = spec.interfaces.find((i) => i.name === interfaceName);
|
|
231
|
+
if (!iface) throw new Error(`Interface "${interfaceName}" not found in spec`);
|
|
232
|
+
const method = iface.methods.find((m) => m.name === methodName);
|
|
233
|
+
if (!method) throw new Error(`Method "${methodName}" not found in interface "${interfaceName}"`);
|
|
234
|
+
return method;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Encode method arguments into contract calldata using a contract spec.
|
|
238
|
+
*
|
|
239
|
+
* Use this when constructing invocations from a spec without generated
|
|
240
|
+
* TypeScript bindings. Generated descriptors use their own resolved codecs.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* const namedCodecs = buildNamedCodecs(spec.type_defs);
|
|
245
|
+
* const calldata = encodeCalldata(
|
|
246
|
+
* spec,
|
|
247
|
+
* "Counter",
|
|
248
|
+
* "increment",
|
|
249
|
+
* [1n],
|
|
250
|
+
* namedCodecs,
|
|
251
|
+
* );
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
function encodeCalldata(spec, interfaceName, methodName, args, namedCodecs) {
|
|
255
|
+
const method = lookupMethod(spec, interfaceName, methodName);
|
|
256
|
+
if (args.length !== method.args.length) throw new Error(`Expected ${method.args.length} args for "${methodName}", got ${args.length}`);
|
|
257
|
+
assertOwnArrayElements(args);
|
|
258
|
+
const w = new CxsWriter();
|
|
259
|
+
for (const [i, arg] of method.args.entries()) resolveCodec(arg.ty, namedCodecs).encode(w, args[i]);
|
|
260
|
+
return calldata(w.finish());
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Decode contract calldata into an object keyed by argument name.
|
|
264
|
+
*
|
|
265
|
+
* This is primarily useful for tooling, tests, and debugging generated
|
|
266
|
+
* invocations.
|
|
267
|
+
*/
|
|
268
|
+
function decodeCalldata(spec, interfaceName, methodName, data, namedCodecs) {
|
|
269
|
+
const method = lookupMethod(spec, interfaceName, methodName);
|
|
270
|
+
try {
|
|
271
|
+
const r = new CxsReader(data);
|
|
272
|
+
const result = {};
|
|
273
|
+
for (const arg of method.args) {
|
|
274
|
+
const argCodec = resolveCodec(arg.ty, namedCodecs);
|
|
275
|
+
defineRecordValue$2(result, arg.name, argCodec.decode(r));
|
|
276
|
+
}
|
|
277
|
+
if (r.remaining !== 0) throw new Error(`Trailing data after decoding "${methodName}": ${r.remaining} bytes remaining`);
|
|
278
|
+
return result;
|
|
279
|
+
} catch (cause) {
|
|
280
|
+
throw new ContractDecodeError(`Failed to decode calldata for "${interfaceName}.${methodName}"${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function assertOwnArrayElements(value) {
|
|
284
|
+
for (let index = 0; index < value.length; index++) if (!Object.hasOwn(value, index)) throw new TypeError(`Array requires own value at index ${index}`);
|
|
285
|
+
}
|
|
286
|
+
function defineRecordValue$2(record, name, value) {
|
|
287
|
+
Object.defineProperty(record, name, {
|
|
288
|
+
configurable: true,
|
|
289
|
+
enumerable: true,
|
|
290
|
+
value,
|
|
291
|
+
writable: true
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/contract/codegen.ts
|
|
296
|
+
/**
|
|
297
|
+
* Build codecs for named contract-spec types.
|
|
298
|
+
*
|
|
299
|
+
* The returned map is required by {@link encodeCalldata},
|
|
300
|
+
* {@link decodeCalldata}, and {@link resolveCodec} whenever a method argument
|
|
301
|
+
* references a named struct, enum, or union. It is keyed by
|
|
302
|
+
* {@link specIdKey} of each type's spec id.
|
|
303
|
+
*
|
|
304
|
+
* @throws If two definitions share a spec id.
|
|
305
|
+
*/
|
|
306
|
+
function buildNamedCodecs(typeDefs) {
|
|
307
|
+
const codecs = Object.create(null);
|
|
308
|
+
const define = (id, codec) => {
|
|
309
|
+
const key = specIdKey(id);
|
|
310
|
+
if (Object.hasOwn(codecs, key)) throw new Error(`Contract spec defines "${formatSpecId(id)}" twice`);
|
|
311
|
+
codecs[key] = codec;
|
|
312
|
+
};
|
|
313
|
+
for (const def of typeDefs) switch (def.v) {
|
|
314
|
+
case ContractTypeDefKind$1.TypeDefEnum:
|
|
315
|
+
define(def.enum_def.name, enumCodec(formatSpecId(def.enum_def.name), def.enum_def.variants.map((variant) => variant.value)));
|
|
316
|
+
break;
|
|
317
|
+
case ContractTypeDefKind$1.TypeDefStruct: {
|
|
318
|
+
const structDef = def.struct_def;
|
|
319
|
+
const name = formatSpecId(structDef.name);
|
|
320
|
+
define(structDef.name, {
|
|
321
|
+
encode(w, val) {
|
|
322
|
+
if (!val || typeof val !== "object" || Array.isArray(val)) throw new TypeError(`Expected object for struct "${name}"`);
|
|
323
|
+
const obj = val;
|
|
324
|
+
for (const field of structDef.fields) resolveCodec(field.ty, codecs).encode(w, ownProperty(obj, field.name, `Struct "${name}"`));
|
|
325
|
+
},
|
|
326
|
+
decode(r) {
|
|
327
|
+
const obj = {};
|
|
328
|
+
for (const field of structDef.fields) {
|
|
329
|
+
const fieldCodec = resolveCodec(field.ty, codecs);
|
|
330
|
+
defineRecordValue$1(obj, field.name, fieldCodec.decode(r));
|
|
331
|
+
}
|
|
332
|
+
return obj;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
case ContractTypeDefKind$1.TypeDefUnion: {
|
|
338
|
+
const unionDef = def.union_def;
|
|
339
|
+
const name = formatSpecId(unionDef.name);
|
|
340
|
+
define(unionDef.name, {
|
|
341
|
+
encode(w, val) {
|
|
342
|
+
if (!val || typeof val !== "object" || Array.isArray(val)) throw new TypeError(`Expected case object for union "${name}"`);
|
|
343
|
+
const obj = val;
|
|
344
|
+
const caseName = ownProperty(obj, "case", `Union "${name}"`);
|
|
345
|
+
const caseSpec = unionDef.cases.find((candidate) => candidate.name === caseName);
|
|
346
|
+
if (!caseSpec) throw new Error(`Unknown case ${String(caseName)} for union "${name}"`);
|
|
347
|
+
w.writeInt32(caseSpec.discriminant);
|
|
348
|
+
encodeUnionCase(caseSpec.case_type, Object.hasOwn(obj, "value") ? obj.value : void 0, w, codecs);
|
|
349
|
+
},
|
|
350
|
+
decode(r) {
|
|
351
|
+
const discriminant = r.readInt32();
|
|
352
|
+
const caseSpec = unionDef.cases.find((c) => c.discriminant === discriminant);
|
|
353
|
+
if (!caseSpec) throw new Error(`Unknown discriminant ${discriminant} for union "${name}"`);
|
|
354
|
+
return decodeUnionCase(caseSpec.case_type, caseSpec.name, r, codecs);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
default: throw new Error(`Unsupported ContractTypeDefKind: ${def.v}`);
|
|
360
|
+
}
|
|
361
|
+
return codecs;
|
|
362
|
+
}
|
|
363
|
+
function enumCodec(name, values) {
|
|
364
|
+
const declared = new Set(values);
|
|
365
|
+
return {
|
|
366
|
+
encode(writer, value) {
|
|
367
|
+
if (typeof value !== "bigint" || !declared.has(value)) throw new Error(`Unknown value ${String(value)} for enum "${name}"`);
|
|
368
|
+
Int32Codec.encode(writer, value);
|
|
369
|
+
},
|
|
370
|
+
decode(reader) {
|
|
371
|
+
const value = Int32Codec.decode(reader);
|
|
372
|
+
if (!declared.has(value)) throw new Error(`Unknown value ${value} for enum "${name}"`);
|
|
373
|
+
return value;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
function encodeUnionCase(caseType, value, w, codecs) {
|
|
378
|
+
switch (caseType.v) {
|
|
379
|
+
case ContractUnionCaseKind.UnionCaseVoid:
|
|
380
|
+
if (value !== void 0) throw new TypeError("Void union case must not contain a value");
|
|
381
|
+
break;
|
|
382
|
+
case ContractUnionCaseKind.UnionCaseTuple: {
|
|
383
|
+
if (!Array.isArray(value) || value.length !== caseType.tuple.length) throw new TypeError(`Tuple union case expected ${caseType.tuple.length} values`);
|
|
384
|
+
const arr = value;
|
|
385
|
+
for (const [i, type] of caseType.tuple.entries()) {
|
|
386
|
+
if (!Object.hasOwn(arr, i)) throw new TypeError(`Tuple union case requires own value at index ${i}`);
|
|
387
|
+
resolveCodec(type, codecs).encode(w, arr[i]);
|
|
388
|
+
}
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
case ContractUnionCaseKind.UnionCaseStruct: {
|
|
392
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new TypeError("Struct union case requires an object value");
|
|
393
|
+
const fields = value;
|
|
394
|
+
for (const field of caseType.struct_fields) resolveCodec(field.ty, codecs).encode(w, ownProperty(fields, field.name, "Struct union case"));
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
default: throw new Error(`Unsupported ContractUnionCaseKind: ${caseType.v}`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function decodeUnionCase(caseType, caseName, r, codecs) {
|
|
401
|
+
switch (caseType.v) {
|
|
402
|
+
case ContractUnionCaseKind.UnionCaseVoid: return { case: caseName };
|
|
403
|
+
case ContractUnionCaseKind.UnionCaseTuple: return {
|
|
404
|
+
case: caseName,
|
|
405
|
+
value: caseType.tuple.map((ty) => {
|
|
406
|
+
return resolveCodec(ty, codecs).decode(r);
|
|
407
|
+
})
|
|
408
|
+
};
|
|
409
|
+
case ContractUnionCaseKind.UnionCaseStruct: {
|
|
410
|
+
const value = {};
|
|
411
|
+
for (const field of caseType.struct_fields) {
|
|
412
|
+
const c = resolveCodec(field.ty, codecs);
|
|
413
|
+
defineRecordValue$1(value, field.name, c.decode(r));
|
|
414
|
+
}
|
|
415
|
+
return {
|
|
416
|
+
case: caseName,
|
|
417
|
+
value
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
default: throw new Error(`Unsupported ContractUnionCaseKind: ${caseType.v}`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function ownProperty(value, name, context) {
|
|
424
|
+
if (!Object.hasOwn(value, name)) throw new TypeError(`${context} requires own property "${name}"`);
|
|
425
|
+
return value[name];
|
|
426
|
+
}
|
|
427
|
+
function defineRecordValue$1(record, name, value) {
|
|
428
|
+
Object.defineProperty(record, name, {
|
|
429
|
+
configurable: true,
|
|
430
|
+
enumerable: true,
|
|
431
|
+
value,
|
|
432
|
+
writable: true
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
//#endregion
|
|
436
|
+
//#region src/contract/hash.ts
|
|
437
|
+
/**
|
|
438
|
+
* Return the network identity of a protocol contract.
|
|
439
|
+
*
|
|
440
|
+
* The hash covers the canonical encoded `Contract`, including its type and
|
|
441
|
+
* payload framing. Hashing only the raw artifact bytes produces a different
|
|
442
|
+
* value and therefore a different derived address.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* import {
|
|
447
|
+
* deriveContractAddress,
|
|
448
|
+
* hashContract,
|
|
449
|
+
* } from "@thruputchain/client";
|
|
450
|
+
*
|
|
451
|
+
* const contractHash = hashContract(contract);
|
|
452
|
+
* const address = deriveContractAddress(wallet, deploymentNonce, contractHash);
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
function hashContract(contract) {
|
|
456
|
+
return sha256Hash(sha256(serialize(contract, codec.Contract)));
|
|
457
|
+
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/derivation/contractAddress.ts
|
|
460
|
+
/**
|
|
461
|
+
* The on-chain address a deployment produces:
|
|
462
|
+
* `sha256(txs(ContractDeploymentHashPreimage))` over the deployer, nonce, and
|
|
463
|
+
* contract hash.
|
|
464
|
+
*/
|
|
465
|
+
function deriveContractAddress(deployer, nonce, contractHash) {
|
|
466
|
+
return contractAddress(sha256(serialize({
|
|
467
|
+
deployer_address: deployer,
|
|
468
|
+
nonce,
|
|
469
|
+
contract_hash: contractHash
|
|
470
|
+
}, codec.ContractDeploymentHashPreimage)));
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/contract/codec.ts
|
|
474
|
+
const contextCodecs = /* @__PURE__ */ new WeakMap();
|
|
475
|
+
const contextEnums = /* @__PURE__ */ new WeakMap();
|
|
476
|
+
/** Build the shared codec context for a generated binding module. */
|
|
477
|
+
function createContractCodecContext(typeDefs) {
|
|
478
|
+
const context = Object.freeze({});
|
|
479
|
+
const typeDefsSnapshot = structuredClone([...typeDefs]);
|
|
480
|
+
const namedCodecs = buildNamedCodecs(typeDefsSnapshot);
|
|
481
|
+
enableResolvedCodecCache(namedCodecs, collectTypeNodes(typeDefsSnapshot));
|
|
482
|
+
contextCodecs.set(context, namedCodecs);
|
|
483
|
+
const enums = /* @__PURE__ */ new Map();
|
|
484
|
+
for (const definition of typeDefsSnapshot) if (definition.v === ContractTypeDefKind$1.TypeDefEnum) enums.set(specIdKey(definition.enum_def.name), new Map(definition.enum_def.variants.map((variant) => [variant.value, variant.name])));
|
|
485
|
+
contextEnums.set(context, enums);
|
|
486
|
+
return context;
|
|
487
|
+
}
|
|
488
|
+
/** @internal */
|
|
489
|
+
function contextEnumVariant(context, type, value) {
|
|
490
|
+
return type.v === ContractTypeKind$1.Named && typeof value === "bigint" ? contextEnums.get(context)?.get(specIdKey(type.named))?.get(value) : void 0;
|
|
491
|
+
}
|
|
492
|
+
function collectTypeNodes(typeDefs) {
|
|
493
|
+
const types = /* @__PURE__ */ new Set();
|
|
494
|
+
const visit = (type) => {
|
|
495
|
+
if (types.has(type)) return;
|
|
496
|
+
types.add(type);
|
|
497
|
+
switch (type.v) {
|
|
498
|
+
case ContractTypeKind$1.Option:
|
|
499
|
+
visit(type.option_inner);
|
|
500
|
+
break;
|
|
501
|
+
case ContractTypeKind$1.Vector:
|
|
502
|
+
visit(type.vector.element);
|
|
503
|
+
break;
|
|
504
|
+
case ContractTypeKind$1.Tuple:
|
|
505
|
+
type.tuple.forEach(visit);
|
|
506
|
+
break;
|
|
507
|
+
case ContractTypeKind$1.Array:
|
|
508
|
+
visit(type.array.element);
|
|
509
|
+
break;
|
|
510
|
+
case ContractTypeKind$1.SignedToken:
|
|
511
|
+
if (type.signed_token_preimage.preimage_type) visit(type.signed_token_preimage.preimage_type);
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
for (const definition of typeDefs) if (definition.v === ContractTypeDefKind$1.TypeDefStruct) definition.struct_def.fields.forEach((field) => visit(field.ty));
|
|
516
|
+
else if (definition.v === ContractTypeDefKind$1.TypeDefUnion) {
|
|
517
|
+
for (const unionCase of definition.union_def.cases) if (unionCase.case_type.v === ContractUnionCaseKind.UnionCaseTuple) unionCase.case_type.tuple.forEach(visit);
|
|
518
|
+
else if (unionCase.case_type.v === ContractUnionCaseKind.UnionCaseStruct) unionCase.case_type.struct_fields.forEach((field) => visit(field.ty));
|
|
519
|
+
}
|
|
520
|
+
return types;
|
|
521
|
+
}
|
|
522
|
+
/** @internal */
|
|
523
|
+
function resolveContextCodec(context, type) {
|
|
524
|
+
const namedCodecs = contextCodecs.get(context);
|
|
525
|
+
if (!namedCodecs) throw new TypeError("Invalid ContractCodecContext");
|
|
526
|
+
return resolveCodec(type, namedCodecs);
|
|
527
|
+
}
|
|
528
|
+
/** @internal */
|
|
529
|
+
function createContractCodec(context, type, label = "contract value") {
|
|
530
|
+
const valueCodec = resolveContextCodec(context, type);
|
|
531
|
+
return {
|
|
532
|
+
encode(value) {
|
|
533
|
+
const writer = new CxsWriter$1();
|
|
534
|
+
valueCodec.encode(writer, value);
|
|
535
|
+
return writer.finish();
|
|
536
|
+
},
|
|
537
|
+
decode(data) {
|
|
538
|
+
return decodeComplete(valueCodec, data, label);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
/** @internal */
|
|
543
|
+
function decodeComplete(valueCodec, data, label) {
|
|
544
|
+
try {
|
|
545
|
+
const reader = new CxsReader$1(data);
|
|
546
|
+
const value = valueCodec.decode(reader);
|
|
547
|
+
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing bytes`);
|
|
548
|
+
return value;
|
|
549
|
+
} catch (cause) {
|
|
550
|
+
if (cause instanceof ContractDecodeError) throw cause;
|
|
551
|
+
throw new ContractDecodeError(`Failed to decode ${label}${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
//#endregion
|
|
555
|
+
//#region src/contract/event.ts
|
|
556
|
+
const maxFilterContractAddresses = 5;
|
|
557
|
+
const maxFilterTopicValues = 16;
|
|
558
|
+
/** Build one generated event descriptor from direct ABI metadata. */
|
|
559
|
+
function defineContractEvent(definition) {
|
|
560
|
+
const eventName = definition.name;
|
|
561
|
+
const signature = fixedBytes(Uint8Array.from(definition.signature), 32);
|
|
562
|
+
if (definition.topics.length > 2) throw new Error(`${eventName} declares ${definition.topics.length + 1} topics; protocol maximum is 3`);
|
|
563
|
+
assertUniqueNames(definition.topics, `${eventName} topic`);
|
|
564
|
+
assertUniqueNames(definition.fields, `${eventName} field`);
|
|
565
|
+
const resolvedTopics = definition.topics.map((metadata) => {
|
|
566
|
+
const expected = topicRepresentation(metadata.type);
|
|
567
|
+
if (metadata.representation !== expected) throw new Error(`${eventName} topic "${metadata.name}" representation "${metadata.representation}" does not match its declared type; expected "${expected}"`);
|
|
568
|
+
if (metadata.representation !== "address" && metadata.representation !== "fixedBytes32" && metadata.representation !== "hash") {
|
|
569
|
+
const signed = metadata.representation.startsWith("i");
|
|
570
|
+
const bits = metadata.representation === "bool" ? 1 : Number(metadata.representation.slice(1));
|
|
571
|
+
const bound = 1n << BigInt(signed ? bits - 1 : bits);
|
|
572
|
+
return {
|
|
573
|
+
name: metadata.name,
|
|
574
|
+
representation: metadata.representation,
|
|
575
|
+
codec: resolveContextCodec(definition.codecs, { v: signed ? ContractTypeKind$1.I256 : ContractTypeKind$1.U256 }),
|
|
576
|
+
min: signed ? -bound : 0n,
|
|
577
|
+
max: bound - 1n
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
return {
|
|
581
|
+
name: metadata.name,
|
|
582
|
+
representation: metadata.representation
|
|
583
|
+
};
|
|
584
|
+
});
|
|
585
|
+
const fields = definition.fields.map((metadata) => ({
|
|
586
|
+
metadata: { ...metadata },
|
|
587
|
+
codec: resolveContextCodec(definition.codecs, metadata.type)
|
|
588
|
+
}));
|
|
589
|
+
const descriptor = {
|
|
590
|
+
get signature() {
|
|
591
|
+
return Uint8Array.from(signature);
|
|
592
|
+
},
|
|
593
|
+
decode(event) {
|
|
594
|
+
if (!bytesEqual(event.topics[0], signature)) return null;
|
|
595
|
+
try {
|
|
596
|
+
const expectedTopicCount = resolvedTopics.length + 1;
|
|
597
|
+
if (event.topics.length !== expectedTopicCount) throw new Error(`expected ${expectedTopicCount} topics, got ${event.topics.length}`);
|
|
598
|
+
assertEventSize(event.topics, event.data);
|
|
599
|
+
const decodedTopics = {};
|
|
600
|
+
for (const [index, metadata] of resolvedTopics.entries()) {
|
|
601
|
+
const rawTopic = event.topics[index + 1];
|
|
602
|
+
if (rawTopic === void 0) throw new Error(`missing topic "${metadata.name}"`);
|
|
603
|
+
const value = decodeTopic(metadata, fixedBytes(rawTopic, 32));
|
|
604
|
+
defineRecordValue(decodedTopics, metadata.name, value);
|
|
605
|
+
}
|
|
606
|
+
const reader = new CxsReader$1(event.data);
|
|
607
|
+
const decodedFields = {};
|
|
608
|
+
for (const field of fields) defineRecordValue(decodedFields, field.metadata.name, field.codec.decode(reader));
|
|
609
|
+
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing payload bytes`);
|
|
610
|
+
return {
|
|
611
|
+
name: eventName,
|
|
612
|
+
topics: decodedTopics,
|
|
613
|
+
fields: decodedFields
|
|
614
|
+
};
|
|
615
|
+
} catch (cause) {
|
|
616
|
+
if (cause instanceof ContractDecodeError) throw cause;
|
|
617
|
+
throw new ContractDecodeError(`Failed to decode ${eventName}${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
618
|
+
}
|
|
619
|
+
},
|
|
620
|
+
filter(input) {
|
|
621
|
+
if (input.addresses.length === 0) throw new Error(`${eventName} filter requires a contract address`);
|
|
622
|
+
if (input.addresses.length > maxFilterContractAddresses) throw new Error(`${eventName} filter supports at most ${maxFilterContractAddresses} contract addresses`);
|
|
623
|
+
for (const address of input.addresses) fixedBytes(address, 32);
|
|
624
|
+
const filterTopics = { topic0: [Uint8Array.from(signature)] };
|
|
625
|
+
const constraints = input.topics;
|
|
626
|
+
if (constraints) {
|
|
627
|
+
const directTopics = new Map(resolvedTopics.map((metadata, index) => ({
|
|
628
|
+
metadata,
|
|
629
|
+
index
|
|
630
|
+
})).filter(({ metadata }) => metadata.representation !== "hash").map((topic) => [topic.metadata.name, topic]));
|
|
631
|
+
for (const [name, constraint] of Object.entries(constraints)) {
|
|
632
|
+
const topic = directTopics.get(name);
|
|
633
|
+
if (topic === void 0) throw new Error(`${eventName} topic "${name}" is not directly filterable`);
|
|
634
|
+
const { metadata, index } = topic;
|
|
635
|
+
const values = filterValues(constraint, eventName, metadata);
|
|
636
|
+
const key = `topic${index + 1}`;
|
|
637
|
+
filterTopics[key] = values.map((value) => encodeTopic(metadata, value, eventName));
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return {
|
|
641
|
+
addresses: [...input.addresses],
|
|
642
|
+
topics: filterTopics
|
|
643
|
+
};
|
|
644
|
+
},
|
|
645
|
+
allFromReceipt(receipt, options) {
|
|
646
|
+
const matches = [];
|
|
647
|
+
for (const event of receipt.events) {
|
|
648
|
+
if (!bytesEqual(event.address, options.address)) continue;
|
|
649
|
+
const decoded = descriptor.decode(event);
|
|
650
|
+
if (decoded !== null) matches.push({
|
|
651
|
+
...decoded,
|
|
652
|
+
address: contractAddress(event.address.slice())
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
return matches;
|
|
656
|
+
},
|
|
657
|
+
singleFromReceipt(receipt, options) {
|
|
658
|
+
const matches = descriptor.allFromReceipt(receipt, options);
|
|
659
|
+
const match = matches[0];
|
|
660
|
+
if (matches.length !== 1 || match === void 0) throw new Error(`Expected exactly one ${eventName} event, got ${matches.length}`);
|
|
661
|
+
return match;
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
return descriptor;
|
|
665
|
+
}
|
|
666
|
+
function topicRepresentation(type) {
|
|
667
|
+
if (type.v === ContractTypeKind$1.Address) return "address";
|
|
668
|
+
if (type.v === ContractTypeKind$1.U32) return "u32";
|
|
669
|
+
if (type.v === ContractTypeKind$1.U64) return "u64";
|
|
670
|
+
if (type.v === ContractTypeKind$1.U128) return "u128";
|
|
671
|
+
if (type.v === ContractTypeKind$1.U256) return "u256";
|
|
672
|
+
if (type.v === ContractTypeKind$1.I32) return "i32";
|
|
673
|
+
if (type.v === ContractTypeKind$1.I64) return "i64";
|
|
674
|
+
if (type.v === ContractTypeKind$1.I128) return "i128";
|
|
675
|
+
if (type.v === ContractTypeKind$1.I256) return "i256";
|
|
676
|
+
if (type.v === ContractTypeKind$1.Bool) return "bool";
|
|
677
|
+
if (type.v === ContractTypeKind$1.Array && type.array.len === 32n && type.array.element?.v === ContractTypeKind$1.U8) return "fixedBytes32";
|
|
678
|
+
return "hash";
|
|
679
|
+
}
|
|
680
|
+
/** Compare public byte identifiers. This function is not constant-time. */
|
|
681
|
+
function bytesEqual(left, right) {
|
|
682
|
+
if (!left || left.length !== right.length) return false;
|
|
683
|
+
for (let index = 0; index < left.length; index++) if (left[index] !== right[index]) return false;
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
function filterValues(constraint, eventName, topic) {
|
|
687
|
+
if (constraint && typeof constraint === "object" && !Array.isArray(constraint) && !(constraint instanceof Uint8Array) && "oneOf" in constraint) {
|
|
688
|
+
const values = constraint.oneOf;
|
|
689
|
+
if (!Array.isArray(values) || values.length === 0) throw new Error(`${eventName} topic "${topic.name}" oneOf must not be empty`);
|
|
690
|
+
if (values.length > maxFilterTopicValues) throw new Error(`${eventName} topic "${topic.name}" oneOf supports at most ${maxFilterTopicValues} values`);
|
|
691
|
+
return values;
|
|
692
|
+
}
|
|
693
|
+
return [constraint];
|
|
694
|
+
}
|
|
695
|
+
function decodeTopic(topic, bytes) {
|
|
696
|
+
switch (topic.representation) {
|
|
697
|
+
case "address": return contractAddress(bytes);
|
|
698
|
+
case "fixedBytes32":
|
|
699
|
+
case "hash": return bytes;
|
|
700
|
+
default: {
|
|
701
|
+
const value = topic.codec.decode(new CxsReader$1(bytes));
|
|
702
|
+
assertScalarRange(topic, value);
|
|
703
|
+
return topic.representation === "bool" ? value === 1n : value;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
function encodeTopic(topic, value, eventName) {
|
|
708
|
+
switch (topic.representation) {
|
|
709
|
+
case "address":
|
|
710
|
+
case "fixedBytes32": return fixedBytes(requireBytes(value, eventName, topic.name), 32);
|
|
711
|
+
case "hash": throw new Error(`${eventName} topic "${topic.name}" is not directly filterable`);
|
|
712
|
+
default: {
|
|
713
|
+
const writer = new CxsWriter$1();
|
|
714
|
+
try {
|
|
715
|
+
if (topic.representation === "bool") {
|
|
716
|
+
if (typeof value !== "boolean") throw new TypeError("Bool expected boolean");
|
|
717
|
+
value = value ? 1n : 0n;
|
|
718
|
+
}
|
|
719
|
+
topic.codec.encode(writer, value);
|
|
720
|
+
assertScalarRange(topic, value);
|
|
721
|
+
} catch (cause) {
|
|
722
|
+
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
723
|
+
throw new Error(`${eventName} topic "${topic.name}" failed to encode${detail}`, { cause });
|
|
724
|
+
}
|
|
725
|
+
return fixedBytes(writer.finish(), 32);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
function assertScalarRange(topic, value) {
|
|
730
|
+
if (value < topic.min || value > topic.max) throw new RangeError(`topic "${topic.name}" is outside the ${topic.representation} range`);
|
|
731
|
+
}
|
|
732
|
+
function requireBytes(value, eventName, topicName) {
|
|
733
|
+
if (!(value instanceof Uint8Array)) throw new TypeError(`${eventName} topic "${topicName}" must be bytes`);
|
|
734
|
+
return value;
|
|
735
|
+
}
|
|
736
|
+
function assertEventSize(topics, data) {
|
|
737
|
+
const size = topics.reduce((total, topic) => total + topic.length, data.length);
|
|
738
|
+
const limit = Number(kMaxEventLength);
|
|
739
|
+
if (size > limit) throw new Error(`serialized event size ${size} exceeds ${limit} bytes`);
|
|
740
|
+
}
|
|
741
|
+
function assertUniqueNames(values, label) {
|
|
742
|
+
const names = /* @__PURE__ */ new Set();
|
|
743
|
+
for (const value of values) {
|
|
744
|
+
if (names.has(value.name)) throw new Error(`Duplicate ${label} name "${value.name}"`);
|
|
745
|
+
names.add(value.name);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
function defineRecordValue(record, name, value) {
|
|
749
|
+
Object.defineProperty(record, name, {
|
|
750
|
+
configurable: true,
|
|
751
|
+
enumerable: true,
|
|
752
|
+
value,
|
|
753
|
+
writable: true
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
//#endregion
|
|
757
|
+
//#region src/actions/viewFreshness.ts
|
|
758
|
+
/** Reject a successful view that violates the caller's snapshot constraint. */
|
|
759
|
+
function assertViewFreshness(height, minHeight) {
|
|
760
|
+
if (minHeight !== void 0 && height < minHeight) throw new Error(`View response height ${height} is below the requested minimum ${minHeight}`);
|
|
761
|
+
}
|
|
762
|
+
//#endregion
|
|
763
|
+
//#region src/actions/viewAuthClaim.ts
|
|
764
|
+
function u32(value, name) {
|
|
765
|
+
serializeU64(value, name);
|
|
766
|
+
if (value > 4294967295n) throw new RangeError(`${name} must fit uint32`);
|
|
767
|
+
return Number(value);
|
|
768
|
+
}
|
|
769
|
+
/** Project public authorization assumptions; this neither signs nor verifies authorization. */
|
|
770
|
+
function toAssumedAuth(claim) {
|
|
771
|
+
switch (claim.claim_type) {
|
|
772
|
+
case AuthClaimType.kSourceAddressAuthData: return {
|
|
773
|
+
kind: "sourceAddress",
|
|
774
|
+
allowedKeys: u32(claim.source_address_claim.allowed_keys, "allowedKeys"),
|
|
775
|
+
requiredWeight: u32(claim.source_address_claim.required_weight, "requiredWeight")
|
|
776
|
+
};
|
|
777
|
+
case AuthClaimType.kPubkeySignature: {
|
|
778
|
+
const { pubkey_hash, nonce } = claim.pubkey_preimage;
|
|
779
|
+
if (!(pubkey_hash instanceof Uint8Array) || pubkey_hash.length !== 32) throw new TypeError("Assumed pubkey hash must contain 32 bytes");
|
|
780
|
+
serializeU64(nonce, "accountNonce");
|
|
781
|
+
return {
|
|
782
|
+
kind: "pubkeySignature",
|
|
783
|
+
pubkeyHash: pubkey_hash.slice(),
|
|
784
|
+
accountNonce: nonce
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
default: throw new TypeError("Unsupported authorization assumption");
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
//#endregion
|
|
791
|
+
//#region src/contract/method.ts
|
|
792
|
+
/** Error thrown by the view API for aborts, faults, or unsupported outcomes. */
|
|
793
|
+
var ContractViewError = class extends Error {
|
|
794
|
+
outcome;
|
|
795
|
+
constructor(outcome) {
|
|
796
|
+
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
797
|
+
super(outcome.kind === "abort" ? `Contract view aborted with ${formatException(outcome.exception, selector)}` : outcome.kind === "unsupported" ? "Unsupported contract simulation outcome" : "Contract view faulted");
|
|
798
|
+
this.name = "ContractViewError";
|
|
799
|
+
this.outcome = outcome;
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
/** Error thrown when a root contract receipt does not produce a typed return. */
|
|
803
|
+
var ContractReceiptError = class extends Error {
|
|
804
|
+
outcome;
|
|
805
|
+
constructor(outcome) {
|
|
806
|
+
const selector = outcome.kind === "abort" ? exceptionSelector(outcome.exception) : void 0;
|
|
807
|
+
super(outcome.kind === "abort" ? `Contract transaction ${toBase58(outcome.transactionHash)} aborted with ${formatException(outcome.exception, selector)}` : `Contract transaction ${toBase58(outcome.transactionHash)} did not return a supported successful result`);
|
|
808
|
+
this.name = "ContractReceiptError";
|
|
809
|
+
this.outcome = outcome;
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
function formatException(exception, selector) {
|
|
813
|
+
if (exception && typeof exception === "object" && "name" in exception && typeof exception.name === "string" && "value" in exception) {
|
|
814
|
+
const value = typeof exception.value === "bigint" ? `${exception.value}n` : JSON.stringify(exception.value, (_, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
815
|
+
const variant = "variant" in exception && typeof exception.variant === "string" ? `.${exception.variant}` : "";
|
|
816
|
+
return `${exception.name}${variant} (${value})`;
|
|
817
|
+
}
|
|
818
|
+
return `unknown selector ${selector === void 0 ? "unknown" : `0x${selector.toString(16)}`}`;
|
|
819
|
+
}
|
|
820
|
+
function exceptionSelector(exception) {
|
|
821
|
+
if (!exception || typeof exception !== "object" || !("selector" in exception)) return;
|
|
822
|
+
return typeof exception.selector === "bigint" ? exception.selector : void 0;
|
|
823
|
+
}
|
|
824
|
+
/** Build one generated method descriptor from direct ABI metadata. */
|
|
825
|
+
function defineContractMethod(definition) {
|
|
826
|
+
const entrypoint = copyEntrypoint(definition.entrypoint);
|
|
827
|
+
const effect = definition.effect ?? null;
|
|
828
|
+
if (effect !== null && ![
|
|
829
|
+
ContractMethodEffect.EffectPure,
|
|
830
|
+
ContractMethodEffect.EffectReadOnly,
|
|
831
|
+
ContractMethodEffect.EffectStateChanging
|
|
832
|
+
].includes(effect)) throw new TypeError(`Unsupported contract method effect ${effect}`);
|
|
833
|
+
const name = definition.interfaceName && definition.publicName ? `${definition.interfaceName}.${definition.publicName}` : toHex0x(entrypoint);
|
|
834
|
+
const encode = (args) => {
|
|
835
|
+
try {
|
|
836
|
+
return encodeArguments(argumentCodecs, args);
|
|
837
|
+
} catch (cause) {
|
|
838
|
+
throw new TypeError(`${name}: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
const argumentCodecs = definition.args.map((argument) => ({
|
|
842
|
+
publicName: argument.publicName,
|
|
843
|
+
codec: resolveContextCodec(definition.codecs, argument.type)
|
|
844
|
+
}));
|
|
845
|
+
const returnCodec = resolveContextCodec(definition.codecs, definition.returnType);
|
|
846
|
+
const exceptionBySelector = /* @__PURE__ */ new Map();
|
|
847
|
+
for (const metadata of definition.throws) {
|
|
848
|
+
if (exceptionBySelector.has(metadata.selector)) throw new Error(`Duplicate contract exception selector ${metadata.selector}`);
|
|
849
|
+
exceptionBySelector.set(metadata.selector, {
|
|
850
|
+
name: metadata.name,
|
|
851
|
+
codec: resolveContextCodec(definition.codecs, metadata.type),
|
|
852
|
+
variant: (value) => contextEnumVariant(definition.codecs, metadata.type, value)
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
const method = {
|
|
856
|
+
name,
|
|
857
|
+
effect,
|
|
858
|
+
viewRequest(options) {
|
|
859
|
+
if ("minStateBlockHeight" in options) throw new TypeError("Set minStateBlockHeight on readViews, not on a prepared call");
|
|
860
|
+
return {
|
|
861
|
+
request: {
|
|
862
|
+
address: contractAddress(new Uint8Array(options.address)),
|
|
863
|
+
entrypoint: copyEntrypoint(entrypoint),
|
|
864
|
+
calldata: encode("args" in options ? options.args : void 0),
|
|
865
|
+
...options.assumedAuth === void 0 ? {} : { assumedAuth: toAssumedAuth(options.assumedAuth) },
|
|
866
|
+
...options.caller ? { caller: contractAddress(new Uint8Array(options.caller)) } : {}
|
|
867
|
+
},
|
|
868
|
+
method
|
|
869
|
+
};
|
|
870
|
+
},
|
|
871
|
+
call(options) {
|
|
872
|
+
if ("minStateBlockHeight" in options) throw new TypeError("minStateBlockHeight is a read constraint, not a transaction option");
|
|
873
|
+
return {
|
|
874
|
+
invocation: {
|
|
875
|
+
invoked_address: contractAddress(new Uint8Array(options.address)),
|
|
876
|
+
entrypoint: copyEntrypoint(entrypoint),
|
|
877
|
+
gas_limit: options.gasLimit,
|
|
878
|
+
calldata: encode("args" in options ? options.args : void 0)
|
|
879
|
+
},
|
|
880
|
+
method
|
|
881
|
+
};
|
|
882
|
+
},
|
|
883
|
+
get entrypoint() {
|
|
884
|
+
return copyEntrypoint(entrypoint);
|
|
885
|
+
},
|
|
886
|
+
invocation(contract, gasLimit, ...args) {
|
|
887
|
+
return {
|
|
888
|
+
invoked_address: contract,
|
|
889
|
+
entrypoint: copyEntrypoint(entrypoint),
|
|
890
|
+
gas_limit: gasLimit,
|
|
891
|
+
calldata: encode(args[0])
|
|
892
|
+
};
|
|
893
|
+
},
|
|
894
|
+
decodeArgs(data) {
|
|
895
|
+
const reader = new CxsReader$1(data);
|
|
896
|
+
const args = Object.fromEntries(argumentCodecs.map((argument) => {
|
|
897
|
+
try {
|
|
898
|
+
return [argument.publicName, argument.codec.decode(reader)];
|
|
899
|
+
} catch (cause) {
|
|
900
|
+
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
901
|
+
throw new ContractDecodeError(`${name}: argument "${argument.publicName}"${detail}`, { cause });
|
|
902
|
+
}
|
|
903
|
+
}));
|
|
904
|
+
if (reader.remaining !== 0) throw new ContractDecodeError(`${name}: ${reader.remaining} trailing calldata bytes`);
|
|
905
|
+
return argumentCodecs.length === 0 ? void 0 : args;
|
|
906
|
+
},
|
|
907
|
+
decodeReturn(data) {
|
|
908
|
+
return decodeComplete(returnCodec, data, `${name} contract return value`);
|
|
909
|
+
},
|
|
910
|
+
decodeException(data) {
|
|
911
|
+
return decodeException(data, exceptionBySelector);
|
|
912
|
+
},
|
|
913
|
+
decodeReceiptOutcome(receipt) {
|
|
914
|
+
const metadata = {
|
|
915
|
+
transactionHash: receipt.transactionHash,
|
|
916
|
+
blockHeight: receipt.blockHeight,
|
|
917
|
+
events: receipt.events
|
|
918
|
+
};
|
|
919
|
+
const execution = receipt.execution;
|
|
920
|
+
if (execution instanceof UnsupportedRpcValue) return {
|
|
921
|
+
kind: "unsupported",
|
|
922
|
+
raw: receipt,
|
|
923
|
+
...metadata
|
|
924
|
+
};
|
|
925
|
+
if (execution.result === "return") return {
|
|
926
|
+
kind: "return",
|
|
927
|
+
value: method.decodeReturn(execution.returnData),
|
|
928
|
+
...metadata
|
|
929
|
+
};
|
|
930
|
+
if (execution.result === "abort") return {
|
|
931
|
+
kind: "abort",
|
|
932
|
+
exception: method.decodeException(execution.abortData),
|
|
933
|
+
abortData: execution.abortData,
|
|
934
|
+
...metadata
|
|
935
|
+
};
|
|
936
|
+
return {
|
|
937
|
+
kind: "failure",
|
|
938
|
+
execution,
|
|
939
|
+
...metadata
|
|
940
|
+
};
|
|
941
|
+
},
|
|
942
|
+
decodeReceipt(receipt) {
|
|
943
|
+
const outcome = method.decodeReceiptOutcome(receipt);
|
|
944
|
+
if (outcome.kind === "return") return {
|
|
945
|
+
value: outcome.value,
|
|
946
|
+
transactionHash: outcome.transactionHash,
|
|
947
|
+
blockHeight: outcome.blockHeight,
|
|
948
|
+
events: outcome.events
|
|
949
|
+
};
|
|
950
|
+
throw new ContractReceiptError(outcome);
|
|
951
|
+
},
|
|
952
|
+
decodeView(response) {
|
|
953
|
+
const outcome = method.decodeViewOutcome(response);
|
|
954
|
+
if (outcome.kind !== "return") throw new ContractViewError(outcome);
|
|
955
|
+
return {
|
|
956
|
+
value: outcome.value,
|
|
957
|
+
stateBlockHeight: outcome.stateBlockHeight,
|
|
958
|
+
gasUsed: outcome.gasUsed,
|
|
959
|
+
events: outcome.events
|
|
960
|
+
};
|
|
961
|
+
},
|
|
962
|
+
async viewOutcome(options) {
|
|
963
|
+
options.signal?.throwIfAborted();
|
|
964
|
+
const minStateBlockHeight = options.minStateBlockHeight;
|
|
965
|
+
if (minStateBlockHeight !== void 0) serializeHeight(minStateBlockHeight);
|
|
966
|
+
const request = {
|
|
967
|
+
...options.assumedAuth === void 0 ? {} : { assumedAuth: toAssumedAuth(options.assumedAuth) },
|
|
968
|
+
address: options.address,
|
|
969
|
+
entrypoint: copyEntrypoint(entrypoint),
|
|
970
|
+
calldata: encode("args" in options ? options.args : void 0),
|
|
971
|
+
...options.caller ? { caller: options.caller } : {},
|
|
972
|
+
...minStateBlockHeight === void 0 ? {} : { minStateBlockHeight }
|
|
973
|
+
};
|
|
974
|
+
const response = await (options.signal ? options.client.simulateInvocation(request, { signal: options.signal }) : options.client.simulateInvocation(request));
|
|
975
|
+
assertViewFreshness(response.stateBlockHeight, minStateBlockHeight);
|
|
976
|
+
return method.decodeViewOutcome(response);
|
|
977
|
+
},
|
|
978
|
+
decodeViewOutcome(response) {
|
|
979
|
+
if (response.output instanceof UnsupportedRpcValue) return {
|
|
980
|
+
kind: "unsupported",
|
|
981
|
+
raw: response,
|
|
982
|
+
stateBlockHeight: response.stateBlockHeight,
|
|
983
|
+
gasUsed: response.gasUsed,
|
|
984
|
+
events: response.events
|
|
985
|
+
};
|
|
986
|
+
switch (response.output.result) {
|
|
987
|
+
case "return": return {
|
|
988
|
+
kind: "return",
|
|
989
|
+
value: method.decodeReturn(response.output.returnData),
|
|
990
|
+
stateBlockHeight: response.stateBlockHeight,
|
|
991
|
+
gasUsed: response.gasUsed,
|
|
992
|
+
events: response.events
|
|
993
|
+
};
|
|
994
|
+
case "abort": return {
|
|
995
|
+
kind: "abort",
|
|
996
|
+
exception: method.decodeException(response.output.abortData),
|
|
997
|
+
abortData: response.output.abortData,
|
|
998
|
+
stateBlockHeight: response.stateBlockHeight,
|
|
999
|
+
gasUsed: response.gasUsed,
|
|
1000
|
+
events: response.events
|
|
1001
|
+
};
|
|
1002
|
+
case "fault": return {
|
|
1003
|
+
kind: "fault",
|
|
1004
|
+
stateBlockHeight: response.stateBlockHeight,
|
|
1005
|
+
gasUsed: response.gasUsed,
|
|
1006
|
+
events: response.events
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
},
|
|
1010
|
+
async view(options) {
|
|
1011
|
+
const outcome = await method.viewOutcome(options);
|
|
1012
|
+
if (outcome.kind === "return") return {
|
|
1013
|
+
value: outcome.value,
|
|
1014
|
+
stateBlockHeight: outcome.stateBlockHeight,
|
|
1015
|
+
gasUsed: outcome.gasUsed,
|
|
1016
|
+
events: outcome.events
|
|
1017
|
+
};
|
|
1018
|
+
throw new ContractViewError(outcome);
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
return method;
|
|
1022
|
+
}
|
|
1023
|
+
function copyEntrypoint(value) {
|
|
1024
|
+
return contractEntrypointID(Uint8Array.from(value));
|
|
1025
|
+
}
|
|
1026
|
+
function encodeArguments(argumentsAndCodecs, args) {
|
|
1027
|
+
if (argumentsAndCodecs.length > 0 && (!args || typeof args !== "object")) throw new TypeError("Contract method arguments must be an object");
|
|
1028
|
+
const values = args;
|
|
1029
|
+
const writer = new CxsWriter$1();
|
|
1030
|
+
for (const argument of argumentsAndCodecs) {
|
|
1031
|
+
if (!values || !Object.hasOwn(values, argument.publicName)) throw new TypeError(`Contract method arguments require own property "${argument.publicName}"`);
|
|
1032
|
+
try {
|
|
1033
|
+
argument.codec.encode(writer, values[argument.publicName]);
|
|
1034
|
+
} catch (cause) {
|
|
1035
|
+
throw new TypeError(`argument "${argument.publicName}": ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
return calldata(writer.finish());
|
|
1039
|
+
}
|
|
1040
|
+
function decodeException(data, exceptionBySelector) {
|
|
1041
|
+
try {
|
|
1042
|
+
const reader = new CxsReader$1(data);
|
|
1043
|
+
const selector = reader.readUint64();
|
|
1044
|
+
const declared = exceptionBySelector.get(selector);
|
|
1045
|
+
if (!declared) return {
|
|
1046
|
+
kind: "unknown",
|
|
1047
|
+
selector,
|
|
1048
|
+
data
|
|
1049
|
+
};
|
|
1050
|
+
const value = declared.codec.decode(reader);
|
|
1051
|
+
const variant = declared.variant(value);
|
|
1052
|
+
if (reader.remaining !== 0) throw new Error(`${reader.remaining} trailing bytes`);
|
|
1053
|
+
return {
|
|
1054
|
+
kind: "declared",
|
|
1055
|
+
selector,
|
|
1056
|
+
name: declared.name,
|
|
1057
|
+
value,
|
|
1058
|
+
...variant === void 0 ? {} : { variant }
|
|
1059
|
+
};
|
|
1060
|
+
} catch (cause) {
|
|
1061
|
+
throw new ContractDecodeError(`Failed to decode contract exception${cause instanceof Error ? `: ${cause.message}` : ""}`, { cause });
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region src/contract/instance.ts
|
|
1066
|
+
/**
|
|
1067
|
+
* Bind a generated interface to an address and public client.
|
|
1068
|
+
* Views preserve heights and events and accept cancellation and minimum-height
|
|
1069
|
+
* constraints. A minimum height is a freshness floor; use readViews when several
|
|
1070
|
+
* results must come from the same snapshot.
|
|
1071
|
+
*
|
|
1072
|
+
* @example
|
|
1073
|
+
* ```ts
|
|
1074
|
+
* const token = getContract({ binding: ERC20.v0_1, address, client });
|
|
1075
|
+
* const balance = await token.getBalanceOf.view({ args: { addr: owner }, signal });
|
|
1076
|
+
* ```
|
|
1077
|
+
*/
|
|
1078
|
+
function getContract(options) {
|
|
1079
|
+
const address = options.address.slice();
|
|
1080
|
+
return Object.fromEntries(Object.entries(options.binding).map(([key, method]) => [key, {
|
|
1081
|
+
...method,
|
|
1082
|
+
viewRequest: (input) => method.viewRequest({
|
|
1083
|
+
...input,
|
|
1084
|
+
address
|
|
1085
|
+
}),
|
|
1086
|
+
view: (input) => method.view({
|
|
1087
|
+
...input,
|
|
1088
|
+
address,
|
|
1089
|
+
client: options.client
|
|
1090
|
+
}),
|
|
1091
|
+
viewOutcome: (input) => method.viewOutcome({
|
|
1092
|
+
...input,
|
|
1093
|
+
address,
|
|
1094
|
+
client: options.client
|
|
1095
|
+
}),
|
|
1096
|
+
call: (input) => method.call({
|
|
1097
|
+
...input,
|
|
1098
|
+
address
|
|
1099
|
+
})
|
|
1100
|
+
}]));
|
|
1101
|
+
}
|
|
1102
|
+
//#endregion
|
|
1103
|
+
export { ContractUnionCaseKind as C, ContractTypeKind$1 as S, specIdKey as T, isSignedToken as _, ContractViewError as a, ContractMethodEffect$1 as b, defineContractEvent as c, deriveContractAddress as d, hashContract as f, resolveCodec as g, encodeCalldata as h, ContractReceiptError as i, createContractCodec as l, decodeCalldata as m, fixedBytes$1 as n, defineContractMethod as o, buildNamedCodecs as p, getContract as r, bytesEqual as s, fixedArray$1 as t, createContractCodecContext as u, signedToken as v, formatSpecId as w, ContractTypeDefKind$1 as x, ContractDecodeError as y };
|
|
1104
|
+
|
|
1105
|
+
//# sourceMappingURL=contract-BpxjDMHw.js.map
|