-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontract-tool-server.js
More file actions
694 lines (607 loc) · 22.3 KB
/
Copy pathcontract-tool-server.js
File metadata and controls
694 lines (607 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/**
* NULS智能合约测试工具 - 后端服务器
*
* 使用说明:
* 1. 安装依赖: npm install express cors body-parser
* 2. 启动服务: node contract-tool-server.js
* 3. 在浏览器打开 contract-tool.html
*/
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const nuls = require('./src/index');
const contractCreate = require('./src/test/contractCreate');
const contractCall = require('./src/test/contractCall');
const contractMulticall = require('./src/test/contractMulticall');
const BigNumber = require('bignumber.js');
const app = express();
const PORT = 3000;
// 中间件
app.use(cors());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
// 静态文件服务
app.use(express.static(__dirname));
// 日志中间件
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
next();
});
/**
* 配置网络环境
* POST /api/config-network
* Body: { network: 'main'|'test'|'dev', customUrl: string, chainId: number }
*/
app.post('/api/config-network', (req, res) => {
try {
const { network, customUrl, chainId } = req.body;
if (network === 'main') {
nuls.mainnet();
} else if (network === 'test') {
nuls.testnet();
} else if (network === 'dev') {
nuls.customnet(chainId || 101, customUrl || 'http://127.0.0.1:18004/jsonrpc');
} else if (network === 'custom' && customUrl) {
nuls.customnet(chainId, customUrl);
}
res.json({
success: true,
message: '网络配置成功',
chainId: nuls.chainId()
});
} catch (error) {
console.error('配置网络失败:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* 创建智能合约
* POST /api/create-contract
* Body: {
* privateKey: string,
* alias: string,
* contractCode: string,
* args: array,
* remark: string,
* chainId: number,
* assetId: number,
* addressPrefix: string
* }
*/
app.post('/api/create-contract', async (req, res) => {
try {
const {
privateKey,
alias,
contractCode,
args,
remark,
chainId,
assetId,
addressPrefix
} = req.body;
// 验证必填参数
if (!privateKey || !contractCode) {
return res.status(400).json({
success: false,
error: '私钥和合约代码为必填项'
});
}
// 配置网络(如果提供了chainId)
if (chainId) {
// 网络应该已经在前端配置好了,这里只是确保一致性
console.log('使用链ID:', chainId);
}
// 导入地址
const importAddress = nuls.importByKey(chainId, privateKey, '', addressPrefix);
const pub = importAddress.pub;
const fromAddress = importAddress.address;
console.log('创建合约 - 发送地址:', fromAddress);
console.log('创建合约 - 参数数量:', args ? args.length : 0);
// 构建合约创建对象
const contractCreateData = {
chainId: chainId,
sender: fromAddress,
alias: alias || '',
contractCode: contractCode,
args: args || []
};
console.log('开始创建合约...');
// 调用SDK创建合约
const result = await contractCreate.createContract(
privateKey,
pub,
fromAddress,
chainId,
assetId,
contractCreateData,
remark || '',
addressPrefix
);
console.log('创建合约返回结果:', result);
// 检查返回结果
if (!result || !result.success) {
throw new Error(result && result.data ? result.data : '创建合约失败');
}
// 返回成功结果
res.json({
success: true,
message: '合约创建成功',
data: {
fromAddress: fromAddress,
contractAddress: result.data.contractAddress,
txHash: result.data.hash,
value: result.data.value
}
});
} catch (error) {
console.error('创建合约失败:', error);
// 提取更友好的错误信息
let errorMessage = error.message || error.toString();
// 检查是否是特定的错误类型
if (errorMessage.includes('获取合约构造函数失败')) {
errorMessage = '合约代码无效或格式错误。请检查:\n1. 合约代码是否完整\n2. 合约代码是否为有效的十六进制字符串\n3. 是否能正确解析合约结构\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('balance') || errorMessage.includes('余额')) {
errorMessage = '账户余额不足。请确保账户有足够的余额支付手续费和gas费。\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('nonce')) {
errorMessage = 'Nonce错误,可能是网络连接问题。请检查网络配置。\n\n详细错误: ' + errorMessage;
}
res.status(500).json({
success: false,
error: errorMessage,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
/**
* 调用智能合约
* POST /api/call-contract
* Body: {
* privateKey: string,
* contractAddress: string,
* methodName: string,
* methodDesc: string,
* value: number,
* args: array,
* remark: string,
* chainId: number,
* assetId: number,
* addressPrefix: string
* }
*/
app.post('/api/call-contract', async (req, res) => {
try {
const {
privateKey,
contractAddress,
methodName,
methodDesc,
value,
args,
remark,
chainId,
assetId,
addressPrefix
} = req.body;
// 验证必填参数
if (!privateKey || !contractAddress || !methodName) {
return res.status(400).json({
success: false,
error: '私钥、合约地址和方法名为必填项'
});
}
// 导入地址
const importAddress = nuls.importByKey(chainId, privateKey, '', addressPrefix);
const pub = importAddress.pub;
const fromAddress = importAddress.address;
console.log('调用合约 - 发送地址:', fromAddress);
console.log('调用合约 - 合约地址:', contractAddress);
console.log('调用合约 - 方法名:', methodName);
console.log('调用合约 - 参数数量:', args ? args.length : 0);
// 构建合约调用对象
const contractCallData = {
chainId: chainId,
sender: fromAddress,
contractAddress: contractAddress,
value: value || 0,
methodName: methodName,
methodDesc: methodDesc || '',
args: args || []
};
console.log('开始调用合约...');
// 调用SDK调用合约
const result = await contractCall.callContract(
privateKey,
pub,
fromAddress,
chainId,
assetId,
contractCallData,
remark || ''
);
console.log('调用合约返回结果:', result);
// 检查返回结果
if (!result || !result.success) {
throw new Error(result && result.data ? result.data : '调用合约失败');
}
// 返回成功结果
res.json({
success: true,
message: '合约调用成功',
data: {
fromAddress: fromAddress,
contractAddress: contractAddress,
methodName: methodName,
txHash: result.data.hash,
value: result.data.value
}
});
} catch (error) {
console.error('调用合约失败:', error);
// 提取更友好的错误信息
let errorMessage = error.message || error.toString();
// 检查是否是特定的错误类型
if (errorMessage.includes('balance') || errorMessage.includes('余额')) {
errorMessage = '账户余额不足。请确保账户有足够的余额支付手续费和gas费。\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('contract') && errorMessage.includes('not found')) {
errorMessage = '合约不存在或地址无效。请检查合约地址是否正确。\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('method') || errorMessage.includes('方法')) {
errorMessage = '合约方法调用失败。请检查:\n1. 方法名是否正确\n2. 参数类型和数量是否匹配\n3. 合约是否支持该方法\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('imputedCallGas')) {
errorMessage = 'Gas预估失败。可能的原因:\n1. 合约地址无效\n2. 方法不存在\n3. 参数错误\n4. 网络连接问题\n\n详细错误: ' + errorMessage;
}
res.status(500).json({
success: false,
error: errorMessage,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
/**
* 普通转账
* POST /api/transfer
* Body: {
* privateKey: string,
* toAddress: string,
* amount: number,
* remark: string,
* chainId: number,
* assetId: number,
* addressPrefix: string
* }
*/
app.post('/api/transfer', async (req, res) => {
try {
const {
privateKey,
toAddress,
amount,
remark,
chainId,
assetId,
addressPrefix
} = req.body;
// 验证必填参数
if (!privateKey || !toAddress || !amount) {
return res.status(400).json({
success: false,
error: '私钥、目标地址和金额为必填项'
});
}
// 导入地址
const importAddress = nuls.importByKey(chainId, privateKey, '', addressPrefix);
const pub = importAddress.pub;
const fromAddress = importAddress.address;
console.log('普通转账 - 发送地址:', fromAddress);
console.log('普通转账 - 目标地址:', toAddress);
console.log('普通转账 - 金额:', amount);
// 获取余额
const {getBalance, countFee, inputsOrOutputs, validateTx, broadcastTx} = require('./src/test/api/util');
const balanceInfo = await getBalance(chainId, chainId, assetId, fromAddress);
if (!balanceInfo || balanceInfo.balance === undefined) {
throw new Error('获取账户余额失败');
}
console.log('账户余额:', new BigNumber(balanceInfo.balance).shiftedBy(0 - nuls.decimals()).toFixed());
// 转换金额(前端传入的是实际金额,需要乘以10^nuls.decimals())
const transferAmount = new BigNumber(amount).shiftedBy(nuls.decimals());
let transferInfo = {
fromAddress: fromAddress,
toAddress: toAddress,
assetsChainId: chainId,
assetsId: assetId,
amount: transferAmount,
fee: new BigNumber("0.001").shiftedBy(nuls.decimals())
};
let newAmount = transferInfo.amount.plus(transferInfo.fee);
if (new BigNumber(balanceInfo.balance).isLessThan(newAmount)) {
throw new Error('余额不足,当前余额: ' +
(new BigNumber(balanceInfo.balance).shiftedBy(0 - nuls.decimals()).toFixed())
+ ',需要: ' +
(newAmount.shiftedBy(0 - nuls.decimals()).toFixed())
);
}
// 组装inputs和outputs
let inOrOutputs = await inputsOrOutputs(transferInfo, balanceInfo, 2);
if (!inOrOutputs.success) {
throw new Error('组装交易失败: ' + inOrOutputs.data);
}
// 组装交易
let tAssemble = await nuls.transactionAssemble(
inOrOutputs.data.inputs,
inOrOutputs.data.outputs,
remark || '',
2
);
// 计算手续费
let newFee = countFee(tAssemble, 1);
// 如果手续费变化,重新组装交易
if (transferInfo.fee.isLessThan(newFee)) {
transferInfo.fee = newFee;
inOrOutputs = await inputsOrOutputs(transferInfo, balanceInfo, 2);
tAssemble = await nuls.transactionAssemble(
inOrOutputs.data.inputs,
inOrOutputs.data.outputs,
remark || '',
2
);
}
// 签名交易
let txhex = await nuls.transactionSerialize(privateKey, pub, tAssemble);
console.log('交易hex:', txhex);
console.log('交易hex:', txhex.substring(0, 100) + '...');
// 验证交易
let validateResult = await validateTx(txhex);
if (!validateResult.success) {
throw new Error('验证交易失败: ' + JSON.stringify(validateResult.error));
}
// 广播交易
let broadcastResult = await broadcastTx(txhex);
console.log('广播结果:', broadcastResult);
if (broadcastResult && broadcastResult.value) {
res.json({
success: true,
message: '转账成功',
data: {
fromAddress: fromAddress,
toAddress: toAddress,
amount: amount,
txHash: broadcastResult.hash || broadcastResult.value,
fee: new BigNumber(newFee).shiftedBy(0 - nuls.decimals()).toFixed()
}
});
} else {
throw new Error('广播交易失败: ' + JSON.stringify(broadcastResult));
}
} catch (error) {
console.error('转账失败:', error);
// 提取更友好的错误信息
let errorMessage = error.message || error.toString();
if (errorMessage.includes('balance') || errorMessage.includes('余额')) {
errorMessage = '账户余额不足。\n\n详细错误: ' + errorMessage;
} else if (errorMessage.includes('address') || errorMessage.includes('地址')) {
errorMessage = '地址格式错误。请检查地址是否正确。\n\n详细错误: ' + errorMessage;
}
res.status(500).json({
success: false,
error: errorMessage,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
/**
* 批量查询NRC20 Token信息
* POST /api/token-info
* Body: {
* contractAddress: string,
* holderAddress?: string
* }
*/
app.post('/api/token-info', async (req, res) => {
try {
const { contractAddress, holderAddress } = req.body;
if (!contractAddress || !contractAddress.trim()) {
return res.status(400).json({
success: false,
error: 'Token合约地址不能为空'
});
}
const normalizedContract = contractAddress.trim();
const contractAddressArray = [normalizedContract, normalizedContract, normalizedContract];
const methodNameArray = ['name', 'symbol', 'decimals'];
const argsArray = ['', '', ''];
const normalizedHolder = holderAddress && holderAddress.trim() ? holderAddress.trim() : null;
if (normalizedHolder) {
contractAddressArray.push(normalizedContract);
methodNameArray.push('balanceOf');
argsArray.push(normalizedHolder);
}
const result = await contractMulticall.multicall(contractAddressArray, methodNameArray, argsArray);
if (!result || typeof result.result !== 'string') {
throw new Error('批量查询返回结果异常');
}
let parsed;
try {
parsed = JSON.parse(result.result);
} catch (error) {
throw new Error('无法解析批量查询结果');
}
const responseData = {
contractAddress: normalizedContract,
name: parsed[0] || '',
symbol: parsed[1] || '',
decimals: Number(parsed[2]) || 0
};
if (normalizedHolder) {
const balanceRaw = parsed[3] || '0';
responseData.balanceRaw = balanceRaw;
responseData.holderAddress = normalizedHolder;
try {
responseData.balanceFormatted = new BigNumber(balanceRaw || 0)
.shiftedBy(0 - (responseData.decimals || 0))
.toFixed();
} catch (error) {
responseData.balanceFormatted = balanceRaw;
}
}
res.json({
success: true,
data: responseData
});
} catch (error) {
console.error('查询Token信息失败:', error);
res.status(500).json({
success: false,
error: error.message || '查询Token信息失败'
});
}
});
/**
* 获取合约ABI信息
* POST /api/get-contract-abi
* Body: { contractAddress: string }
*/
app.post('/api/get-contract-abi', async (req, res) => {
try {
const { contractAddress } = req.body;
if (!contractAddress || !contractAddress.trim()) {
return res.status(400).json({
success: false,
error: '合约地址不能为空'
});
}
const chainId = nuls.chainId();
if (!chainId) {
return res.status(400).json({
success: false,
error: '请先配置网络'
});
}
// 调用 getContract RPC
const axios = require('axios').default;
const rpcUrl = axios.defaults.baseURL || 'http://127.0.0.1:18004/jsonrpc';
const http = require('./src/test/api/https');
const response = await http.postComplete(rpcUrl, 'getContract', [chainId, contractAddress.trim()]);
if (response.error) {
throw new Error(response.error.message || '获取合约信息失败');
}
if (!response.result || !response.result.method) {
throw new Error('合约信息格式错误');
}
// 提取可调用函数(view=false, event=false, name!="<init>")
const callableMethods = response.result.method.filter(method =>
method.view === false &&
method.event === false &&
method.name !== '<init>'
);
res.json({
success: true,
data: {
contractAddress: contractAddress.trim(),
contractInfo: response.result,
callableMethods: callableMethods
}
});
} catch (error) {
console.error('获取合约ABI失败:', error);
res.status(500).json({
success: false,
error: error.message || '获取合约ABI失败'
});
}
});
/**
* 获取网络配置信息
* GET /api/network-config
*/
app.get('/api/network-config', (req, res) => {
res.json({
success: true,
data: {
chainId: nuls.chainId(),
decimals: nuls.decimals()
}
});
});
/**
* 健康检查
* GET /api/health
*/
app.get('/api/health', (req, res) => {
res.json({
success: true,
message: 'NULS智能合约测试工具服务运行中',
timestamp: new Date().toISOString(),
chainId: nuls.chainId(),
decimals: nuls.decimals()
});
});
/**
* 获取地址信息
* POST /api/get-address
* Body: { privateKey: string, chainId: number, addressPrefix: string }
*/
app.post('/api/get-address', (req, res) => {
try {
const { privateKey, chainId, addressPrefix } = req.body;
if (!privateKey) {
return res.status(400).json({
success: false,
error: '私钥为必填项'
});
}
const importAddress = nuls.importByKey(chainId, privateKey, '', addressPrefix);
res.json({
success: true,
data: {
address: importAddress.address,
publicKey: importAddress.pub
}
});
} catch (error) {
console.error('获取地址失败:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});
// 错误处理中间件
app.use((err, req, res, next) => {
console.error('服务器错误:', err);
res.status(500).json({
success: false,
error: err.message || '服务器内部错误'
});
});
// 启动服务器
app.listen(PORT, () => {
console.log('='.repeat(60));
console.log('NULS智能合约测试工具服务已启动');
console.log('='.repeat(60));
console.log(`服务地址: http://localhost:${PORT}`);
console.log(`前端页面: http://localhost:${PORT}/contract-tool.html`);
console.log(`健康检查: http://localhost:${PORT}/api/health`);
console.log('='.repeat(60));
console.log('API端点:');
console.log(' POST /api/config-network - 配置网络环境');
console.log(' POST /api/transfer - 普通转账');
console.log(' POST /api/create-contract - 创建智能合约');
console.log(' POST /api/call-contract - 调用智能合约');
console.log(' POST /api/get-address - 获取地址信息');
console.log(' GET /api/health - 健康检查');
console.log('='.repeat(60));
console.log('\n按 Ctrl+C 停止服务\n');
});
// 优雅关闭
process.on('SIGINT', () => {
console.log('\n正在关闭服务器...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n正在关闭服务器...');
process.exit(0);
});