essential-eth 0.2.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/lib/cjs/classes/Contract.d.ts +69 -0
  2. package/lib/cjs/classes/Contract.js +122 -0
  3. package/lib/cjs/{rpc → classes}/get-block.test.d.ts +0 -0
  4. package/lib/cjs/{rpc → classes}/get-block.test.js +5 -5
  5. package/lib/cjs/classes/test/Contract/crv-abi.d.ts +2 -0
  6. package/lib/cjs/classes/test/Contract/crv-abi.js +488 -0
  7. package/lib/{esm/rpc/get-block.test.d.ts → cjs/classes/test/Contract/crv.test.d.ts} +0 -0
  8. package/lib/cjs/classes/test/Contract/crv.test.js +48 -0
  9. package/lib/cjs/classes/test/Contract/fei-abi.d.ts +2 -0
  10. package/lib/cjs/classes/test/Contract/fei-abi.js +526 -0
  11. package/lib/cjs/classes/test/Contract/fei.test.d.ts +1 -0
  12. package/lib/cjs/classes/test/Contract/fei.test.js +62 -0
  13. package/lib/cjs/classes/test/Contract/foo-abi.d.ts +2 -0
  14. package/lib/cjs/classes/test/Contract/foo-abi.js +42 -0
  15. package/lib/cjs/classes/test/Contract/foo.test.d.ts +1 -0
  16. package/lib/cjs/classes/test/Contract/foo.test.js +25 -0
  17. package/lib/cjs/classes/test/Contract/uni.test.d.ts +1 -0
  18. package/lib/cjs/classes/test/Contract/uni.test.js +67 -0
  19. package/lib/cjs/classes/test/Contract/uniswap-abi.d.ts +2 -0
  20. package/lib/cjs/classes/test/Contract/uniswap-abi.js +121 -0
  21. package/lib/cjs/{rpc → classes}/utils/clean-block.d.ts +0 -0
  22. package/lib/cjs/{rpc → classes}/utils/clean-block.js +0 -0
  23. package/lib/cjs/{rpc → classes}/utils/clean-transaction.d.ts +0 -0
  24. package/lib/cjs/{rpc → classes}/utils/clean-transaction.js +0 -0
  25. package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +3 -0
  26. package/lib/cjs/classes/utils/encode-decode-transaction.js +100 -0
  27. package/lib/cjs/classes/utils/fetchers.d.ts +9 -0
  28. package/lib/cjs/{rpc → classes}/utils/fetchers.js +8 -1
  29. package/lib/cjs/{rpc → classes}/utils/hex-to-decimal.d.ts +0 -0
  30. package/lib/cjs/{rpc → classes}/utils/hex-to-decimal.js +0 -0
  31. package/lib/cjs/index.d.ts +10 -6
  32. package/lib/cjs/index.js +12 -9
  33. package/lib/cjs/{rpc/index.d.ts → providers/JsonRpcProvider.d.ts} +5 -1
  34. package/lib/cjs/{rpc/index.js → providers/JsonRpcProvider.js} +14 -7
  35. package/lib/cjs/shared/tiny-big/tiny-big.d.ts +2 -2
  36. package/lib/cjs/types/Contract.types.d.ts +22 -0
  37. package/lib/cjs/types/Contract.types.js +2 -0
  38. package/lib/cjs/{ether-to-wei.d.ts → utils/ether-to-wei.d.ts} +3 -2
  39. package/lib/cjs/{ether-to-wei.js → utils/ether-to-wei.js} +4 -8
  40. package/lib/cjs/{is-address.d.ts → utils/is-address.d.ts} +0 -0
  41. package/lib/cjs/{is-address.js → utils/is-address.js} +3 -3
  42. package/lib/cjs/utils/tests/ether-to-wei.test.d.ts +1 -0
  43. package/lib/cjs/utils/tests/ether-to-wei.test.js +66 -0
  44. package/lib/cjs/utils/tests/is-address.test.d.ts +1 -0
  45. package/lib/cjs/utils/tests/is-address.test.js +56 -0
  46. package/lib/cjs/utils/tests/to-checksum-address.test.d.ts +1 -0
  47. package/lib/cjs/utils/tests/to-checksum-address.test.js +43 -0
  48. package/lib/cjs/utils/tests/wei-to-ether.test.d.ts +1 -0
  49. package/lib/cjs/utils/tests/wei-to-ether.test.js +69 -0
  50. package/lib/cjs/{to-checksum-address.d.ts → utils/to-checksum-address.d.ts} +0 -0
  51. package/lib/cjs/{to-checksum-address.js → utils/to-checksum-address.js} +1 -1
  52. package/lib/cjs/{wei-to-ether.d.ts → utils/wei-to-ether.d.ts} +3 -1
  53. package/lib/cjs/{wei-to-ether.js → utils/wei-to-ether.js} +4 -8
  54. package/lib/esm/classes/Contract.d.ts +12 -0
  55. package/lib/esm/classes/Contract.js +63 -0
  56. package/lib/esm/classes/get-block.test.d.ts +1 -0
  57. package/lib/esm/{rpc → classes}/get-block.test.js +5 -5
  58. package/lib/esm/classes/test/Contract/crv-abi.d.ts +2 -0
  59. package/lib/esm/classes/test/Contract/crv-abi.js +470 -0
  60. package/lib/esm/classes/test/Contract/crv.test.d.ts +1 -0
  61. package/lib/esm/classes/test/Contract/crv.test.js +43 -0
  62. package/lib/esm/classes/test/Contract/fei-abi.d.ts +2 -0
  63. package/lib/esm/classes/test/Contract/fei-abi.js +521 -0
  64. package/lib/esm/classes/test/Contract/fei.test.d.ts +1 -0
  65. package/lib/esm/classes/test/Contract/fei.test.js +46 -0
  66. package/lib/esm/classes/test/Contract/foo-abi.d.ts +2 -0
  67. package/lib/esm/classes/test/Contract/foo-abi.js +33 -0
  68. package/lib/esm/classes/test/Contract/foo.test.d.ts +1 -0
  69. package/lib/esm/classes/test/Contract/foo.test.js +14 -0
  70. package/lib/esm/classes/test/Contract/uni.test.d.ts +1 -0
  71. package/lib/esm/classes/test/Contract/uni.test.js +59 -0
  72. package/lib/esm/classes/test/Contract/uniswap-abi.d.ts +2 -0
  73. package/lib/esm/classes/test/Contract/uniswap-abi.js +116 -0
  74. package/lib/esm/{rpc → classes}/utils/clean-block.d.ts +0 -0
  75. package/lib/esm/{rpc → classes}/utils/clean-block.js +0 -0
  76. package/lib/esm/{rpc → classes}/utils/clean-transaction.d.ts +0 -0
  77. package/lib/esm/{rpc → classes}/utils/clean-transaction.js +0 -0
  78. package/lib/esm/classes/utils/encode-decode-transaction.d.ts +3 -0
  79. package/lib/esm/classes/utils/encode-decode-transaction.js +85 -0
  80. package/lib/esm/classes/utils/fetchers.d.ts +9 -0
  81. package/lib/esm/{rpc → classes}/utils/fetchers.js +8 -1
  82. package/lib/esm/{rpc → classes}/utils/hex-to-decimal.d.ts +0 -0
  83. package/lib/esm/{rpc → classes}/utils/hex-to-decimal.js +0 -0
  84. package/lib/esm/index.d.ts +10 -6
  85. package/lib/esm/index.js +7 -6
  86. package/lib/esm/{rpc/index.d.ts → providers/JsonRpcProvider.d.ts} +2 -1
  87. package/lib/esm/{rpc/index.js → providers/JsonRpcProvider.js} +8 -5
  88. package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -2
  89. package/lib/esm/types/Contract.types.d.ts +22 -0
  90. package/lib/esm/types/Contract.types.js +1 -0
  91. package/lib/esm/utils/ether-to-wei.d.ts +3 -0
  92. package/lib/esm/utils/ether-to-wei.js +7 -0
  93. package/lib/esm/{is-address.d.ts → utils/is-address.d.ts} +0 -0
  94. package/lib/esm/{is-address.js → utils/is-address.js} +2 -2
  95. package/lib/esm/utils/tests/ether-to-wei.test.d.ts +1 -0
  96. package/lib/esm/utils/tests/ether-to-wei.test.js +36 -0
  97. package/lib/esm/utils/tests/is-address.test.d.ts +1 -0
  98. package/lib/esm/utils/tests/is-address.test.js +49 -0
  99. package/lib/esm/utils/tests/to-checksum-address.test.d.ts +1 -0
  100. package/lib/esm/utils/tests/to-checksum-address.test.js +36 -0
  101. package/lib/esm/utils/tests/wei-to-ether.test.d.ts +1 -0
  102. package/lib/esm/utils/tests/wei-to-ether.test.js +38 -0
  103. package/lib/esm/{to-checksum-address.d.ts → utils/to-checksum-address.d.ts} +0 -0
  104. package/lib/esm/{to-checksum-address.js → utils/to-checksum-address.js} +1 -1
  105. package/lib/esm/utils/wei-to-ether.d.ts +3 -0
  106. package/lib/esm/utils/wei-to-ether.js +7 -0
  107. package/package.json +11 -11
  108. package/readme.md +68 -5
  109. package/lib/cjs/rpc/utils/fetchers.d.ts +0 -7
  110. package/lib/esm/ether-to-wei.d.ts +0 -2
  111. package/lib/esm/ether-to-wei.js +0 -8
  112. package/lib/esm/rpc/utils/fetchers.d.ts +0 -7
  113. package/lib/esm/wei-to-ether.d.ts +0 -1
  114. package/lib/esm/wei-to-ether.js +0 -8
