@geekberry/js-ethereum-sdk

JavaScript Ethereum Software Development Kit

Usage no npm install needed!

<script type="module">
  import geekberryJsEthereumSdk from 'https://cdn.skypack.dev/@geekberry/js-ethereum-sdk';
</script>

README

@geekberry/js-ethereum-sdk


BLOCK_NUMBER

blockNumber label

  • PENDING 'pending': the currently mined block (including pending transactions)
  • LATEST 'latest': the latest block (current head of the block chain)
  • EARLIEST 'earliest': earliest block number, same as 0.

TRANSACTION_GAS

number

gas use for pure transfer transaction

  • Examples
> CONST.TRANSACTION_GAS
 21000

Contract

Contract with all its methods and events defined in its abi.

Contract.prototype.constructor

contract "code" definition:

6080................6080.................a264.........0033...............................
| <-                     create contract transaction `data`                          -> |
| <- deploy code -> | <- runtime code -> | <- metadata -> | <- constructor arguments -> |
| <-                contract `bytecode`                -> |
                    | <-       code as `getCode`       -> |
  • Parameters
Name Type Required Default Description
options object true
options.abi array true The json interface for the contract to instantiate
options.address string false The address of the smart contract to call, can be added later using contract.address = '0x1234...'
options.bytecode string false The byte code of the contract, can be added later using contract.constructor.code = '0x1234...'
client Ethereum true Ethereum instance.
  • Returns

object

  • Examples
> const contract = client.Contract({ abi, bytecode, address });
   {
      abi: ContractABI { contract: [Circular *1] },
      address: '0x8e2f2e68eb75bb8b18caafe9607242d4748f8d98',
      constructor: [Function: bound call],
      name: [Function: bound call],
      'name()': [Function: bound call],
      '0x06fdde03': [Function: bound call],
      balanceOf: [Function: bound call],
      'balanceOf(address)': [Function: bound call],
      '0x70a08231': [Function: bound call],
      send: [Function: bound call],
      'send(address,uint256,bytes)': [Function: bound call],
      '0x9bd9bbc6': [Function: bound call],
      Transfer: [Function: bound call],
      'Transfer(address,address,uint256)': [Function: bound call],
      '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef': [Function: bound call]
   }
> contract.constructor.bytecode; // input code
   "0x6080..."
> const contract = client.Contract({
   address: '0x8e2f2e68eb75bb8b18caafe9607242d4748f8d98',
   abi: [
      {
        type: 'function',
        name: 'name',
        inputs: [],
        outputs: [{ type: 'string' }],
      },
      {
        type: 'function',
        name: 'balanceOf',
        inputs: [{ type: 'address' }],
        outputs: [{ type: 'uint256' }],
      },
      {
        name: 'send',
        type: 'function',
        inputs: [
          { type: 'address', name: 'recipient' },
          { type: 'uint256', name: 'amount' },
          { type: 'bytes', name: 'data' },
        ],
        outputs: [{ type: 'bool' }],
      },
    ]
   });
> contract.address
   "0x8e2f2e68eb75bb8b18caafe9607242d4748f8d98"
> await contract.name(); // call a method without parameter, get decoded return value.
   "FansCoin"
> await contract.name().call({ to: '0x8b8689c7f3014a4d86e4d1d0daaf74a47f5e0f27' }); // call a method with options
   "client USDT"
> await contract.balanceOf('0x19c742cec42b9e4eff3b84cdedcde2f58a36f44f'); // call a method with parameters, get decoded return value.
   10000000000000000000n
> transaction = await client.getTransactionByHash('0x2055f3287f1a6ce77d91f5dfdf7517a531b3a560fee1265f27dc1ff92314530b');
> contract.abi.decodeData(transaction.data)
   {
      name: 'send',
      fullName: 'send(address recipient, uint256 amount, bytes data)',
      type: 'send(address,uint256,bytes)',
      signature: '0x9bd9bbc6',
      array: [
        '0x80bb30efc5683758128b404fe5da03432eb16634',
        60000000000000000000n,
        <Buffer 1f 3c 6b 96 96 60 4c dc 3c e1 ca 27 7d 4c 69 a9 c2 77 0c 9f>
      ],
      object: {
        recipient: '0x80bb30efc5683758128b404fe5da03432eb16634',
        amount: 60000000000000000000n,
        data: <Buffer 1f 3c 6b 96 96 60 4c dc 3c e1 ca 27 7d 4c 69 a9 c2 77 0c 9f>
      }
    }
> receipt = await client.getTransactionReceipt('0x2055f3287f1a6ce77d91f5dfdf7517a531b3a560fee1265f27dc1ff92314530b');
> contract.abi.decodeLog(receipt.logs[1]);
   {
      name: 'Transfer',
      fullName: 'Transfer(address indexed from, address indexed to, uint256 value)',
      type: 'Transfer(address,address,uint256)',
      signature: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
      array: [
        '0x1f3c6b9696604cdc3ce1ca277d4c69a9c2770c9f',
        '0x80bb30efc5683758128b404fe5da03432eb16634',
        60000000000000000000n
      ],
      object: {
        from: '0x1f3c6b9696604cdc3ce1ca277d4c69a9c2770c9f',
        to: '0x80bb30efc5683758128b404fe5da03432eb16634',
        value: 60000000000000000000n
      }
    }

Ethereum

A sdk of ethereum.

Ethereum.prototype.constructor

  • Parameters
