anchor-sdk 0.1.40 → 0.1.41-internal.1
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/AnchorERC1155Client.js +23 -2
- package/dist/AnchorPayClient.d.ts +1 -1
- package/dist/AnchorPayClient.js +98 -40
- package/package.json +1 -1
|
@@ -102,6 +102,14 @@ class AnchorERC1155Client {
|
|
|
102
102
|
if (!this.walletClient || !this.account) {
|
|
103
103
|
throw new Error("Wallet client and account are required to send transactions");
|
|
104
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
|
+
});
|
|
105
113
|
const txHash = await this.walletClient.writeContract({
|
|
106
114
|
account: this.account,
|
|
107
115
|
address: contractAddress,
|
|
@@ -111,7 +119,10 @@ class AnchorERC1155Client {
|
|
|
111
119
|
this.formatMintRequest(signedRequest.request),
|
|
112
120
|
signedRequest.signature,
|
|
113
121
|
],
|
|
114
|
-
chain:
|
|
122
|
+
chain: this.publicClient.chain,
|
|
123
|
+
maxFeePerGas,
|
|
124
|
+
maxPriorityFeePerGas,
|
|
125
|
+
gas,
|
|
115
126
|
});
|
|
116
127
|
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
117
128
|
hash: txHash,
|
|
@@ -227,13 +238,23 @@ class AnchorERC1155Client {
|
|
|
227
238
|
if (!this.walletClient || !this.account) {
|
|
228
239
|
throw new Error("Wallet client and account are required to send transactions");
|
|
229
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
|
+
});
|
|
230
248
|
const txHash = await this.walletClient.writeContract({
|
|
231
249
|
account: this.account,
|
|
232
250
|
address: contractAddress,
|
|
233
251
|
abi: AnchorERC1155_1.AnchorERC1155ABI,
|
|
234
252
|
functionName: "multicall",
|
|
235
253
|
args: [data],
|
|
236
|
-
chain:
|
|
254
|
+
chain: this.publicClient.chain,
|
|
255
|
+
maxFeePerGas,
|
|
256
|
+
maxPriorityFeePerGas,
|
|
257
|
+
gas,
|
|
237
258
|
});
|
|
238
259
|
return await this.publicClient.waitForTransactionReceipt({ hash: txHash });
|
|
239
260
|
}
|
package/dist/AnchorPayClient.js
CHANGED
|
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.AnchorPayClient = void 0;
|
|
7
7
|
const viem_1 = require("viem");
|
|
8
|
-
const AnchorPay_json_1 = __importDefault(require("./abi/AnchorPay.json"));
|
|
9
8
|
const constants_1 = require("./constants");
|
|
9
|
+
const AnchorPay_json_1 = __importDefault(require("./abi/AnchorPay.json"));
|
|
10
10
|
/**
|
|
11
11
|
* AnchorPay 客户端
|
|
12
12
|
* 用于与 AnchorPay 合约交互
|
|
@@ -34,39 +34,51 @@ class AnchorPayClient {
|
|
|
34
34
|
* @param options 交易选项
|
|
35
35
|
* @returns 交易收据或交易数据
|
|
36
36
|
*/
|
|
37
|
-
async sendETH(recipient, amount, data =
|
|
37
|
+
async sendETH(recipient, amount, data = "0x", options) {
|
|
38
38
|
// 如果需要发送交易,但没有钱包客户端或账户
|
|
39
|
-
if (options?.sendTransaction !== false &&
|
|
39
|
+
if (options?.sendTransaction !== false &&
|
|
40
|
+
(!this.walletClient || !this.account)) {
|
|
40
41
|
throw new Error("Wallet client and account are required to send transactions");
|
|
41
42
|
}
|
|
42
43
|
// 准备交易数据
|
|
43
44
|
const calldata = (0, viem_1.encodeFunctionData)({
|
|
44
45
|
abi: AnchorPay_json_1.default,
|
|
45
|
-
functionName:
|
|
46
|
-
args: [recipient, constants_1.NATIVE_TOKEN_ADDRESS, amount, data]
|
|
46
|
+
functionName: "send",
|
|
47
|
+
args: [recipient, constants_1.NATIVE_TOKEN_ADDRESS, amount, data],
|
|
47
48
|
});
|
|
48
49
|
// 如果不需要发送交易,只返回交易数据(AA 模式)
|
|
49
50
|
if (options?.sendTransaction === false) {
|
|
50
51
|
return {
|
|
51
52
|
to: this.contracts?.anchorPay || "",
|
|
52
53
|
data: calldata,
|
|
53
|
-
value: amount
|
|
54
|
+
value: amount,
|
|
54
55
|
};
|
|
55
56
|
}
|
|
57
|
+
// 估算 gas 和费用
|
|
58
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
59
|
+
const gas = await this.publicClient.estimateGas({
|
|
60
|
+
to: this.contracts?.anchorPay || "",
|
|
61
|
+
data: calldata,
|
|
62
|
+
value: amount,
|
|
63
|
+
account: this.account,
|
|
64
|
+
});
|
|
56
65
|
// 发送交易(EOA 模式)
|
|
66
|
+
if (!this.walletClient || !this.account) {
|
|
67
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
68
|
+
}
|
|
57
69
|
const txHash = await this.walletClient.sendTransaction({
|
|
58
70
|
account: this.account,
|
|
59
71
|
to: this.contracts?.anchorPay || "",
|
|
60
72
|
data: calldata,
|
|
61
73
|
value: amount,
|
|
62
|
-
maxFeePerGas: options?.maxFeePerGas,
|
|
63
|
-
maxPriorityFeePerGas: options?.maxPriorityFeePerGas,
|
|
64
|
-
gas: options?.gas,
|
|
65
|
-
chain:
|
|
74
|
+
maxFeePerGas: options?.maxFeePerGas || maxFeePerGas,
|
|
75
|
+
maxPriorityFeePerGas: options?.maxPriorityFeePerGas || maxPriorityFeePerGas,
|
|
76
|
+
gas: options?.gas || gas,
|
|
77
|
+
chain: this.publicClient.chain,
|
|
66
78
|
});
|
|
67
79
|
// 等待交易确认并返回收据
|
|
68
80
|
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
69
|
-
hash: txHash
|
|
81
|
+
hash: txHash,
|
|
70
82
|
});
|
|
71
83
|
return receipt;
|
|
72
84
|
}
|
|
@@ -79,38 +91,57 @@ class AnchorPayClient {
|
|
|
79
91
|
* @param options 交易选项
|
|
80
92
|
* @returns 交易收据或交易数据
|
|
81
93
|
*/
|
|
82
|
-
async sendERC20(recipient, tokenAddress, amount, data =
|
|
94
|
+
async sendERC20(recipient, tokenAddress, amount, data = "0x", options) {
|
|
83
95
|
// 如果需要发送交易,但没有钱包客户端或账户
|
|
84
|
-
if (options?.sendTransaction !== false &&
|
|
96
|
+
if (options?.sendTransaction !== false &&
|
|
97
|
+
(!this.walletClient || !this.account)) {
|
|
85
98
|
throw new Error("Wallet client and account are required to send transactions");
|
|
86
99
|
}
|
|
87
100
|
// 如果需要自动批准,则先批准代币(默认开启自动批准)
|
|
88
|
-
if (
|
|
101
|
+
if (options?.autoApprove !== false && this.walletClient && this.account) {
|
|
89
102
|
try {
|
|
90
103
|
console.log(`Checking ERC20 token approval status: ${tokenAddress}`);
|
|
91
104
|
// 检查授权
|
|
92
|
-
const allowance = await this.publicClient.readContract({
|
|
105
|
+
const allowance = (await this.publicClient.readContract({
|
|
93
106
|
address: tokenAddress,
|
|
94
107
|
abi: constants_1.ERC20_ABI,
|
|
95
|
-
functionName:
|
|
96
|
-
args: [this.account.address, this.contracts?.anchorPay]
|
|
97
|
-
});
|
|
108
|
+
functionName: "allowance",
|
|
109
|
+
args: [this.account.address, this.contracts?.anchorPay],
|
|
110
|
+
}));
|
|
98
111
|
console.log(`Current allowance: ${allowance}, Required amount: ${amount}, AnchorPay contract address: ${this.contracts?.anchorPay}`);
|
|
99
112
|
if (allowance < amount) {
|
|
100
113
|
console.log(`Approving ${amount} tokens to AnchorPay contract...`);
|
|
101
114
|
// 批准代币 - 使用最大可能值进行授权,避免多次授权
|
|
102
115
|
const maxApproveAmount = 2n ** 256n - 1n;
|
|
116
|
+
// 估算 gas 和费用
|
|
117
|
+
const { maxFeePerGas: approveMaxFeePerGas, maxPriorityFeePerGas: approveMaxPriorityFeePerGas, } = await this.publicClient.estimateFeesPerGas();
|
|
118
|
+
const approveCalldata = (0, viem_1.encodeFunctionData)({
|
|
119
|
+
abi: constants_1.ERC20_ABI,
|
|
120
|
+
functionName: "approve",
|
|
121
|
+
args: [this.contracts?.anchorPay, maxApproveAmount],
|
|
122
|
+
});
|
|
123
|
+
const approveGas = await this.publicClient.estimateGas({
|
|
124
|
+
to: tokenAddress,
|
|
125
|
+
data: approveCalldata,
|
|
126
|
+
value: 0n,
|
|
127
|
+
account: this.account,
|
|
128
|
+
});
|
|
103
129
|
const approveTxHash = await this.walletClient.writeContract({
|
|
104
130
|
account: this.account,
|
|
105
131
|
address: tokenAddress,
|
|
106
132
|
abi: constants_1.ERC20_ABI,
|
|
107
|
-
functionName:
|
|
133
|
+
functionName: "approve",
|
|
108
134
|
args: [this.contracts?.anchorPay, maxApproveAmount],
|
|
109
|
-
chain:
|
|
135
|
+
chain: this.publicClient.chain,
|
|
136
|
+
maxFeePerGas: approveMaxFeePerGas,
|
|
137
|
+
maxPriorityFeePerGas: approveMaxPriorityFeePerGas,
|
|
138
|
+
gas: approveGas,
|
|
110
139
|
});
|
|
111
140
|
console.log(`Approval transaction sent, hash: ${approveTxHash}`);
|
|
112
141
|
// 等待交易确认
|
|
113
|
-
await this.publicClient.waitForTransactionReceipt({
|
|
142
|
+
await this.publicClient.waitForTransactionReceipt({
|
|
143
|
+
hash: approveTxHash,
|
|
144
|
+
});
|
|
114
145
|
console.log(`Approval transaction confirmed`);
|
|
115
146
|
}
|
|
116
147
|
else {
|
|
@@ -125,31 +156,42 @@ class AnchorPayClient {
|
|
|
125
156
|
// 准备交易数据
|
|
126
157
|
const calldata = (0, viem_1.encodeFunctionData)({
|
|
127
158
|
abi: AnchorPay_json_1.default,
|
|
128
|
-
functionName:
|
|
129
|
-
args: [recipient, tokenAddress, amount, data]
|
|
159
|
+
functionName: "send",
|
|
160
|
+
args: [recipient, tokenAddress, amount, data],
|
|
130
161
|
});
|
|
131
162
|
// 如果不需要发送交易,只返回交易数据(AA 模式)
|
|
132
163
|
if (options?.sendTransaction === false) {
|
|
133
164
|
return {
|
|
134
165
|
to: this.contracts?.anchorPay || "",
|
|
135
166
|
data: calldata,
|
|
136
|
-
value: 0n
|
|
167
|
+
value: 0n,
|
|
137
168
|
};
|
|
138
169
|
}
|
|
170
|
+
// 估算 gas 和费用
|
|
171
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
172
|
+
const gas = await this.publicClient.estimateGas({
|
|
173
|
+
to: this.contracts?.anchorPay || "",
|
|
174
|
+
data: calldata,
|
|
175
|
+
value: 0n,
|
|
176
|
+
account: this.account,
|
|
177
|
+
});
|
|
139
178
|
// 发送交易(EOA 模式)
|
|
179
|
+
if (!this.walletClient || !this.account) {
|
|
180
|
+
throw new Error("Wallet client and account are required to send transactions");
|
|
181
|
+
}
|
|
140
182
|
const txHash = await this.walletClient.sendTransaction({
|
|
141
183
|
account: this.account,
|
|
142
184
|
to: this.contracts?.anchorPay || "",
|
|
143
185
|
data: calldata,
|
|
144
186
|
// value: amount,
|
|
145
|
-
maxFeePerGas: options?.maxFeePerGas,
|
|
146
|
-
maxPriorityFeePerGas: options?.maxPriorityFeePerGas,
|
|
147
|
-
gas: options?.gas,
|
|
148
|
-
chain:
|
|
187
|
+
maxFeePerGas: options?.maxFeePerGas || maxFeePerGas,
|
|
188
|
+
maxPriorityFeePerGas: options?.maxPriorityFeePerGas || maxPriorityFeePerGas,
|
|
189
|
+
gas: options?.gas || gas,
|
|
190
|
+
chain: this.publicClient.chain,
|
|
149
191
|
});
|
|
150
192
|
// 等待交易确认并返回收据
|
|
151
193
|
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
152
|
-
hash: txHash
|
|
194
|
+
hash: txHash,
|
|
153
195
|
});
|
|
154
196
|
return receipt;
|
|
155
197
|
}
|
|
@@ -160,12 +202,12 @@ class AnchorPayClient {
|
|
|
160
202
|
* @returns 实现者地址
|
|
161
203
|
*/
|
|
162
204
|
async getInterfaceImplementer(account, interfaceHash) {
|
|
163
|
-
return await this.publicClient.readContract({
|
|
205
|
+
return (await this.publicClient.readContract({
|
|
164
206
|
address: this.contracts?.anchorPay || "",
|
|
165
207
|
abi: AnchorPay_json_1.default,
|
|
166
|
-
functionName:
|
|
167
|
-
args: [account, interfaceHash]
|
|
168
|
-
});
|
|
208
|
+
functionName: "getInterfaceImplementer",
|
|
209
|
+
args: [account, interfaceHash],
|
|
210
|
+
}));
|
|
169
211
|
}
|
|
170
212
|
/**
|
|
171
213
|
* 设置接口实现者(仅限管理员)
|
|
@@ -178,17 +220,33 @@ class AnchorPayClient {
|
|
|
178
220
|
if (!this.walletClient || !this.account) {
|
|
179
221
|
throw new Error("Wallet client and account are required to send transactions");
|
|
180
222
|
}
|
|
223
|
+
// 估算 gas 和费用
|
|
224
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await this.publicClient.estimateFeesPerGas();
|
|
225
|
+
const calldata = (0, viem_1.encodeFunctionData)({
|
|
226
|
+
abi: AnchorPay_json_1.default,
|
|
227
|
+
functionName: "setInterfaceImplementer",
|
|
228
|
+
args: [account, interfaceHash, implementer],
|
|
229
|
+
});
|
|
230
|
+
const gas = await this.publicClient.estimateGas({
|
|
231
|
+
to: this.contracts?.anchorPay || "",
|
|
232
|
+
data: calldata,
|
|
233
|
+
value: 0n,
|
|
234
|
+
account: this.account,
|
|
235
|
+
});
|
|
181
236
|
const txHash = await this.walletClient.writeContract({
|
|
182
237
|
account: this.account,
|
|
183
238
|
address: this.contracts?.anchorPay || "",
|
|
184
239
|
abi: AnchorPay_json_1.default,
|
|
185
|
-
functionName:
|
|
240
|
+
functionName: "setInterfaceImplementer",
|
|
186
241
|
args: [account, interfaceHash, implementer],
|
|
187
|
-
chain:
|
|
242
|
+
chain: this.publicClient.chain,
|
|
243
|
+
maxFeePerGas,
|
|
244
|
+
maxPriorityFeePerGas,
|
|
245
|
+
gas,
|
|
188
246
|
});
|
|
189
247
|
// 等待交易确认并返回收据
|
|
190
248
|
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
191
|
-
hash: txHash
|
|
249
|
+
hash: txHash,
|
|
192
250
|
});
|
|
193
251
|
return receipt;
|
|
194
252
|
}
|
|
@@ -200,17 +258,17 @@ class AnchorPayClient {
|
|
|
200
258
|
* @returns 交易数据对象,包含 to、data 和 value 字段
|
|
201
259
|
*/
|
|
202
260
|
getERC20ApprovalData(tokenAddress, amount, spender) {
|
|
203
|
-
const approveSpender = spender ||
|
|
261
|
+
const approveSpender = spender || this.contracts?.anchorPay || "";
|
|
204
262
|
// 编码 approve 函数调用
|
|
205
263
|
const data = (0, viem_1.encodeFunctionData)({
|
|
206
264
|
abi: constants_1.ERC20_ABI,
|
|
207
|
-
functionName:
|
|
208
|
-
args: [approveSpender, amount]
|
|
265
|
+
functionName: "approve",
|
|
266
|
+
args: [approveSpender, amount],
|
|
209
267
|
});
|
|
210
268
|
return {
|
|
211
269
|
to: tokenAddress,
|
|
212
270
|
data,
|
|
213
|
-
value: 0n // ERC20 批准不需要发送 ETH
|
|
271
|
+
value: 0n, // ERC20 批准不需要发送 ETH
|
|
214
272
|
};
|
|
215
273
|
}
|
|
216
274
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anchor-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.41-internal.1",
|
|
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",
|