anchor-sdk 0.1.43-beta.1 → 0.1.43-beta.4

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.
Files changed (38) hide show
  1. package/dist/constants.d.ts +1 -1
  2. package/dist/generated/Api.d.ts +199 -197
  3. package/dist/index.js +3 -1379
  4. package/dist/index.js.map +12 -11
  5. package/dist/react/index.js +3 -8
  6. package/dist/react/index.js.map +19 -0
  7. package/package.json +15 -15
  8. package/dist/AnchorApiClientV2.d.ts.map +0 -1
  9. package/dist/AnchorApiClientV2.js +0 -358
  10. package/dist/AnchorERC1155Client.d.ts.map +0 -1
  11. package/dist/AnchorERC1155Client.js +0 -301
  12. package/dist/AnchorPayClient.d.ts.map +0 -1
  13. package/dist/AnchorPayClient.js +0 -908
  14. package/dist/abi/AnchorERC1155.d.ts.map +0 -1
  15. package/dist/abi/AnchorERC1155.js +0 -1122
  16. package/dist/abi/AnchorPay.json +0 -578
  17. package/dist/constants.d.ts.map +0 -1
  18. package/dist/constants.js +0 -367
  19. package/dist/generated/Api.d.ts.map +0 -1
  20. package/dist/generated/Api.js +0 -868
  21. package/dist/index.d.ts.map +0 -1
  22. package/dist/react/AnchorReactSDK.js +0 -192
  23. package/dist/typechain/AnchorERC1155.d.ts.map +0 -1
  24. package/dist/typechain/AnchorERC1155.js +0 -2
  25. package/dist/typechain/AnchorPay.d.ts.map +0 -1
  26. package/dist/typechain/AnchorPay.js +0 -2
  27. package/dist/typechain/common.d.ts.map +0 -1
  28. package/dist/typechain/common.js +0 -2
  29. package/dist/typechain/factories/AnchorERC1155__factory.d.ts.map +0 -1
  30. package/dist/typechain/factories/AnchorERC1155__factory.js +0 -1766
  31. package/dist/typechain/factories/AnchorPay__factory.d.ts.map +0 -1
  32. package/dist/typechain/factories/AnchorPay__factory.js +0 -469
  33. package/dist/typechain/factories/index.d.ts.map +0 -1
  34. package/dist/typechain/factories/index.js +0 -10
  35. package/dist/typechain/index.d.ts.map +0 -1
  36. package/dist/typechain/index.js +0 -41
  37. package/dist/types.d.ts.map +0 -1
  38. package/dist/types.js +0 -2