Name Type Required Default Description
options object false Provider constructor options.
options.defaultGasPrice string,number false The default gas price in drip to use for transactions.
options.url string false Url of node to connect.
options.timeout number false Request time out in ms
options.logger Object false Logger object with 'info' and 'error' method.
  • Examples
> const client = new Ethereum({
     url: 'http://localhost:8000',
     defaultGasPrice: 100,
     logger: console,
   });

Ethereum.prototype.provider

WebsocketProvider,HttpProvider,BaseProvider

Provider for rpc call

Ethereum.prototype.wallet

Wallet

Wallet for sendTransaction to get Account by from field

Ethereum.prototype.defaultGasPrice

number,string

Default gas price for following methods:

  • client.sendTransaction

Ethereum.prototype.Contract

A shout cut for new Contract(options, client);

  • Parameters
Name Type Required Default Description
options object true See Contract.constructor
  • Returns

Contract - A Contract instance

Ethereum.prototype.close

close connection.

  • Examples
> client.close();

Ethereum.prototype.getNetVersion

  • Returns

Promise.<string>

  • Examples
> await client.getNetVersion()
   "Mainnet"

Ethereum.prototype.getChainId

  • Returns

Promise.<number>

  • Examples
> await client.getChainId()
   42

Ethereum.prototype.getProtocolVersion

  • Returns

Promise.<number>

  • Examples
> await client.getProtocolVersion()
   65

Ethereum.prototype.getGasPrice

Returns the current price per gas in Wei.

  • Returns

Promise.<BigInt> Gas price in drip.

  • Examples
> await client.getGasPrice();
   1n

Ethereum.prototype.getBalance

Returns the balance of the account of given address.

  • Parameters
Name Type Required Default Description
address string true The address to get the balance of.
blockNumber string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber
  • Returns

Promise.<BigInt> The balance in Wei.

  • Examples
> await client.getBalance("0x1c1e72f0c37968557b3d85a3f32747792798bbde");
   824812401057514588670n

Ethereum.prototype.getTransactionCount

Returns the next nonce should be used by given address.

  • Parameters
Name Type Required Default Description
address string true The address to get the numbers of transactions from.
blockNumber string,number false See format.blockNumber
  • Returns

Promise.<number> The next nonce should be used by given address.

  • Examples
> await client.getTransactionCount("0x1c1e72f0c37968557b3d85a3f32747792798bbde");
   1449

Ethereum.prototype.getBlockNumber

Returns the block number of given parameter.

  • Returns

Promise.<number> integer of the current block number of given parameter.

  • Examples
> await client.getEpochNumber();
   443

Ethereum.prototype.getBlockByNumber

Returns information about a block by block number.

  • Parameters
Name Type Required Default Description
blockNumber string,number false See format.blockNumber
detail boolean false false If true it returns the full transaction objects, if false only the hashes of the transactions.
  • Returns

Promise.<(object|null)> See getBlockByHash

  • Examples
> await client.getBlockByNumber('latest', true);
   {
      author: '0x0010f94b296a852aaac52ea6c5ac72e03afd032d',
      difficulty: 131072n,
      extraData: '0xd5830105048650617269747986312e31352e31826c69',
      gasLimit: 4700000n,
      gasUsed: 0n,
      hash: '0xc06fce521f37996a72053c301484ae9746ae70ee848e0476780a9b915e16e749',
      logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
      miner: '0x0010f94b296a852aaac52ea6c5ac72e03afd032d',
      number: 4096,
      parentHash: '0xb4c94836f52fae22057225d7dd774b08c7e4673a137dee80cd05b6a90b4197ec',
      receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
      sealFields: [
        '0x84162e398f',
        '0xb8414614b11ef1c77413a7b5adff0fe944c0637873d43123146deb1c8f4c7e4c39795e4ce607b38cc4059523f1a2ef4c7c8e2c090567c3e0f7dc0a53ddc61f665fa500'
      ],
      sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
      signature: '4614b11ef1c77413a7b5adff0fe944c0637873d43123146deb1c8f4c7e4c39795e4ce607b38cc4059523f1a2ef4c7c8e2c090567c3e0f7dc0a53ddc61f665fa500',
      size: 565,
      stateRoot: '0xa483a83d65d35e0360c5559e04496c39bcbb134ecabde9e7684f2ffde3934b4b',
      step: '372128143',
      timestamp: 1488512571,
      totalDifficulty: 537001984n,
      transactions: [],
      transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
      uncles: []
   }

Ethereum.prototype.getBlockByHash

Returns information about a block by hash.

  • Parameters
Name Type Required Default Description
blockHash string true hash of a block.
detail boolean false false If true it returns the full transaction objects, if false only the hashes of the transactions.
  • Returns

Promise.<(object|null)> See getBlockByNumber

Ethereum.prototype.getTransactionByHash

Returns the information about a transaction requested by transaction hash.

  • Parameters
Name Type Required Default Description
transactionHash string true hash of a transaction
  • Returns

Promise.<(object|null)> transaction object, or null when no transaction was found:

  • Examples
