github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/internal/web3ext/web3ext.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:40</date> 10 //</624342642808852480> 11 12 13 //包web3ext包含geth特定的web3.js扩展。 14 package web3ext 15 16 var Modules = map[string]string{ 17 "admin": Admin_JS, 18 "chequebook": Chequebook_JS, 19 "clique": Clique_JS, 20 "ethash": Ethash_JS, 21 "debug": Debug_JS, 22 "eth": Eth_JS, 23 "miner": Miner_JS, 24 "net": Net_JS, 25 "personal": Personal_JS, 26 "rpc": RPC_JS, 27 "shh": Shh_JS, 28 "swarmfs": SWARMFS_JS, 29 "txpool": TxPool_JS, 30 "dpos": Dpos_JS, 31 } 32 33 const Chequebook_JS = ` 34 web3._extend({ 35 property: 'chequebook', 36 methods: [ 37 new web3._extend.Method({ 38 name: 'deposit', 39 call: 'chequebook_deposit', 40 params: 1, 41 inputFormatter: [null] 42 }), 43 new web3._extend.Property({ 44 name: 'balance', 45 getter: 'chequebook_balance', 46 outputFormatter: web3._extend.utils.toDecimal 47 }), 48 new web3._extend.Method({ 49 name: 'cash', 50 call: 'chequebook_cash', 51 params: 1, 52 inputFormatter: [null] 53 }), 54 new web3._extend.Method({ 55 name: 'issue', 56 call: 'chequebook_issue', 57 params: 2, 58 inputFormatter: [null, null] 59 }), 60 ] 61 }); 62 ` 63 64 const Dpos_JS = ` 65 web3._extend({ 66 property: 'dpos', 67 methods: [ 68 new web3._extend.Method({ 69 name: 'getValidators', 70 call: 'dpos_getValidators', 71 params: 1, 72 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] 73 }), 74 new web3._extend.Method({ 75 name: 'getConfirmedBlockNumber', 76 call: 'dpos_getConfirmedBlockNumber', 77 params: 0, 78 outputFormatter: web3._extend.utils.toBigNumber 79 }), 80 ] 81 }); 82 ` 83 84 const Clique_JS = ` 85 web3._extend({ 86 property: 'clique', 87 methods: [ 88 new web3._extend.Method({ 89 name: 'getSnapshot', 90 call: 'clique_getSnapshot', 91 params: 1, 92 inputFormatter: [null] 93 }), 94 new web3._extend.Method({ 95 name: 'getSnapshotAtHash', 96 call: 'clique_getSnapshotAtHash', 97 params: 1 98 }), 99 new web3._extend.Method({ 100 name: 'getSigners', 101 call: 'clique_getSigners', 102 params: 1, 103 inputFormatter: [null] 104 }), 105 new web3._extend.Method({ 106 name: 'getSignersAtHash', 107 call: 'clique_getSignersAtHash', 108 params: 1 109 }), 110 new web3._extend.Method({ 111 name: 'propose', 112 call: 'clique_propose', 113 params: 2 114 }), 115 new web3._extend.Method({ 116 name: 'discard', 117 call: 'clique_discard', 118 params: 1 119 }), 120 ], 121 properties: [ 122 new web3._extend.Property({ 123 name: 'proposals', 124 getter: 'clique_proposals' 125 }), 126 ] 127 }); 128 ` 129 130 const Ethash_JS = ` 131 web3._extend({ 132 property: 'ethash', 133 methods: [ 134 new web3._extend.Method({ 135 name: 'getWork', 136 call: 'ethash_getWork', 137 params: 0 138 }), 139 new web3._extend.Method({ 140 name: 'getHashrate', 141 call: 'ethash_getHashrate', 142 params: 0 143 }), 144 new web3._extend.Method({ 145 name: 'submitWork', 146 call: 'ethash_submitWork', 147 params: 3, 148 }), 149 new web3._extend.Method({ 150 name: 'submitHashRate', 151 call: 'ethash_submitHashRate', 152 params: 2, 153 }), 154 ] 155 }); 156 ` 157 158 const Admin_JS = ` 159 web3._extend({ 160 property: 'admin', 161 methods: [ 162 new web3._extend.Method({ 163 name: 'addPeer', 164 call: 'admin_addPeer', 165 params: 1 166 }), 167 new web3._extend.Method({ 168 name: 'removePeer', 169 call: 'admin_removePeer', 170 params: 1 171 }), 172 new web3._extend.Method({ 173 name: 'addTrustedPeer', 174 call: 'admin_addTrustedPeer', 175 params: 1 176 }), 177 new web3._extend.Method({ 178 name: 'removeTrustedPeer', 179 call: 'admin_removeTrustedPeer', 180 params: 1 181 }), 182 new web3._extend.Method({ 183 name: 'exportChain', 184 call: 'admin_exportChain', 185 params: 1, 186 inputFormatter: [null] 187 }), 188 new web3._extend.Method({ 189 name: 'importChain', 190 call: 'admin_importChain', 191 params: 1 192 }), 193 new web3._extend.Method({ 194 name: 'sleepBlocks', 195 call: 'admin_sleepBlocks', 196 params: 2 197 }), 198 new web3._extend.Method({ 199 name: 'startRPC', 200 call: 'admin_startRPC', 201 params: 4, 202 inputFormatter: [null, null, null, null] 203 }), 204 new web3._extend.Method({ 205 name: 'stopRPC', 206 call: 'admin_stopRPC' 207 }), 208 new web3._extend.Method({ 209 name: 'startWS', 210 call: 'admin_startWS', 211 params: 4, 212 inputFormatter: [null, null, null, null] 213 }), 214 new web3._extend.Method({ 215 name: 'stopWS', 216 call: 'admin_stopWS' 217 }), 218 ], 219 properties: [ 220 new web3._extend.Property({ 221 name: 'nodeInfo', 222 getter: 'admin_nodeInfo' 223 }), 224 new web3._extend.Property({ 225 name: 'peers', 226 getter: 'admin_peers' 227 }), 228 new web3._extend.Property({ 229 name: 'datadir', 230 getter: 'admin_datadir' 231 }), 232 ] 233 }); 234 ` 235 236 const Debug_JS = ` 237 web3._extend({ 238 property: 'debug', 239 methods: [ 240 new web3._extend.Method({ 241 name: 'printBlock', 242 call: 'debug_printBlock', 243 params: 1 244 }), 245 new web3._extend.Method({ 246 name: 'getBlockRlp', 247 call: 'debug_getBlockRlp', 248 params: 1 249 }), 250 new web3._extend.Method({ 251 name: 'setHead', 252 call: 'debug_setHead', 253 params: 1 254 }), 255 new web3._extend.Method({ 256 name: 'seedHash', 257 call: 'debug_seedHash', 258 params: 1 259 }), 260 new web3._extend.Method({ 261 name: 'dumpBlock', 262 call: 'debug_dumpBlock', 263 params: 1 264 }), 265 new web3._extend.Method({ 266 name: 'chaindbProperty', 267 call: 'debug_chaindbProperty', 268 params: 1, 269 outputFormatter: console.log 270 }), 271 new web3._extend.Method({ 272 name: 'chaindbCompact', 273 call: 'debug_chaindbCompact', 274 }), 275 new web3._extend.Method({ 276 name: 'metrics', 277 call: 'debug_metrics', 278 params: 1 279 }), 280 new web3._extend.Method({ 281 name: 'verbosity', 282 call: 'debug_verbosity', 283 params: 1 284 }), 285 new web3._extend.Method({ 286 name: 'vmodule', 287 call: 'debug_vmodule', 288 params: 1 289 }), 290 new web3._extend.Method({ 291 name: 'backtraceAt', 292 call: 'debug_backtraceAt', 293 params: 1, 294 }), 295 new web3._extend.Method({ 296 name: 'stacks', 297 call: 'debug_stacks', 298 params: 0, 299 outputFormatter: console.log 300 }), 301 new web3._extend.Method({ 302 name: 'freeOSMemory', 303 call: 'debug_freeOSMemory', 304 params: 0, 305 }), 306 new web3._extend.Method({ 307 name: 'setGCPercent', 308 call: 'debug_setGCPercent', 309 params: 1, 310 }), 311 new web3._extend.Method({ 312 name: 'memStats', 313 call: 'debug_memStats', 314 params: 0, 315 }), 316 new web3._extend.Method({ 317 name: 'gcStats', 318 call: 'debug_gcStats', 319 params: 0, 320 }), 321 new web3._extend.Method({ 322 name: 'cpuProfile', 323 call: 'debug_cpuProfile', 324 params: 2 325 }), 326 new web3._extend.Method({ 327 name: 'startCPUProfile', 328 call: 'debug_startCPUProfile', 329 params: 1 330 }), 331 new web3._extend.Method({ 332 name: 'stopCPUProfile', 333 call: 'debug_stopCPUProfile', 334 params: 0 335 }), 336 new web3._extend.Method({ 337 name: 'goTrace', 338 call: 'debug_goTrace', 339 params: 2 340 }), 341 new web3._extend.Method({ 342 name: 'startGoTrace', 343 call: 'debug_startGoTrace', 344 params: 1 345 }), 346 new web3._extend.Method({ 347 name: 'stopGoTrace', 348 call: 'debug_stopGoTrace', 349 params: 0 350 }), 351 new web3._extend.Method({ 352 name: 'blockProfile', 353 call: 'debug_blockProfile', 354 params: 2 355 }), 356 new web3._extend.Method({ 357 name: 'setBlockProfileRate', 358 call: 'debug_setBlockProfileRate', 359 params: 1 360 }), 361 new web3._extend.Method({ 362 name: 'writeBlockProfile', 363 call: 'debug_writeBlockProfile', 364 params: 1 365 }), 366 new web3._extend.Method({ 367 name: 'mutexProfile', 368 call: 'debug_mutexProfile', 369 params: 2 370 }), 371 new web3._extend.Method({ 372 name: 'setMutexProfileFraction', 373 call: 'debug_setMutexProfileFraction', 374 params: 1 375 }), 376 new web3._extend.Method({ 377 name: 'writeMutexProfile', 378 call: 'debug_writeMutexProfile', 379 params: 1 380 }), 381 new web3._extend.Method({ 382 name: 'writeMemProfile', 383 call: 'debug_writeMemProfile', 384 params: 1 385 }), 386 new web3._extend.Method({ 387 name: 'traceBlock', 388 call: 'debug_traceBlock', 389 params: 2, 390 inputFormatter: [null, null] 391 }), 392 new web3._extend.Method({ 393 name: 'traceBlockFromFile', 394 call: 'debug_traceBlockFromFile', 395 params: 2, 396 inputFormatter: [null, null] 397 }), 398 new web3._extend.Method({ 399 name: 'traceBlockByNumber', 400 call: 'debug_traceBlockByNumber', 401 params: 2, 402 inputFormatter: [null, null] 403 }), 404 new web3._extend.Method({ 405 name: 'traceBlockByHash', 406 call: 'debug_traceBlockByHash', 407 params: 2, 408 inputFormatter: [null, null] 409 }), 410 new web3._extend.Method({ 411 name: 'traceTransaction', 412 call: 'debug_traceTransaction', 413 params: 2, 414 inputFormatter: [null, null] 415 }), 416 new web3._extend.Method({ 417 name: 'preimage', 418 call: 'debug_preimage', 419 params: 1, 420 inputFormatter: [null] 421 }), 422 new web3._extend.Method({ 423 name: 'getBadBlocks', 424 call: 'debug_getBadBlocks', 425 params: 0, 426 }), 427 new web3._extend.Method({ 428 name: 'storageRangeAt', 429 call: 'debug_storageRangeAt', 430 params: 5, 431 }), 432 new web3._extend.Method({ 433 name: 'getModifiedAccountsByNumber', 434 call: 'debug_getModifiedAccountsByNumber', 435 params: 2, 436 inputFormatter: [null, null], 437 }), 438 new web3._extend.Method({ 439 name: 'getModifiedAccountsByHash', 440 call: 'debug_getModifiedAccountsByHash', 441 params: 2, 442 inputFormatter:[null, null], 443 }), 444 ], 445 properties: [] 446 }); 447 ` 448 449 const Eth_JS = ` 450 web3._extend({ 451 property: 'eth', 452 methods: [ 453 new web3._extend.Method({ 454 name: 'sign', 455 call: 'eth_sign', 456 params: 2, 457 inputFormatter: [web3._extend.formatters.inputAddressFormatter, null] 458 }), 459 new web3._extend.Method({ 460 name: 'resend', 461 call: 'eth_resend', 462 params: 3, 463 inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal] 464 }), 465 new web3._extend.Method({ 466 name: 'signTransaction', 467 call: 'eth_signTransaction', 468 params: 1, 469 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 470 }), 471 new web3._extend.Method({ 472 name: 'submitTransaction', 473 call: 'eth_submitTransaction', 474 params: 1, 475 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 476 }), 477 new web3._extend.Method({ 478 name: 'getRawTransaction', 479 call: 'eth_getRawTransactionByHash', 480 params: 1 481 }), 482 new web3._extend.Method({ 483 name: 'getRawTransactionFromBlock', 484 call: function(args) { 485 return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex'; 486 }, 487 params: 2, 488 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex] 489 }), 490 ], 491 properties: [ 492 new web3._extend.Property({ 493 name: 'pendingTransactions', 494 getter: 'eth_pendingTransactions', 495 outputFormatter: function(txs) { 496 var formatted = []; 497 for (var i = 0; i < txs.length; i++) { 498 formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i])); 499 formatted[i].blockHash = null; 500 } 501 return formatted; 502 } 503 }), 504 ] 505 }); 506 ` 507 508 const Miner_JS = ` 509 web3._extend({ 510 property: 'miner', 511 methods: [ 512 new web3._extend.Method({ 513 name: 'start', 514 call: 'miner_start', 515 params: 1, 516 inputFormatter: [null] 517 }), 518 new web3._extend.Method({ 519 name: 'stop', 520 call: 'miner_stop' 521 }), 522 new web3._extend.Method({ 523 name: 'setValidator', 524 call: 'miner_setValidator', 525 params: 1, 526 inputFormatter: [web3._extend.formatters.inputAddressFormatter] 527 }), 528 new web3._extend.Method({ 529 name: 'setCoinbase', 530 call: 'miner_setCoinbase', 531 params: 1, 532 inputFormatter: [web3._extend.formatters.inputAddressFormatter] 533 }), 534 new web3._extend.Method({ 535 name: 'setExtra', 536 call: 'miner_setExtra', 537 params: 1 538 }), 539 new web3._extend.Method({ 540 name: 'setGasPrice', 541 call: 'miner_setGasPrice', 542 params: 1, 543 inputFormatter: [web3._extend.utils.fromDecimal] 544 }), 545 new web3._extend.Method({ 546 name: 'setRecommitInterval', 547 call: 'miner_setRecommitInterval', 548 params: 1, 549 }), 550 new web3._extend.Method({ 551 name: 'getHashrate', 552 call: 'miner_getHashrate' 553 }), 554 ], 555 properties: [] 556 }); 557 ` 558 559 const Net_JS = ` 560 web3._extend({ 561 property: 'net', 562 methods: [], 563 properties: [ 564 new web3._extend.Property({ 565 name: 'version', 566 getter: 'net_version' 567 }), 568 ] 569 }); 570 ` 571 572 const Personal_JS = ` 573 web3._extend({ 574 property: 'personal', 575 methods: [ 576 new web3._extend.Method({ 577 name: 'importRawKey', 578 call: 'personal_importRawKey', 579 params: 2 580 }), 581 new web3._extend.Method({ 582 name: 'sign', 583 call: 'personal_sign', 584 params: 3, 585 inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null] 586 }), 587 new web3._extend.Method({ 588 name: 'ecRecover', 589 call: 'personal_ecRecover', 590 params: 2 591 }), 592 new web3._extend.Method({ 593 name: 'openWallet', 594 call: 'personal_openWallet', 595 params: 2 596 }), 597 new web3._extend.Method({ 598 name: 'deriveAccount', 599 call: 'personal_deriveAccount', 600 params: 3 601 }), 602 new web3._extend.Method({ 603 name: 'signTransaction', 604 call: 'personal_signTransaction', 605 params: 2, 606 inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null] 607 }), 608 ], 609 properties: [ 610 new web3._extend.Property({ 611 name: 'listWallets', 612 getter: 'personal_listWallets' 613 }), 614 ] 615 }) 616 ` 617 618 const RPC_JS = ` 619 web3._extend({ 620 property: 'rpc', 621 methods: [], 622 properties: [ 623 new web3._extend.Property({ 624 name: 'modules', 625 getter: 'rpc_modules' 626 }), 627 ] 628 }); 629 ` 630 631 const Shh_JS = ` 632 web3._extend({ 633 property: 'shh', 634 methods: [ 635 ], 636 properties: 637 [ 638 new web3._extend.Property({ 639 name: 'version', 640 getter: 'shh_version', 641 outputFormatter: web3._extend.utils.toDecimal 642 }), 643 new web3._extend.Property({ 644 name: 'info', 645 getter: 'shh_info' 646 }), 647 ] 648 }); 649 ` 650 651 const SWARMFS_JS = ` 652 web3._extend({ 653 property: 'swarmfs', 654 methods: 655 [ 656 new web3._extend.Method({ 657 name: 'mount', 658 call: 'swarmfs_mount', 659 params: 2 660 }), 661 new web3._extend.Method({ 662 name: 'unmount', 663 call: 'swarmfs_unmount', 664 params: 1 665 }), 666 new web3._extend.Method({ 667 name: 'listmounts', 668 call: 'swarmfs_listmounts', 669 params: 0 670 }), 671 ] 672 }); 673 ` 674 675 const TxPool_JS = ` 676 web3._extend({ 677 property: 'txpool', 678 methods: [], 679 properties: 680 [ 681 new web3._extend.Property({ 682 name: 'content', 683 getter: 'txpool_content' 684 }), 685 new web3._extend.Property({ 686 name: 'inspect', 687 getter: 'txpool_inspect' 688 }), 689 new web3._extend.Property({ 690 name: 'status', 691 getter: 'txpool_status', 692 outputFormatter: function(status) { 693 status.pending = web3._extend.utils.toDecimal(status.pending); 694 status.queued = web3._extend.utils.toDecimal(status.queued); 695 return status; 696 } 697 }), 698 ] 699 }); 700 ` 701