@@ -0,0 +1,116 @@
1
+ export const uniswapABI = [
2
+ {
3
+ inputs: [
4
+ {
5
+ internalType: 'address',
6
+ name: 'token_',
7
+ type: 'address',
8
+ },
9
+ {
10
+ internalType: 'bytes32',
11
+ name: 'merkleRoot_',
12
+ type: 'bytes32',
13
+ },
14
+ ],
15
+ stateMutability: 'nonpayable',
16
+ type: 'constructor',
17
+ },
18
+ {
19
+ anonymous: false,
20
+ inputs: [
21
+ {
22
+ indexed: false,
23
+ internalType: 'uint256',
24
+ name: 'index',
25
+ type: 'uint256',
26
+ },
27
+ {
28
+ indexed: false,
29
+ internalType: 'address',
30
+ name: 'account',
31
+ type: 'address',
32
+ },
33
+ {
34
+ indexed: false,
35
+ internalType: 'uint256',
36
+ name: 'amount',
37
+ type: 'uint256',
38
+ },
39
+ ],
40
+ name: 'Claimed',
41
+ type: 'event',
42
+ },
43
+ {
44
+ inputs: [
45
+ {
46
+ internalType: 'uint256',
47
+ name: 'index',
48
+ type: 'uint256',
49
+ },
50
+ {
51
+ internalType: 'address',
52
+ name: 'account',
53
+ type: 'address',
54
+ },
55
+ {
56
+ internalType: 'uint256',
57
+ name: 'amount',
58
+ type: 'uint256',
59
+ },
60
+ {
61
+ internalType: 'bytes32[]',
62
+ name: 'merkleProof',
63
+ type: 'bytes32[]',
64
+ },
65
+ ],
66
+ name: 'claim',
67
+ outputs: [],
68
+ stateMutability: 'nonpayable',
69
+ type: 'function',
70
+ },
71
+ {
72
+ inputs: [
73
+ {
74
+ internalType: 'uint256',
75
+ name: 'index',
76
+ type: 'uint256',
77
+ },
78
+ ],
79
+ name: 'isClaimed',
80
+ outputs: [
81
+ {
82
+ internalType: 'bool',
83
+ name: '',
84
+ type: 'bool',
85
+ },
86
+ ],
87
+ stateMutability: 'view',
88
+ type: 'function',
89
+ },
90
+ {
91
+ inputs: [],
92
+ name: 'merkleRoot',
93
+ outputs: [
94
+ {
95
+ internalType: 'bytes32',
96
+ name: '',
97
+ type: 'bytes32',
98
+ },
99
+ ],
100
+ stateMutability: 'view',
101
+ type: 'function',
102
+ },
103
+ {
104
+ inputs: [],
105
+ name: 'token',
106
+ outputs: [
107
+ {
108
+ internalType: 'address',
109
+ name: '',
110
+ type: 'address',
111
+ },
112
+ ],
113
+ stateMutability: 'view',
114
+ type: 'function',
115
+ },
116
+ ];
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ import { JSONABIArgument } from '../../types/Contract.types';
2
+ export declare function encodeData(jsonABIArgument: JSONABIArgument, args: any[]): string;
3
+ export declare function decodeRPCResponse(jsonABIArgument: JSONABIArgument, nodeResponse: string): string | number | boolean | import("../..").TinyBig | (string | number | boolean | import("../..").TinyBig)[];
@@ -0,0 +1,85 @@
1
+ import { Keccak } from 'sha3';
2
+ import { tinyBig, toChecksumAddress } from '../..';
3
+ import { hexToDecimal } from './hex-to-decimal';
4
+ const hexTrue = '0000000000000000000000000000000000000000000000000000000000000001';
5
+ const hexFalse = '0000000000000000000000000000000000000000000000000000000000000000';
6
+ function expandType(type) {
7
+ if (type === 'uint[]') {
8
+ return 'uint256[]';
9
+ }
10
+ else if (type === 'int[]') {
11
+ return 'int256[]';
12
+ }
13
+ return type;
14
+ }
15
+ export function encodeData(jsonABIArgument, args) {
16
+ const hash = new Keccak(256);
17
+ const functionString = `${jsonABIArgument.name}(${jsonABIArgument.inputs.map((input) => expandType(input.type))})`;
18
+ const functionHash = hash.update(functionString).digest('hex');
19
+ const jsonABIInputsLength = jsonABIArgument.inputs.length;
20
+ let shouldValidateInputLength = true;
21
+ if (jsonABIArgument.inputs.find((input) => input.type.includes('['))) {
22
+ shouldValidateInputLength = false;
23
+ }
24
+ if (shouldValidateInputLength && args.length !== jsonABIInputsLength) {
25
+ throw new Error(`args inputs of "${args.length}" does not match expected length of "${jsonABIArgument.inputs.length}"`);
26
+ }
27
+ const argsWithTypes = (jsonABIArgument.inputs || []).reduce((acc, input, i) => {
28
+ var _a;
29
+ if (input.type.includes('[')) {
30
+ const basicType = (_a = /([^[]*)\[.*$/g.exec(input.type)) === null || _a === void 0 ? void 0 : _a[1];
31
+ args.forEach((arg) => {
32
+ acc = acc.concat([[arg, basicType]]);
33
+ });
34
+ return acc;
35
+ }
36
+ else {
37
+ return acc.concat([[args[i], input.type]]);
38
+ }
39
+ }, []);
40
+ const encodedArgs = argsWithTypes.map(([arg, inputType]) => {
41
+ let rawArg = arg;
42
+ if (inputType === 'bool') {
43
+ return arg ? hexTrue : hexFalse;
44
+ }
45
+ else if (inputType.startsWith('bytes')) {
46
+ const argEncoded = rawArg
47
+ .split('')
48
+ .map((character) => character.charCodeAt(0).toString(16))
49
+ .join('');
50
+ const paddedEncodedArg = argEncoded.padEnd(64, '0');
51
+ return paddedEncodedArg;
52
+ }
53
+ else if (inputType === 'address') {
54
+ rawArg = arg.replace(/^0x/g, '').toLowerCase();
55
+ }
56
+ const argEncoded = rawArg.toString(16);
57
+ const paddedEncodedArg = argEncoded.padStart(64, '0');
58
+ return paddedEncodedArg;
59
+ });
60
+ const functionEncoded = functionHash.slice(0, 8);
61
+ const data = `0x${functionEncoded}${encodedArgs.join('')}`;
62
+ return data;
63
+ }
64
+ export function decodeRPCResponse(jsonABIArgument, nodeResponse) {
65
+ const rawOutputs = jsonABIArgument.outputs;
66
+ const encodedOutputs = nodeResponse.slice(2).match(/.{1,64}/g);
67
+ const outputs = (encodedOutputs || []).map((output, i) => {
68
+ const outputType = (rawOutputs || [])[i].type;
69
+ switch (outputType) {
70
+ case 'bool':
71
+ return output === hexTrue;
72
+ case 'address':
73
+ return toChecksumAddress(`0x${output.slice(24)}`);
74
+ case 'uint256':
75
+ return tinyBig(hexToDecimal(`0x${output}`));
76
+ case 'bytes32':
77
+ return `0x${output}`;
78
+ case 'uint8':
79
+ return Number(hexToDecimal(`0x${output}`));
80
+ default:
81
+ throw new Error(`essential-eth does not yet support "${outputType}" inputs. Make a PR today!"`);
82
+ }
83
+ });
84
+ return outputs.length === 1 ? outputs[0] : outputs;
85
+ }
@@ -0,0 +1,9 @@
1
+ export declare function post(url: string, body: Record<string, unknown>): Promise<any>;
2
+ declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_call';
3
+ export declare function buildRPCPostBody(method: RPCMethodName, params: any[]): {
4
+ jsonrpc: string;
5
+ id: number;
6
+ method: RPCMethodName;
7
+ params: any[];
8
+ };
9
+ export {};
@@ -6,7 +6,14 @@ export function post(url, body) {
6
6
  'Content-Type': 'application/json',
7
7
  },
8
8
  body: JSON.stringify(body),
9
- }).then((r) => r.json());
9
+ })
10
+ .then((r) => r.json())
11
+ .then((response) => {
12
+ if (response.error) {
13
+ throw new Error(response.error);
14
+ }
15
+ return response.result;
16
+ });
10
17
  }