> await client.getTransactionByHash('0x984087858bf39943a4deb7069383a90535563aed511248bdcb0982b3acfb65c7');
   {
      blockHash: '0x0f375d45b687dde0f319e0a915144a53361eccf29eacecb007ae1bf45e505ddb',
      blockNumber: 24792868,
      chainId: 42,
      condition: null,
      creates: '0x6c0aa308bd810863ae5c0348604642863a5896c9',
      from: '0xf09b8dda559292111af945e91717da39eef34ade',
      gas: 1636419n,
      gasPrice: 1000000000n,
      hash: '0x984087858bf39943a4deb7069383a90535563aed511248bdcb0982b3acfb65c7',
      input: '0x60806040523480156200001157600080fd5b506040518060400160405280600981526020016844654375732042544360b81b815250604051806040016040528060048152602001634542544360e01b815250816004908051906020019062000069929190620001e9565b5080516200007f906005906020840190620001e9565b50506006805461ff001960ff1990911660121716905550620000a3600033620000d5565b620000cf7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33620000d5565b62000285565b620000e18282620000e5565b5050565b6000828152602081815260409091206200010a91839062000be16200015e821b17901c565b15620000e1576200011a6200017e565b6001600160a01b0316816001600160a01b0316837f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a45050565b600062000175836001600160a01b03841662000182565b90505b92915050565b3390565b6000620001908383620001d1565b620001c85750815460018181018455600084815260208082209093018490558454848252828601909352604090209190915562000178565b50600062000178565b60009081526001919091016020526040902054151590565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200022c57805160ff19168380011785556200025c565b828001600101855582156200025c579182015b828111156200025c5782518255916020019190600101906200023f565b506200026a9291506200026e565b5090565b5b808211156200026a57600081556001016200026f565b6118c080620002956000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c806370a08231116100f9578063a457c2d711610097578063d539139311610071578063d53913931461051f578063d547741f14610527578063dd62ed3e14610553578063e63ab1e914610581576101a9565b8063a457c2d7146104aa578063a9059cbb146104d6578063ca15c87314610502576101a9565b80639010d07c116100d35780639010d07c1461042f57806391d148541461046e57806395d89b411461049a578063a217fddf146104a2576101a9565b806370a08231146103d557806379cc6790146103fb5780638456cb5914610427576101a9565b8063313ce567116101665780633f4ba83a116101405780633f4ba83a1461037c57806340c10f191461038457806342966c68146103b05780635c975abb146103cd576101a9565b8063313ce5671461030657806336568abe146103245780633950935114610350576101a9565b806306fdde03146101ae578063095ea7b31461022b57806318160ddd1461026b57806323b872dd14610285578063248a9ca3146102bb5780632f2ff15d146102d8575b600080fd5b6101b6610589565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101f05781810151838201526020016101d8565b50505050905090810190601f16801561021d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102576004803603604081101561024157600080fd5b506001600160a01b03813516906020013561061f565b604080519115158252519081900360200190f35b61027361063d565b60408051918252519081900360200190f35b6102576004803603606081101561029b57600080fd5b506001600160a01b03813581169160208101359091169060400135610643565b610273600480360360208110156102d157600080fd5b50356106ca565b610304600480360360408110156102ee57600080fd5b50803590602001356001600160a01b03166106df565b005b61030e61074b565b6040805160ff9092168252519081900360200190f35b6103046004803603604081101561033a57600080fd5b50803590602001356001600160a01b0316610754565b6102576004803603604081101561036657600080fd5b506001600160a01b0381351690602001356107b5565b610304610803565b6103046004803603604081101561039a57600080fd5b506001600160a01b038135169060200135610872565b610304600480360360208110156103c657600080fd5b50356108e1565b6102576108f5565b610273600480360360208110156103eb57600080fd5b50356001600160a01b0316610903565b6103046004803603604081101561041157600080fd5b506001600160a01b03813516906020013561091e565b610304610978565b6104526004803603604081101561044557600080fd5b50803590602001356109e5565b604080516001600160a01b039092168252519081900360200190f35b6102576004803603604081101561048457600080fd5b50803590602001356001600160a01b0316610a04565b6101b6610a1c565b610273610a7d565b610257600480360360408110156104c057600080fd5b506001600160a01b038135169060200135610a82565b610257600480360360408110156104ec57600080fd5b506001600160a01b038135169060200135610aea565b6102736004803603602081101561051857600080fd5b5035610afe565b610273610b15565b6103046004803603604081101561053d57600080fd5b50803590602001356001600160a01b0316610b39565b6102736004803603604081101561056957600080fd5b506001600160a01b0381358116916020013516610b92565b610273610bbd565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106155780601f106105ea57610100808354040283529160200191610615565b820191906000526020600020905b8154815290600101906020018083116105f857829003601f168201915b5050505050905090565b600061063361062c610bf6565b8484610bfa565b5060015b92915050565b60035490565b6000610650848484610ce6565b6106c08461065c610bf6565b6106bb856040518060600160405280602881526020016116ea602891396001600160a01b038a1660009081526002602052604081209061069a610bf6565b6001600160a01b031681526020810191909152604001600020549190610e43565b610bfa565b5060019392505050565b60009081526020819052604090206002015490565b600082815260208190526040902060020154610702906106fd610bf6565b610a04565b61073d5760405162461bcd60e51b815260040180806020018281038252602f8152602001806115e8602f913960400191505060405180910390fd5b6107478282610eda565b5050565b60065460ff1690565b61075c610bf6565b6001600160a01b0316816001600160a01b0316146107ab5760405162461bcd60e51b815260040180806020018281038252602f815260200180611832602f913960400191505060405180910390fd5b6107478282610f43565b60006106336107c2610bf6565b846106bb85600260006107d3610bf6565b6001600160a01b03908116825260208083019390935260409182016000908120918c168152925290205490610fac565b61082d7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33610a04565b6108685760405162461bcd60e51b81526004018080602001828103825260398152602001806116396039913960400191505060405180910390fd5b610870611006565b565b61089c7f9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a633610a04565b6108d75760405162461bcd60e51b81526004018080602001828103825260368152602001806117126036913960400191505060405180910390fd5b61074782826110a7565b6108f26108ec610bf6565b82611199565b50565b600654610100900460ff1690565b6001600160a01b031660009081526001602052604090205490565b6000610955826040518060600160405280602481526020016117486024913961094e86610949610bf6565b610b92565b9190610e43565b905061096983610963610bf6565b83610bfa565b6109738383611199565b505050565b6109a27f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33610a04565b6109dd5760405162461bcd60e51b81526004018080602001828103825260378152602001806117d66037913960400191505060405180910390fd5b610870611295565b60008281526020819052604081206109fd908361131a565b9392505050565b60008281526020819052604081206109fd9083611326565b60058054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106155780601f106105ea57610100808354040283529160200191610615565b600081565b6000610633610a8f610bf6565b846106bb8560405180606001604052806025815260200161180d6025913960026000610ab9610bf6565b6001600160a01b03908116825260208083019390935260409182016000908120918d16815292529020549190610e43565b6000610633610af7610bf6565b8484610ce6565b60008181526020819052604081206106379061133b565b7f9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a681565b600082815260208190526040902060020154610b57906106fd610bf6565b6107ab5760405162461bcd60e51b81526004018080602001828103825260308152602001806116ba6030913960400191505060405180910390fd5b6001600160a01b03918216600090815260026020908152604080832093909416825291909152205490565b7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a81565b60006109fd836001600160a01b038416611346565b3390565b6001600160a01b038316610c3f5760405162461bcd60e51b81526004018080602001828103825260248152602001806117b26024913960400191505060405180910390fd5b6001600160a01b038216610c845760405162461bcd60e51b81526004018080602001828103825260228152602001806116726022913960400191505060405180910390fd5b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316610d2b5760405162461bcd60e51b815260040180806020018281038252602581526020018061178d6025913960400191505060405180910390fd5b6001600160a01b038216610d705760405162461bcd60e51b81526004018080602001828103825260238152602001806115c56023913960400191505060405180910390fd5b610d7b838383611390565b610db881604051806060016040528060268152602001611694602691396001600160a01b0386166000908152600160205260409020549190610e43565b6001600160a01b038085166000908152600160205260408082209390935590841681522054610de79082610fac565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008184841115610ed25760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610e97578181015183820152602001610e7f565b50505050905090810190601f168015610ec45780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b6000828152602081905260409020610ef29082610be1565b1561074757610eff610bf6565b6001600160a01b0316816001600160a01b0316837f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a45050565b6000828152602081905260409020610f5b908261139b565b1561074757610f68610bf6565b6001600160a01b0316816001600160a01b0316837ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b60405160405180910390a45050565b6000828201838110156109fd576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b61100e6108f5565b611056576040805162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b604482015290519081900360640190fd5b6006805461ff00191690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa61108a610bf6565b604080516001600160a01b039092168252519081900360200190a1565b6001600160a01b038216611102576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300'... 3996 more characters,
      nonce: 1008,
      publicKey: '0x54dada743fa835164c693546b3f2e04129e8b8d0b03b2d68cbdac73bee3679a2950dd864834472c742ed1be598d22d691a32542426eab423b781051c99e0f945',
      r: '0x618082ac56778588961ca2d1383ffcb90d2db1692edcae8f83aea8414278e488',
      raw: '0xf91ba98203f0843b9aca008318f8438080b91b5560806040523480156200001157600080fd5b506040518060400160405280600981526020016844654375732042544360b81b815250604051806040016040528060048152602001634542544360e01b815250816004908051906020019062000069929190620001e9565b5080516200007f906005906020840190620001e9565b50506006805461ff001960ff1990911660121716905550620000a3600033620000d5565b620000cf7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33620000d5565b62000285565b620000e18282620000e5565b5050565b6000828152602081815260409091206200010a91839062000be16200015e821b17901c565b15620000e1576200011a6200017e565b6001600160a01b0316816001600160a01b0316837f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a45050565b600062000175836001600160a01b03841662000182565b90505b92915050565b3390565b6000620001908383620001d1565b620001c85750815460018181018455600084815260208082209093018490558454848252828601909352604090209190915562000178565b50600062000178565b60009081526001919091016020526040902054151590565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200022c57805160ff19168380011785556200025c565b828001600101855582156200025c579182015b828111156200025c5782518255916020019190600101906200023f565b506200026a9291506200026e565b5090565b5b808211156200026a57600081556001016200026f565b6118c080620002956000396000f3fe608060405234801561001057600080fd5b50600436106101a95760003560e01c806370a08231116100f9578063a457c2d711610097578063d539139311610071578063d53913931461051f578063d547741f14610527578063dd62ed3e14610553578063e63ab1e914610581576101a9565b8063a457c2d7146104aa578063a9059cbb146104d6578063ca15c87314610502576101a9565b80639010d07c116100d35780639010d07c1461042f57806391d148541461046e57806395d89b411461049a578063a217fddf146104a2576101a9565b806370a08231146103d557806379cc6790146103fb5780638456cb5914610427576101a9565b8063313ce567116101665780633f4ba83a116101405780633f4ba83a1461037c57806340c10f191461038457806342966c68146103b05780635c975abb146103cd576101a9565b8063313ce5671461030657806336568abe146103245780633950935114610350576101a9565b806306fdde03146101ae578063095ea7b31461022b57806318160ddd1461026b57806323b872dd14610285578063248a9ca3146102bb5780632f2ff15d146102d8575b600080fd5b6101b6610589565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101f05781810151838201526020016101d8565b50505050905090810190601f16801561021d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102576004803603604081101561024157600080fd5b506001600160a01b03813516906020013561061f565b604080519115158252519081900360200190f35b61027361063d565b60408051918252519081900360200190f35b6102576004803603606081101561029b57600080fd5b506001600160a01b03813581169160208101359091169060400135610643565b610273600480360360208110156102d157600080fd5b50356106ca565b610304600480360360408110156102ee57600080fd5b50803590602001356001600160a01b03166106df565b005b61030e61074b565b6040805160ff9092168252519081900360200190f35b6103046004803603604081101561033a57600080fd5b50803590602001356001600160a01b0316610754565b6102576004803603604081101561036657600080fd5b506001600160a01b0381351690602001356107b5565b610304610803565b6103046004803603604081101561039a57600080fd5b506001600160a01b038135169060200135610872565b610304600480360360208110156103c657600080fd5b50356108e1565b6102576108f5565b610273600480360360208110156103eb57600080fd5b50356001600160a01b0316610903565b6103046004803603604081101561041157600080fd5b506001600160a01b03813516906020013561091e565b610304610978565b6104526004803603604081101561044557600080fd5b50803590602001356109e5565b604080516001600160a01b039092168252519081900360200190f35b6102576004803603604081101561048457600080fd5b50803590602001356001600160a01b0316610a04565b6101b6610a1c565b610273610a7d565b610257600480360360408110156104c057600080fd5b506001600160a01b038135169060200135610a82565b610257600480360360408110156104ec57600080fd5b506001600160a01b038135169060200135610aea565b6102736004803603602081101561051857600080fd5b5035610afe565b610273610b15565b6103046004803603604081101561053d57600080fd5b50803590602001356001600160a01b0316610b39565b6102736004803603604081101561056957600080fd5b506001600160a01b0381358116916020013516610b92565b610273610bbd565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106155780601f106105ea57610100808354040283529160200191610615565b820191906000526020600020905b8154815290600101906020018083116105f857829003601f168201915b5050505050905090565b600061063361062c610bf6565b8484610bfa565b5060015b92915050565b60035490565b6000610650848484610ce6565b6106c08461065c610bf6565b6106bb856040518060600160405280602881526020016116ea602891396001600160a01b038a1660009081526002602052604081209061069a610bf6565b6001600160a01b031681526020810191909152604001600020549190610e43565b610bfa565b5060019392505050565b60009081526020819052604090206002015490565b600082815260208190526040902060020154610702906106fd610bf6565b610a04565b61073d5760405162461bcd60e51b815260040180806020018281038252602f8152602001806115e8602f913960400191505060405180910390fd5b6107478282610eda565b5050565b60065460ff1690565b61075c610bf6565b6001600160a01b0316816001600160a01b0316146107ab5760405162461bcd60e51b815260040180806020018281038252602f815260200180611832602f913960400191505060405180910390fd5b6107478282610f43565b60006106336107c2610bf6565b846106bb85600260006107d3610bf6565b6001600160a01b03908116825260208083019390935260409182016000908120918c168152925290205490610fac565b61082d7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33610a04565b6108685760405162461bcd60e51b81526004018080602001828103825260398152602001806116396039913960400191505060405180910390fd5b610870611006565b565b61089c7f9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a633610a04565b6108d75760405162461bcd60e51b81526004018080602001828103825260368152602001806117126036913960400191505060405180910390fd5b61074782826110a7565b6108f26108ec610bf6565b82611199565b50565b600654610100900460ff1690565b6001600160a01b031660009081526001602052604090205490565b6000610955826040518060600160405280602481526020016117486024913961094e86610949610bf6565b610b92565b9190610e43565b905061096983610963610bf6565b83610bfa565b6109738383611199565b505050565b6109a27f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a33610a04565b6109dd5760405162461bcd60e51b81526004018080602001828103825260378152602001806117d66037913960400191505060405180910390fd5b610870611295565b60008281526020819052604081206109fd908361131a565b9392505050565b60008281526020819052604081206109fd9083611326565b60058054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156106155780601f106105ea57610100808354040283529160200191610615565b600081565b6000610633610a8f610bf6565b846106bb8560405180606001604052806025815260200161180d6025913960026000610ab9610bf6565b6001600160a01b03908116825260208083019390935260409182016000908120918d16815292529020549190610e43565b6000610633610af7610bf6565b8484610ce6565b60008181526020819052604081206106379061133b565b7f9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a681565b600082815260208190526040902060020154610b57906106fd610bf6565b6107ab5760405162461bcd60e51b81526004018080602001828103825260308152602001806116ba6030913960400191505060405180910390fd5b6001600160a01b03918216600090815260026020908152604080832093909416825291909152205490565b7f65d7a28e3265b37a6474929f336521b332c1681b933f6cb9f3376673440d862a81565b60006109fd836001600160a01b038416611346565b3390565b6001600160a01b038316610c3f5760405162461bcd60e51b81526004018080602001828103825260248152602001806117b26024913960400191505060405180910390fd5b6001600160a01b038216610c845760405162461bcd60e51b81526004018080602001828103825260228152602001806116726022913960400191505060405180910390fd5b6001600160a01b03808416600081815260026020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316610d2b5760405162461bcd60e51b815260040180806020018281038252602581526020018061178d6025913960400191505060405180910390fd5b6001600160a01b038216610d705760405162461bcd60e51b81526004018080602001828103825260238152602001806115c56023913960400191505060405180910390fd5b610d7b838383611390565b610db881604051806060016040528060268152602001611694602691396001600160a01b0386166000908152600160205260409020549190610e43565b6001600160a01b038085166000908152600160205260408082209390935590841681522054610de79082610fac565b6001600160a01b0380841660008181526001602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008184841115610ed25760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610e97578181015183820152602001610e7f565b50505050905090810190601f168015610ec45780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b6000828152602081905260409020610ef29082610be1565b1561074757610eff610bf6565b6001600160a01b0316816001600160a01b0316837f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d60405160405180910390a45050565b6000828152602081905260409020610f5b908261139b565b1561074757610f68610bf6565b6001600160a01b0316816001600160a01b0316837ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b60405160405180910390a45050565b6000828201838110156109fd576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b61100e6108f5565b611056576040805162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b604482015290519081900360640190fd5b6006805461ff00191690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa61108a610bf6565b604080516001600160a01b039092168252519081900360200190a1565b6001600160a01b038216611102576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420'... 4170 more characters,
      s: '0x6445a2b853334205e8f5bd341e13cb1eeb2f66b99a8ac674e2801911ae6f5b4e',
      standardV: '0x0',
      to: null,
      transactionIndex: 78,
      v: 119,
      value: 0n
   }

