github.com/aquanetwork/aquachain@v1.7.8/internal/web3ext/web3ext.go (about)

     1  // Copyright 2015 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // package web3ext contains aquachain specific web3.js extensions.
    18  package web3ext
    19  
    20  var Modules = map[string]string{
    21  	"admin":      Admin_JS,
    22  	"chequebook": Chequebook_JS,
    23  	"debug":      Debug_JS,
    24  	"aqua":       Aqua_JS,
    25  	"miner":      Miner_JS,
    26  	"net":        Net_JS,
    27  	"personal":   Personal_JS,
    28  	"rpc":        RPC_JS,
    29  	"shh":        Shh_JS,
    30  	"swarmfs":    SWARMFS_JS,
    31  	"txpool":     TxPool_JS,
    32  }
    33  
    34  const Chequebook_JS = `
    35  web3._extend({
    36  	property: 'chequebook',
    37  	methods: [
    38  		new web3._extend.Method({
    39  			name: 'deposit',
    40  			call: 'chequebook_deposit',
    41  			params: 1,
    42  			inputFormatter: [null]
    43  		}),
    44  		new web3._extend.Property({
    45  			name: 'balance',
    46  			getter: 'chequebook_balance',
    47  			outputFormatter: web3._extend.utils.toDecimal
    48  		}),
    49  		new web3._extend.Method({
    50  			name: 'cash',
    51  			call: 'chequebook_cash',
    52  			params: 1,
    53  			inputFormatter: [null]
    54  		}),
    55  		new web3._extend.Method({
    56  			name: 'issue',
    57  			call: 'chequebook_issue',
    58  			params: 2,
    59  			inputFormatter: [null, null]
    60  		}),
    61  	]
    62  });
    63  `
    64  
    65  const Admin_JS = `
    66  web3._extend({
    67  	property: 'admin',
    68  	methods: [
    69  		new web3._extend.Method({
    70  			name: 'addPeer',
    71  			call: 'admin_addPeer',
    72  			params: 1
    73  		}),
    74  		new web3._extend.Method({
    75  			name: 'removePeer',
    76  			call: 'admin_removePeer',
    77  			params: 1
    78  		}),
    79  		new web3._extend.Method({
    80  			name: 'exportChain',
    81  			call: 'admin_exportChain',
    82  			params: 1,
    83  			inputFormatter: [null]
    84  		}),
    85  		new web3._extend.Method({
    86  			name: 'exportState',
    87  			call: 'admin_exportState',
    88  			params: 1,
    89  			inputFormatter: [null]
    90  		}),
    91  		new web3._extend.Method({
    92  			name: 'exportRealloc',
    93  			call: 'admin_exportRealloc',
    94  			params: 1,
    95  			inputFormatter: [null]
    96  		}),
    97  		new web3._extend.Method({
    98  			name: 'importChain',
    99  			call: 'admin_importChain',
   100  			params: 1
   101  		}),
   102  		new web3._extend.Method({
   103  			name: 'supply',
   104  			call: 'admin_supply',
   105  			outputFormatter: web3._extend.utils.fromWei
   106  		}),
   107  		new web3._extend.Method({
   108  			name: 'richlist',
   109  			call: 'admin_getRichlist',
   110  			inputFormatter: [null],
   111  			params: 1
   112  		}),
   113  		new web3._extend.Method({
   114  			name: 'sleepBlocks',
   115  			call: 'admin_sleepBlocks',
   116  			params: 2
   117  		}),
   118  		new web3._extend.Method({
   119  			name: 'startRPC',
   120  			call: 'admin_startRPC',
   121  			params: 4,
   122  			inputFormatter: [null, null, null, null]
   123  		}),
   124  		new web3._extend.Method({
   125  			name: 'stopRPC',
   126  			call: 'admin_stopRPC'
   127  		}),
   128  		new web3._extend.Method({
   129  			name: 'startWS',
   130  			call: 'admin_startWS',
   131  			params: 4,
   132  			inputFormatter: [null, null, null, null]
   133  		}),
   134  		new web3._extend.Method({
   135  			name: 'stopWS',
   136  			call: 'admin_stopWS'
   137  		}),
   138  	],
   139  	properties: [
   140  		new web3._extend.Property({
   141  			name: 'nodeInfo',
   142  			getter: 'admin_nodeInfo'
   143  		}),
   144  		new web3._extend.Property({
   145  			name: 'peers',
   146  			getter: 'admin_peers'
   147  		}),
   148  		new web3._extend.Property({
   149  			name: 'datadir',
   150  			getter: 'admin_datadir'
   151  		}),
   152  	]
   153  });
   154  `
   155  
   156  const Debug_JS = `
   157  web3._extend({
   158  	property: 'debug',
   159  	methods: [
   160  		new web3._extend.Method({
   161  			name: 'printBlock',
   162  			call: 'debug_printBlock',
   163  			params: 1
   164  		}),
   165  		new web3._extend.Method({
   166  			name: 'getBlockRlp',
   167  			call: 'debug_getBlockRlp',
   168  			params: 1
   169  		}),
   170  		new web3._extend.Method({
   171  			name: 'setHead',
   172  			call: 'debug_setHead',
   173  			params: 1
   174  		}),
   175  		new web3._extend.Method({
   176  			name: 'seedHash',
   177  			call: 'debug_seedHash',
   178  			params: 1
   179  		}),
   180  		new web3._extend.Method({
   181  			name: 'dumpBlock',
   182  			call: 'debug_dumpBlock',
   183  			params: 1
   184  		}),
   185  		new web3._extend.Method({
   186  			name: 'chaindbProperty',
   187  			call: 'debug_chaindbProperty',
   188  			params: 1,
   189  			outputFormatter: console.log
   190  		}),
   191  		new web3._extend.Method({
   192  			name: 'chaindbCompact',
   193  			call: 'debug_chaindbCompact',
   194  		}),
   195  		new web3._extend.Method({
   196  			name: 'metrics',
   197  			call: 'debug_metrics',
   198  			params: 1
   199  		}),
   200  		new web3._extend.Method({
   201  			name: 'verbosity',
   202  			call: 'debug_verbosity',
   203  			params: 1
   204  		}),
   205  		new web3._extend.Method({
   206  			name: 'vmodule',
   207  			call: 'debug_vmodule',
   208  			params: 1
   209  		}),
   210  		new web3._extend.Method({
   211  			name: 'backtraceAt',
   212  			call: 'debug_backtraceAt',
   213  			params: 1,
   214  		}),
   215  		new web3._extend.Method({
   216  			name: 'stacks',
   217  			call: 'debug_stacks',
   218  			params: 0,
   219  			outputFormatter: console.log
   220  		}),
   221  		new web3._extend.Method({
   222  			name: 'freeOSMemory',
   223  			call: 'debug_freeOSMemory',
   224  			params: 0,
   225  		}),
   226  		new web3._extend.Method({
   227  			name: 'setGCPercent',
   228  			call: 'debug_setGCPercent',
   229  			params: 1,
   230  		}),
   231  		new web3._extend.Method({
   232  			name: 'memStats',
   233  			call: 'debug_memStats',
   234  			params: 0,
   235  		}),
   236  		new web3._extend.Method({
   237  			name: 'gcStats',
   238  			call: 'debug_gcStats',
   239  			params: 0,
   240  		}),
   241  		new web3._extend.Method({
   242  			name: 'cpuProfile',
   243  			call: 'debug_cpuProfile',
   244  			params: 2
   245  		}),
   246  		new web3._extend.Method({
   247  			name: 'startCPUProfile',
   248  			call: 'debug_startCPUProfile',
   249  			params: 1
   250  		}),
   251  		new web3._extend.Method({
   252  			name: 'stopCPUProfile',
   253  			call: 'debug_stopCPUProfile',
   254  			params: 0
   255  		}),
   256  		new web3._extend.Method({
   257  			name: 'goTrace',
   258  			call: 'debug_goTrace',
   259  			params: 2
   260  		}),
   261  		new web3._extend.Method({
   262  			name: 'startGoTrace',
   263  			call: 'debug_startGoTrace',
   264  			params: 1
   265  		}),
   266  		new web3._extend.Method({
   267  			name: 'stopGoTrace',
   268  			call: 'debug_stopGoTrace',
   269  			params: 0
   270  		}),
   271  		new web3._extend.Method({
   272  			name: 'blockProfile',
   273  			call: 'debug_blockProfile',
   274  			params: 2
   275  		}),
   276  		new web3._extend.Method({
   277  			name: 'setBlockProfileRate',
   278  			call: 'debug_setBlockProfileRate',
   279  			params: 1
   280  		}),
   281  		new web3._extend.Method({
   282  			name: 'writeBlockProfile',
   283  			call: 'debug_writeBlockProfile',
   284  			params: 1
   285  		}),
   286  		new web3._extend.Method({
   287  			name: 'writeMemProfile',
   288  			call: 'debug_writeMemProfile',
   289  			params: 1
   290  		}),
   291  		new web3._extend.Method({
   292  			name: 'traceBlock',
   293  			call: 'debug_traceBlock',
   294  			params: 2,
   295  			inputFormatter: [null, null]
   296  		}),
   297  		new web3._extend.Method({
   298  			name: 'traceBlockFromFile',
   299  			call: 'debug_traceBlockFromFile',
   300  			params: 2,
   301  			inputFormatter: [null, null]
   302  		}),
   303  		new web3._extend.Method({
   304  			name: 'traceBlockByNumber',
   305  			call: 'debug_traceBlockByNumber',
   306  			params: 2,
   307  			inputFormatter: [null, null]
   308  		}),
   309  		new web3._extend.Method({
   310  			name: 'traceBlockByHash',
   311  			call: 'debug_traceBlockByHash',
   312  			params: 2,
   313  			inputFormatter: [null, null]
   314  		}),
   315  		new web3._extend.Method({
   316  			name: 'traceTransaction',
   317  			call: 'debug_traceTransaction',
   318  			params: 2,
   319  			inputFormatter: [null, null]
   320  		}),
   321  		new web3._extend.Method({
   322  			name: 'preimage',
   323  			call: 'debug_preimage',
   324  			params: 1,
   325  			inputFormatter: [null]
   326  		}),
   327  		new web3._extend.Method({
   328  			name: 'getBadBlocks',
   329  			call: 'debug_getBadBlocks',
   330  			params: 0,
   331  		}),
   332  		new web3._extend.Method({
   333  			name: 'storageRangeAt',
   334  			call: 'debug_storageRangeAt',
   335  			params: 5,
   336  		}),
   337  		new web3._extend.Method({
   338  			name: 'getModifiedAccountsByNumber',
   339  			call: 'debug_getModifiedAccountsByNumber',
   340  			params: 2,
   341  			inputFormatter: [null, null],
   342  		}),
   343  		new web3._extend.Method({
   344  			name: 'getModifiedAccountsByHash',
   345  			call: 'debug_getModifiedAccountsByHash',
   346  			params: 2,
   347  			inputFormatter:[null, null],
   348  		}),
   349  	],
   350  	properties: []
   351  });
   352  `
   353  
   354  const Aqua_JS = `
   355  web3._extend({
   356  	property: 'aqua',
   357  	methods: [
   358  		new web3._extend.Method({
   359  			name: 'sign',
   360  			call: 'aqua_sign',
   361  			params: 2,
   362  			inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
   363  		}),
   364  		new web3._extend.Method({
   365  			name: 'resend',
   366  			call: 'aqua_resend',
   367  			params: 3,
   368  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
   369  		}),
   370  		new web3._extend.Method({
   371  			name: 'signTransaction',
   372  			call: 'aqua_signTransaction',
   373  			params: 1,
   374  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
   375  		}),
   376  		new web3._extend.Method({
   377  			name: 'submitTransaction',
   378  			call: 'aqua_submitTransaction',
   379  			params: 1,
   380  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
   381  		}),
   382  		new web3._extend.Method({
   383  			name: 'getRawTransaction',
   384  			call: 'aqua_getRawTransactionByHash',
   385  			params: 1
   386  		}),
   387  		new web3._extend.Method({
   388  			name: 'getRawTransactionFromBlock',
   389  			call: function(args) {
   390  				return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'aqua_getRawTransactionByBlockHashAndIndex' : 'aqua_getRawTransactionByBlockNumberAndIndex';
   391  			},
   392  			params: 2,
   393  			inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
   394  		}),
   395  	],
   396  	properties: [
   397  		new web3._extend.Property({
   398  			name: 'pendingTransactions',
   399  			getter: 'aqua_pendingTransactions',
   400  			outputFormatter: function(txs) {
   401  				var formatted = [];
   402  				for (var i = 0; i < txs.length; i++) {
   403  					formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
   404  					formatted[i].blockHash = null;
   405  				}
   406  				return formatted;
   407  			}
   408  		}),
   409  	]
   410  });
   411  `
   412  
   413  const Miner_JS = `
   414  web3._extend({
   415  	property: 'miner',
   416  	methods: [
   417  		new web3._extend.Method({
   418  			name: 'start',
   419  			call: 'miner_start',
   420  			params: 1,
   421  			inputFormatter: [null]
   422  		}),
   423  		new web3._extend.Method({
   424  			name: 'stop',
   425  			call: 'miner_stop'
   426  		}),
   427  		new web3._extend.Method({
   428  			name: 'setAquabase',
   429  			call: 'miner_setAquabase',
   430  			params: 1,
   431  			inputFormatter: [web3._extend.formatters.inputAddressFormatter]
   432  		}),
   433  		new web3._extend.Method({
   434  			name: 'setExtra',
   435  			call: 'miner_setExtra',
   436  			params: 1
   437  		}),
   438  		new web3._extend.Method({
   439  			name: 'setGasPrice',
   440  			call: 'miner_setGasPrice',
   441  			params: 1,
   442  			inputFormatter: [web3._extend.utils.fromDecimal]
   443  		}),
   444  		new web3._extend.Method({
   445  			name: 'getHashrate',
   446  			call: 'miner_getHashrate'
   447  		}),
   448  	],
   449  	properties: []
   450  });
   451  `
   452  
   453  const Net_JS = `
   454  web3._extend({
   455  	property: 'net',
   456  	methods: [],
   457  	properties: [
   458  		new web3._extend.Property({
   459  			name: 'version',
   460  			getter: 'net_version'
   461  		}),
   462  	]
   463  });
   464  `
   465  
   466  const Personal_JS = `
   467  web3._extend({
   468  	property: 'personal',
   469  	methods: [
   470  		new web3._extend.Method({
   471  			name: 'importRawKey',
   472  			call: 'personal_importRawKey',
   473  			params: 2
   474  		}),
   475  		new web3._extend.Method({
   476  			name: 'sign',
   477  			call: 'personal_sign',
   478  			params: 3,
   479  			inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
   480  		}),
   481  		new web3._extend.Method({
   482  			name: 'ecRecover',
   483  			call: 'personal_ecRecover',
   484  			params: 2
   485  		}),
   486  		new web3._extend.Method({
   487  			name: 'openWallet',
   488  			call: 'personal_openWallet',
   489  			params: 2
   490  		}),
   491  		new web3._extend.Method({
   492  			name: 'deriveAccount',
   493  			call: 'personal_deriveAccount',
   494  			params: 3
   495  		}),
   496  		new web3._extend.Method({
   497  			name: 'signTransaction',
   498  			call: 'personal_signTransaction',
   499  			params: 2,
   500  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
   501  		}),
   502  	],
   503  	properties: [
   504  		new web3._extend.Property({
   505  			name: 'listWallets',
   506  			getter: 'personal_listWallets'
   507  		}),
   508  	]
   509  })
   510  `
   511  
   512  const RPC_JS = `
   513  web3._extend({
   514  	property: 'rpc',
   515  	methods: [],
   516  	properties: [
   517  		new web3._extend.Property({
   518  			name: 'modules',
   519  			getter: 'rpc_modules'
   520  		}),
   521  	]
   522  });
   523  `
   524  
   525  const Shh_JS = `
   526  web3._extend({
   527  	property: 'shh',
   528  	methods: [
   529  	],
   530  	properties:
   531  	[
   532  		new web3._extend.Property({
   533  			name: 'version',
   534  			getter: 'shh_version',
   535  			outputFormatter: web3._extend.utils.toDecimal
   536  		}),
   537  		new web3._extend.Property({
   538  			name: 'info',
   539  			getter: 'shh_info'
   540  		}),
   541  	]
   542  });
   543  `
   544  
   545  const SWARMFS_JS = `
   546  web3._extend({
   547  	property: 'swarmfs',
   548  	methods:
   549  	[
   550  		new web3._extend.Method({
   551  			name: 'mount',
   552  			call: 'swarmfs_mount',
   553  			params: 2
   554  		}),
   555  		new web3._extend.Method({
   556  			name: 'unmount',
   557  			call: 'swarmfs_unmount',
   558  			params: 1
   559  		}),
   560  		new web3._extend.Method({
   561  			name: 'listmounts',
   562  			call: 'swarmfs_listmounts',
   563  			params: 0
   564  		}),
   565  	]
   566  });
   567  `
   568  
   569  const TxPool_JS = `
   570  web3._extend({
   571  	property: 'txpool',
   572  	methods: [],
   573  	properties:
   574  	[
   575  		new web3._extend.Property({
   576  			name: 'content',
   577  			getter: 'txpool_content'
   578  		}),
   579  		new web3._extend.Property({
   580  			name: 'inspect',
   581  			getter: 'txpool_inspect'
   582  		}),
   583  		new web3._extend.Property({
   584  			name: 'status',
   585  			getter: 'txpool_status',
   586  			outputFormatter: function(status) {
   587  				status.pending = web3._extend.utils.toDecimal(status.pending);
   588  				status.queued = web3._extend.utils.toDecimal(status.queued);
   589  				return status;
   590  			}
   591  		}),
   592  	]
   593  });
   594  `