1673503320
ethers
and web3
ethers
web3
npm install --save essential-eth # TypeScript included
# or yarn
yarn add essential-eth # TypeScript included
Browsers:
<!-- index.html -->
<script src="https://unpkg.com/essential-eth@0.6.2"></script>
import { etherToWei } from 'essential-eth';
// or in a non-import environment
const { etherToWei } = require('essential-eth');
arrayify
arrayify(value: number | BytesLike | Hexable, options: DataOptions): Uint8Array
View Example
import { arrayify } from 'essential-eth';
arrayify(1);
// Uint8Array(1) [ 1 ]
arrayify(0x1234);
// Uint8Array(2) [ 18, 52 ]
arrayify('0x1', { hexPad: 'right' });
// Uint8Array(1) [ 16 ]
computeAddress
computeAddress(key: string): string
View Example
import { computeAddress } from 'essential-eth';
computeAddress(
'0x0458eb591f407aef12936bd2989ca699cf5061de9c4964dd6eb6005fd8f580c407434447e813969a1be6e9954b002cad84dfc67a69e032b273e4695e7d0db2d952',
); // public key
// '0xA2902059a7BF992f1450BACD7357CCAa5cC8336a'
computeAddress(
'0x2f2c419acf4a1da8c1ebea75bb3fcfbd3ec2aa3bf0162901ccdc2f38b8f92427',
); // private key
// '0xA2902059a7BF992f1450BACD7357CCAa5cC8336a'
computePublicKey
computePublicKey(privKey: BytesLike): string
View Example
import { computePublicKey } from 'essential-eth';
computePublicKey(
'0xb27cc8dea0177d910110e8d3ec5480d56c723abf433529f4063f261ffdb9297c',
);
// '0x045cd0032015eecfde49f82f4e149d804e8ac6e3a0bface32e37c72a71ceac864fe84da7e8df84342f7b11dfb753c4d158f636142b46b29cf7f0f171ae0aa4fb87'
computePublicKey([
50, 102, 50, 99, 52, 49, 57, 97, 99, 102, 52, 97, 49, 100, 97, 56, 99, 49,
101, 98, 101, 97, 55, 53, 98, 98, 51, 102, 99, 102, 98, 100,
]);
// '0x04a9cea77eca949df84f661cee153426fb51f2294b9364b4fac240df57360b9b0ac9c99e4d7966491ab4c81f8c82e0cd24ec5759832ad4ab736d22c7d90b806ee8'
concat
concat(arrayOfBytesLike: Array<BytesLikeWithNumber>): Uint8Array
View Example
import { concat } from 'essential-eth';
concat([0, 1]);
// Uint8Array(2) [ 0, 1 ]
etherToGwei
etherToGwei(etherQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { etherToGwei } from 'essential-eth';
etherToGwei('1000').toString();
// '1000000000000'
etherToGwei(1000).toString();
// '1000000000000'
etherToGwei('1000').toNumber();
// 1000000000000
etherToGwei(1000).toNumber();
// 1000000000000
etherToWei
etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { etherToWei } from 'essential-eth';
etherToWei('1000').toString();
// '1000000000000000000000'
etherToWei(1000).toString();
// '1000000000000000000000'
etherToWei('1000').toNumber();
// 1000000000000000000000
etherToWei(1000).toNumber();
// 1000000000000000000000
gweiToEther
gweiToEther(gweiQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { gweiToEther } from 'essential-eth';
gweiToEther('1000000000000').toString();
// '1000'
gweiToEther(1000000000000).toString();
// '1000'
gweiToEther('1000000000000').toNumber();
// 1000
gweiToEther(1000000000000).toNumber();
// 1000
hashMessage
hashMessage(message: string | Bytes): string
View Example
import { hashMessage } from 'essential-eth';
hashMessage('Hello World');
// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'
hexConcat
hexConcat(items: Array<BytesLike>): string
View Example
import { hexConcat } from 'essential-eth';
hexConcat([[2, 4, 0, 1], 9, '0x2934', '0x3947']);
// '0x020400010929343947'
hexDataLength
hexDataLength(data: BytesLike): undefined
View Example
import { hexDataLength } from 'essential-eth';
hexDataLength([2, 4, 0, 1]);
// 4
hexDataLength('0x3925');
// 2
hexDataSlice
hexDataSlice(data: BytesLikeWithNumber, offset: number, endOffset: number): string
View Example
import { hexDataSlice } from 'essential-eth';
hexDataSlice([20, 6, 48], 0, 2);
// '0x1406'
hexStripZeros
hexStripZeros(value: BytesLike): string
View Example
import { hexStripZeros } from 'essential-eth';
hexStripZeros([0, 0, 0, 48]);
// '0x30'
hexValue
hexValue(value: number | bigint | BytesLike | Hexable): string
View Example
import { hexValue } from 'essential-eth';
hexValue(39);
// '0x27'
hexValue([9, 4, 19, 4]);
// '0x9041304'
hexZeroPad
hexZeroPad(value: BytesLikeWithNumber, length: number): string
View Example
import { hexZeroPad } from 'essential-eth';
hexZeroPad('0x60', 2);
// '0x0060'
hexZeroPad(0x60, 3);
// '0x000060'
hexZeroPad('12345', 1);
// Throws
hexlify
hexlify(value: number | bigint | BytesLike | Hexable, options: DataOptions): string
View Example
import { hexlify } from 'essential-eth';
hexlify(4);
// '0x04'
hexlify(14);
// '0x0e'
isAddress
isAddress(address: string): boolean
View Example
import { isAddress } from 'essential-eth';
isAddress('0xc0deaf6bd3f0c6574a6a625ef2f22f62a5150eab');
// true
isAddress('bad');
// false
// Does NOT support ENS.
isAddress('vitalik.eth');
// false
isBytes
isBytes(value: any): value
View Example
import { isBytes } from 'essential-eth';
isBytes([1, 2, 3]);
// true
isBytes(false);
// false
isBytes(new Uint8Array(1));
// true
isBytesLike
isBytesLike(value: any): value
View Example
import { isBytesLike } from 'essential-eth';
isBytesLike([1, 2, 3]);
// true
isBytesLike(false);
// false
isBytesLike(new Uint8Array(1));
// true
isHexString
isHexString(value: any, length: number): boolean
View Example
import { isHexString } from 'essential-eth';
isHexString('0x4924');
// true
isHexString('0x4924', 4);
// false
// length of 4 in bytes would mean a hex string with 8 characters
jsonRpcProvider
jsonRpcProvider(rpcUrl: string): JsonRpcProvider
View Example
import { jsonRpcProvider } from 'essential-eth';
jsonRpcProvider()
.getBlock('latest')
.then((block) => {
console.log(block.number);
});
// 14530496
keccak256
keccak256(data: BytesLike): string
View Example
import { keccak256 } from 'essential-eth';
keccak256('essential-eth');
// '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
keccak256('0x123');
// '0x5fa2358263196dbbf23d1ca7a509451f7a2f64c15837bfbb81298b1e3e24e4fa'
pack
pack(types: Array<string>, values: Array<any>): string
View Example
import { pack } from 'essential-eth';
const types = ['bool', 'string', 'uint64'];
const values = [true, 'text', 30];
pack(types, values);
// '0x0174657874000000000000001e'
solidityKeccak256
solidityKeccak256(types: Array<string>, values: Array<any>): string
View Example
import { solidityKeccak256 } from 'essential-eth';
const types = ['string', 'bool', 'uint32'];
const values = ['essential-eth is great', true, 14];
solidityKeccak256(types, values);
// '0xe4d4c8e809faac09d58f468f0aeab9474fe8965d554c6c0f868c433c3fd6acab'
const types = ['bytes4', 'uint32[5]'];
const values = [
[116, 101, 115, 116],
[5, 3, 4, 9, 18],
];
solidityKeccak256(types, values);
// '0x038707a887f09355dc545412b058e7ba8f3c74047050c7c5e5e52eec608053d9'
splitSignature
splitSignature(signature: SignatureLike): Signature
View Example
import { splitSignature } from 'essential-eth';
const signature = '0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee331b';
splitSignature(signature);
{
r: "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b39716",
s: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
_vs: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
recoveryParam: 0,
v: 27,
yParityAndS: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
compact: "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33"
}
stripZeros
stripZeros(value: BytesLike): Uint8Array
View Example
import { stripZeros } from 'essential-eth';
stripZeros('0x00002834');
// Uint8Array { [Iterator] 0: 40, 1: 52 }
// Equivalent to '0x2834'
tinyBig
tinyBig(value: string | number | TinyBig | Big): TinyBig
View Example
import { tinyBig } from 'essential-eth';
tinyBig(10).times(3).toNumber();
// 30
toChecksumAddress
toChecksumAddress(address: string): string
View Example
import { toChecksumAddress } from 'essential-eth';
toChecksumAddress('0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359');
// '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
Similar to "getAddress" in ethers.js
Similar to "toChecksumAddress" in web3.js
toUtf8Bytes
toUtf8Bytes(data: string): Uint8Array
View Example
import { toUtf8Bytes } from 'essential-eth';
toUtf8Bytes('essential-eth');
// Uint8Array { [Iterator] 0: 101, 1: 115, 2: 115, 3: 101, 4: 110, 5: 116, 6: 105, 7: 97, 8: 108, 9: 45, 10: 101, 11: 116, 12: 104 }
toUtf8Bytes('ethereum');
// Uint8Array { [Iterator] 0: 101, 1: 116, 2: 104, 3: 101, 4: 114, 5: 101, 6: 117, 7: 109 }
weiToEther
weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { weiToEther } from 'essential-eth';
weiToEther('1000000000000000000000').toString();
// '1000'
weiToEther(1000000000000000000000).toString();
// '1000'
weiToEther('1000000000000000000000').toNumber();
// 1000
weiToEther(1000000000000000000000).toNumber();
// 1000
zeroPad
zeroPad(value: BytesLike, length: number): Uint8Array
View Example
import { zeroPad } from 'essential-eth';
zeroPad('0x039284');
// Uint8Array { [Iterator] 0: 0, 1: 0, 2: 0, 3: 3, 4: 146, 5: 132 }
// Equivalent to 0x000000039284
zeroPad([39, 25, 103, 45], 5);
// Uint8Array { [Iterator] 0: 0, 1: 39, 2: 25, 3: 103, 4: 45 }
TinyBig
is just Big
but expands scientific notation on toNumber()
and toString()
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try POKT or Infura */);
// OR for very quick testing (limited to 10,000 requests)
const provider = new JsonRpcProvider();
Want a redundant provider that handles outages?
import { FallthroughProvider } from 'essential-eth';
// The FallthroughProvider handles falling through to the next valid URL.
// It's dynamic to never trust one URL again when it fails * until it has tried all other provided URLs
// The default timeout for a request is 8 seconds after which it moves to the next URL
const provider = new FallthroughProvider([
'https://bad.com',
'https://free-eth-node.com/api/eth',
]);
provider.getGasPrice().toNumber();
/*
39695942769
*/
call
provider.call(transaction: TransactionRequest, blockTag?: BlockTag): Promise<string>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.call({
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
data: '0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE',
});
// '0x0000000000000000000000000000000000000000000000000858898f93629000'
estimateGas
provider.estimateGas(transaction: TransactionRequest): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.estimateGas({
// Wrapped ETH address
to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
data: '0xd0e30db0',
value: etherToWei('1.0').toHexString(),
});
// { TinyBig: "27938" }
getBalance
provider.getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
// 28798127851528138
getBlock
provider.getBlock(timeFrame?: BlockTag, returnTransactionObjects?: boolean): Promise<BlockResponse>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBlock(14879862);
// {
// baseFeePerGas: { TinyBig: 39095728776 },
// difficulty: { TinyBig: 14321294455359973 },
// extraData: "0x486976656f6e2073672d6865617679",
// gasLimit: { TinyBig: 29970620 },
// gasUsed: { TinyBig: 20951384 },
// hash: "0x563b458ec3c4f87393b53f70bdddc0058497109b784d8cacd9247ddf267049ab",
// logsBloom:
// "0x9f38794fe80b521794df6efad8b0d2e9582f9ec3959a3f9384bda0fa371cfa5fac5af9d515c6bdf1ec325f5b5f7ebdd6a3a9fae17b38a86d4dc4b0971afc68d8086640550f4c156e6f923f4a1bb94fb0bed6cdcc474c5c64bfeff7a4a906f72b9a7b94004ee58efc53d63ac66961acd3a431b2d896cc9fd75f6072960bced45f770587caf130f57504decfcb63c6ca8fbc5bdbd749edd5a99a7375d2b81872289adb775fb3c928259f4be39c6d3f4d5b6217822979bb88c1f1fb62429b1b6d41cf4e3f77f9e1db3f5723108f1e5b1255dd734ad8cdb11e7ea22487c788e67c83777b6f395e504ca59c64f52245ee6de3804cf809e5caa4f0ea6a9aa9eb6ed801",
// miner: "0x1aD91ee08f21bE3dE0BA2ba6918E714dA6B45836",
// mixHash: "0x73cc9419bfb89c9d41c3a8c34ce56b5ebe468bdcf870258d2e77262275d580ec",
// nonce: "0x976f3f5d596ffb08",
// number: 14879862,
// parentHash: "0x95986ae14a71face8d9a6a379edd875b2e8bc73e4de0d9d460e7752bddb0f579",
// receiptsRoot: "0x8e6ba2fd9bee602b653dae6e3132f16538c2c5df24f1df8c000392053f73defa",
// sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
// size: { TinyBig: 134483 },
// stateRoot: "0xbf2bb67bd1c741f3d00904b8451d7c2cf4e3a2726f5a5884792ede2074747b85",
// timestamp: { TinyBig: 1654016186 },
// totalDifficulty: { TinyBig: 50478104614257705213748 },
// transactions: [
// "0xb3326a9149809603a2c28545e50e4f7d16e194bf5ee9764e0544603854c4a8d2",
// "0x8b42095f8d335404a4896b2817b8e5e3d86a5a87cb434a8eec295d5280a7f48e",
// "0x882f78fcb73f0f7ad0700bb0424a8b4beb366aaa93b88a3562c49a8d0ce4dcff",
// ...
// ],
// transactionsRoot: "0x5934902f3dcc263ec34f24318179bf6301f53f4834685792066026f3a4849d72",
// uncles: [],
// }
getBlockNumber
provider.getBlockNumber(): Promise<number>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBlockNumber();
// 1053312
getCode
provider.getCode(address: string, blockTag?: BlockTag): Promise<string>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await jsonRpcProvider().getCode(
'0xaC6095720221C79C6E7C638d260A2eFBC5D8d880',
'latest',
);
// '0x608060405234801561001057600080fd5b506004361061...'
getGasPrice
provider.getGasPrice(): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getGasPrice();
// 52493941856
getLogs
provider.getLogs(filter: Filter | FilterByBlockHash): Promise<Array<Log>>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
provider.getLogs({
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
],
fromBlock: 14825027,
toBlock: 14825039,
});
[
{
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
blockHash:
'0x8e0dfac2f704851960f866c8708b3bef2f66c0fee0329cf25ff0261b264ca6bc',
blockNumber: 14825029,
data: '0x000000000000000000000000000000000000000000000000005f862ee352a38a',
logIndex: 384,
removed: false,
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
'0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45',
],
transactionHash:
'0xbd49031be16f8fd1775f4e0fe79b408ffd8ae9c65b2827ee47e3238e3f51f4c0',
transactionIndex: 226,
},
];
getNetwork
provider.getNetwork(): Promise<Network>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
jsonRpcProvider('https://free-eth-node.com/api/eth').getNetwork();
// { chainId: 1, name: 'eth', ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' }
jsonRpcProvider('https://free-eth-node.com/api/MATIC').getNetwork();
// { chainId: 137, name: 'MATIC', ensAddress: null }
getTransaction
provider.getTransaction(transactionHash: string): Promise<TransactionResponse>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransaction(
'0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
);
// {
// accessList: [],
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// chainId: 1,
// from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
// gas: { TinyBig: 112163 },
// gasPrice: { TinyBig: 48592426858 },
// hash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// input: "0x83259f17000000000000000000000000000000000000000000...",
// maxFeePerGas: { TinyBig: 67681261618 },
// maxPriorityFeePerGas: { TinyBig: 1500000000 },
// nonce: { TinyBig: 129 },
// r: "0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc",
// s: "0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c",
// to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// transactionIndex: 29,
// type: 2,
// v: 0,
// value: { TinyBig: 0 },
// confirmations: 298140,
// }
getTransactionCount
provider.getTransactionCount(address: string, blockTag?: BlockTag): Promise<number>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
);
// 1060000
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
'latest',
);
// 1060000
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
14649390,
);
// 1053312
getTransactionReceipt
provider.getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransactionReceipt(
'0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
);
// {
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// contractAddress: null,
// cumulativeGasUsed: { TinyBig: 3067973 },
// effectiveGasPrice: { TinyBig: 48592426858 },
// from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
// gasUsed: { TinyBig: 112163 },
// logs: [
// {
// address: "0x0eDF9bc41Bbc1354c70e2107F80C42caE7FBBcA8",
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
// logIndex: 42,
// topics: [
// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
// "0x00000000000000000000000039b72d136ba3e4cef35f48cd09587ffab754dd8b",
// "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
// ],
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// },
// {
// address: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
// logIndex: 43,
// topics: [
// "0x34fcbac0073d7c3d388e51312faf357774904998eeb8fca628b9e6f65ee1cbf7",
// "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
// "0x0000000000000000000000000000000000000000000000000000000000000003",
// ],
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// },
// ],
// logsBloom: "0x00000000000000000000000000000...",
// status: 1,
// to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// type: 2,
// byzantium: true,
// confirmations: 298171,
// }
⚠️ Only read functions are currently supported. ⚠️
Contract
support is experimental, do not use this in production yet. (even though earni.fi does)Encoding support:
bool
bytes
address
Decoding support:
bool
address
uint256
bytes32
uint8
Assume all types outside the above types will break for now
import { Contract, jsonRpcProvider, JSONABI } from 'essential-eth';
// UNI airdrop contract
const contractAddress = '0x090D4613473dEE047c3f2706764f49E0821D256e';
const provider = jsonRpcProvider(/* RPC URL optional */);
const abi: JSONABI = [
{
inputs: [
{
internalType: 'uint256',
name: 'index',
type: 'uint256',
},
],
name: 'isClaimed',
outputs: [
{
internalType: 'bool',
name: '',
type: 'bool',
},
],
stateMutability: 'view',
type: 'function',
},
];
const contract = new Contract(contractAddress, abi, provider);
(async () => {
// prints boolean as to whether index 0 has claimed airdrop or not
console.log(await contract.isClaimed(0));
})();
contractFunctionName(args)
Any function on a contract. Returns are the same as ethers.js
, except that instead of BigNumber, essential-eth
always returns a TinyBig
🧪 This repo is under active development. The API is less-fully featured than web3
and ethers
. More functions added often!
👨🏻💻 Breaking changes will exist between minor versions until 1.0.0
(Versions go major.minor.patch
)
Throughout the documentation for essential-eth
, you may notice that some functions are compared to ethers.js
and web3.js
. The keywords identical
, similar
, and dissimilar
are used to help you migrate to essential-eth
. Here's a guide on what these keywords mean:
TinyBig
vs Bn.js
).identical
.similar
, and potentially learn how to alter your implementation to make essential-eth
work for you.Note: In web3.js
, almost every method or function can be passed a callback. essential-eth
, like ethers.js
, does not include this functionality and trusts that users will rely on the much more modern promise and async/await patterns.
In partnership with GitPOAP, Essential ETH wants to recognize all contributors for their contributions toward the growth of this library. Developers can validate their contributions on Github and showcase their GitPOAP as proof-of-work toward their Web3 identity.
GitPOAP is a contributor recognition platform built on top of the POAP Protcol (Proof of Attendence Protocol). Similar to a POAP, each GitPOAP represents a digital collectible. It serves as a badge of validation for the action taken by the individual. By serving as a bridge between Github and Web3, it allows repo maintainers to recognize contributors for their meaningful contributions. Learn more about GitPOAP, here.
Here are two steps that you can take to quailify for a GitPOAP:
Contribute to any open issue and submit a PR. Upon approval of PR merge, the GitPOAP will automatically be issued to the Github account.
Head to gitpoap.io and connect your GitHub account to mint!
Author: dawsbot
Source code: https://github.com/dawsbot/essential-eth
License:
1673503320
ethers
and web3
ethers
web3
npm install --save essential-eth # TypeScript included
# or yarn
yarn add essential-eth # TypeScript included
Browsers:
<!-- index.html -->
<script src="https://unpkg.com/essential-eth@0.6.2"></script>
import { etherToWei } from 'essential-eth';
// or in a non-import environment
const { etherToWei } = require('essential-eth');
arrayify
arrayify(value: number | BytesLike | Hexable, options: DataOptions): Uint8Array
View Example
import { arrayify } from 'essential-eth';
arrayify(1);
// Uint8Array(1) [ 1 ]
arrayify(0x1234);
// Uint8Array(2) [ 18, 52 ]
arrayify('0x1', { hexPad: 'right' });
// Uint8Array(1) [ 16 ]
computeAddress
computeAddress(key: string): string
View Example
import { computeAddress } from 'essential-eth';
computeAddress(
'0x0458eb591f407aef12936bd2989ca699cf5061de9c4964dd6eb6005fd8f580c407434447e813969a1be6e9954b002cad84dfc67a69e032b273e4695e7d0db2d952',
); // public key
// '0xA2902059a7BF992f1450BACD7357CCAa5cC8336a'
computeAddress(
'0x2f2c419acf4a1da8c1ebea75bb3fcfbd3ec2aa3bf0162901ccdc2f38b8f92427',
); // private key
// '0xA2902059a7BF992f1450BACD7357CCAa5cC8336a'
computePublicKey
computePublicKey(privKey: BytesLike): string
View Example
import { computePublicKey } from 'essential-eth';
computePublicKey(
'0xb27cc8dea0177d910110e8d3ec5480d56c723abf433529f4063f261ffdb9297c',
);
// '0x045cd0032015eecfde49f82f4e149d804e8ac6e3a0bface32e37c72a71ceac864fe84da7e8df84342f7b11dfb753c4d158f636142b46b29cf7f0f171ae0aa4fb87'
computePublicKey([
50, 102, 50, 99, 52, 49, 57, 97, 99, 102, 52, 97, 49, 100, 97, 56, 99, 49,
101, 98, 101, 97, 55, 53, 98, 98, 51, 102, 99, 102, 98, 100,
]);
// '0x04a9cea77eca949df84f661cee153426fb51f2294b9364b4fac240df57360b9b0ac9c99e4d7966491ab4c81f8c82e0cd24ec5759832ad4ab736d22c7d90b806ee8'
concat
concat(arrayOfBytesLike: Array<BytesLikeWithNumber>): Uint8Array
View Example
import { concat } from 'essential-eth';
concat([0, 1]);
// Uint8Array(2) [ 0, 1 ]
etherToGwei
etherToGwei(etherQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { etherToGwei } from 'essential-eth';
etherToGwei('1000').toString();
// '1000000000000'
etherToGwei(1000).toString();
// '1000000000000'
etherToGwei('1000').toNumber();
// 1000000000000
etherToGwei(1000).toNumber();
// 1000000000000
etherToWei
etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { etherToWei } from 'essential-eth';
etherToWei('1000').toString();
// '1000000000000000000000'
etherToWei(1000).toString();
// '1000000000000000000000'
etherToWei('1000').toNumber();
// 1000000000000000000000
etherToWei(1000).toNumber();
// 1000000000000000000000
gweiToEther
gweiToEther(gweiQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { gweiToEther } from 'essential-eth';
gweiToEther('1000000000000').toString();
// '1000'
gweiToEther(1000000000000).toString();
// '1000'
gweiToEther('1000000000000').toNumber();
// 1000
gweiToEther(1000000000000).toNumber();
// 1000
hashMessage
hashMessage(message: string | Bytes): string
View Example
import { hashMessage } from 'essential-eth';
hashMessage('Hello World');
// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'
hexConcat
hexConcat(items: Array<BytesLike>): string
View Example
import { hexConcat } from 'essential-eth';
hexConcat([[2, 4, 0, 1], 9, '0x2934', '0x3947']);
// '0x020400010929343947'
hexDataLength
hexDataLength(data: BytesLike): undefined
View Example
import { hexDataLength } from 'essential-eth';
hexDataLength([2, 4, 0, 1]);
// 4
hexDataLength('0x3925');
// 2
hexDataSlice
hexDataSlice(data: BytesLikeWithNumber, offset: number, endOffset: number): string
View Example
import { hexDataSlice } from 'essential-eth';
hexDataSlice([20, 6, 48], 0, 2);
// '0x1406'
hexStripZeros
hexStripZeros(value: BytesLike): string
View Example
import { hexStripZeros } from 'essential-eth';
hexStripZeros([0, 0, 0, 48]);
// '0x30'
hexValue
hexValue(value: number | bigint | BytesLike | Hexable): string
View Example
import { hexValue } from 'essential-eth';
hexValue(39);
// '0x27'
hexValue([9, 4, 19, 4]);
// '0x9041304'
hexZeroPad
hexZeroPad(value: BytesLikeWithNumber, length: number): string
View Example
import { hexZeroPad } from 'essential-eth';
hexZeroPad('0x60', 2);
// '0x0060'
hexZeroPad(0x60, 3);
// '0x000060'
hexZeroPad('12345', 1);
// Throws
hexlify
hexlify(value: number | bigint | BytesLike | Hexable, options: DataOptions): string
View Example
import { hexlify } from 'essential-eth';
hexlify(4);
// '0x04'
hexlify(14);
// '0x0e'
isAddress
isAddress(address: string): boolean
View Example
import { isAddress } from 'essential-eth';
isAddress('0xc0deaf6bd3f0c6574a6a625ef2f22f62a5150eab');
// true
isAddress('bad');
// false
// Does NOT support ENS.
isAddress('vitalik.eth');
// false
isBytes
isBytes(value: any): value
View Example
import { isBytes } from 'essential-eth';
isBytes([1, 2, 3]);
// true
isBytes(false);
// false
isBytes(new Uint8Array(1));
// true
isBytesLike
isBytesLike(value: any): value
View Example
import { isBytesLike } from 'essential-eth';
isBytesLike([1, 2, 3]);
// true
isBytesLike(false);
// false
isBytesLike(new Uint8Array(1));
// true
isHexString
isHexString(value: any, length: number): boolean
View Example
import { isHexString } from 'essential-eth';
isHexString('0x4924');
// true
isHexString('0x4924', 4);
// false
// length of 4 in bytes would mean a hex string with 8 characters
jsonRpcProvider
jsonRpcProvider(rpcUrl: string): JsonRpcProvider
View Example
import { jsonRpcProvider } from 'essential-eth';
jsonRpcProvider()
.getBlock('latest')
.then((block) => {
console.log(block.number);
});
// 14530496
keccak256
keccak256(data: BytesLike): string
View Example
import { keccak256 } from 'essential-eth';
keccak256('essential-eth');
// '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
keccak256('0x123');
// '0x5fa2358263196dbbf23d1ca7a509451f7a2f64c15837bfbb81298b1e3e24e4fa'
pack
pack(types: Array<string>, values: Array<any>): string
View Example
import { pack } from 'essential-eth';
const types = ['bool', 'string', 'uint64'];
const values = [true, 'text', 30];
pack(types, values);
// '0x0174657874000000000000001e'
solidityKeccak256
solidityKeccak256(types: Array<string>, values: Array<any>): string
View Example
import { solidityKeccak256 } from 'essential-eth';
const types = ['string', 'bool', 'uint32'];
const values = ['essential-eth is great', true, 14];
solidityKeccak256(types, values);
// '0xe4d4c8e809faac09d58f468f0aeab9474fe8965d554c6c0f868c433c3fd6acab'
const types = ['bytes4', 'uint32[5]'];
const values = [
[116, 101, 115, 116],
[5, 3, 4, 9, 18],
];
solidityKeccak256(types, values);
// '0x038707a887f09355dc545412b058e7ba8f3c74047050c7c5e5e52eec608053d9'
splitSignature
splitSignature(signature: SignatureLike): Signature
View Example
import { splitSignature } from 'essential-eth';
const signature = '0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee331b';
splitSignature(signature);
{
r: "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b39716",
s: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
_vs: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
recoveryParam: 0,
v: 27,
yParityAndS: "0x47252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33",
compact: "0x60bc4ed91f2021aefe7045f3f77bd12f87eb733aee24bd1965343b3c27b3971647252185b7d2abb411b01b5d1ac4ab41ea486df1e9b396758c1aec6c1b6eee33"
}
stripZeros
stripZeros(value: BytesLike): Uint8Array
View Example
import { stripZeros } from 'essential-eth';
stripZeros('0x00002834');
// Uint8Array { [Iterator] 0: 40, 1: 52 }
// Equivalent to '0x2834'
tinyBig
tinyBig(value: string | number | TinyBig | Big): TinyBig
View Example
import { tinyBig } from 'essential-eth';
tinyBig(10).times(3).toNumber();
// 30
toChecksumAddress
toChecksumAddress(address: string): string
View Example
import { toChecksumAddress } from 'essential-eth';
toChecksumAddress('0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359');
// '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
Similar to "getAddress" in ethers.js
Similar to "toChecksumAddress" in web3.js
toUtf8Bytes
toUtf8Bytes(data: string): Uint8Array
View Example
import { toUtf8Bytes } from 'essential-eth';
toUtf8Bytes('essential-eth');
// Uint8Array { [Iterator] 0: 101, 1: 115, 2: 115, 3: 101, 4: 110, 5: 116, 6: 105, 7: 97, 8: 108, 9: 45, 10: 101, 11: 116, 12: 104 }
toUtf8Bytes('ethereum');
// Uint8Array { [Iterator] 0: 101, 1: 116, 2: 104, 3: 101, 4: 114, 5: 101, 6: 117, 7: 109 }
weiToEther
weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig
View Example
import { weiToEther } from 'essential-eth';
weiToEther('1000000000000000000000').toString();
// '1000'
weiToEther(1000000000000000000000).toString();
// '1000'
weiToEther('1000000000000000000000').toNumber();
// 1000
weiToEther(1000000000000000000000).toNumber();
// 1000
zeroPad
zeroPad(value: BytesLike, length: number): Uint8Array
View Example
import { zeroPad } from 'essential-eth';
zeroPad('0x039284');
// Uint8Array { [Iterator] 0: 0, 1: 0, 2: 0, 3: 3, 4: 146, 5: 132 }
// Equivalent to 0x000000039284
zeroPad([39, 25, 103, 45], 5);
// Uint8Array { [Iterator] 0: 0, 1: 39, 2: 25, 3: 103, 4: 45 }
TinyBig
is just Big
but expands scientific notation on toNumber()
and toString()
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try POKT or Infura */);
// OR for very quick testing (limited to 10,000 requests)
const provider = new JsonRpcProvider();
Want a redundant provider that handles outages?
import { FallthroughProvider } from 'essential-eth';
// The FallthroughProvider handles falling through to the next valid URL.
// It's dynamic to never trust one URL again when it fails * until it has tried all other provided URLs
// The default timeout for a request is 8 seconds after which it moves to the next URL
const provider = new FallthroughProvider([
'https://bad.com',
'https://free-eth-node.com/api/eth',
]);
provider.getGasPrice().toNumber();
/*
39695942769
*/
call
provider.call(transaction: TransactionRequest, blockTag?: BlockTag): Promise<string>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.call({
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
data: '0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE',
});
// '0x0000000000000000000000000000000000000000000000000858898f93629000'
estimateGas
provider.estimateGas(transaction: TransactionRequest): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.estimateGas({
// Wrapped ETH address
to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
data: '0xd0e30db0',
value: etherToWei('1.0').toHexString(),
});
// { TinyBig: "27938" }
getBalance
provider.getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
// 28798127851528138
getBlock
provider.getBlock(timeFrame?: BlockTag, returnTransactionObjects?: boolean): Promise<BlockResponse>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBlock(14879862);
// {
// baseFeePerGas: { TinyBig: 39095728776 },
// difficulty: { TinyBig: 14321294455359973 },
// extraData: "0x486976656f6e2073672d6865617679",
// gasLimit: { TinyBig: 29970620 },
// gasUsed: { TinyBig: 20951384 },
// hash: "0x563b458ec3c4f87393b53f70bdddc0058497109b784d8cacd9247ddf267049ab",
// logsBloom:
// "0x9f38794fe80b521794df6efad8b0d2e9582f9ec3959a3f9384bda0fa371cfa5fac5af9d515c6bdf1ec325f5b5f7ebdd6a3a9fae17b38a86d4dc4b0971afc68d8086640550f4c156e6f923f4a1bb94fb0bed6cdcc474c5c64bfeff7a4a906f72b9a7b94004ee58efc53d63ac66961acd3a431b2d896cc9fd75f6072960bced45f770587caf130f57504decfcb63c6ca8fbc5bdbd749edd5a99a7375d2b81872289adb775fb3c928259f4be39c6d3f4d5b6217822979bb88c1f1fb62429b1b6d41cf4e3f77f9e1db3f5723108f1e5b1255dd734ad8cdb11e7ea22487c788e67c83777b6f395e504ca59c64f52245ee6de3804cf809e5caa4f0ea6a9aa9eb6ed801",
// miner: "0x1aD91ee08f21bE3dE0BA2ba6918E714dA6B45836",
// mixHash: "0x73cc9419bfb89c9d41c3a8c34ce56b5ebe468bdcf870258d2e77262275d580ec",
// nonce: "0x976f3f5d596ffb08",
// number: 14879862,
// parentHash: "0x95986ae14a71face8d9a6a379edd875b2e8bc73e4de0d9d460e7752bddb0f579",
// receiptsRoot: "0x8e6ba2fd9bee602b653dae6e3132f16538c2c5df24f1df8c000392053f73defa",
// sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
// size: { TinyBig: 134483 },
// stateRoot: "0xbf2bb67bd1c741f3d00904b8451d7c2cf4e3a2726f5a5884792ede2074747b85",
// timestamp: { TinyBig: 1654016186 },
// totalDifficulty: { TinyBig: 50478104614257705213748 },
// transactions: [
// "0xb3326a9149809603a2c28545e50e4f7d16e194bf5ee9764e0544603854c4a8d2",
// "0x8b42095f8d335404a4896b2817b8e5e3d86a5a87cb434a8eec295d5280a7f48e",
// "0x882f78fcb73f0f7ad0700bb0424a8b4beb366aaa93b88a3562c49a8d0ce4dcff",
// ...
// ],
// transactionsRoot: "0x5934902f3dcc263ec34f24318179bf6301f53f4834685792066026f3a4849d72",
// uncles: [],
// }
getBlockNumber
provider.getBlockNumber(): Promise<number>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getBlockNumber();
// 1053312
getCode
provider.getCode(address: string, blockTag?: BlockTag): Promise<string>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await jsonRpcProvider().getCode(
'0xaC6095720221C79C6E7C638d260A2eFBC5D8d880',
'latest',
);
// '0x608060405234801561001057600080fd5b506004361061...'
getGasPrice
provider.getGasPrice(): Promise<TinyBig>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getGasPrice();
// 52493941856
getLogs
provider.getLogs(filter: Filter | FilterByBlockHash): Promise<Array<Log>>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
provider.getLogs({
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
],
fromBlock: 14825027,
toBlock: 14825039,
});
[
{
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
blockHash:
'0x8e0dfac2f704851960f866c8708b3bef2f66c0fee0329cf25ff0261b264ca6bc',
blockNumber: 14825029,
data: '0x000000000000000000000000000000000000000000000000005f862ee352a38a',
logIndex: 384,
removed: false,
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
'0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45',
],
transactionHash:
'0xbd49031be16f8fd1775f4e0fe79b408ffd8ae9c65b2827ee47e3238e3f51f4c0',
transactionIndex: 226,
},
];
getNetwork
provider.getNetwork(): Promise<Network>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
jsonRpcProvider('https://free-eth-node.com/api/eth').getNetwork();
// { chainId: 1, name: 'eth', ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' }
jsonRpcProvider('https://free-eth-node.com/api/MATIC').getNetwork();
// { chainId: 137, name: 'MATIC', ensAddress: null }
getTransaction
provider.getTransaction(transactionHash: string): Promise<TransactionResponse>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransaction(
'0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
);
// {
// accessList: [],
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// chainId: 1,
// from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
// gas: { TinyBig: 112163 },
// gasPrice: { TinyBig: 48592426858 },
// hash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// input: "0x83259f17000000000000000000000000000000000000000000...",
// maxFeePerGas: { TinyBig: 67681261618 },
// maxPriorityFeePerGas: { TinyBig: 1500000000 },
// nonce: { TinyBig: 129 },
// r: "0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc",
// s: "0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c",
// to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// transactionIndex: 29,
// type: 2,
// v: 0,
// value: { TinyBig: 0 },
// confirmations: 298140,
// }
getTransactionCount
provider.getTransactionCount(address: string, blockTag?: BlockTag): Promise<number>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
);
// 1060000
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
'latest',
);
// 1060000
await provider.getTransactionCount(
'0x71660c4005ba85c37ccec55d0c4493e66fe775d3',
14649390,
);
// 1053312
getTransactionReceipt
provider.getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>
View Example
import { JsonRpcProvider } from 'essential-eth';
const provider = new JsonRpcProvider('RPC URL HERE' /* Try Infura or POKT */);
await provider.getTransactionReceipt(
'0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
);
// {
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// contractAddress: null,
// cumulativeGasUsed: { TinyBig: 3067973 },
// effectiveGasPrice: { TinyBig: 48592426858 },
// from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
// gasUsed: { TinyBig: 112163 },
// logs: [
// {
// address: "0x0eDF9bc41Bbc1354c70e2107F80C42caE7FBBcA8",
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
// logIndex: 42,
// topics: [
// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
// "0x00000000000000000000000039b72d136ba3e4cef35f48cd09587ffab754dd8b",
// "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
// ],
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// },
// {
// address: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
// blockNumber: 14578286,
// data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
// logIndex: 43,
// topics: [
// "0x34fcbac0073d7c3d388e51312faf357774904998eeb8fca628b9e6f65ee1cbf7",
// "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
// "0x0000000000000000000000000000000000000000000000000000000000000003",
// ],
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// },
// ],
// logsBloom: "0x00000000000000000000000000000...",
// status: 1,
// to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
// transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
// transactionIndex: 29,
// type: 2,
// byzantium: true,
// confirmations: 298171,
// }
⚠️ Only read functions are currently supported. ⚠️
Contract
support is experimental, do not use this in production yet. (even though earni.fi does)Encoding support:
bool
bytes
address
Decoding support:
bool
address
uint256
bytes32
uint8
Assume all types outside the above types will break for now
import { Contract, jsonRpcProvider, JSONABI } from 'essential-eth';
// UNI airdrop contract
const contractAddress = '0x090D4613473dEE047c3f2706764f49E0821D256e';
const provider = jsonRpcProvider(/* RPC URL optional */);
const abi: JSONABI = [
{
inputs: [
{
internalType: 'uint256',
name: 'index',
type: 'uint256',
},
],
name: 'isClaimed',
outputs: [
{
internalType: 'bool',
name: '',
type: 'bool',
},
],
stateMutability: 'view',
type: 'function',
},
];
const contract = new Contract(contractAddress, abi, provider);
(async () => {
// prints boolean as to whether index 0 has claimed airdrop or not
console.log(await contract.isClaimed(0));
})();
contractFunctionName(args)
Any function on a contract. Returns are the same as ethers.js
, except that instead of BigNumber, essential-eth
always returns a TinyBig
🧪 This repo is under active development. The API is less-fully featured than web3
and ethers
. More functions added often!
👨🏻💻 Breaking changes will exist between minor versions until 1.0.0
(Versions go major.minor.patch
)
Throughout the documentation for essential-eth
, you may notice that some functions are compared to ethers.js
and web3.js
. The keywords identical
, similar
, and dissimilar
are used to help you migrate to essential-eth
. Here's a guide on what these keywords mean:
TinyBig
vs Bn.js
).identical
.similar
, and potentially learn how to alter your implementation to make essential-eth
work for you.Note: In web3.js
, almost every method or function can be passed a callback. essential-eth
, like ethers.js
, does not include this functionality and trusts that users will rely on the much more modern promise and async/await patterns.
In partnership with GitPOAP, Essential ETH wants to recognize all contributors for their contributions toward the growth of this library. Developers can validate their contributions on Github and showcase their GitPOAP as proof-of-work toward their Web3 identity.
GitPOAP is a contributor recognition platform built on top of the POAP Protcol (Proof of Attendence Protocol). Similar to a POAP, each GitPOAP represents a digital collectible. It serves as a badge of validation for the action taken by the individual. By serving as a bridge between Github and Web3, it allows repo maintainers to recognize contributors for their meaningful contributions. Learn more about GitPOAP, here.
Here are two steps that you can take to quailify for a GitPOAP:
Contribute to any open issue and submit a PR. Upon approval of PR merge, the GitPOAP will automatically be issued to the Github account.
Head to gitpoap.io and connect your GitHub account to mint!
Author: dawsbot
Source code: https://github.com/dawsbot/essential-eth
License:
1673116440
A guide to the basic differences between Web3.js and Ethers.js, the two most popular libraries for interacting with the Ethereum blockchain. And two example frontend apps using React + Hooks!
Inside the smart-contracts
folder, you will find a simple Truffle project with the following Solidity contract:
pragma solidity ^0.5.0;
contract Counter {
uint count = 0;
function increment() public {
count = count + 1;
}
function getCount() public view returns (uint) {
return count;
}
}
Before you run any of the frontend UIs, make sure to start the development console with truffle develop
, and then run the migrate
command to compile and deploy the contract onto the development chain.
There are two folders (app-ethers
and app-web3js
) each containing a simple React frontend for the above contract. The only substantial difference between these two UIs is located in the useCounterContract.js
files.
Here are the direct links for your convenience:
In each of these apps, you can serve the frontends with the following commands:
npm install
npm start
This will serve the frontend on http://localhost:1234
which you can view in your browser.
There are three major portions in this code: the setup, reading (calling a constant method), and writing (calling a non-constant mutating method).
With Web3.js, we need the following to instantiate a connected contract instance that can make read/write calls:
from
address (for send
transactions)Note that the networkId
is required for us to fetch the deployed address from our contract artifact.
// Web3.js
const web3 = new Web3("http://127.0.0.1:8545");
const accounts = await web3.eth.getAccounts();
const networkId = await web3.eth.net.getId();
const contractAddress = artifact.networks[networkId].address;
contractInstance = new web3.eth.Contract(artifact.abi, contractAddress, {
from: accounts[0],
});
With Ethers.js, we need the following for our contract instance:
Signer
object (similar to Provider
, but with a specified Signer
)// Ethers.js
const provider = new ethers.providers.JsonRpcProvider();
const network = await provider.getNetwork();
const contractAddress = artifact.networks[network.chainId].address;
contractInstance = new ethers.Contract(
contractAddress,
artifact.abi,
provider.getSigner(),
);
// Web3.js
const count = await contractInstance.methods.getCount().call();
console.log(count); // returns a String
// Ethers.js
const count = await contractInstance.getCount();
console.log(count); // returns a BigNumber instance
These two are very similar, but in our example Ethers.js returns a BigNumber instance by default whereas Web3.js will return the number as a String.
// Web3.js
await contract.current.methods.increment().send();
// tx has been mined
// Ethers.js
const tx = await contract.current.increment();
await tx.wait(); // wait for mining
Note that Web3.js will return a PromiEvent which allows you to subscribe to confirmations, errors, and the transaction hash.
Ethers.js will return a transaction object where a bunch of information relating to the transaction is kept. You can grab the hash via tx.hash
, but you must await
on tx.wait()
if you want to make sure it has been mined.
Author: adrianmcli
Source code: https://github.com/adrianmcli/web3-vs-ethers
1671818520
In this article, we will explore Web3.js Vs Ethers.js: Comparison of Web3 Libraries. In Ethereum development, there are two JavaScript libraries that are quite popular among developers, Web3.js and ethers.js. But which one is better? By comparing the two in an action-packed Web3.js vs ethers.js battle, we’ll see if there’s a reason why the blockchain industry has slowly migrated towards ethers.js and if this library deserves the top spot on the leaderboard compared to Web3.js. However, although both are popular libraries, many experienced developers have found another alternative that has proven to be more beneficial – the Moralis Streams API. Will this API ultimately bring an end to the Web3.js vs ethers.js debate? Let’s find out!
As we dive deeper into the upcoming sections, you’ll learn what ethers.js is. Before explaining what Web3.js is, you’ll also explore “ethers.js 2.0”. Once you know the basics of each of the two Ethereum JS libraries, we will do a thorough Web3.js vs ethers.js comparison. This will help you decide which ETH JavaScript library to utilize during your development endeavors. However, judging by the positive feedback from many dapp (decentralized application) developers who already use Moralis’ Streams API, we believe you’ll be eager to ditch Web3.js and ethers.js yourself. After all, this “ethers.js 2.0” provider is at your disposal with a free Moralis account.
So, what is ethers.js? Ethers.js is a JavaScript (JS) library that aims to be a complete and compact solution for Ethereum development. Since many other popular blockchains follow Ethereum’s lead and use the same virtual machine (Ethereum Virtual Machine [EVM]), this JS library also supports development on other EVM-compatible programmable blockchains. As such, it’s not surprising why some Ethereum devs listen to the blockchain with ethers.js.
In addition to JavaScript support, ethers.js also covers utility functions in TypeScript (TS). Furthermore, this library offers many neat features. For example, it comes with extensive documentation, includes a huge collection of maintained test cases, keeps your private keys in your client safe and sound, and lets you create JavaScript objects from any contract ABI, including ABIv2 and ethers Human-Readable ABI with meta classes. To help you form an even clearer picture of what ethers.js is and some of its other features than the ones stated earlier, let’s look at the following list:
At this point, we are one step closer to the Web3.js vs ethers.js section. However, by learning about “ethers.js 2.0”, you might lose interest in that comparison and decide to start building with that excellent tool right away instead. After all, many developers are now using the Moralis Streams API as an “ethers.js 2.0” alternative instead of ethers.js. Further, most devs will probably follow that path in the future. Moreover, it’s easy to see why when comparing ethers.js vs Web3 streams. The gist of this comparison can be summarized in the following image:
While both ethers.js and the Web3 Streams API support real-time events and multiple chains, ethers.js 2.0 (a.k.a. Streams API) takes things to the next level. Starting with reliability, with ethers.js, you cannot reach the highest levels since you have to depend on node providers. Further, you always need to ensure that the provider supports the chains you want to focus on and that the nodes stay live. On the other hand, Moralis’ Streams API takes that aspect out of the equation and offers 100% reliability.
With ethers.js 2.0, you also get to filter on-chain events. Accordingly, you can focus on the data that really interest you. Furthermore, the Streams API even offers you to cover multiple addresses. So, instead of creating separate listeners for all contracts, you simply pool them together under a single stream. With this advanced listening tool for on-chain events, you can even focus on wallet addresses. As such, you get to use any changes to any wallet address that interest you as a trigger. This enables you to take your dapps and customer service to the next level. Last but not least, unlike ethers.js, the Moralis Streams API provides you with parsed data. In turn, you do not need to worry about processing the data. Instead, you can focus on utilizing the responses right away.
It would be wrong to say that the ethers.js library is not a decent open-source solution for listening to the blockchain. After all, it gets the job done. However, as presented above, even this young ETH JavaScript library comes with several limitations, which will hold you back when creating dapps.
With that in mind, you must admit that there are many instances where having an alternative that covers all those additional options would be a real asset. After all, it would make listening to blockchain events a lot more user-friendly. Moreover, after looking at all the advantages listed in the picture above, it’s no surprise that devs are leaving ethers.js. However, before you determine if the Streams API is the right tool for you, make sure to learn more about it.
Explore the Moralis Web3 Streams API
Furthermore, when you create your free Moralis account, which is your key to the Streams API, you automatically gain access to other powerful tools. These include the industry-leading Web3 Auth API, the ultimate NFT API, and the best Token API. As such, you can create all sorts of killer dapps the easy way.
It’s worth pointing out that in the last two years or so, ethers.js became the go-to ETH JS library. However, to get the most out of the upcoming Web3.js vs ethers.js comparison, you need to know the basics of Web3.js as well.
Just like ethers.js, Web3.js is also an open-source JS library. However, it was on the scene one year before ethers.js and was created by the Ethereum Foundation. This library’s main purpose is to facilitate seamless interactions with the Ethereum chain. Furthermore, Web3.js establishes communication with Ethereum nodes through the “JavaScript Object Notation – remote procedure call” (JSON-RPC) protocol. A lot like ethers.js, this JS library also features modules. In fact, you can learn about its five modules below:
Ultimately, it’s safe to say that the two ETH JavaScript libraries are quite similar. They serve the same or very similar purposes. However, there are some distinct differences between the two that we’ll point out in the upcoming “Web3.js vs ethers.js” section!
To make this Web3.js vs ethers.js comparison as concise and convenient as possible, we’ll break it down into several categories. As such, we will look at the teams behind them, their popularity, downloads, updates, testing, web performance, documentation, and licenses.
Aspects | Web3.js | Ethers.js |
Teams | – The Ethereum Foundation – An extensive organization with many developers offering support – Lack of clarity of who is responsible for support | – Richard “RicMoo” Moore – Smaller team – Pretty clear who’s responsible for what |
Popularity | – First-mover advantage – More stars and repositories on GitHub | – Faster speed of growth and adoption |
Downloads | – 2018: 91,936 – 2019: 113,272 (+23%) – 2020: 161,257 (+42%) – 2021: 247,999 (+54%) – 2022: 587,567 (+137%) | – 2018: 53,863 – 2019: 66,959 (+24%) – 2020: 163,799 (+145%) – 2021: 304,273 (+86%) – 2022: 948,981 (+212%) |
Updates | – Relatively regular updates | – Relatively regular updates |
Testing | – No pre-written tests | – Pre-written tests and clear documentation of its tests |
Web Performance | – Larger in size – Loads slightly slower | – Smaller in size – Loads slightly faster |
Documentation | – Room for improvement – Provides all you need to get started – Link to the Web3.js documentation: https://web3js.readthedocs.io/en/v3.0.0-rc.5/ | – Room for improvement – Provides all you need to get started – Link to the ethers.js documentation: https://docs.ethers.io/v5/ |
License | – LGLv3 license | – MIT license |
The Ethereum Foundation created the Web3.js library and continues to back it. Since this is a large organization, there are many developers offering support. However, when many devs are involved, it also means that there is no clear distinction as to who is responsible for what. On the other hand, Richard “RicMoo” Moore developed ethers.js in 2016 and continues to maintain it. Hence, this also makes things clear about who’s responsible for what.
For a long time, Web3.js was overall a more popular option, mainly because it had the first-mover advantage. Further, according to the stats on GitHub, Web3.js is still in the lead – it has more stars and repositories. However, ethers.js’ speed of growth and adoption puts it in the leading spot. Moreover, looking at the downloads of these two ETH JS libraries, we must be careful not to focus on the overall number of downloads. After all, due to Web3.js’ one-year advantage, that wouldn’t paint a clear picture. Instead, you need to focus on daily downloads per specific year. According to Alchemy’s report from October 2022, ethers.js had nearly twice as many daily downloads as Web3.js in the first three quarters of 2022.
Nonetheless, as a developer, you know that updates are critical. Hence, you probably prefer a library that is always up-to-date and in the best possible shape. After all, it is via updates that support teams remove all known bugs and add new features. That said, both libraries do an equally good job in that aspect.
Ethers.js has pre-written tests and clear documentation, which puts it ahead of Web3.js in this perspective. However, it’s important to remember that every new version levels the field.
Out of the two ETH JavaScript libraries, ethers.js is noticeably smaller in size. The latter may be the main reason why it loads slightly faster than Web3.js. This may offer better performance, especially for smaller dapps. However, since one or the other library is used for various projects, there’s basically no data for the exact same dapps using Web3.js and ethers.js. Thus, the web performance aspect of the Wweb3.js vs ethers.js comparison remains non-conclusive.
None of the two ETH JS libraries maintains flawless documentation. However, both docs provide all you need to start working with them. Moreover, personal preferences play a key in that aspect. After all, what may be perfect documentation for one dev may feel poorly structured for another. As such, make sure to explore both docs and decide for yourself.
Note: Always check the version of the library and make sure to use the matching version of the documentation.
As you can see in the table outlined earlier, Web3.js has an LGLv3 license, while ethers.js has an MIT license. So, if your project has specific requirements regarding the license, you need to keep that aspect of the “Web3.js vs ethers.js” debate in mind. That said, in some cases, it is best to hire a license expert.
Bonus: For all of you who enjoy watching videos, here’s also a neat comparison of Moralis’ Streams API and ethers.js:
We covered a lot of ground in today’s article. Aside from quite a detailed Web3.js vs ethers.js comparison, we also provided you with summaries of each of the two libraries. As such, even if you’ve never used any of them previously, you now have a solid idea of what they are all about. Ultimately, you learned that in the last two years, ethers.js took the lead and became the ETH JavaScript library that most devs focus on. However, we also explained that there is a new tool many developers have decided to utilize instead – Moralis’ Web3 Streams API. Last but not least, you even had an opportunity to watch a video that neatly outlines the benefits of this future-proof tool.
Whether you decide to use Web3.js, ethers.js, or the Streams API, know that this industry is shaping the future of the digital world. As such, learning how to create killer dapps is a path that offers countless opportunities. With that in mind, make sure to expand your blockchain development horizons by visiting the Moralis docs, our Web3 YouTube channel, and our crypto blog. These outlets provide you with countless tutorials and topics that can help you become a dapp developer for free. For instance, some of our latest articles explain what web3.storage is, how Web3 storage works, what Palm NFT Studio is, and much more.
Of course, you may also take a more professional approach to your crypto education. In that case, enroll in Moralis Academy. There you can partake in countless courses to help you become blockchain certified. However, we recommend you start with blockchain and Bitcoin fundamentals to build a strong foundation.
Original article sourced at: https://moralis.io
1636597554
In this video, I will show you how to send Ether to an Ethereum smart contract with Web3 and cover the different ether transfer options:
- calling a function Send Ether to a smart contract by calling a function
- sending Ether directly to the smart contract (fallback function)
I will also show you how to send Ether to a regular address with Web3.
#blockchain #web3 #ethereum #eth
1643182957
Web3.js and Ethers.js are two popular JavaScript libraries for Web3 development. In this article we compare Web3.js vs Ethers.js!
These days, crypto developers can choose among multiple programmable blockchains to build on. And, while you can hear the term “Ethereum killers” being thrown around quite often, the first mover remains the king. As such, the majority of blockchain developers still focus on Ethereum. Moreover, since other programmable chains tend to follow Ethereum’s lead, most of them are EVM-compatible. Thus, the same JavaScript libraries can be used to deploy dApps (decentralized applications) across multiple chains. With that in mind, you ought to take a closer look at Web3.js vs Ethers.js comparison.
Herein, you will get a chance to learn what Web3.js and Ethers.js are. You’ll also learn about JavaScript modules. Moreover, we’ll take a quick overview of the modules of each of the two JavaScript ETH libraries. However, the core of this article is the Web3.js vs Ethers.js comparison. It will help you establish a clear picture of the benefits and disadvantages of each library. Moreover, it should also make things clearer why the majority of the crypto industry is moving towards the younger of the two libraries. With that said, you can also expect the ultimate Web3 development platform Moralis (a.k.a. Firebase for crypto) to soon start natively running Ethers.js.
#blockchain #web3 #moralis #ethers #javascript