Ethereum.prototype.getTransactionReceipt

Returns the information about a transaction receipt requested by transaction hash.

  • Parameters
Name Type Required Default Description
transactionHash string true Hash of a transaction
  • Returns

Promise.<(object|null)> A transaction receipt object, or null when no transaction was found or the transaction was not executed yet:

  • Examples
> await client.getTransactionReceipt('0x984087858bf39943a4deb7069383a90535563aed511248bdcb0982b3acfb65c7');
   {
      blockHash: '0x0f375d45b687dde0f319e0a915144a53361eccf29eacecb007ae1bf45e505ddb',
      blockNumber: 24792868,
      contractAddress: '0x6c0aa308bd810863ae5c0348604642863a5896c9',
      cumulativeGasUsed: 9196345n,
      from: '0xf09b8dda559292111af945e91717da39eef34ade',
      gasUsed: 1636419n,
      logs: [],
      logsBloom: '0x00000004000000000000000000000000000000000000000080000000000000000000000000000000000100000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000020000000000000008000800000000000000000000000000000000000000080000000000002000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000100002000000020000000000000000000000000000000000000000400000000000000000000000000',
      status: 1,
      to: null,
      transactionHash: '0x984087858bf39943a4deb7069383a90535563aed511248bdcb0982b3acfb65c7',
      transactionIndex: 78
   }

