anchor-sdk 0.1.42-beta.1 → 0.1.42-beta.3
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/dist/AnchorApiClientV2.d.ts +9 -4
- package/dist/AnchorApiClientV2.js +316 -0
- package/dist/AnchorERC1155Client.d.ts +0 -1
- package/dist/AnchorERC1155Client.js +301 -0
- package/dist/AnchorPayClient.d.ts +0 -1
- package/dist/AnchorPayClient.js +908 -0
- package/dist/abi/AnchorERC1155.d.ts +0 -1
- package/dist/abi/AnchorERC1155.js +1122 -0
- package/dist/abi/AnchorPay.json +578 -0
- package/dist/constants.d.ts +1 -2
- package/dist/constants.js +367 -0
- package/dist/generated/Api.d.ts +692 -606
- package/dist/generated/Api.js +794 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +1379 -3
- package/dist/react/AnchorReactSDK.d.ts +59 -0
- package/dist/react/AnchorReactSDK.js +192 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +8 -0
- package/dist/typechain/AnchorERC1155.d.ts +2 -3
- package/dist/typechain/AnchorERC1155.js +2 -0
- package/dist/typechain/AnchorPay.d.ts +2 -3
- package/dist/typechain/AnchorPay.js +2 -0
- package/dist/typechain/common.d.ts +1 -2
- package/dist/typechain/common.js +2 -0
- package/dist/typechain/factories/AnchorERC1155__factory.d.ts +0 -1
- package/dist/typechain/factories/AnchorERC1155__factory.js +1766 -0
- package/dist/typechain/factories/AnchorPay__factory.d.ts +0 -1
- package/dist/typechain/factories/AnchorPay__factory.js +469 -0
- package/dist/typechain/factories/index.d.ts +0 -1
- package/dist/typechain/factories/index.js +10 -0
- package/dist/typechain/index.d.ts +0 -1
- package/dist/typechain/index.js +41 -0
- package/dist/types.d.ts +0 -1
- package/dist/types.js +2 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { BadgeClaimSignatureRequest, BadgeSingleCheckRequest, BatchMintRequestVO, MintResponse, NFTMintRequestVO, UserOpHashProcessRequest, WebResultBadgeAssetsResponse, WebResultBadgeCheckResponse, WebResultBadgeClaimSignatureResponse, WebResultBadgeClaimableListResponse, WebResultBadgeDetailResponse, WebResultBadgeSeriesDetailResponse, WebResultBadgeSeriesResponse, WebResultBadgeVerifiersResponse, WebResultTransactionHashProcess, WebResultUserOpHashProcess } from "./generated/Api";
|
|
2
|
+
import { Chain } from "viem";
|
|
3
3
|
/**
|
|
4
4
|
* Anchor API V2 客户端配置
|
|
5
5
|
*/
|
|
@@ -95,6 +95,7 @@ export declare class AnchorApiClientV2 {
|
|
|
95
95
|
*/
|
|
96
96
|
getUserClaimableBadges(params?: {
|
|
97
97
|
series?: string;
|
|
98
|
+
tokenIds?: string[];
|
|
98
99
|
limit?: number;
|
|
99
100
|
nextToken?: string;
|
|
100
101
|
}): Promise<WebResultBadgeClaimableListResponse>;
|
|
@@ -128,6 +129,7 @@ export declare class AnchorApiClientV2 {
|
|
|
128
129
|
getBadgeSeriesDetail(seriesId: string, params?: {
|
|
129
130
|
status?: string;
|
|
130
131
|
businessType?: string;
|
|
132
|
+
tokenIds?: string[];
|
|
131
133
|
}): Promise<WebResultBadgeSeriesDetailResponse>;
|
|
132
134
|
/**
|
|
133
135
|
* 获取徽章详情
|
|
@@ -150,7 +152,11 @@ export declare class AnchorApiClientV2 {
|
|
|
150
152
|
* @param data 徽章检查请求
|
|
151
153
|
* @returns 徽章检查响应
|
|
152
154
|
*/
|
|
153
|
-
checkUserClaimableBadges(data:
|
|
155
|
+
checkUserClaimableBadges(data: {
|
|
156
|
+
walletType?: "ALL" | "EVM" | "SOLANA";
|
|
157
|
+
groups?: string[];
|
|
158
|
+
[key: string]: any;
|
|
159
|
+
}): Promise<WebResultBadgeCheckResponse>;
|
|
154
160
|
/**
|
|
155
161
|
* 获取徽章领取签名
|
|
156
162
|
* @route POST /v2/badges/signatures
|
|
@@ -205,4 +211,3 @@ export declare class AnchorApiClientV2 {
|
|
|
205
211
|
*/
|
|
206
212
|
userOpHashProcess(data: UserOpHashProcessRequest): Promise<WebResultUserOpHashProcess>;
|
|
207
213
|
}
|
|
208
|
-
//# sourceMappingURL=AnchorApiClientV2.d.ts.map
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnchorApiClientV2 = void 0;
|
|
4
|
+
const Api_1 = require("./generated/Api");
|
|
5
|
+
/**
|
|
6
|
+
* Anchor API V2 客户端
|
|
7
|
+
* 基于生成的 API 类型和端点,提供完整的 Anchor API 功能
|
|
8
|
+
*
|
|
9
|
+
* 这个客户端使用生成的 API 类型和端点,同时提供
|
|
10
|
+
* 自动 header 注入和配置管理功能。
|
|
11
|
+
*/
|
|
12
|
+
class AnchorApiClientV2 {
|
|
13
|
+
/**
|
|
14
|
+
* 创建 Anchor API V2 客户端
|
|
15
|
+
* @param config 客户端配置
|
|
16
|
+
*/
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.network = config.network;
|
|
19
|
+
this.authToken = config.authToken;
|
|
20
|
+
this.projectId = config.projectId;
|
|
21
|
+
this.chainId = config.network.id.toString();
|
|
22
|
+
this.onTokenExpired = config.onTokenExpired;
|
|
23
|
+
// 初始化生成的 API
|
|
24
|
+
this.generatedApi = new Api_1.Api({
|
|
25
|
+
baseUrl: config.apiBaseUrl.endsWith("/")
|
|
26
|
+
? config.apiBaseUrl.slice(0, -1)
|
|
27
|
+
: config.apiBaseUrl,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 设置认证令牌
|
|
32
|
+
* @param token 认证令牌
|
|
33
|
+
*/
|
|
34
|
+
setAuthToken(token) {
|
|
35
|
+
this.authToken = token;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 设置 Token 过期回调
|
|
39
|
+
* @param callback Token 过期回调函数
|
|
40
|
+
*/
|
|
41
|
+
setTokenExpiredCallback(callback) {
|
|
42
|
+
this.onTokenExpired = callback;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 设置项目 ID
|
|
46
|
+
* @param projectId 项目 ID
|
|
47
|
+
*/
|
|
48
|
+
setProjectId(projectId) {
|
|
49
|
+
this.projectId = projectId;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 设置链 ID
|
|
53
|
+
* @param chainId 链 ID
|
|
54
|
+
*/
|
|
55
|
+
setChainId(chainId) {
|
|
56
|
+
this.chainId = chainId;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 创建请求头
|
|
60
|
+
* @returns 请求头对象
|
|
61
|
+
*/
|
|
62
|
+
createHeaders() {
|
|
63
|
+
const headers = {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
};
|
|
66
|
+
// 添加标准化的请求头
|
|
67
|
+
if (this.authToken) {
|
|
68
|
+
headers["Authorization"] = `Bearer ${this.authToken}`;
|
|
69
|
+
headers["jwt_token"] = `${this.authToken}`;
|
|
70
|
+
}
|
|
71
|
+
if (this.projectId) {
|
|
72
|
+
headers["X-Project-Id"] = this.projectId;
|
|
73
|
+
headers["saas_id"] = this.projectId;
|
|
74
|
+
}
|
|
75
|
+
if (this.chainId) {
|
|
76
|
+
headers["X-Chain-Id"] = this.chainId;
|
|
77
|
+
headers["chain_id"] = this.chainId;
|
|
78
|
+
}
|
|
79
|
+
return headers;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 获取请求参数(包含 headers)
|
|
83
|
+
* @returns 请求参数对象
|
|
84
|
+
*/
|
|
85
|
+
getRequestParams() {
|
|
86
|
+
return {
|
|
87
|
+
headers: this.createHeaders(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 处理 API 调用的错误和 token 过期
|
|
92
|
+
* @param apiCall API 调用函数
|
|
93
|
+
* @returns API 调用结果
|
|
94
|
+
*/
|
|
95
|
+
async handleApiCall(apiCall) {
|
|
96
|
+
try {
|
|
97
|
+
const result = await apiCall();
|
|
98
|
+
// 检查响应体中的 token 过期信息
|
|
99
|
+
if (this.isTokenExpiredResponse(result)) {
|
|
100
|
+
console.log("Token expired: JWT token is invalid or expired");
|
|
101
|
+
const error = new Error("Token expired: JWT token is invalid or expired");
|
|
102
|
+
if (this.onTokenExpired) {
|
|
103
|
+
this.onTokenExpired(error);
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
// 检查 HTTP 401 状态码的 token 过期
|
|
111
|
+
if (error?.status === 401) {
|
|
112
|
+
if (this.onTokenExpired) {
|
|
113
|
+
this.onTokenExpired(error);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 检查响应是否为 token 过期错误
|
|
121
|
+
* @param response API 响应
|
|
122
|
+
* @returns 是否为 token 过期错误
|
|
123
|
+
*/
|
|
124
|
+
isTokenExpiredResponse(response) {
|
|
125
|
+
// 检查响应体中的 token 过期标识
|
|
126
|
+
// 优先检查:success 为 false 且 code 为 9101(标准 token 过期错误码)
|
|
127
|
+
if (response?.success === false && response?.code === "9101") {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
// 检查 msgKey 是否为特定的 JWT 错误
|
|
131
|
+
if (response?.success === false &&
|
|
132
|
+
response?.msgKey === "params.jwt.check.invalid") {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
// ==================== V2 API Methods ====================
|
|
138
|
+
/**
|
|
139
|
+
* 获取徽章验证器
|
|
140
|
+
* @route GET /v2/badges/verifiers
|
|
141
|
+
* @param platform 平台标识符
|
|
142
|
+
* @param type 条件类型
|
|
143
|
+
* @returns 徽章验证器响应
|
|
144
|
+
*/
|
|
145
|
+
async getBadgeVerifiers(platform, type) {
|
|
146
|
+
return this.handleApiCall(async () => {
|
|
147
|
+
const query = { platform, type };
|
|
148
|
+
const response = await this.generatedApi.v2.getBadgeVerifiers(query, this.getRequestParams());
|
|
149
|
+
return response.data;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 获取用户可领取徽章
|
|
154
|
+
* @route GET /v2/badges/claimable
|
|
155
|
+
* @param params 查询参数
|
|
156
|
+
* @param params.series 徽章系列过滤器
|
|
157
|
+
* @param params.limit 每页项目数
|
|
158
|
+
* @param params.nextToken 分页令牌
|
|
159
|
+
* @returns 徽章可领取列表响应
|
|
160
|
+
*/
|
|
161
|
+
async getUserClaimableBadges(params) {
|
|
162
|
+
return this.handleApiCall(async () => {
|
|
163
|
+
const response = await this.generatedApi.v2.getUserClaimableBadges(params, this.getRequestParams());
|
|
164
|
+
return response.data;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 获取用户徽章资产
|
|
169
|
+
* @route GET /v2/badges/assets
|
|
170
|
+
* @param params 查询参数
|
|
171
|
+
* @param params.includeClaimable 徽章状态是否包含可领取
|
|
172
|
+
* @param params.strategy 返回策略 BADGE_ALL/BADGE_MAX/BADGE_REWARD
|
|
173
|
+
* @param params.series 徽章系列过滤器
|
|
174
|
+
* @param params.limit 每页项目数
|
|
175
|
+
* @param params.nextToken 分页令牌
|
|
176
|
+
* @returns 徽章资产响应
|
|
177
|
+
*/
|
|
178
|
+
async getUserBadgeAssets(params) {
|
|
179
|
+
return this.handleApiCall(async () => {
|
|
180
|
+
const response = await this.generatedApi.v2.getUserBadgeAssets(params, this.getRequestParams());
|
|
181
|
+
return response.data;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 获取徽章系列详情
|
|
186
|
+
* @route GET /v2/badges/series/{seriesId}
|
|
187
|
+
* @param seriesId 系列标识符
|
|
188
|
+
* @param params 查询参数
|
|
189
|
+
* @param params.status 状态过滤器
|
|
190
|
+
* @param params.businessType 业务类型过滤器
|
|
191
|
+
* @returns 徽章系列详情响应
|
|
192
|
+
*/
|
|
193
|
+
async getBadgeSeriesDetail(seriesId, params) {
|
|
194
|
+
return this.handleApiCall(async () => {
|
|
195
|
+
const response = await this.generatedApi.v2.getBadgeSeriesDetail(seriesId, params, this.getRequestParams());
|
|
196
|
+
return response.data;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 获取徽章详情
|
|
201
|
+
* @route GET /v2/badges/{badgeId}
|
|
202
|
+
* @param badgeId 徽章标识符
|
|
203
|
+
* @returns 徽章详情响应
|
|
204
|
+
*/
|
|
205
|
+
async getBadgeDetail(badgeId) {
|
|
206
|
+
return this.handleApiCall(async () => {
|
|
207
|
+
const response = await this.generatedApi.v2.getBadgeDetail(badgeId, this.getRequestParams());
|
|
208
|
+
return response.data;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* 检查单个徽章可领取状态
|
|
213
|
+
* @route POST /v2/badges/{badgeId}
|
|
214
|
+
* @param badgeId 徽章标识符
|
|
215
|
+
* @param data 徽章检查请求数据
|
|
216
|
+
* @returns 徽章可领取响应
|
|
217
|
+
*/
|
|
218
|
+
async checkSingleBadgeClaimable(badgeId, data) {
|
|
219
|
+
return this.handleApiCall(async () => {
|
|
220
|
+
const response = await this.generatedApi.v2.checkSingleBadgeClaimable(badgeId, data, this.getRequestParams());
|
|
221
|
+
return response.data;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 检查用户可领取徽章
|
|
226
|
+
* @route POST /v2/badges
|
|
227
|
+
* @param data 徽章检查请求
|
|
228
|
+
* @returns 徽章检查响应
|
|
229
|
+
*/
|
|
230
|
+
async checkUserClaimableBadges(data) {
|
|
231
|
+
return this.handleApiCall(async () => {
|
|
232
|
+
const response = await this.generatedApi.v2.checkUserClaimableBadges(data, this.getRequestParams());
|
|
233
|
+
return response.data;
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* 获取徽章领取签名
|
|
238
|
+
* @route POST /v2/badges/signatures
|
|
239
|
+
* @param data 徽章领取签名请求
|
|
240
|
+
* @returns 徽章领取签名响应
|
|
241
|
+
*/
|
|
242
|
+
async getBadgeClaimSignatures(data) {
|
|
243
|
+
return this.handleApiCall(async () => {
|
|
244
|
+
const response = await this.generatedApi.v2.getBadgeClaimSignatures(data, this.getRequestParams());
|
|
245
|
+
return response.data;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* 获取徽章系列列表
|
|
250
|
+
* @route GET /v2/badges/series
|
|
251
|
+
* @param params 查询参数
|
|
252
|
+
* @param params.includeBadges 是否包含徽章
|
|
253
|
+
* @param params.status 状态过滤器
|
|
254
|
+
* @param params.businessType 业务类型过滤器
|
|
255
|
+
* @param params.limit 每页数量
|
|
256
|
+
* @returns 徽章系列响应
|
|
257
|
+
*/
|
|
258
|
+
async getBadgeSeries(params) {
|
|
259
|
+
return this.handleApiCall(async () => {
|
|
260
|
+
const response = await this.generatedApi.v2.getBadgeSeries(params, this.getRequestParams());
|
|
261
|
+
return response.data;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Anchor 铸造
|
|
266
|
+
* @route POST /v2/mint/{type}
|
|
267
|
+
* @param type 铸造类型
|
|
268
|
+
* @param data 铸造请求数据
|
|
269
|
+
* @returns 铸造响应
|
|
270
|
+
*/
|
|
271
|
+
async mint(type, data) {
|
|
272
|
+
return this.handleApiCall(async () => {
|
|
273
|
+
const response = await this.generatedApi.v2.mint(type, data, this.getRequestParams());
|
|
274
|
+
return response.data;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Anchor 批量铸造
|
|
279
|
+
* @route POST /v2/batch/mint/{type}
|
|
280
|
+
* @param type 铸造类型
|
|
281
|
+
* @param data 批量铸造请求数据
|
|
282
|
+
* @returns 批量铸造响应
|
|
283
|
+
*/
|
|
284
|
+
async batchMint(type, data) {
|
|
285
|
+
return this.handleApiCall(async () => {
|
|
286
|
+
const response = await this.generatedApi.v2.batchMint(type, data, this.getRequestParams());
|
|
287
|
+
return response.data;
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
// ==================== Transaction Methods ====================
|
|
291
|
+
/**
|
|
292
|
+
* 处理交易哈希(加速交易处理)
|
|
293
|
+
* @route POST /v1/transaction/process/{txHash}
|
|
294
|
+
* @param txHash 交易哈希
|
|
295
|
+
* @returns 交易哈希处理响应
|
|
296
|
+
*/
|
|
297
|
+
async transactionHashProcess(txHash) {
|
|
298
|
+
return this.handleApiCall(async () => {
|
|
299
|
+
const response = await this.generatedApi.v1.transactionHashProcess(txHash, this.getRequestParams());
|
|
300
|
+
return response.data;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* 处理用户操作哈希
|
|
305
|
+
* @route POST /v1/transaction/process/userOpHash
|
|
306
|
+
* @param data 用户操作哈希处理请求
|
|
307
|
+
* @returns 用户操作哈希处理响应
|
|
308
|
+
*/
|
|
309
|
+
async userOpHashProcess(data) {
|
|
310
|
+
return this.handleApiCall(async () => {
|
|
311
|
+
const response = await this.generatedApi.v1.userOpHashProcess(data, this.getRequestParams());
|
|
312
|
+
return response.data;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
exports.AnchorApiClientV2 = AnchorApiClientV2;
|
|
@@ -0,0 +1,301 @@
|
|
|
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;
|