@@ -1,301 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AnchorERC1155Client = void 0;
4
- const viem_1 = require("viem");
5
- const AnchorERC1155_1 = require("./abi/AnchorERC1155");
6
- const typechain_1 = require("./typechain");
7
- /**
8
- * AnchorERC1155 客户端
9
- * 用于与 AnchorERC1155 合约交互
10
- */
11
- class AnchorERC1155Client {
12
- /**
13
- * 创建 AnchorERC1155 客户端
14
- * @param publicClient 公共客户端(只读操作)
15
- * @param network 网络配置
16
- * @param walletClient 钱包客户端(写入操作)
17
- * @param account 账户(可选,如果不提供则使用只读模式)
18
- */
19
- constructor(publicClient, network, walletClient, account, contracts) {
20
- this.publicClient = publicClient;
21
- this.network = network;
22
- this.walletClient = walletClient;
23
- this.account = account;
24
- this.contracts = contracts || {};
25
- }
26
- /**
27
- * 获取代币 URI
28
- * @param tokenId 代币 ID
29
- * @returns 代币 URI
30
- */
31
- async getTokenURI(tokenId) {
32
- if (!this.contracts?.anchorERC1155) {
33
- throw new Error("AnchorERC1155 contract address not set, please check network configuration");
34
- }
35
- return (await this.publicClient.readContract({
36
- address: this.contracts.anchorERC1155,
37
- abi: AnchorERC1155_1.AnchorERC1155ABI,
38
- functionName: "uri",
39
- args: [tokenId],
40
- }));
41
- }
42
- /**
43
- * 获取代币名称
44
- * @returns 代币名称
45
- */
46
- async getName() {
47
- if (!this.contracts?.anchorERC1155) {
48
- throw new Error("AnchorERC1155 contract address not set, please check network configuration");
49
- }
50
- return (await this.publicClient.readContract({
51
- address: this.contracts.anchorERC1155,
52
- abi: AnchorERC1155_1.AnchorERC1155ABI,
53
- functionName: "name",
54
- }));
55
- }
56
- /**
57
- * 获取代币符号
58
- * @returns 代币符号
59
- */
60
- async getSymbol() {
61
- if (!this.contracts?.anchorERC1155) {
62
- throw new Error("AnchorERC1155 contract address not set, please check network configuration");
63
- }
64
- return (await this.publicClient.readContract({
65
- address: this.contracts.anchorERC1155,
66
- abi: AnchorERC1155_1.AnchorERC1155ABI,
67
- functionName: "symbol",
68
- }));
69
- }
70
- /**
71
- * 使用签名铸造代币(免费代币)
72
- * @param signedRequest 签名铸造请求
73
- * @param options 选项,可以控制是否直接发送交易
74
- * @returns 如果直接发送交易,返回交易收据;否则返回交易数据
75
- */
76
- async mintWithSignature(signedRequest, anchorERC1155Address, options) {
77
- if (BigInt(signedRequest.request.price) !== 0n) {
78
- throw new Error("mintWithSignature method can only be used to mint free tokens");
79
- }
80
- const contractAddress = anchorERC1155Address || this.contracts?.anchorERC1155;
81
- // 用 typechain encodeFunctionData,参数断言为 any 以兼容类型
82
- const data = typechain_1.AnchorERC1155__factory.createInterface().encodeFunctionData("mintWithSignature", [
83
- this.formatMintRequest(signedRequest.request),
84
- signedRequest.signature,
85
- ]);
86
- // const data = encodeFunctionData({
87
- // abi: AnchorERC1155ABI,
88
- // functionName: "mintWithSignature",
89
- // args: [
90
- // this.formatMintRequest(signedRequest.request),
91
- // signedRequest.signature as `0x${string}`,
92
- // ],
93
- // });
94
- console.log(data, "data");
95
- if (options?.sendTransaction === false) {
96
- return {
97
- to: contractAddress,
98
- data,
99
- value: 0n,
100
- };
101
- }
102
- if (!this.walletClient || !this.account) {
103
- throw new Error("Wallet client and account are required to send transactions");
104
- }
105
- // 估算 gas 和费用
106
- const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
107
- const gas = await this.publicClient.estimateGas({
108
- to: contractAddress,
109
- data: data,
110
- value: 0n,
111
- account: this.account,
112
- });
113
- const txHash = await this.walletClient.writeContract({
114
- account: this.account,
115
- address: contractAddress,
116
- abi: typechain_1.AnchorERC1155__factory.abi,
117
- functionName: "mintWithSignature",
118
- args: [
119
- this.formatMintRequest(signedRequest.request),
120
- signedRequest.signature,
121
- ],
122
- chain: this.publicClient.chain,
123
- maxFeePerGas,
124
- maxPriorityFeePerGas,
125
- gas,
126
- });
127
- const receipt = await this.publicClient.waitForTransactionReceipt({
128
- hash: txHash,
129
- });
130
- return receipt;
131
- }
132
- /**
133
- * 编码签名铸造请求(用于通过 AnchorPay 支付)
134
- * @param signedRequest 签名铸造请求
135
- * @returns 编码后的数据
136
- */
137
- encodeTokenReceivedData(signedRequest) {
138
- // 计算接口哈希 - AnchorERC1155 接口标识
139
- const interfaceHash = (0, viem_1.keccak256)((0, viem_1.stringToHex)("AnchorERC1155"));
140
- // 准备签名数据
141
- const signature = signedRequest.signature.startsWith("0x")
142
- ? signedRequest.signature
143
- : `0x${signedRequest.signature}`;
144
- // 格式化请求数据
145
- const formattedRequest = this.formatMintRequest(signedRequest.request);
146
- console.log("Formatted request data:", JSON.stringify(formattedRequest, (_, v) => typeof v === "bigint" ? v.toString() : v));
147
- console.log("Signature:", signature);
148
- // 编码tokenReceived函数调用
149
- // tokenReceived(address token, address from, address to, uint256 amount, bytes calldata userData)
150
- // 其中userData是通过abi.encode(MintRequest, signature)编码的
151
- // 1. 首先编码userData - 这是合约中通过abi.decode解码的部分
152
- const userData = (0, viem_1.encodeAbiParameters)([
153
- {
154
- components: [
155
- { name: "to", type: "address" },
156
- { name: "tokenId", type: "uint256" },
157
- { name: "quantity", type: "uint256" },
158
- { name: "price", type: "uint256" },
159
- { name: "currency", type: "address" },
160
- { name: "validityStartTimestamp", type: "uint128" },
161
- { name: "validityEndTimestamp", type: "uint128" },
162
- { name: "uid", type: "bytes32" },
163
- { name: "chainId", type: "uint256" },
164
- { name: "projectHandle", type: "bytes32" },
165
- ],
166
- name: "req",
167
- type: "tuple",
168
- },
169
- { name: "signature", type: "bytes" },
170
- ], [formattedRequest, signature]);
171
- console.log("Encoded userData:", userData);
172
- // 返回完整的编码数据
173
- return (0, viem_1.concat)([interfaceHash, userData]);
174
- }
175
- /**
176
- * 编码签名铸造请求(用于通过 AnchorPay 支付),使用空接口字符串
177
- * @param signedRequest 签名铸造请求
178
- * @returns 编码后的数据
179
- */
180
- encodeTokenReceivedDataWithEmptyInterface(signedRequest) {
181
- // 计算接口哈希 - 使用空字符串
182
- const interfaceHash = (0, viem_1.keccak256)((0, viem_1.stringToHex)(""));
183
- // 准备签名数据
184
- const signature = signedRequest.signature.startsWith("0x")
185
- ? signedRequest.signature
186
- : `0x${signedRequest.signature}`;
187
- // 格式化请求数据
188
- const formattedRequest = this.formatMintRequest(signedRequest.request);
189
- console.log("Formatted request data (empty interface):", JSON.stringify(formattedRequest, (_, v) => typeof v === "bigint" ? v.toString() : v));
190
- console.log("Signature:", signature);
191
- // 编码userData - 与标准方法相同,只是接口哈希不同
192
- const userData = (0, viem_1.encodeAbiParameters)([
193
- {
194
- components: [
195
- { name: "to", type: "address" },
196
- { name: "tokenId", type: "uint256" },
197
- { name: "quantity", type: "uint256" },
198
- { name: "price", type: "uint256" },
199
- { name: "currency", type: "address" },
200
- { name: "validityStartTimestamp", type: "uint128" },
201
- { name: "validityEndTimestamp", type: "uint128" },
202
- { name: "uid", type: "bytes32" },
203
- { name: "chainId", type: "uint256" },
204
- { name: "projectHandle", type: "bytes32" },
205
- ],
206
- name: "req",
207
- type: "tuple",
208
- },
209
- { name: "signature", type: "bytes" },
210
- ], [formattedRequest, signature]);
211
- console.log("Encoded userData (empty interface):", userData);
212
- // 返回完整的编码数据
213
- return (0, viem_1.concat)([interfaceHash, userData]);
214
- }
215
- /**
216
- * multicall 方法,用于批量执行交易
217
- * @param data 编码后的交易数据数组
218
- * @param options 选项,可以控制是否直接发送交易
219
- * @returns 如果直接发送交易,返回交易收据;否则返回交易数据
220
- */
221
- async multicall(data, options) {
222
- if (!this.contracts?.anchorERC1155) {
223
- throw new Error("AnchorERC1155 contract address not set, please check network configuration");
224
- }
225
- const contractAddress = this.contracts.anchorERC1155;
226
- const calldata = (0, viem_1.encodeFunctionData)({
227
- abi: AnchorERC1155_1.AnchorERC1155ABI,
228
- functionName: "multicall",
229
- args: [data],
230
- });
231
- if (options?.sendTransaction === false) {
232
- return {
233
- to: contractAddress,
234
- data: calldata,
235
- value: 0n,
236
- };
237
- }
238
- if (!this.walletClient || !this.account) {
239
- throw new Error("Wallet client and account are required to send transactions");
240
- }
241
- const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
242
- const gas = await this.publicClient.estimateGas({
243
- to: contractAddress,
244
- data: calldata,
245
- value: 0n,
246
- account: this.account,
247
- });
248
- const txHash = await this.walletClient.writeContract({
249
- account: this.account,
250
- address: contractAddress,
251
- abi: AnchorERC1155_1.AnchorERC1155ABI,
252
- functionName: "multicall",
253
- args: [data],
254
- chain: this.publicClient.chain,
255
- maxFeePerGas,
256
- maxPriorityFeePerGas,
257
- gas,
258
- });
259
- return await this.publicClient.waitForTransactionReceipt({ hash: txHash });
260
- }
261
- /**
262
- * 格式化铸造请求,将 bigint 转换为字符串
263
- * @param request 铸造请求
264
- * @returns 格式化后的铸造请求
265
- */
266
- formatMintRequest(request) {
267
- let uid;
268
- if (typeof request.uid === "string") {
269
- if (request.uid.startsWith("0x")) {
270
- uid = request.uid;
271
- }
272
- else {
273
- uid = (0, viem_1.keccak256)((0, viem_1.stringToHex)(request.uid));
274
- }
275
- }
276
- else {
277
- uid = request.uid;
278
- }
279
- // 计算当前时间戳(秒)
280
- const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
281
- return {
282
- to: request.to,
283
- tokenId: BigInt(request.tokenId),
284
- quantity: BigInt(request.quantity),
285
- price: BigInt(request.price),
286
- currency: request.currency,
287
- validityStartTimestamp: request.validityStartTimestamp
288
- ? BigInt(request.validityStartTimestamp)
289
- : currentTimestamp,
290
- validityEndTimestamp: request.validityEndTimestamp
291
- ? BigInt(request.validityEndTimestamp)
292
- : request.validUntil
293
- ? BigInt(request.validUntil)
294
- : currentTimestamp + 3600n, // 默认1小时后过期
295
- uid: uid,
296
- chainId: BigInt(request.chainId || this.network.id),
297
- projectHandle: request.projectHandle,
298
- };
299
- }
300
- }
301
- exports.AnchorERC1155Client = AnchorERC1155Client;
@@ -1 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