Ethereum.prototype.sendRawTransaction

Creates new message call transaction or a contract creation for signed transactions.

  • Parameters
Name Type Required Default Description
hex string,Buffer true The signed transaction data.
  • Returns

Promise.<PendingTransaction> The transaction hash, or the zero hash if the transaction is not yet available.

  • Examples
> await client.sendRawTransaction('0xf85f800382520894bbd9e9b...');
   "0xbe007c3eca92d01f3917f33ae983f40681182cf618defe75f490a65aac016914"

Ethereum.prototype.sendTransaction

  • Parameters
Name Type Required Default Description
options object true
  • Returns

Promise.<PendingTransaction>

Ethereum.prototype.getCode

Returns the code of given contract.

  • Parameters
Name Type Required Default Description
address string true Address to contract.
blockNumber string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber
  • Returns

Promise.<string> Byte code of contract, or 0x if the contract does not exist.

  • Examples
> await client.getCode('0xb385b84f08161f92a195953b980c8939679e906a');
   "0x6080604052348015600f57600080fd5b506004361060325760003560e01c806306661abd1460375780638..."

Ethereum.prototype.getStorageAt

Returns storage entries from a given contract.

  • Parameters
Name Type Required Default Description
address string true Address to contract.
position string true The given position.
blockNumber string,number false 'latest_state' See format.blockNumber
  • Returns

