gitee.com/liu-zhao234568/cntest@v1.0.0/internal/web3ext/web3ext.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // package web3ext contains geth specific web3.js extensions. 18 package web3ext 19 20 var Modules = map[string]string{ 21 "admin": AdminJs, 22 "clique": CliqueJs, 23 "ethash": EthashJs, 24 "debug": DebugJs, 25 "eth": EthJs, 26 "miner": MinerJs, 27 "net": NetJs, 28 "personal": PersonalJs, 29 "rpc": RpcJs, 30 "txpool": TxpoolJs, 31 "les": LESJs, 32 "vflux": VfluxJs, 33 } 34 35 const CliqueJs = ` 36 web3._extend({ 37 property: 'clique', 38 methods: [ 39 new web3._extend.Method({ 40 name: 'getSnapshot', 41 call: 'clique_getSnapshot', 42 params: 1, 43 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] 44 }), 45 new web3._extend.Method({ 46 name: 'getSnapshotAtHash', 47 call: 'clique_getSnapshotAtHash', 48 params: 1 49 }), 50 new web3._extend.Method({ 51 name: 'getSigners', 52 call: 'clique_getSigners', 53 params: 1, 54 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] 55 }), 56 new web3._extend.Method({ 57 name: 'getSignersAtHash', 58 call: 'clique_getSignersAtHash', 59 params: 1 60 }), 61 new web3._extend.Method({ 62 name: 'propose', 63 call: 'clique_propose', 64 params: 2 65 }), 66 new web3._extend.Method({ 67 name: 'discard', 68 call: 'clique_discard', 69 params: 1 70 }), 71 new web3._extend.Method({ 72 name: 'status', 73 call: 'clique_status', 74 params: 0 75 }), 76 new web3._extend.Method({ 77 name: 'getSigner', 78 call: 'clique_getSigner', 79 params: 1, 80 inputFormatter: [null] 81 }), 82 ], 83 properties: [ 84 new web3._extend.Property({ 85 name: 'proposals', 86 getter: 'clique_proposals' 87 }), 88 ] 89 }); 90 ` 91 92 const EthashJs = ` 93 web3._extend({ 94 property: 'ethash', 95 methods: [ 96 new web3._extend.Method({ 97 name: 'getWork', 98 call: 'ethash_getWork', 99 params: 0 100 }), 101 new web3._extend.Method({ 102 name: 'getHashrate', 103 call: 'ethash_getHashrate', 104 params: 0 105 }), 106 new web3._extend.Method({ 107 name: 'submitWork', 108 call: 'ethash_submitWork', 109 params: 3, 110 }), 111 new web3._extend.Method({ 112 name: 'submitHashrate', 113 call: 'ethash_submitHashrate', 114 params: 2, 115 }), 116 ] 117 }); 118 ` 119 120 const AdminJs = ` 121 web3._extend({ 122 property: 'admin', 123 methods: [ 124 new web3._extend.Method({ 125 name: 'addPeer', 126 call: 'admin_addPeer', 127 params: 1 128 }), 129 new web3._extend.Method({ 130 name: 'removePeer', 131 call: 'admin_removePeer', 132 params: 1 133 }), 134 new web3._extend.Method({ 135 name: 'addTrustedPeer', 136 call: 'admin_addTrustedPeer', 137 params: 1 138 }), 139 new web3._extend.Method({ 140 name: 'removeTrustedPeer', 141 call: 'admin_removeTrustedPeer', 142 params: 1 143 }), 144 new web3._extend.Method({ 145 name: 'exportChain', 146 call: 'admin_exportChain', 147 params: 3, 148 inputFormatter: [null, null, null] 149 }), 150 new web3._extend.Method({ 151 name: 'importChain', 152 call: 'admin_importChain', 153 params: 1 154 }), 155 new web3._extend.Method({ 156 name: 'sleepBlocks', 157 call: 'admin_sleepBlocks', 158 params: 2 159 }), 160 new web3._extend.Method({ 161 name: 'startHTTP', 162 call: 'admin_startHTTP', 163 params: 5, 164 inputFormatter: [null, null, null, null, null] 165 }), 166 new web3._extend.Method({ 167 name: 'stopHTTP', 168 call: 'admin_stopHTTP' 169 }), 170 // This method is deprecated. 171 new web3._extend.Method({ 172 name: 'startRPC', 173 call: 'admin_startRPC', 174 params: 5, 175 inputFormatter: [null, null, null, null, null] 176 }), 177 // This method is deprecated. 178 new web3._extend.Method({ 179 name: 'stopRPC', 180 call: 'admin_stopRPC' 181 }), 182 new web3._extend.Method({ 183 name: 'startWS', 184 call: 'admin_startWS', 185 params: 4, 186 inputFormatter: [null, null, null, null] 187 }), 188 new web3._extend.Method({ 189 name: 'stopWS', 190 call: 'admin_stopWS' 191 }), 192 ], 193 properties: [ 194 new web3._extend.Property({ 195 name: 'nodeInfo', 196 getter: 'admin_nodeInfo' 197 }), 198 new web3._extend.Property({ 199 name: 'peers', 200 getter: 'admin_peers' 201 }), 202 new web3._extend.Property({ 203 name: 'datadir', 204 getter: 'admin_datadir' 205 }), 206 ] 207 }); 208 ` 209 210 const DebugJs = ` 211 web3._extend({ 212 property: 'debug', 213 methods: [ 214 new web3._extend.Method({ 215 name: 'accountRange', 216 call: 'debug_accountRange', 217 params: 6, 218 inputFormatter: [web3._extend.formatters.inputDefaultBlockNumberFormatter, null, null, null, null, null], 219 }), 220 new web3._extend.Method({ 221 name: 'printBlock', 222 call: 'debug_printBlock', 223 params: 1, 224 outputFormatter: console.log 225 }), 226 new web3._extend.Method({ 227 name: 'getBlockRlp', 228 call: 'debug_getBlockRlp', 229 params: 1 230 }), 231 new web3._extend.Method({ 232 name: 'testSignCliqueBlock', 233 call: 'debug_testSignCliqueBlock', 234 params: 2, 235 inputFormatter: [web3._extend.formatters.inputAddressFormatter, null], 236 }), 237 new web3._extend.Method({ 238 name: 'setHead', 239 call: 'debug_setHead', 240 params: 1 241 }), 242 new web3._extend.Method({ 243 name: 'seedHash', 244 call: 'debug_seedHash', 245 params: 1 246 }), 247 new web3._extend.Method({ 248 name: 'dumpBlock', 249 call: 'debug_dumpBlock', 250 params: 1, 251 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] 252 }), 253 new web3._extend.Method({ 254 name: 'chaindbProperty', 255 call: 'debug_chaindbProperty', 256 params: 1, 257 outputFormatter: console.log 258 }), 259 new web3._extend.Method({ 260 name: 'chaindbCompact', 261 call: 'debug_chaindbCompact', 262 }), 263 new web3._extend.Method({ 264 name: 'verbosity', 265 call: 'debug_verbosity', 266 params: 1 267 }), 268 new web3._extend.Method({ 269 name: 'vmodule', 270 call: 'debug_vmodule', 271 params: 1 272 }), 273 new web3._extend.Method({ 274 name: 'backtraceAt', 275 call: 'debug_backtraceAt', 276 params: 1, 277 }), 278 new web3._extend.Method({ 279 name: 'stacks', 280 call: 'debug_stacks', 281 params: 0, 282 outputFormatter: console.log 283 }), 284 new web3._extend.Method({ 285 name: 'freeOSMemory', 286 call: 'debug_freeOSMemory', 287 params: 0, 288 }), 289 new web3._extend.Method({ 290 name: 'setGCPercent', 291 call: 'debug_setGCPercent', 292 params: 1, 293 }), 294 new web3._extend.Method({ 295 name: 'memStats', 296 call: 'debug_memStats', 297 params: 0, 298 }), 299 new web3._extend.Method({ 300 name: 'gcStats', 301 call: 'debug_gcStats', 302 params: 0, 303 }), 304 new web3._extend.Method({ 305 name: 'cpuProfile', 306 call: 'debug_cpuProfile', 307 params: 2 308 }), 309 new web3._extend.Method({ 310 name: 'startCPUProfile', 311 call: 'debug_startCPUProfile', 312 params: 1 313 }), 314 new web3._extend.Method({ 315 name: 'stopCPUProfile', 316 call: 'debug_stopCPUProfile', 317 params: 0 318 }), 319 new web3._extend.Method({ 320 name: 'goTrace', 321 call: 'debug_goTrace', 322 params: 2 323 }), 324 new web3._extend.Method({ 325 name: 'startGoTrace', 326 call: 'debug_startGoTrace', 327 params: 1 328 }), 329 new web3._extend.Method({ 330 name: 'stopGoTrace', 331 call: 'debug_stopGoTrace', 332 params: 0 333 }), 334 new web3._extend.Method({ 335 name: 'blockProfile', 336 call: 'debug_blockProfile', 337 params: 2 338 }), 339 new web3._extend.Method({ 340 name: 'setBlockProfileRate', 341 call: 'debug_setBlockProfileRate', 342 params: 1 343 }), 344 new web3._extend.Method({ 345 name: 'writeBlockProfile', 346 call: 'debug_writeBlockProfile', 347 params: 1 348 }), 349 new web3._extend.Method({ 350 name: 'mutexProfile', 351 call: 'debug_mutexProfile', 352 params: 2 353 }), 354 new web3._extend.Method({ 355 name: 'setMutexProfileFraction', 356 call: 'debug_setMutexProfileFraction', 357 params: 1 358 }), 359 new web3._extend.Method({ 360 name: 'writeMutexProfile', 361 call: 'debug_writeMutexProfile', 362 params: 1 363 }), 364 new web3._extend.Method({ 365 name: 'writeMemProfile', 366 call: 'debug_writeMemProfile', 367 params: 1 368 }), 369 new web3._extend.Method({ 370 name: 'traceBlock', 371 call: 'debug_traceBlock', 372 params: 2, 373 inputFormatter: [null, null] 374 }), 375 new web3._extend.Method({ 376 name: 'traceBlockFromFile', 377 call: 'debug_traceBlockFromFile', 378 params: 2, 379 inputFormatter: [null, null] 380 }), 381 new web3._extend.Method({ 382 name: 'traceBadBlock', 383 call: 'debug_traceBadBlock', 384 params: 1, 385 inputFormatter: [null] 386 }), 387 new web3._extend.Method({ 388 name: 'standardTraceBadBlockToFile', 389 call: 'debug_standardTraceBadBlockToFile', 390 params: 2, 391 inputFormatter: [null, null] 392 }), 393 new web3._extend.Method({ 394 name: 'standardTraceBlockToFile', 395 call: 'debug_standardTraceBlockToFile', 396 params: 2, 397 inputFormatter: [null, null] 398 }), 399 new web3._extend.Method({ 400 name: 'traceBlockByNumber', 401 call: 'debug_traceBlockByNumber', 402 params: 2, 403 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, null] 404 }), 405 new web3._extend.Method({ 406 name: 'traceBlockByHash', 407 call: 'debug_traceBlockByHash', 408 params: 2, 409 inputFormatter: [null, null] 410 }), 411 new web3._extend.Method({ 412 name: 'traceTransaction', 413 call: 'debug_traceTransaction', 414 params: 2, 415 inputFormatter: [null, null] 416 }), 417 new web3._extend.Method({ 418 name: 'traceCall', 419 call: 'debug_traceCall', 420 params: 3, 421 inputFormatter: [null, null, null] 422 }), 423 new web3._extend.Method({ 424 name: 'preimage', 425 call: 'debug_preimage', 426 params: 1, 427 inputFormatter: [null] 428 }), 429 new web3._extend.Method({ 430 name: 'getBadBlocks', 431 call: 'debug_getBadBlocks', 432 params: 0, 433 }), 434 new web3._extend.Method({ 435 name: 'storageRangeAt', 436 call: 'debug_storageRangeAt', 437 params: 5, 438 }), 439 new web3._extend.Method({ 440 name: 'getModifiedAccountsByNumber', 441 call: 'debug_getModifiedAccountsByNumber', 442 params: 2, 443 inputFormatter: [null, null], 444 }), 445 new web3._extend.Method({ 446 name: 'getModifiedAccountsByHash', 447 call: 'debug_getModifiedAccountsByHash', 448 params: 2, 449 inputFormatter:[null, null], 450 }), 451 new web3._extend.Method({ 452 name: 'freezeClient', 453 call: 'debug_freezeClient', 454 params: 1, 455 }), 456 ], 457 properties: [] 458 }); 459 ` 460 461 const EthJs = ` 462 web3._extend({ 463 property: 'eth', 464 methods: [ 465 new web3._extend.Method({ 466 name: 'chainId', 467 call: 'eth_chainId', 468 params: 0 469 }), 470 new web3._extend.Method({ 471 name: 'sign', 472 call: 'eth_sign', 473 params: 2, 474 inputFormatter: [web3._extend.formatters.inputAddressFormatter, null] 475 }), 476 new web3._extend.Method({ 477 name: 'resend', 478 call: 'eth_resend', 479 params: 3, 480 inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal] 481 }), 482 new web3._extend.Method({ 483 name: 'signTransaction', 484 call: 'eth_signTransaction', 485 params: 1, 486 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 487 }), 488 new web3._extend.Method({ 489 name: 'estimateGas', 490 call: 'eth_estimateGas', 491 params: 2, 492 inputFormatter: [web3._extend.formatters.inputCallFormatter, web3._extend.formatters.inputBlockNumberFormatter], 493 outputFormatter: web3._extend.utils.toDecimal 494 }), 495 new web3._extend.Method({ 496 name: 'submitTransaction', 497 call: 'eth_submitTransaction', 498 params: 1, 499 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 500 }), 501 new web3._extend.Method({ 502 name: 'fillTransaction', 503 call: 'eth_fillTransaction', 504 params: 1, 505 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 506 }), 507 new web3._extend.Method({ 508 name: 'getHeaderByNumber', 509 call: 'eth_getHeaderByNumber', 510 params: 1, 511 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] 512 }), 513 new web3._extend.Method({ 514 name: 'getHeaderByHash', 515 call: 'eth_getHeaderByHash', 516 params: 1 517 }), 518 new web3._extend.Method({ 519 name: 'getBlockByNumber', 520 call: 'eth_getBlockByNumber', 521 params: 2, 522 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, function (val) { return !!val; }] 523 }), 524 new web3._extend.Method({ 525 name: 'getBlockByHash', 526 call: 'eth_getBlockByHash', 527 params: 2, 528 inputFormatter: [null, function (val) { return !!val; }] 529 }), 530 new web3._extend.Method({ 531 name: 'getRawTransaction', 532 call: 'eth_getRawTransactionByHash', 533 params: 1 534 }), 535 new web3._extend.Method({ 536 name: 'getRawTransactionFromBlock', 537 call: function(args) { 538 return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex'; 539 }, 540 params: 2, 541 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex] 542 }), 543 new web3._extend.Method({ 544 name: 'getProof', 545 call: 'eth_getProof', 546 params: 3, 547 inputFormatter: [web3._extend.formatters.inputAddressFormatter, null, web3._extend.formatters.inputBlockNumberFormatter] 548 }), 549 new web3._extend.Method({ 550 name: 'createAccessList', 551 call: 'eth_createAccessList', 552 params: 2, 553 inputFormatter: [null, web3._extend.formatters.inputBlockNumberFormatter], 554 }), 555 new web3._extend.Method({ 556 name: 'feeHistory', 557 call: 'eth_feeHistory', 558 params: 3, 559 inputFormatter: [null, web3._extend.formatters.inputBlockNumberFormatter, null] 560 }), 561 ], 562 properties: [ 563 new web3._extend.Property({ 564 name: 'pendingTransactions', 565 getter: 'eth_pendingTransactions', 566 outputFormatter: function(txs) { 567 var formatted = []; 568 for (var i = 0; i < txs.length; i++) { 569 formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i])); 570 formatted[i].blockHash = null; 571 } 572 return formatted; 573 } 574 }), 575 new web3._extend.Property({ 576 name: 'maxPriorityFeePerGas', 577 getter: 'eth_maxPriorityFeePerGas', 578 outputFormatter: web3._extend.utils.toBigNumber 579 }), 580 ] 581 }); 582 ` 583 584 const MinerJs = ` 585 web3._extend({ 586 property: 'miner', 587 methods: [ 588 new web3._extend.Method({ 589 name: 'start', 590 call: 'miner_start', 591 params: 1, 592 inputFormatter: [null] 593 }), 594 new web3._extend.Method({ 595 name: 'stop', 596 call: 'miner_stop' 597 }), 598 new web3._extend.Method({ 599 name: 'setEtherbase', 600 call: 'miner_setEtherbase', 601 params: 1, 602 inputFormatter: [web3._extend.formatters.inputAddressFormatter] 603 }), 604 new web3._extend.Method({ 605 name: 'setExtra', 606 call: 'miner_setExtra', 607 params: 1 608 }), 609 new web3._extend.Method({ 610 name: 'setGasPrice', 611 call: 'miner_setGasPrice', 612 params: 1, 613 inputFormatter: [web3._extend.utils.fromDecimal] 614 }), 615 new web3._extend.Method({ 616 name: 'setGasLimit', 617 call: 'miner_setGasLimit', 618 params: 1, 619 inputFormatter: [web3._extend.utils.fromDecimal] 620 }), 621 new web3._extend.Method({ 622 name: 'setRecommitInterval', 623 call: 'miner_setRecommitInterval', 624 params: 1, 625 }), 626 new web3._extend.Method({ 627 name: 'getHashrate', 628 call: 'miner_getHashrate' 629 }), 630 ], 631 properties: [] 632 }); 633 ` 634 635 const NetJs = ` 636 web3._extend({ 637 property: 'net', 638 methods: [], 639 properties: [ 640 new web3._extend.Property({ 641 name: 'version', 642 getter: 'net_version' 643 }), 644 ] 645 }); 646 ` 647 648 const PersonalJs = ` 649 web3._extend({ 650 property: 'personal', 651 methods: [ 652 new web3._extend.Method({ 653 name: 'importRawKey', 654 call: 'personal_importRawKey', 655 params: 2 656 }), 657 new web3._extend.Method({ 658 name: 'sign', 659 call: 'personal_sign', 660 params: 3, 661 inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null] 662 }), 663 new web3._extend.Method({ 664 name: 'ecRecover', 665 call: 'personal_ecRecover', 666 params: 2 667 }), 668 new web3._extend.Method({ 669 name: 'openWallet', 670 call: 'personal_openWallet', 671 params: 2 672 }), 673 new web3._extend.Method({ 674 name: 'deriveAccount', 675 call: 'personal_deriveAccount', 676 params: 3 677 }), 678 new web3._extend.Method({ 679 name: 'signTransaction', 680 call: 'personal_signTransaction', 681 params: 2, 682 inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null] 683 }), 684 new web3._extend.Method({ 685 name: 'unpair', 686 call: 'personal_unpair', 687 params: 2 688 }), 689 new web3._extend.Method({ 690 name: 'initializeWallet', 691 call: 'personal_initializeWallet', 692 params: 1 693 }) 694 ], 695 properties: [ 696 new web3._extend.Property({ 697 name: 'listWallets', 698 getter: 'personal_listWallets' 699 }), 700 ] 701 }) 702 ` 703 704 const RpcJs = ` 705 web3._extend({ 706 property: 'rpc', 707 methods: [], 708 properties: [ 709 new web3._extend.Property({ 710 name: 'modules', 711 getter: 'rpc_modules' 712 }), 713 ] 714 }); 715 ` 716 717 const TxpoolJs = ` 718 web3._extend({ 719 property: 'txpool', 720 methods: [], 721 properties: 722 [ 723 new web3._extend.Property({ 724 name: 'content', 725 getter: 'txpool_content' 726 }), 727 new web3._extend.Property({ 728 name: 'inspect', 729 getter: 'txpool_inspect' 730 }), 731 new web3._extend.Property({ 732 name: 'status', 733 getter: 'txpool_status', 734 outputFormatter: function(status) { 735 status.pending = web3._extend.utils.toDecimal(status.pending); 736 status.queued = web3._extend.utils.toDecimal(status.queued); 737 return status; 738 } 739 }), 740 new web3._extend.Method({ 741 name: 'contentFrom', 742 call: 'txpool_contentFrom', 743 params: 1, 744 }), 745 ] 746 }); 747 ` 748 749 const LESJs = ` 750 web3._extend({ 751 property: 'les', 752 methods: 753 [ 754 new web3._extend.Method({ 755 name: 'getCheckpoint', 756 call: 'les_getCheckpoint', 757 params: 1 758 }), 759 new web3._extend.Method({ 760 name: 'clientInfo', 761 call: 'les_clientInfo', 762 params: 1 763 }), 764 new web3._extend.Method({ 765 name: 'priorityClientInfo', 766 call: 'les_priorityClientInfo', 767 params: 3 768 }), 769 new web3._extend.Method({ 770 name: 'setClientParams', 771 call: 'les_setClientParams', 772 params: 2 773 }), 774 new web3._extend.Method({ 775 name: 'setDefaultParams', 776 call: 'les_setDefaultParams', 777 params: 1 778 }), 779 new web3._extend.Method({ 780 name: 'addBalance', 781 call: 'les_addBalance', 782 params: 2 783 }), 784 ], 785 properties: 786 [ 787 new web3._extend.Property({ 788 name: 'latestCheckpoint', 789 getter: 'les_latestCheckpoint' 790 }), 791 new web3._extend.Property({ 792 name: 'checkpointContractAddress', 793 getter: 'les_getCheckpointContractAddress' 794 }), 795 new web3._extend.Property({ 796 name: 'serverInfo', 797 getter: 'les_serverInfo' 798 }), 799 ] 800 }); 801 ` 802 803 const VfluxJs = ` 804 web3._extend({ 805 property: 'vflux', 806 methods: 807 [ 808 new web3._extend.Method({ 809 name: 'distribution', 810 call: 'vflux_distribution', 811 params: 2 812 }), 813 new web3._extend.Method({ 814 name: 'timeout', 815 call: 'vflux_timeout', 816 params: 2 817 }), 818 new web3._extend.Method({ 819 name: 'value', 820 call: 'vflux_value', 821 params: 2 822 }), 823 ], 824 properties: 825 [ 826 new web3._extend.Property({ 827 name: 'requestStats', 828 getter: 'vflux_requestStats' 829 }), 830 ] 831 }); 832 `