anchor-sdk 0.1.41-internal.4 → 0.1.41-internal.5
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/AnchorPayClient.d.ts +40 -0
- package/dist/AnchorPayClient.js +219 -0
- package/dist/abi/AnchorPay.json +126 -0
- package/package.json +1 -1
|
@@ -76,4 +76,44 @@ export declare class AnchorPayClient {
|
|
|
76
76
|
data: string;
|
|
77
77
|
value: bigint;
|
|
78
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* 查询账户在 AnchorPay 合约中的代币余额
|
|
81
|
+
* @param account 账户地址
|
|
82
|
+
* @param token 代币地址(使用 NATIVE_TOKEN_ADDRESS 表示原生代币)
|
|
83
|
+
* @returns 余额
|
|
84
|
+
*/
|
|
85
|
+
balanceOf(account: Address, token: Address): Promise<bigint>;
|
|
86
|
+
/**
|
|
87
|
+
* 为指定地址存入 ETH 到 AnchorPay 合约
|
|
88
|
+
* @param recipient 接收者地址
|
|
89
|
+
* @param amount 金额(单位:wei)
|
|
90
|
+
* @param options 交易选项
|
|
91
|
+
* @returns 交易收据或交易数据
|
|
92
|
+
*/
|
|
93
|
+
depositETH(recipient: Address, amount: bigint, options?: PaymentOptions): Promise<TransactionReceipt | {
|
|
94
|
+
to: string;
|
|
95
|
+
data: string;
|
|
96
|
+
value: bigint;
|
|
97
|
+
}>;
|
|
98
|
+
/**
|
|
99
|
+
* 为指定地址存入 ERC20 代币到 AnchorPay 合约
|
|
100
|
+
* @param recipient 接收者地址
|
|
101
|
+
* @param tokenAddress ERC20 代币地址
|
|
102
|
+
* @param amount 金额
|
|
103
|
+
* @param options 交易选项
|
|
104
|
+
* @returns 交易收据或交易数据
|
|
105
|
+
*/
|
|
106
|
+
depositERC20(recipient: Address, tokenAddress: Address, amount: bigint, options?: PaymentOptions): Promise<TransactionReceipt | {
|
|
107
|
+
to: string;
|
|
108
|
+
data: string;
|
|
109
|
+
value: bigint;
|
|
110
|
+
}>;
|
|
111
|
+
/**
|
|
112
|
+
* 从 AnchorPay 合约提取代币
|
|
113
|
+
* @param tokenAddress 代币地址(使用 NATIVE_TOKEN_ADDRESS 表示原生代币)
|
|
114
|
+
* @param amount 金额
|
|
115
|
+
* @param options 交易选项
|
|
116
|
+
* @returns 交易收据
|
|
117
|
+
*/
|
|
118
|
+
withdraw(tokenAddress: Address, amount: bigint, options?: PaymentOptions): Promise<TransactionReceipt>;
|
|
79
119
|
}
|
package/dist/AnchorPayClient.js
CHANGED
|
@@ -271,5 +271,224 @@ class AnchorPayClient {
|
|
|
271
271
|
value: 0n, // ERC20 批准不需要发送 ETH
|
|
272
272
|
};
|
|
273
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* 查询账户在 AnchorPay 合约中的代币余额
|
|
276
|
+
* @param account 账户地址
|
|
277
|
+
* @param token 代币地址(使用 NATIVE_TOKEN_ADDRESS 表示原生代币)
|
|
278
|
+
* @returns 余额
|
|
279
|
+
*/
|
|
280
|
+
async balanceOf(account, token) {
|
|
281
|
+
return (await this.publicClient.readContract({
|
|
282
|
+
address: this.contracts?.anchorPay || "",
|
|
283
|
+
abi: AnchorPay_json_1.default,
|
|
284
|
+
functionName: "balanceOf",
|
|
285
|
+
args: [account, token],
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* 为指定地址存入 ETH 到 AnchorPay 合约
|
|
290
|
+
* @param recipient 接收者地址
|
|
291
|
+
* @param amount 金额(单位:wei)
|
|
292
|
+
* @param options 交易选项
|
|
293
|
+
* @returns 交易收据或交易数据
|
|
294
|
+
*/
|
|
295
|
+
async depositETH(recipient, amount, options) {
|
|
296
|
+
// 如果需要发送交易,但没有钱包客户端或账户
|
|
297
|
+
if (options?.sendTransaction !== false &&
|
|
298
|
+
(!this.walletClient || !this.account)) {
|
|
299
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
300
|
+
}
|
|
301
|
+
// 准备交易数据
|
|
302
|
+
const calldata = (0, viem_1.encodeFunctionData)({
|
|
303
|
+
abi: AnchorPay_json_1.default,
|
|
304
|
+
functionName: "depositFor",
|
|
305
|
+
args: [recipient, constants_1.NATIVE_TOKEN_ADDRESS, amount],
|
|
306
|
+
});
|
|
307
|
+
// 如果不需要发送交易,只返回交易数据(AA 模式)
|
|
308
|
+
if (options?.sendTransaction === false) {
|
|
309
|
+
return {
|
|
310
|
+
to: this.contracts?.anchorPay || "",
|
|
311
|
+
data: calldata,
|
|
312
|
+
value: amount,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
// 估算 gas 和费用
|
|
316
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
317
|
+
const gas = await this.publicClient.estimateGas({
|
|
318
|
+
to: this.contracts?.anchorPay || "",
|
|
319
|
+
data: calldata,
|
|
320
|
+
value: amount,
|
|
321
|
+
account: this.account,
|
|
322
|
+
});
|
|
323
|
+
// 发送交易(EOA 模式)
|
|
324
|
+
if (!this.walletClient || !this.account) {
|
|
325
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
326
|
+
}
|
|
327
|
+
const txHash = await this.walletClient.sendTransaction({
|
|
328
|
+
account: this.account,
|
|
329
|
+
to: this.contracts?.anchorPay || "",
|
|
330
|
+
data: calldata,
|
|
331
|
+
value: amount,
|
|
332
|
+
maxFeePerGas: options?.maxFeePerGas || maxFeePerGas,
|
|
333
|
+
maxPriorityFeePerGas: options?.maxPriorityFeePerGas || maxPriorityFeePerGas,
|
|
334
|
+
gas: options?.gas || gas,
|
|
335
|
+
chain: this.publicClient.chain,
|
|
336
|
+
});
|
|
337
|
+
// 等待交易确认并返回收据
|
|
338
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
339
|
+
hash: txHash,
|
|
340
|
+
});
|
|
341
|
+
return receipt;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* 为指定地址存入 ERC20 代币到 AnchorPay 合约
|
|
345
|
+
* @param recipient 接收者地址
|
|
346
|
+
* @param tokenAddress ERC20 代币地址
|
|
347
|
+
* @param amount 金额
|
|
348
|
+
* @param options 交易选项
|
|
349
|
+
* @returns 交易收据或交易数据
|
|
350
|
+
*/
|
|
351
|
+
async depositERC20(recipient, tokenAddress, amount, options) {
|
|
352
|
+
// 如果需要发送交易,但没有钱包客户端或账户
|
|
353
|
+
if (options?.sendTransaction !== false &&
|
|
354
|
+
(!this.walletClient || !this.account)) {
|
|
355
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
356
|
+
}
|
|
357
|
+
// 如果需要自动批准,则先批准代币(默认开启自动批准)
|
|
358
|
+
if (options?.autoApprove !== false && this.walletClient && this.account) {
|
|
359
|
+
try {
|
|
360
|
+
console.log(`Checking ERC20 token approval status: ${tokenAddress}`);
|
|
361
|
+
// 检查授权
|
|
362
|
+
const allowance = (await this.publicClient.readContract({
|
|
363
|
+
address: tokenAddress,
|
|
364
|
+
abi: constants_1.ERC20_ABI,
|
|
365
|
+
functionName: "allowance",
|
|
366
|
+
args: [this.account.address, this.contracts?.anchorPay],
|
|
367
|
+
}));
|
|
368
|
+
console.log(`Current allowance: ${allowance}, Required amount: ${amount}, AnchorPay contract address: ${this.contracts?.anchorPay}`);
|
|
369
|
+
if (allowance < amount) {
|
|
370
|
+
console.log(`Approving ${amount} tokens to AnchorPay contract...`);
|
|
371
|
+
// 批准代币 - 使用最大可能值进行授权,避免多次授权
|
|
372
|
+
const maxApproveAmount = 2n ** 256n - 1n;
|
|
373
|
+
// 估算 gas 和费用
|
|
374
|
+
const { maxFeePerGas: approveMaxFeePerGas, maxPriorityFeePerGas: approveMaxPriorityFeePerGas, } = await this.publicClient.estimateFeesPerGas();
|
|
375
|
+
const approveCalldata = (0, viem_1.encodeFunctionData)({
|
|
376
|
+
abi: constants_1.ERC20_ABI,
|
|
377
|
+
functionName: "approve",
|
|
378
|
+
args: [this.contracts?.anchorPay, maxApproveAmount],
|
|
379
|
+
});
|
|
380
|
+
const approveGas = await this.publicClient.estimateGas({
|
|
381
|
+
to: tokenAddress,
|
|
382
|
+
data: approveCalldata,
|
|
383
|
+
value: 0n,
|
|
384
|
+
account: this.account,
|
|
385
|
+
});
|
|
386
|
+
const approveTxHash = await this.walletClient.writeContract({
|
|
387
|
+
account: this.account,
|
|
388
|
+
address: tokenAddress,
|
|
389
|
+
abi: constants_1.ERC20_ABI,
|
|
390
|
+
functionName: "approve",
|
|
391
|
+
args: [this.contracts?.anchorPay, maxApproveAmount],
|
|
392
|
+
chain: this.publicClient.chain,
|
|
393
|
+
maxFeePerGas: approveMaxFeePerGas,
|
|
394
|
+
maxPriorityFeePerGas: approveMaxPriorityFeePerGas,
|
|
395
|
+
gas: approveGas,
|
|
396
|
+
});
|
|
397
|
+
console.log(`Approval transaction sent, hash: ${approveTxHash}`);
|
|
398
|
+
// 等待交易确认
|
|
399
|
+
await this.publicClient.waitForTransactionReceipt({
|
|
400
|
+
hash: approveTxHash,
|
|
401
|
+
});
|
|
402
|
+
console.log(`Approval transaction confirmed`);
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
console.log(`Sufficient allowance already exists, no need for additional approval`);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
console.error(`Approval failed:`, error);
|
|
410
|
+
throw new Error(`ERC20 approval failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// 准备交易数据
|
|
414
|
+
const calldata = (0, viem_1.encodeFunctionData)({
|
|
415
|
+
abi: AnchorPay_json_1.default,
|
|
416
|
+
functionName: "depositFor",
|
|
417
|
+
args: [recipient, tokenAddress, amount],
|
|
418
|
+
});
|
|
419
|
+
// 如果不需要发送交易,只返回交易数据(AA 模式)
|
|
420
|
+
if (options?.sendTransaction === false) {
|
|
421
|
+
return {
|
|
422
|
+
to: this.contracts?.anchorPay || "",
|
|
423
|
+
data: calldata,
|
|
424
|
+
value: 0n,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
// 估算 gas 和费用
|
|
428
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
429
|
+
const gas = await this.publicClient.estimateGas({
|
|
430
|
+
to: this.contracts?.anchorPay || "",
|
|
431
|
+
data: calldata,
|
|
432
|
+
value: 0n,
|
|
433
|
+
account: this.account,
|
|
434
|
+
});
|
|
435
|
+
// 发送交易(EOA 模式)
|
|
436
|
+
if (!this.walletClient || !this.account) {
|
|
437
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
438
|
+
}
|
|
439
|
+
const txHash = await this.walletClient.sendTransaction({
|
|
440
|
+
account: this.account,
|
|
441
|
+
to: this.contracts?.anchorPay || "",
|
|
442
|
+
data: calldata,
|
|
443
|
+
maxFeePerGas: options?.maxFeePerGas || maxFeePerGas,
|
|
444
|
+
maxPriorityFeePerGas: options?.maxPriorityFeePerGas || maxPriorityFeePerGas,
|
|
445
|
+
gas: options?.gas || gas,
|
|
446
|
+
chain: this.publicClient.chain,
|
|
447
|
+
});
|
|
448
|
+
// 等待交易确认并返回收据
|
|
449
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
450
|
+
hash: txHash,
|
|
451
|
+
});
|
|
452
|
+
return receipt;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* 从 AnchorPay 合约提取代币
|
|
456
|
+
* @param tokenAddress 代币地址(使用 NATIVE_TOKEN_ADDRESS 表示原生代币)
|
|
457
|
+
* @param amount 金额
|
|
458
|
+
* @param options 交易选项
|
|
459
|
+
* @returns 交易收据
|
|
460
|
+
*/
|
|
461
|
+
async withdraw(tokenAddress, amount, options) {
|
|
462
|
+
if (!this.walletClient || !this.account) {
|
|
463
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
464
|
+
}
|
|
465
|
+
// 估算 gas 和费用
|
|
466
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
467
|
+
const calldata = (0, viem_1.encodeFunctionData)({
|
|
468
|
+
abi: AnchorPay_json_1.default,
|
|
469
|
+
functionName: "withdraw",
|
|
470
|
+
args: [tokenAddress, amount],
|
|
471
|
+
});
|
|
472
|
+
const gas = await this.publicClient.estimateGas({
|
|
473
|
+
to: this.contracts?.anchorPay || "",
|
|
474
|
+
data: calldata,
|
|
475
|
+
value: 0n,
|
|
476
|
+
account: this.account,
|
|
477
|
+
});
|
|
478
|
+
const txHash = await this.walletClient.sendTransaction({
|
|
479
|
+
account: this.account,
|
|
480
|
+
to: this.contracts?.anchorPay || "",
|
|
481
|
+
data: calldata,
|
|
482
|
+
maxFeePerGas: options?.maxFeePerGas || maxFeePerGas,
|
|
483
|
+
maxPriorityFeePerGas: options?.maxPriorityFeePerGas || maxPriorityFeePerGas,
|
|
484
|
+
gas: options?.gas || gas,
|
|
485
|
+
chain: this.publicClient.chain,
|
|
486
|
+
});
|
|
487
|
+
// 等待交易确认并返回收据
|
|
488
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
489
|
+
hash: txHash,
|
|
490
|
+
});
|
|
491
|
+
return receipt;
|
|
492
|
+
}
|
|
274
493
|
}
|
|
275
494
|
exports.AnchorPayClient = AnchorPayClient;
|
package/dist/abi/AnchorPay.json
CHANGED
|
@@ -24,6 +24,53 @@
|
|
|
24
24
|
"outputs": [],
|
|
25
25
|
"stateMutability": "nonpayable"
|
|
26
26
|
},
|
|
27
|
+
{
|
|
28
|
+
"type": "function",
|
|
29
|
+
"name": "balanceOf",
|
|
30
|
+
"inputs": [
|
|
31
|
+
{
|
|
32
|
+
"name": "account",
|
|
33
|
+
"type": "address",
|
|
34
|
+
"internalType": "address"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"name": "token",
|
|
38
|
+
"type": "address",
|
|
39
|
+
"internalType": "address"
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"outputs": [
|
|
43
|
+
{
|
|
44
|
+
"name": "",
|
|
45
|
+
"type": "uint256",
|
|
46
|
+
"internalType": "uint256"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"stateMutability": "view"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"type": "function",
|
|
53
|
+
"name": "depositFor",
|
|
54
|
+
"inputs": [
|
|
55
|
+
{
|
|
56
|
+
"name": "recipient",
|
|
57
|
+
"type": "address",
|
|
58
|
+
"internalType": "address"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"name": "token",
|
|
62
|
+
"type": "address",
|
|
63
|
+
"internalType": "address"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"name": "amount",
|
|
67
|
+
"type": "uint256",
|
|
68
|
+
"internalType": "uint256"
|
|
69
|
+
}
|
|
70
|
+
],
|
|
71
|
+
"outputs": [],
|
|
72
|
+
"stateMutability": "payable"
|
|
73
|
+
},
|
|
27
74
|
{
|
|
28
75
|
"type": "function",
|
|
29
76
|
"name": "getInterfaceImplementer",
|
|
@@ -203,6 +250,55 @@
|
|
|
203
250
|
"outputs": [],
|
|
204
251
|
"stateMutability": "payable"
|
|
205
252
|
},
|
|
253
|
+
{
|
|
254
|
+
"type": "function",
|
|
255
|
+
"name": "withdraw",
|
|
256
|
+
"inputs": [
|
|
257
|
+
{
|
|
258
|
+
"name": "token",
|
|
259
|
+
"type": "address",
|
|
260
|
+
"internalType": "address"
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
"name": "amount",
|
|
264
|
+
"type": "uint256",
|
|
265
|
+
"internalType": "uint256"
|
|
266
|
+
}
|
|
267
|
+
],
|
|
268
|
+
"outputs": [],
|
|
269
|
+
"stateMutability": "nonpayable"
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
"type": "event",
|
|
273
|
+
"name": "Deposited",
|
|
274
|
+
"inputs": [
|
|
275
|
+
{
|
|
276
|
+
"name": "from",
|
|
277
|
+
"type": "address",
|
|
278
|
+
"indexed": true,
|
|
279
|
+
"internalType": "address"
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
"name": "recipient",
|
|
283
|
+
"type": "address",
|
|
284
|
+
"indexed": true,
|
|
285
|
+
"internalType": "address"
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
"name": "token",
|
|
289
|
+
"type": "address",
|
|
290
|
+
"indexed": true,
|
|
291
|
+
"internalType": "address"
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
"name": "amount",
|
|
295
|
+
"type": "uint256",
|
|
296
|
+
"indexed": false,
|
|
297
|
+
"internalType": "uint256"
|
|
298
|
+
}
|
|
299
|
+
],
|
|
300
|
+
"anonymous": false
|
|
301
|
+
},
|
|
206
302
|
{
|
|
207
303
|
"type": "event",
|
|
208
304
|
"name": "Initialized",
|
|
@@ -323,6 +419,31 @@
|
|
|
323
419
|
],
|
|
324
420
|
"anonymous": false
|
|
325
421
|
},
|
|
422
|
+
{
|
|
423
|
+
"type": "event",
|
|
424
|
+
"name": "Withdrawn",
|
|
425
|
+
"inputs": [
|
|
426
|
+
{
|
|
427
|
+
"name": "recipient",
|
|
428
|
+
"type": "address",
|
|
429
|
+
"indexed": true,
|
|
430
|
+
"internalType": "address"
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
"name": "token",
|
|
434
|
+
"type": "address",
|
|
435
|
+
"indexed": true,
|
|
436
|
+
"internalType": "address"
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
"name": "amount",
|
|
440
|
+
"type": "uint256",
|
|
441
|
+
"indexed": false,
|
|
442
|
+
"internalType": "uint256"
|
|
443
|
+
}
|
|
444
|
+
],
|
|
445
|
+
"anonymous": false
|
|
446
|
+
},
|
|
326
447
|
{
|
|
327
448
|
"type": "error",
|
|
328
449
|
"name": "AddressEmptyCode",
|
|
@@ -360,6 +481,11 @@
|
|
|
360
481
|
"name": "FailedCall",
|
|
361
482
|
"inputs": []
|
|
362
483
|
},
|
|
484
|
+
{
|
|
485
|
+
"type": "error",
|
|
486
|
+
"name": "InsufficientBalance",
|
|
487
|
+
"inputs": []
|
|
488
|
+
},
|
|
363
489
|
{
|
|
364
490
|
"type": "error",
|
|
365
491
|
"name": "InsufficientPayment",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anchor-sdk",
|
|
3
|
-
"version": "0.1.41-internal.
|
|
3
|
+
"version": "0.1.41-internal.5",
|
|
4
4
|
"description": "TypeScript SDK for interacting with Anchor ecosystem - badge minting, payment processing, and ERC1155 token management",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|