Promise.<(string|null)> Storage entry of given query, or null if the it does not exist.

  • Examples
> await client.getStorageAt('0x866aca87ff33a0ae05d2164b3d999a804f583222', '0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9')
   "0x000000000000000000000000000000000000000000000000000000000000162e"

Ethereum.prototype.call

Virtually call a contract, return the output data.

  • Parameters
Name Type Required Default Description
options object true See Transaction
blockNumber string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber
  • Returns

Promise.<string> The output data.

Ethereum.prototype.estimateGas

Virtually call a contract, return the estimate gas used and storage collateralized.

  • Parameters
Name Type Required Default Description
options object true See Transaction
blockNumber string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber
  • Returns

Promise.<BigInt> : estimate gas used

Ethereum.prototype.getLogs

Returns logs matching the filter provided.

  • Parameters
Name Type Required Default Description
options object false
options.fromBlock string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber. Search will be applied from this block number.
options.toBlock string,number false CONST.BLOCK_NUMBER.LATEST See format.blockNumber. Search will be applied up until (and including) this block number.
options.blockHash string false Array of up to 128 block hashes that the search will be applied to. This will override from/to block fields if it's not null
options.address string,Array.<string> false Search contract addresses. If null, match all. If specified, log must be produced by one of these addresses.
options.topics array false Search topics. Logs can have 4 topics: the function signature and up to 3 indexed event arguments. The elements of topics match the corresponding log topics. Example: ["0xA", null, ["0xB", "0xC"], null] matches logs with "0xA" as the 1st topic AND ("0xB" OR "0xC") as the 3rd topic. If null, match all.
  • Returns