11
18
  export function buildRPCPostBody(method, params) {
12
19
  return {
File without changes
@@ -1,7 +1,11 @@
1
- import { etherToWei } from './ether-to-wei';
2
- import { isAddress } from './is-address';
3
- import { EssentialEth } from './rpc/index';
1
+ import { Transaction } from 'ethers';
2
+ import { Contract } from './classes/Contract';
3
+ import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
4
4
  import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
5
- import { toChecksumAddress } from './to-checksum-address';
6
- import { weiToEther } from './wei-to-ether';
7
- export { toChecksumAddress, isAddress, etherToWei, weiToEther, tinyBig, TinyBig, EssentialEth, };
5
+ import { Block } from './types/block.types';
6
+ import { JSONABI, JSONABIArgument } from './types/Contract.types';
7
+ import { etherToWei } from './utils/ether-to-wei';
8
+ import { isAddress } from './utils/is-address';
9
+ import { toChecksumAddress } from './utils/to-checksum-address';
10
+ import { weiToEther } from './utils/wei-to-ether';
11
+ export { jsonRpcProvider, JsonRpcProvider, toChecksumAddress, isAddress, etherToWei, weiToEther, tinyBig, TinyBig, Contract, JSONABI, Block, JSONABIArgument, Transaction, };
package/lib/esm/index.js CHANGED
@@ -1,7 +1,8 @@
1
- import { etherToWei } from './ether-to-wei';
2
- import { isAddress } from './is-address';
3
- import { EssentialEth } from './rpc/index';
1
+ import { Contract } from './classes/Contract';
2
+ import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
4
3
  import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
5
- import { toChecksumAddress } from './to-checksum-address';
6
- import { weiToEther } from './wei-to-ether';
7
- export { toChecksumAddress, isAddress, etherToWei, weiToEther, tinyBig, TinyBig, EssentialEth, };
4
+ import { etherToWei } from './utils/ether-to-wei';
5
+ import { isAddress } from './utils/is-address';
6
+ import { toChecksumAddress } from './utils/to-checksum-address';
7
+ import { weiToEther } from './utils/wei-to-ether';
8
+ export { jsonRpcProvider, JsonRpcProvider, toChecksumAddress, isAddress, etherToWei, weiToEther, tinyBig, TinyBig, Contract, };
@@ -1,6 +1,7 @@
1
1
  import { Block } from '../types/block.types';
2
- export declare class EssentialEth {
2
+ export declare class JsonRpcProvider {
3
3
  _rpcUrl: string;
4
4
  constructor(rpcUrl?: string);
5
5
  getBlock(timeFrame: 'latest' | 'earliest' | 'pending' | number, returnTransactionObjects?: boolean): Promise<Block>;
6
6
  }
7
+ export declare function jsonRpcProvider(rpcUrl?: string): JsonRpcProvider;
@@ -7,9 +7,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { cleanBlock } from './utils/clean-block';
11
- import { buildRPCPostBody, post } from './utils/fetchers';
12
- export class EssentialEth {
10
+ import { cleanBlock } from '../classes/utils/clean-block';
11
+ import { buildRPCPostBody, post } from '../classes/utils/fetchers';
12
+ export class JsonRpcProvider {
13
13
  constructor(rpcUrl) {
14
14
  this._rpcUrl = rpcUrl || 'https://free-eth-node.com/api/eth';
15
15
  }
@@ -22,11 +22,14 @@ export class EssentialEth {
22
22
  else {
23
23
  rpcTimeFrame = timeFrame;
24
24
  }
25
- const nodeResponse = yield post(this._rpcUrl, buildRPCPostBody('eth_getBlockByNumber', [
25
+ const nodeResponse = (yield post(this._rpcUrl, buildRPCPostBody('eth_getBlockByNumber', [
26
26
  rpcTimeFrame,
27
27
  returnTransactionObjects,
28
- ])).then((data) => data.result);
28
+ ])));
29
29
  return cleanBlock(nodeResponse, returnTransactionObjects);
30
30
  });
31
31
  }
32
32
  }
33
+ export function jsonRpcProvider(rpcUrl) {
34
+ return new JsonRpcProvider(rpcUrl);
35
+ }
@@ -1,7 +1,7 @@
1
1
  import Big from 'big.js';
2
2
  export declare class TinyBig extends Big {
3
- constructor(value: number | string);
3
+ constructor(value: number | string | TinyBig);
4
4
  toNumber(): number;
5
5
  toString(): string;
6
6
  }
7
- export declare function tinyBig(value: number | string): TinyBig;
7
+ export declare function tinyBig(value: number | string | TinyBig): TinyBig;
@@ -0,0 +1,22 @@
1
+ export declare type ContractTypes = 'bool' | 'bytes1' | 'bytes2' | 'bytes3' | 'bytes4' | 'bytes5' | 'bytes6' | 'bytes7' | 'bytes8' | 'bytes9' | 'bytes10' | 'bytes11' | 'bytes12' | 'bytes13' | 'bytes14' | 'bytes15' | 'bytes16' | 'bytes17' | 'bytes18' | 'bytes19' | 'bytes20' | 'bytes21' | 'bytes22' | 'bytes23' | 'bytes24' | 'bytes25' | 'bytes26' | 'bytes27' | 'bytes28' | 'bytes29' | 'bytes30' | 'bytes31' | 'bytes32' | 'bytes32[]' | 'address' | 'address payable' | 'address[4]' | 'address[100]' | 'uint256' | 'uint256[100]' | 'uint8' | 'uint32' | string;
2
+ export declare type ContractInterface = JSONABI;
3
+ export declare type ContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
4
+ export declare type JSONABIArgument = {
5
+ anonymous?: false;
6
+ inputs: {
7
+ internalType?: ContractTypes | string;
8
+ name: string;
9
+ type: ContractTypes;
10
+ indexed?: boolean;
11
+ }[];
12
+ name?: string;
13
+ outputs?: {
14
+ internalType?: ContractTypes | string;
15
+ name: string;
16
+ type: ContractTypes;
17
+ }[];
18
+ stateMutability?: 'view' | 'nonpayable' | string;
19
+ type: 'function' | 'event' | 'constructor' | 'error';
20
+ gas?: number;
21
+ };
22
+ export declare type JSONABI = JSONABIArgument[];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import Big from 'big.js';
2
+ import { TinyBig } from '../shared/tiny-big/tiny-big';
3
+ export declare function etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig;
@@ -0,0 +1,7 @@
1
+ import { tinyBig } from '../shared/tiny-big/tiny-big';
2
+ import { validateType } from '../shared/validate-type';
3
+ export function etherToWei(etherQuantity) {
4
+ validateType(etherQuantity, ['string', 'number', 'object']);
5
+ const result = tinyBig(etherQuantity).times('1000000000000000000');
6
+ return tinyBig(result);
7
+ }
@@ -1,5 +1,5 @@
1
- import { toChecksumAddress } from '.';
2
- import { validateType } from './shared/validate-type';
1
+ import { toChecksumAddress } from '..';
2
+ import { validateType } from '../shared/validate-type';
3
3
  export function isAddress(address) {
4
4
  validateType(address, ['string']);
5
5
  try {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ import Big from 'big.js';
2
+ import * as ethers from 'ethers';
3
+ import web3 from 'web3';
4
+ import { etherToWei, tinyBig } from '../../index';
5
+ import { scientificStrToDecimalStr } from '../../shared/tiny-big/helpers';
6
+ describe('ether-to-wei', () => {
7
+ it('happy path', () => {
8
+ expect(etherToWei('100').toString()).toStrictEqual('100000000000000000000');
9
+ expect(etherToWei(100).toString()).toStrictEqual('100000000000000000000');
10
+ expect(etherToWei('1000.0').toString()).toStrictEqual('1000000000000000000000');
11
+ expect(etherToWei(1000).toString()).toStrictEqual('1000000000000000000000');
12
+ expect(etherToWei('1000.0').toNumber()).toStrictEqual(1000000000000000000000);
13
+ expect(etherToWei(tinyBig(1000)).toString()).toStrictEqual('1000000000000000000000');
14
+ expect(etherToWei(tinyBig('1000.0')).toNumber()).toStrictEqual(1000000000000000000000);
15
+ expect(etherToWei(Big(1000)).toString()).toStrictEqual('1000000000000000000000');
16
+ expect(etherToWei(Big('1000.0')).toNumber()).toStrictEqual(1000000000000000000000);
17
+ });
18
+ it('matches ethers and web3 toString', () => {
19
+ expect(etherToWei('-09999.0').toString()).toStrictEqual(ethers.utils.parseEther('-09999.0').toString());
20
+ expect(etherToWei('-09999.0').toString()).toStrictEqual(web3.utils.toWei('-09999.0', 'ether'));
21
+ });
22
+ it('matches ethers and web3 toNumber', () => {
23
+ expect(etherToWei('9').toNumber()).toStrictEqual(Number(ethers.utils.parseEther('9')));
24
+ expect(etherToWei('9').toNumber()).toStrictEqual(Number(web3.utils.toWei('9', 'ether')));
25
+ expect(etherToWei('-0999999.90').toNumber()).toStrictEqual(Number(ethers.utils.parseEther('-0999999.90')));
26
+ expect(etherToWei('-0999999.9').toNumber()).toStrictEqual(Number(scientificStrToDecimalStr(web3.utils.toWei('-0999999.9', 'ether'))));
27
+ });
28
+ it('wrong types', () => {
29
+ expect(() => {
30
+ etherToWei(false);
31
+ }).toThrow();
32
+ expect(() => {
33
+ etherToWei([1, 2, 3]);
34
+ }).toThrow();
35
+ });
36
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
1
+ import { utils } from 'ethers';
2
+ import web3 from 'web3';
3
+ import { isAddress } from '../../index';
4
+ describe('is-address', () => {
5
+ it('should validate real addresses', () => {
6
+ const addresses = [
7
+ '0x52908400098527886E0F7030069857D2E4169EE7',
8
+ '0x8617E340B3D01FA5F11F306F4090FD50E238070D',
9
+ '0xde709f2102306220921060314715629080e2fb77',
10
+ '0x27b1fdb04752bbc536007a920d24acb045561c26',
11
+ '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
12
+ '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
13
+ '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
14
+ '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
15
+ '27b1fdb04752bbc536007a920d24acb045561c26',
16
+ ];
17
+ addresses.forEach((address) => {
18
+ expect(isAddress(address)).toStrictEqual(utils.isAddress(address));
19
+ expect(isAddress(address)).toStrictEqual(web3.utils.isAddress(address));
20
+ });
21
+ });
22
+ it('should return false on invalid addresses', () => {
23
+ const addresses = [
24
+ '0x5290840009852',
25
+ '0x8617E340b3D01FA5F11F306F4090FD50E238070D',
26
+ '0x8617e340b3d01FA5F11F306F4090FD50E238070D',
27
+ ' 0xde709f2102306220921060314715629080e2fb77',
28
+ 'dawsbot.eth',
29
+ '',
30
+ 'xyz',
31
+ '0x123',
32
+ ];
33
+ addresses.forEach((address) => {
34
+ const essentialEthIsAddress = isAddress(address);
35
+ const ethersIsAddress = utils.isAddress(address);
36
+ const web3IsAddress = web3.utils.isAddress(address);
37
+ expect(essentialEthIsAddress).toStrictEqual(ethersIsAddress);
38
+ expect(essentialEthIsAddress).toStrictEqual(web3IsAddress);
39
+ });
40
+ });
41
+ it('invalid type inputs', () => {
42
+ expect(() => {
43
+ isAddress(false);
44
+ }).toThrow('string required. Received boolean');
45
+ expect(() => {
46
+ isAddress([1, 2, 3]);
47
+ }).toThrow('string required. Received object');
48
+ });
49
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ import { utils } from 'ethers';
2
+ import web3 from 'web3';
3
+ import { toChecksumAddress } from '../../index';
4
+ describe('ether-to-wei', () => {
5
+ it('happy path', () => {
6
+ const addresses = [
7
+ '0x52908400098527886E0F7030069857D2E4169EE7',
8
+ '0x8617E340B3D01FA5F11F306F4090FD50E238070D',
9
+ '0xde709f2102306220921060314715629080e2fb77',
10
+ '0x27b1fdb04752bbc536007a920d24acb045561c26',
11
+ '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
12
+ '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
13
+ '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
14
+ '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
15
+ ];
16
+ addresses.forEach((address) => {
17
+ expect(toChecksumAddress(address)).toStrictEqual(utils.getAddress(address));
18
+ expect(toChecksumAddress(address)).toStrictEqual(web3.utils.toChecksumAddress(address));
19
+ });
20
+ });
21
+ it('invalid inputs', () => {
22
+ const invalidChecksumAddress = '0x8617E340b3D01FA5F11F306F4090FD50E238070D';
23
+ expect(() => {
24
+ toChecksumAddress(invalidChecksumAddress);
25
+ }).toThrow(`Invalid Checksum address for "${invalidChecksumAddress}"`);
26
+ expect(() => {
27
+ toChecksumAddress(false);
28
+ }).toThrow('string required. Received boolean');
29
+ expect(() => {
30
+ toChecksumAddress([1, 2, 3]);
31
+ }).toThrow('string required. Received object');
32
+ expect(() => {
33
+ toChecksumAddress('0x1');
34
+ }).toThrow('Invalid Ethereum address "0x1"');
35
+ });
36
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ import Big from 'big.js';
2
+ import * as ethers from 'ethers';
3
+ import web3 from 'web3';
4
+ import { tinyBig, weiToEther } from '../../index';
5
+ describe('wei-to-ether', () => {
6
+ it('happy path', () => {
7
+ expect(weiToEther('100000000000000000000.0').toString()).toStrictEqual('100');
8
+ expect(weiToEther(100000000000000000000.0).toString()).toStrictEqual('100');
9
+ expect(weiToEther('1000000000000000000000.0').toNumber()).toStrictEqual(1000);
10
+ expect(weiToEther(1000000000000000000000.0).toNumber()).toStrictEqual(1000);
11
+ expect(weiToEther(tinyBig('1000000000000000000000.0')).toNumber()).toStrictEqual(1000);
12
+ expect(weiToEther(tinyBig(1000000000000000000000.0)).toNumber()).toStrictEqual(1000);
13
+ expect(weiToEther(Big('1000000000000000000000.0')).toNumber()).toStrictEqual(1000);
14
+ expect(weiToEther(Big(1000000000000000000000.0)).toNumber()).toStrictEqual(1000);
15
+ });
16
+ describe('matches ethers and web3 toString', () => {
17
+ it('toString', () => {
18
+ expect(weiToEther('10').toString()).toStrictEqual(ethers.utils.formatEther('10'));
19
+ expect(weiToEther('1000000000000000000000').toString()).toStrictEqual('1000');
20
+ expect(weiToEther('10').toString()).toStrictEqual(web3.utils.fromWei('10', 'ether'));
21
+ expect(weiToEther('1000000000000000000000').toString()).toStrictEqual(web3.utils.fromWei('1000000000000000000000', 'ether'));
22
+ });
23
+ it('toNumber', () => {
24
+ expect(weiToEther('9').toNumber()).toStrictEqual(Number(ethers.utils.formatEther('9')));
25
+ expect(weiToEther('9').toNumber()).toStrictEqual(Number(web3.utils.fromWei('9', 'ether')));
26
+ expect(weiToEther('999999').toNumber()).toStrictEqual(Number(ethers.utils.formatEther('999999')));
27
+ expect(weiToEther('999999').toNumber()).toStrictEqual(Number(web3.utils.fromWei('999999', 'ether')));
28
+ });
29
+ });
30
+ it('wrong types', () => {
31
+ expect(() => {
32
+ weiToEther(false);
33
+ }).toThrow();
34
+ expect(() => {
35
+ weiToEther([1, 2, 3]);
36
+ }).toThrow();
37
+ });
38
+ });
@@ -1,5 +1,5 @@
1
1
  import { Keccak } from 'sha3';
2
- import { validateType } from './shared/validate-type';
2
+ import { validateType } from '../shared/validate-type';
3
3
  export function toChecksumAddress(address) {
4
4
  validateType(address, ['string']);
5
5
  if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
@@ -0,0 +1,3 @@
1
+ import Big from 'big.js';
2
+ import { TinyBig } from './../shared/tiny-big/tiny-big';
3
+ export declare function weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig;
@@ -0,0 +1,7 @@
1
+ import { tinyBig } from '../shared/tiny-big/tiny-big';
2
+ import { validateType } from '../shared/validate-type';
3
+ export function weiToEther(weiQuantity) {
4
+ validateType(weiQuantity, ['string', 'number', 'object']);
5
+ const result = tinyBig(weiQuantity).div('1000000000000000000');
6
+ return tinyBig(result);
7
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "essential-eth",
3
3
  "description": "Ultralight JS library for Ethereum utilities",
4
- "version": "0.2.2",
4
+ "version": "0.3.3",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
7
  "main": "./lib/cjs/index.js",
@@ -33,32 +33,32 @@
33
33
  "@types/body-parser": "^1.19.1",
34
34
  "@types/eslint": "^7.28.0",
35
35
  "@types/express": "^4.17.13",
36
- "@types/jest": "^27.0.1",
36
+ "@types/jest": "^27.0.2",
37
37
  "@types/jest-dev-server": "^5.0.0",
38
- "@types/node": "^16.9.2",
39
- "@types/prettier": "^2.3.2",
38
+ "@types/node": "^16.10.1",
39
+ "@types/prettier": "^2.4.0",
40
40
  "@types/supertest": "^2.0.11",
41
- "@typescript-eslint/eslint-plugin": "^4.31.1",
42
- "@typescript-eslint/parser": "^4.31.1",
41
+ "@typescript-eslint/eslint-plugin": "^4.32.0",
42
+ "@typescript-eslint/parser": "^4.32.0",
43
43
  "body-parser": "^1.19.0",
44
44
  "eslint": "^7.32.0",
45
45
  "eslint-plugin-jest": "^24.4.2",
46
46
  "ethers": "^5.4.7",
47
47
  "express": "^4.17.1",
48
48
  "husky": "^4.3.0",
49
- "jest": "^27.2.0",
50
- "jest-dev-server": "^5.0.3",
49
+ "jest": "^27.2.2",
50
+ "jest-dev-server": "^6.0.0",
51
51
  "just-omit": "^1.2.0",
52
52
  "lint-staged": "^11.1.2",
53
53
  "npm-run-all": "^4.1.5",
54
54
  "prettier": "^2.4.1",
55
- "prettier-plugin-organize-imports": "^2.3.3",
55
+ "prettier-plugin-organize-imports": "^2.3.4",
56
56
  "supertest": "^6.1.6",
57
57
  "ts-jest": "^27.0.5",
58
58
  "ts-node": "^10.2.1",
59
- "typedoc": "^0.22.3",
59
+ "typedoc": "^0.22.4",
60
60
  "typescript": "^4.4.3",
61
- "web3": "^1.5.2"
61
+ "web3": "^1.5.3"
62
62
  },
63
63
  "dependencies": {
64
64
  "big.js": "^6.1.1",