Promise.<Array.<object>> Array of log, that the logs matching the filter provided:

  • Examples
> await client.getLogs({
      address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
      fromEpoch: 25053320,
      toEpoch: 25053330,
      topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'],
    });
   [
   {
        address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
        blockHash: '0x1b7be9c59838df1c0e86e2b2fa7f60e87cb5113e9a7ef2e7d91aff4dc4883be2',
        blockNumber: 25053327,
        data: '0x000000000000000000000000000000000000000000000000016345785d8a0000',
        logIndex: 10,
        removed: false,
        topics: [
          '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
          '0x00000000000000000000000091af345dbc5807975ad5d9face987c808a6649ba',
          '0x0000000000000000000000005388ce76b762530ec8df097e012462389ce8a70c'
        ],
        transactionHash: '0x220d8ef94f713d80c62abd3cf40f0640d8da0f2a4d5086eb846537e7de02a54e',
        transactionIndex: 2,
        transactionLogIndex: 1,
        type: 'mined'
     }
   ]

Ethereum.prototype.subscribe

Subscribe event by name and got id, and provider will emit event by id

Note: suggest use client.subscribeXXX to subscribe

  • Parameters
Name Type Required Default Description
name string true Subscription name
...args array true Subscription arguments
  • Returns

Promise.<string> Id of subscription

  • Examples
> client = new Ethereum({url:'ws://127.0.0.1:12535'})
> id = await client.subscribe('newHeads');
   "0x8fe7879a1681e9b9"
> client.provider.on(id, data=>console.log(data));
   {...}

Ethereum.prototype.subscribeNewHeads

The newHeads topic streams all new block headers participating in the consensus.

  • Returns

Promise.<Subscription> EventEmitter instance with the follow events:

  • 'data': see getBlockByHash
  • Examples
> subscription = await client.subscribeNewHeads()
> subscription.on('data', data=>console.log(data))
   {
      number: 24908378,
      hash: '0xb6b8641011e90f747c8addb8ae38c622348f07e852af50f688c2bcbd975b4402',
      parentHash: '0xf6ef56360d258f7ae1eb91c3dcee477c2022d1ce5e0780f9852b5a1ea953ae5b',
      sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
      logsBloom: '0x000000400000200001000080000000000000010000100800020800000010000001800000000020000000040000000000002000100000000000000000002000000000002000000000000400080000000000010200000802000000a400c000000400000801820000008004601000002a20200000000000800010002030002000400800040200008010240000000900000001804001008000000000100080080200021000000000002000000010000103000000000000000000000000a00000020000020002000000000000800000000000000800000000080000008020800020020010000040080008000001000000200008000000800000480000000000814000',
      transactionsRoot: '0xf499abdb0439a896543f0bc9ca5b6f110452b3d1b3fa672626e917065b52139b',
      stateRoot: '0x0225ba245d86c0f64bd4cd7690b6af2a41ccef62034353f22ec6f72e8dc87b58',
      receiptsRoot: '0xb61ec060497fbb6208bc17255553ced836d1e9a468154d2f1a415825a1e66e57',
      miner: '0x03801efb0efe2a25ede5dd3a003ae880c0292e4d',
      author: '0x03801efb0efe2a25ede5dd3a003ae880c0292e4d',
      difficulty: 340282366920938463463374607431768211454n,
      extraData: '0xdb830302058c4f70656e457468657265756d86312e34372e30826c69',
      size: 5925,
      gasLimit: 12499988n,
      gasUsed: 1853185n,
      timestamp: 1621323232,
    }

Ethereum.prototype.subscribeLogs

The logs topic streams all logs matching a certain filter, in order. In case of a pivot chain reorg (which might affect recent logs), a special revert message is sent. All logs received previously that belong to blocks larger than the one in this message should be considered invalid.

  • Parameters
Name Type Required Default Description
options object false
options.address string,Array.<string> false Search contract addresses. If null, match all. If specified, log must be produced by one of these addresses.
options.topics array false Search topics. Logs can have 4 topics: the function signature and up to 3 indexed event arguments. The elements of topics match the corresponding log topics. Example: ["0xA", null, ["0xB", "0xC"], null] matches logs with "0xA" as the 1st topic AND ("0xB" OR "0xC") as the 3rd topic. If null, match all.
  • Returns

Promise.<Subscription> EventEmitter instance with the follow events:

  • 'data': see getLogs
  • 'removed': see getLogs
  • Examples
> subscription = await client.subscribeLogs()
> subscription.on('data', data=>console.log(data))
   {
      address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
      blockHash: '0x1b7be9c59838df1c0e86e2b2fa7f60e87cb5113e9a7ef2e7d91aff4dc4883be2',
      blockNumber: 25053327,
      data: '0x000000000000000000000000000000000000000000000000016345785d8a0000',
      logIndex: 10,
      removed: false,
      topics: [
        '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
        '0x00000000000000000000000091af345dbc5807975ad5d9face987c808a6649ba',
        '0x0000000000000000000000005388ce76b762530ec8df097e012462389ce8a70c'
      ],
      transactionHash: '0x220d8ef94f713d80c62abd3cf40f0640d8da0f2a4d5086eb846537e7de02a54e',
      transactionIndex: 2,
      transactionLogIndex: 1,
      type: 'mined'
   }
> subscription.on('removed', data=>console.log(data))

Ethereum.prototype.subscribeNewPendingTransactions

  • Returns

Promise.<Subscription>

  • Examples
> subscription = await client.subscribeNewPendingTransactions()
> subscription.on('data', data=>console.log(data))
   "0x984087858bf39943a4deb7069383a90535563aed511248bdcb0982b3acfb65c7"

Ethereum.prototype.unsubscribe

Unsubscribe subscription.

  • Parameters
Name Type Required Default Description
id string,Subscription true Subscription id
  • Returns

Promise.<boolean> Is success

  • Examples
> id = await client.subscribe('newPendingTransactions');
> await client.unsubscribe(id);
   true
> await client.unsubscribe(id);
   false
> subscription = await client.subscribeLogs();
> await client.unsubscribe(subscription);
   true

BaseProvider

BaseProvider.prototype.constructor

  • Parameters
Name Type Required Default Description
options object false
options.url string true Full json rpc http url
options.timeout number false 60*1000 Request time out in ms
options.logger object false Logger with info and error
  • Returns

BaseProvider

BaseProvider.prototype.requestId

Gen a random json rpc id. It is used in call method, overwrite it to gen your own id.

  • Returns

string

BaseProvider.prototype.call

Call a json rpc method with params

  • Parameters
Name Type Required Default Description
method string true Json rpc method name.
...params array false Json rpc method params.
  • Returns

Promise.<*> Json rpc method return value.

  • Examples
> await provider.call('eth_blockNumber');
> await provider.call('eth_getBlockByHash', blockHash);

BaseProvider.prototype.batch

Batch call json rpc methods with params

  • Parameters
Name Type Required Default Description
array Array.<object> true Array of object with "method" and "params"
  • Returns

Promise.<Array>

  • Examples
> await provider.batch([
  { method: 'cfx_blockNumber' },
  { method: 'cfx_getBalance', params: ['0x0123456789012345678901234567890123456789'] },
  { method: 'InValidInput' },
])
   [ '0x3b734d', '0x22374d959c622f74728', RPCError: Method not found ]

HttpProvider

Http protocol json rpc provider.


providerFactory

  • Parameters
Name Type Required Default Description
options object true
options.url string true
  • Returns

WebsocketProvider,HttpProvider,BaseProvider

  • Examples
> providerFactory()
 BaseProvider {
    url: undefined,
    timeout: 300000,
    logger: { info: [Function: info], error: [Function: error] }
  }
> providerFactory({ url: 'http://localhost:12537' })
 HttpProvider {
    url: 'http://localhost:12537',
    timeout: 300000,
    logger: { info: [Function: info], error: [Function: error] }
  }
> providerFactory({
    url: 'http://main.confluxrpc.org',
    timeout: 60 * 60 * 1000,
    logger: console,
  }
 HttpProvider {
    url: 'http://main.confluxrpc.org',
    timeout: 3600000,
    logger: {...}
  }

WebSocketProvider

Websocket protocol json rpc provider.

WebSocketProvider.prototype.constructor

  • Parameters
Name Type Required Default Description
options object false See W3CWebSocket
options.url string true Full json rpc http url
options.timeout number false 60*1000 Request time out in ms
options.logger object false Logger with info and error
options.protocols Array.<string> false See w3
options.origin string false
options.headers object false
options.requestOptions object false
options.clientConfig object false See websocket/lib/WebSocketClient
options.clientConfig.maxReceivedFrameSize number false 0x100000 1MiB max frame size.
options.clientConfig.maxReceivedMessageSize number false 0x800000 8MiB max message size, only applicable if assembleFragments is true
options.clientConfig.closeTimeout number false 5000 The number of milliseconds to wait after sending a close frame for an acknowledgement to come back before giving up and just closing the socket.
  • Returns

WebSocketProvider


Subscription

Subscription event emitter


Transaction

Transaction.prototype.constructor

Create a transaction.

  • Parameters
Name Type Required Default Description
options object true
options.from string false The sender address.
options.nonce string,number false This allows to overwrite your own pending transactions that use the same nonce.
options.gasPrice string,number false The price of gas for this transaction in drip.
options.to string false The destination address of the message, left undefined for a contract-creation transaction.
options.value string,number false The value transferred for the transaction in drip, also the endowment if it’s a contract-creation transaction.
options.chainId string,number false The chain ID specified by the sender.
options.data string,Buffer false Either a ABI byte string containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code.
options.r string,Buffer false ECDSA signature r
options.s string,Buffer false ECDSA signature s
options.v number false ECDSA recovery id
  • Returns

Transaction

Transaction.prototype.hash

Getter of transaction hash include signature.

Note: calculate every time.

  • Returns

string,undefined If transaction has r,s,v return hex string, else return undefined.

Transaction.prototype.sign

Sign transaction and set 'r','s','v'.

  • Parameters
Name Type Required Default Description
privateKey string true Private key hex string.
  • Returns

Transaction

Transaction.prototype.recover

Recover public key from signed Transaction.

  • Returns

string

Transaction.prototype.encode <a id="Transa