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.
- package/lib/cjs/classes/Contract.d.ts +69 -0
- package/lib/cjs/classes/Contract.js +122 -0
- package/lib/cjs/{rpc → classes}/get-block.test.d.ts +0 -0
- package/lib/cjs/{rpc → classes}/get-block.test.js +5 -5
- package/lib/cjs/classes/test/Contract/crv-abi.d.ts +2 -0
- package/lib/cjs/classes/test/Contract/crv-abi.js +488 -0
- package/lib/{esm/rpc/get-block.test.d.ts → cjs/classes/test/Contract/crv.test.d.ts} +0 -0
- package/lib/cjs/classes/test/Contract/crv.test.js +48 -0
- package/lib/cjs/classes/test/Contract/fei-abi.d.ts +2 -0
- package/lib/cjs/classes/test/Contract/fei-abi.js +526 -0
- package/lib/cjs/classes/test/Contract/fei.test.d.ts +1 -0
- package/lib/cjs/classes/test/Contract/fei.test.js +62 -0
- package/lib/cjs/classes/test/Contract/foo-abi.d.ts +2 -0
- package/lib/cjs/classes/test/Contract/foo-abi.js +42 -0
- package/lib/cjs/classes/test/Contract/foo.test.d.ts +1 -0
- package/lib/cjs/classes/test/Contract/foo.test.js +25 -0
- package/lib/cjs/classes/test/Contract/uni.test.d.ts +1 -0
- package/lib/cjs/classes/test/Contract/uni.test.js +67 -0
- package/lib/cjs/classes/test/Contract/uniswap-abi.d.ts +2 -0
- package/lib/cjs/classes/test/Contract/uniswap-abi.js +121 -0
- package/lib/cjs/{rpc → classes}/utils/clean-block.d.ts +0 -0
- package/lib/cjs/{rpc → classes}/utils/clean-block.js +0 -0
- package/lib/cjs/{rpc → classes}/utils/clean-transaction.d.ts +0 -0
- package/lib/cjs/{rpc → classes}/utils/clean-transaction.js +0 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +3 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.js +100 -0
- package/lib/cjs/classes/utils/fetchers.d.ts +9 -0
- package/lib/cjs/{rpc → classes}/utils/fetchers.js +8 -1
- package/lib/cjs/{rpc → classes}/utils/hex-to-decimal.d.ts +0 -0
- package/lib/cjs/{rpc → classes}/utils/hex-to-decimal.js +0 -0
- package/lib/cjs/index.d.ts +10 -6
- package/lib/cjs/index.js +12 -9
- package/lib/cjs/{rpc/index.d.ts → providers/JsonRpcProvider.d.ts} +5 -1
- package/lib/cjs/{rpc/index.js → providers/JsonRpcProvider.js} +14 -7
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +2 -2
- package/lib/cjs/types/Contract.types.d.ts +22 -0
- package/lib/cjs/types/Contract.types.js +2 -0
- package/lib/cjs/{ether-to-wei.d.ts → utils/ether-to-wei.d.ts} +3 -2
- package/lib/cjs/{ether-to-wei.js → utils/ether-to-wei.js} +4 -8
- package/lib/cjs/{is-address.d.ts → utils/is-address.d.ts} +0 -0
- package/lib/cjs/{is-address.js → utils/is-address.js} +3 -3
- package/lib/cjs/utils/tests/ether-to-wei.test.d.ts +1 -0
- package/lib/cjs/utils/tests/ether-to-wei.test.js +66 -0
- package/lib/cjs/utils/tests/is-address.test.d.ts +1 -0
- package/lib/cjs/utils/tests/is-address.test.js +56 -0
- package/lib/cjs/utils/tests/to-checksum-address.test.d.ts +1 -0
- package/lib/cjs/utils/tests/to-checksum-address.test.js +43 -0
- package/lib/cjs/utils/tests/wei-to-ether.test.d.ts +1 -0
- package/lib/cjs/utils/tests/wei-to-ether.test.js +69 -0
- package/lib/cjs/{to-checksum-address.d.ts → utils/to-checksum-address.d.ts} +0 -0
- package/lib/cjs/{to-checksum-address.js → utils/to-checksum-address.js} +1 -1
- package/lib/cjs/{wei-to-ether.d.ts → utils/wei-to-ether.d.ts} +3 -1
- package/lib/cjs/{wei-to-ether.js → utils/wei-to-ether.js} +4 -8
- package/lib/esm/classes/Contract.d.ts +12 -0
- package/lib/esm/classes/Contract.js +63 -0
- package/lib/esm/classes/get-block.test.d.ts +1 -0
- package/lib/esm/{rpc → classes}/get-block.test.js +5 -5
- package/lib/esm/classes/test/Contract/crv-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/crv-abi.js +470 -0
- package/lib/esm/classes/test/Contract/crv.test.d.ts +1 -0
- package/lib/esm/classes/test/Contract/crv.test.js +43 -0
- package/lib/esm/classes/test/Contract/fei-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/fei-abi.js +521 -0
- package/lib/esm/classes/test/Contract/fei.test.d.ts +1 -0
- package/lib/esm/classes/test/Contract/fei.test.js +46 -0
- package/lib/esm/classes/test/Contract/foo-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/foo-abi.js +33 -0
- package/lib/esm/classes/test/Contract/foo.test.d.ts +1 -0
- package/lib/esm/classes/test/Contract/foo.test.js +14 -0
- package/lib/esm/classes/test/Contract/uni.test.d.ts +1 -0
- package/lib/esm/classes/test/Contract/uni.test.js +59 -0
- package/lib/esm/classes/test/Contract/uniswap-abi.d.ts +2 -0
- package/lib/esm/classes/test/Contract/uniswap-abi.js +116 -0
- package/lib/esm/{rpc → classes}/utils/clean-block.d.ts +0 -0
- package/lib/esm/{rpc → classes}/utils/clean-block.js +0 -0
- package/lib/esm/{rpc → classes}/utils/clean-transaction.d.ts +0 -0
- package/lib/esm/{rpc → classes}/utils/clean-transaction.js +0 -0
- package/lib/esm/classes/utils/encode-decode-transaction.d.ts +3 -0
- package/lib/esm/classes/utils/encode-decode-transaction.js +85 -0
- package/lib/esm/classes/utils/fetchers.d.ts +9 -0
- package/lib/esm/{rpc → classes}/utils/fetchers.js +8 -1
- package/lib/esm/{rpc → classes}/utils/hex-to-decimal.d.ts +0 -0
- package/lib/esm/{rpc → classes}/utils/hex-to-decimal.js +0 -0
- package/lib/esm/index.d.ts +10 -6
- package/lib/esm/index.js +7 -6
- package/lib/esm/{rpc/index.d.ts → providers/JsonRpcProvider.d.ts} +2 -1
- package/lib/esm/{rpc/index.js → providers/JsonRpcProvider.js} +8 -5
- package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -2
- package/lib/esm/types/Contract.types.d.ts +22 -0
- package/lib/esm/types/Contract.types.js +1 -0
- package/lib/esm/utils/ether-to-wei.d.ts +3 -0
- package/lib/esm/utils/ether-to-wei.js +7 -0
- package/lib/esm/{is-address.d.ts → utils/is-address.d.ts} +0 -0
- package/lib/esm/{is-address.js → utils/is-address.js} +2 -2
- package/lib/esm/utils/tests/ether-to-wei.test.d.ts +1 -0
- package/lib/esm/utils/tests/ether-to-wei.test.js +36 -0
- package/lib/esm/utils/tests/is-address.test.d.ts +1 -0
- package/lib/esm/utils/tests/is-address.test.js +49 -0
- package/lib/esm/utils/tests/to-checksum-address.test.d.ts +1 -0
- package/lib/esm/utils/tests/to-checksum-address.test.js +36 -0
- package/lib/esm/utils/tests/wei-to-ether.test.d.ts +1 -0
- package/lib/esm/utils/tests/wei-to-ether.test.js +38 -0
- package/lib/esm/{to-checksum-address.d.ts → utils/to-checksum-address.d.ts} +0 -0
- package/lib/esm/{to-checksum-address.js → utils/to-checksum-address.js} +1 -1
- package/lib/esm/utils/wei-to-ether.d.ts +3 -0
- package/lib/esm/utils/wei-to-ether.js +7 -0
- package/package.json +11 -11
- package/readme.md +68 -5
- package/lib/cjs/rpc/utils/fetchers.d.ts +0 -7
- package/lib/esm/ether-to-wei.d.ts +0 -2
- package/lib/esm/ether-to-wei.js +0 -8
- package/lib/esm/rpc/utils/fetchers.d.ts +0 -7
- package/lib/esm/wei-to-ether.d.ts +0 -1
- package/lib/esm/wei-to-ether.js +0 -8
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const contracts_1 = require("@ethersproject/contracts");
|
|
13
|
+
const ethers_1 = require("ethers");
|
|
14
|
+
const JsonRpcProvider_1 = require("../../../providers/JsonRpcProvider");
|
|
15
|
+
const Contract_1 = require("../../Contract");
|
|
16
|
+
const uniswap_abi_1 = require("./uniswap-abi");
|
|
17
|
+
// The JSONABI
|
|
18
|
+
const JSONABI = uniswap_abi_1.uniswapABI;
|
|
19
|
+
const rpcURL = 'https://free-eth-node.com/api/eth';
|
|
20
|
+
const ethersProvider = (0, ethers_1.getDefaultProvider)(rpcURL);
|
|
21
|
+
const essentialEthProvider = new JsonRpcProvider_1.JsonRpcProvider(rpcURL);
|
|
22
|
+
// The UNI airdrop merkle address
|
|
23
|
+
// https://etherscan.io/address/0x090D4613473dEE047c3f2706764f49E0821D256e#readContract
|
|
24
|
+
const contractAddress = '0x090D4613473dEE047c3f2706764f49E0821D256e';
|
|
25
|
+
const smartContractIsUniClaimed = (contract, index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const isClaimed = (yield contract.isClaimed(index));
|
|
27
|
+
return isClaimed;
|
|
28
|
+
});
|
|
29
|
+
const smartContractGetUniMerkleRoot = (contract) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const merkleRoot = (yield contract.merkleRoot());
|
|
31
|
+
return merkleRoot;
|
|
32
|
+
});
|
|
33
|
+
const smartContractGetUniTokenAddress = (contract) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const merkleRoot = (yield contract.token());
|
|
35
|
+
return merkleRoot;
|
|
36
|
+
});
|
|
37
|
+
const ethersContract = new contracts_1.Contract(contractAddress, JSONABI, ethersProvider);
|
|
38
|
+
const essentialEthContract = new Contract_1.Contract(contractAddress, JSONABI, essentialEthProvider);
|
|
39
|
+
describe('UNI contract', () => {
|
|
40
|
+
it('should fetch "address" data-type', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
const [ethersResponse, essentialEthResponse] = yield Promise.all([
|
|
42
|
+
smartContractGetUniTokenAddress(ethersContract),
|
|
43
|
+
smartContractGetUniTokenAddress(essentialEthContract),
|
|
44
|
+
]);
|
|
45
|
+
/* 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984 */
|
|
46
|
+
expect(ethersResponse).toStrictEqual(essentialEthResponse);
|
|
47
|
+
}));
|
|
48
|
+
it('should fetch "bytes32" merkle root', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
const [ethersResponse, essentialEthResponse] = yield Promise.all([
|
|
50
|
+
smartContractGetUniMerkleRoot(ethersContract),
|
|
51
|
+
smartContractGetUniMerkleRoot(essentialEthContract),
|
|
52
|
+
]);
|
|
53
|
+
/* 0xc8500f8e2fcf3c9a32880e1b973fb28acc88be35787a8abcf9981b2b65dbdeb5 */
|
|
54
|
+
expect(ethersResponse).toStrictEqual(essentialEthResponse);
|
|
55
|
+
}));
|
|
56
|
+
it('should fetch isClaimed "boolean" for random airdrop indexes', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
+
/* indexes of addresses in the merkle tree */
|
|
58
|
+
const randomIndexes = [0, 4, 102, 999, 999999];
|
|
59
|
+
for (const i of randomIndexes) {
|
|
60
|
+
const [ethersClaimed, essentialEthClaimed] = yield Promise.all([
|
|
61
|
+
smartContractIsUniClaimed(ethersContract, i),
|
|
62
|
+
smartContractIsUniClaimed(essentialEthContract, i),
|
|
63
|
+
]);
|
|
64
|
+
expect(ethersClaimed).toStrictEqual(essentialEthClaimed);
|
|
65
|
+
}
|
|
66
|
+
}));
|
|
67
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniswapABI = void 0;
|
|
4
|
+
exports.uniswapABI =
|
|
5
|
+
// http://api.etherscan.io/api?module=contract&action=getabi&address=0x090d4613473dee047c3f2706764f49e0821d256e&format=raw
|
|
6
|
+
[
|
|
7
|
+
{
|
|
8
|
+
inputs: [
|
|
9
|
+
{
|
|
10
|
+
internalType: 'address',
|
|
11
|
+
name: 'token_',
|
|
12
|
+
type: 'address',
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
internalType: 'bytes32',
|
|
16
|
+
name: 'merkleRoot_',
|
|
17
|
+
type: 'bytes32',
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
stateMutability: 'nonpayable',
|
|
21
|
+
type: 'constructor',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
anonymous: false,
|
|
25
|
+
inputs: [
|
|
26
|
+
{
|
|
27
|
+
indexed: false,
|
|
28
|
+
internalType: 'uint256',
|
|
29
|
+
name: 'index',
|
|
30
|
+
type: 'uint256',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
indexed: false,
|
|
34
|
+
internalType: 'address',
|
|
35
|
+
name: 'account',
|
|
36
|
+
type: 'address',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
indexed: false,
|
|
40
|
+
internalType: 'uint256',
|
|
41
|
+
name: 'amount',
|
|
42
|
+
type: 'uint256',
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
name: 'Claimed',
|
|
46
|
+
type: 'event',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
inputs: [
|
|
50
|
+
{
|
|
51
|
+
internalType: 'uint256',
|
|
52
|
+
name: 'index',
|
|
53
|
+
type: 'uint256',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
internalType: 'address',
|
|
57
|
+
name: 'account',
|
|
58
|
+
type: 'address',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
internalType: 'uint256',
|
|
62
|
+
name: 'amount',
|
|
63
|
+
type: 'uint256',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
internalType: 'bytes32[]',
|
|
67
|
+
name: 'merkleProof',
|
|
68
|
+
type: 'bytes32[]',
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
name: 'claim',
|
|
72
|
+
outputs: [],
|
|
73
|
+
stateMutability: 'nonpayable',
|
|
74
|
+
type: 'function',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
inputs: [
|
|
78
|
+
{
|
|
79
|
+
internalType: 'uint256',
|
|
80
|
+
name: 'index',
|
|
81
|
+
type: 'uint256',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
name: 'isClaimed',
|
|
85
|
+
outputs: [
|
|
86
|
+
{
|
|
87
|
+
internalType: 'bool',
|
|
88
|
+
name: '',
|
|
89
|
+
type: 'bool',
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
stateMutability: 'view',
|
|
93
|
+
type: 'function',
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
inputs: [],
|
|
97
|
+
name: 'merkleRoot',
|
|
98
|
+
outputs: [
|
|
99
|
+
{
|
|
100
|
+
internalType: 'bytes32',
|
|
101
|
+
name: '',
|
|
102
|
+
type: 'bytes32',
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
stateMutability: 'view',
|
|
106
|
+
type: 'function',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
inputs: [],
|
|
110
|
+
name: 'token',
|
|
111
|
+
outputs: [
|
|
112
|
+
{
|
|
113
|
+
internalType: 'address',
|
|
114
|
+
name: '',
|
|
115
|
+
type: 'address',
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
stateMutability: 'view',
|
|
119
|
+
type: 'function',
|
|
120
|
+
},
|
|
121
|
+
];
|
|
File without changes
|
|
File without changes
|
|
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,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeRPCResponse = exports.encodeData = void 0;
|
|
4
|
+
const sha3_1 = require("sha3");
|
|
5
|
+
const __1 = require("../..");
|
|
6
|
+
const hex_to_decimal_1 = require("./hex-to-decimal");
|
|
7
|
+
const hexTrue = '0000000000000000000000000000000000000000000000000000000000000001';
|
|
8
|
+
const hexFalse = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
9
|
+
function expandType(type) {
|
|
10
|
+
// https://docs.soliditylang.org/en/v0.8.7/types.html#integers
|
|
11
|
+
if (type === 'uint[]') {
|
|
12
|
+
return 'uint256[]';
|
|
13
|
+
}
|
|
14
|
+
else if (type === 'int[]') {
|
|
15
|
+
return 'int256[]';
|
|
16
|
+
}
|
|
17
|
+
return type;
|
|
18
|
+
}
|
|
19
|
+
function encodeData(jsonABIArgument, args) {
|
|
20
|
+
const hash = new sha3_1.Keccak(256);
|
|
21
|
+
/* first 4 bytes will create the data parameter */
|
|
22
|
+
const functionString = `${jsonABIArgument.name}(${jsonABIArgument.inputs.map((input) => expandType(input.type))})`;
|
|
23
|
+
// encoding learnt from https://ethereum.stackexchange.com/questions/3514/how-to-call-a-contract-method-using-the-eth-call-json-rpc-api
|
|
24
|
+
const functionHash = hash.update(functionString).digest('hex');
|
|
25
|
+
// no arrays
|
|
26
|
+
const jsonABIInputsLength = jsonABIArgument.inputs.length;
|
|
27
|
+
let shouldValidateInputLength = true;
|
|
28
|
+
// inputs contains 1 or more arrays
|
|
29
|
+
if (jsonABIArgument.inputs.find((input) => input.type.includes('['))) {
|
|
30
|
+
shouldValidateInputLength = false;
|
|
31
|
+
}
|
|
32
|
+
if (shouldValidateInputLength && args.length !== jsonABIInputsLength) {
|
|
33
|
+
throw new Error(`args inputs of "${args.length}" does not match expected length of "${jsonABIArgument.inputs.length}"`);
|
|
34
|
+
}
|
|
35
|
+
const argsWithTypes = (jsonABIArgument.inputs || []).reduce((acc, input, i) => {
|
|
36
|
+
var _a;
|
|
37
|
+
if (input.type.includes('[')) {
|
|
38
|
+
// strip array and length like "[2]" from type
|
|
39
|
+
const basicType = (_a = /([^[]*)\[.*$/g.exec(input.type)) === null || _a === void 0 ? void 0 : _a[1];
|
|
40
|
+
args.forEach((arg) => {
|
|
41
|
+
acc = acc.concat([[arg, basicType]]);
|
|
42
|
+
});
|
|
43
|
+
return acc;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return acc.concat([[args[i], input.type]]);
|
|
47
|
+
}
|
|
48
|
+
}, []);
|
|
49
|
+
const encodedArgs = argsWithTypes.map(([arg, inputType]) => {
|
|
50
|
+
let rawArg = arg;
|
|
51
|
+
if (inputType === 'bool') {
|
|
52
|
+
return arg ? hexTrue : hexFalse;
|
|
53
|
+
}
|
|
54
|
+
else if (inputType.startsWith('bytes')) {
|
|
55
|
+
// encode each character to hex
|
|
56
|
+
const argEncoded = rawArg
|
|
57
|
+
.split('')
|
|
58
|
+
.map((character) => character.charCodeAt(0).toString(16))
|
|
59
|
+
.join('');
|
|
60
|
+
const paddedEncodedArg = argEncoded.padEnd(64, '0');
|
|
61
|
+
return paddedEncodedArg;
|
|
62
|
+
}
|
|
63
|
+
// remove leading "0x" on address types
|
|
64
|
+
else if (inputType === 'address') {
|
|
65
|
+
rawArg = arg.replace(/^0x/g, '').toLowerCase();
|
|
66
|
+
}
|
|
67
|
+
const argEncoded = rawArg.toString(16);
|
|
68
|
+
const paddedEncodedArg = argEncoded.padStart(64, '0');
|
|
69
|
+
return paddedEncodedArg;
|
|
70
|
+
});
|
|
71
|
+
const functionEncoded = functionHash.slice(0, 8);
|
|
72
|
+
const data = `0x${functionEncoded}${encodedArgs.join('')}`;
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
exports.encodeData = encodeData;
|
|
76
|
+
function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
77
|
+
const rawOutputs = jsonABIArgument.outputs;
|
|
78
|
+
// chunk response every 64 characters
|
|
79
|
+
const encodedOutputs = nodeResponse.slice(2).match(/.{1,64}/g);
|
|
80
|
+
const outputs = (encodedOutputs || []).map((output, i) => {
|
|
81
|
+
const outputType = (rawOutputs || [])[i].type;
|
|
82
|
+
switch (outputType) {
|
|
83
|
+
case 'bool':
|
|
84
|
+
return output === hexTrue;
|
|
85
|
+
case 'address':
|
|
86
|
+
/* address types have 26 leading zeroes to remove */
|
|
87
|
+
return (0, __1.toChecksumAddress)(`0x${output.slice(24)}`);
|
|
88
|
+
case 'uint256':
|
|
89
|
+
return (0, __1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(`0x${output}`));
|
|
90
|
+
case 'bytes32':
|
|
91
|
+
return `0x${output}`;
|
|
92
|
+
case 'uint8':
|
|
93
|
+
return Number((0, hex_to_decimal_1.hexToDecimal)(`0x${output}`));
|
|
94
|
+
default:
|
|
95
|
+
throw new Error(`essential-eth does not yet support "${outputType}" inputs. Make a PR today!"`);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return outputs.length === 1 ? outputs[0] : outputs;
|
|
99
|
+
}
|
|
100
|
+
exports.decodeRPCResponse = decodeRPCResponse;
|
|
@@ -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 {};
|
|
@@ -12,7 +12,14 @@ function post(url, body) {
|
|
|
12
12
|
'Content-Type': 'application/json',
|
|
13
13
|
},
|
|
14
14
|
body: JSON.stringify(body),
|
|
15
|
-
})
|
|
15
|
+
})
|
|
16
|
+
.then((r) => r.json())
|
|
17
|
+
.then((response) => {
|
|
18
|
+
if (response.error) {
|
|
19
|
+
throw new Error(response.error);
|
|
20
|
+
}
|
|
21
|
+
return response.result;
|
|
22
|
+
});
|
|
16
23
|
}
|
|
17
24
|
exports.post = post;
|
|
18
25
|
function buildRPCPostBody(method, params) {
|
|
File without changes
|
|
File without changes
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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 {
|
|
6
|
-
import {
|
|
7
|
-
|
|
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/cjs/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
const
|
|
7
|
-
Object.defineProperty(exports, "
|
|
8
|
-
|
|
9
|
-
Object.defineProperty(exports, "EssentialEth", { enumerable: true, get: function () { return index_1.EssentialEth; } });
|
|
3
|
+
exports.Contract = exports.TinyBig = exports.tinyBig = exports.weiToEther = exports.etherToWei = exports.isAddress = exports.toChecksumAddress = exports.JsonRpcProvider = exports.jsonRpcProvider = void 0;
|
|
4
|
+
const Contract_1 = require("./classes/Contract");
|
|
5
|
+
Object.defineProperty(exports, "Contract", { enumerable: true, get: function () { return Contract_1.Contract; } });
|
|
6
|
+
const JsonRpcProvider_1 = require("./providers/JsonRpcProvider");
|
|
7
|
+
Object.defineProperty(exports, "JsonRpcProvider", { enumerable: true, get: function () { return JsonRpcProvider_1.JsonRpcProvider; } });
|
|
8
|
+
Object.defineProperty(exports, "jsonRpcProvider", { enumerable: true, get: function () { return JsonRpcProvider_1.jsonRpcProvider; } });
|
|
10
9
|
const tiny_big_1 = require("./shared/tiny-big/tiny-big");
|
|
11
10
|
Object.defineProperty(exports, "tinyBig", { enumerable: true, get: function () { return tiny_big_1.tinyBig; } });
|
|
12
11
|
Object.defineProperty(exports, "TinyBig", { enumerable: true, get: function () { return tiny_big_1.TinyBig; } });
|
|
13
|
-
const
|
|
12
|
+
const ether_to_wei_1 = require("./utils/ether-to-wei");
|
|
13
|
+
Object.defineProperty(exports, "etherToWei", { enumerable: true, get: function () { return ether_to_wei_1.etherToWei; } });
|
|
14
|
+
const is_address_1 = require("./utils/is-address");
|
|
15
|
+
Object.defineProperty(exports, "isAddress", { enumerable: true, get: function () { return is_address_1.isAddress; } });
|
|
16
|
+
const to_checksum_address_1 = require("./utils/to-checksum-address");
|
|
14
17
|
Object.defineProperty(exports, "toChecksumAddress", { enumerable: true, get: function () { return to_checksum_address_1.toChecksumAddress; } });
|
|
15
|
-
const wei_to_ether_1 = require("./wei-to-ether");
|
|
18
|
+
const wei_to_ether_1 = require("./utils/wei-to-ether");
|
|
16
19
|
Object.defineProperty(exports, "weiToEther", { enumerable: true, get: function () { return wei_to_ether_1.weiToEther; } });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Block } from '../types/block.types';
|
|
2
|
-
export declare class
|
|
2
|
+
export declare class JsonRpcProvider {
|
|
3
3
|
/**
|
|
4
4
|
* The URL to your Eth node. Consider POKT or Infura
|
|
5
5
|
*/
|
|
@@ -10,3 +10,7 @@ export declare class EssentialEth {
|
|
|
10
10
|
*/
|
|
11
11
|
getBlock(timeFrame: 'latest' | 'earliest' | 'pending' | number, returnTransactionObjects?: boolean): Promise<Block>;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Helper function to avoid "new"
|
|
15
|
+
*/
|
|
16
|
+
export declare function jsonRpcProvider(rpcUrl?: string): JsonRpcProvider;
|
|
@@ -9,10 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
-
const clean_block_1 = require("
|
|
14
|
-
const fetchers_1 = require("
|
|
15
|
-
class
|
|
12
|
+
exports.jsonRpcProvider = exports.JsonRpcProvider = void 0;
|
|
13
|
+
const clean_block_1 = require("../classes/utils/clean-block");
|
|
14
|
+
const fetchers_1 = require("../classes/utils/fetchers");
|
|
15
|
+
class JsonRpcProvider {
|
|
16
16
|
constructor(rpcUrl) {
|
|
17
17
|
this._rpcUrl = rpcUrl || 'https://free-eth-node.com/api/eth';
|
|
18
18
|
}
|
|
@@ -30,12 +30,19 @@ class EssentialEth {
|
|
|
30
30
|
// "latest", "earliest", and "pending" require no manipulation
|
|
31
31
|
rpcTimeFrame = timeFrame;
|
|
32
32
|
}
|
|
33
|
-
const nodeResponse = yield (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_getBlockByNumber', [
|
|
33
|
+
const nodeResponse = (yield (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_getBlockByNumber', [
|
|
34
34
|
rpcTimeFrame,
|
|
35
35
|
returnTransactionObjects,
|
|
36
|
-
]))
|
|
36
|
+
])));
|
|
37
37
|
return (0, clean_block_1.cleanBlock)(nodeResponse, returnTransactionObjects);
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
exports.
|
|
41
|
+
exports.JsonRpcProvider = JsonRpcProvider;
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to avoid "new"
|
|
44
|
+
*/
|
|
45
|
+
function jsonRpcProvider(rpcUrl) {
|
|
46
|
+
return new JsonRpcProvider(rpcUrl);
|
|
47
|
+
}
|
|
48
|
+
exports.jsonRpcProvider = jsonRpcProvider;
|
|
@@ -4,11 +4,11 @@ import Big from 'big.js';
|
|
|
4
4
|
* The only important things to know are the "toString" and "toNumber" functions
|
|
5
5
|
*/
|
|
6
6
|
export declare class TinyBig extends Big {
|
|
7
|
-
constructor(value: number | string);
|
|
7
|
+
constructor(value: number | string | TinyBig);
|
|
8
8
|
toNumber(): number;
|
|
9
9
|
toString(): string;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* Helper factory function so that you don't have to type "new" when instantiating a new TinyBig
|
|
13
13
|
*/
|
|
14
|
-
export declare function tinyBig(value: number | string): TinyBig;
|
|
14
|
+
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[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
3
|
/**
|
|
3
4
|
* Similar to ["parseEther" in ethers.js](https://docs.ethers.io/v4/api-utils.html#ether-strings-and-wei)
|
|
4
5
|
*
|
|
@@ -20,4 +21,4 @@ import { TinyBig } from './shared/tiny-big/tiny-big';
|
|
|
20
21
|
* // 1000000000000000000000
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
23
|
-
export declare function etherToWei(etherQuantity: string | number): TinyBig;
|
|
24
|
+
export declare function etherToWei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.etherToWei = void 0;
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const validate_type_1 = require("./shared/validate-type");
|
|
4
|
+
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
|
+
const validate_type_1 = require("../shared/validate-type");
|
|
10
6
|
/**
|
|
11
7
|
* Similar to ["parseEther" in ethers.js](https://docs.ethers.io/v4/api-utils.html#ether-strings-and-wei)
|
|
12
8
|
*
|
|
@@ -29,8 +25,8 @@ const validate_type_1 = require("./shared/validate-type");
|
|
|
29
25
|
* ```
|
|
30
26
|
*/
|
|
31
27
|
function etherToWei(etherQuantity) {
|
|
32
|
-
(0, validate_type_1.validateType)(etherQuantity, ['string', 'number']);
|
|
33
|
-
const result = (0,
|
|
28
|
+
(0, validate_type_1.validateType)(etherQuantity, ['string', 'number', 'object']);
|
|
29
|
+
const result = (0, tiny_big_1.tinyBig)(etherQuantity).times('1000000000000000000');
|
|
34
30
|
return (0, tiny_big_1.tinyBig)(result);
|
|
35
31
|
}
|
|
36
32
|
exports.etherToWei = etherToWei;
|
|
File without changes
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isAddress = void 0;
|
|
4
|
-
const
|
|
5
|
-
const validate_type_1 = require("
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const validate_type_1 = require("../shared/validate-type");
|
|
6
6
|
/**
|
|
7
7
|
* Returns a boolean as to whether the input is a valid address.
|
|
8
8
|
* Does NOT support ICAP addresses
|
|
@@ -10,7 +10,7 @@ const validate_type_1 = require("./shared/validate-type");
|
|
|
10
10
|
function isAddress(address) {
|
|
11
11
|
(0, validate_type_1.validateType)(address, ['string']);
|
|
12
12
|
try {
|
|
13
|
-
(0,
|
|
13
|
+
(0, __1.toChecksumAddress)(address);
|
|
14
14
|
return true;
|
|
15
15
|
}
|
|
16
16
|
catch (error) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
const big_js_1 = __importDefault(require("big.js"));
|
|
26
|
+
const ethers = __importStar(require("ethers"));
|
|
27
|
+
const web3_1 = __importDefault(require("web3"));
|
|
28
|
+
const index_1 = require("../../index");
|
|
29
|
+
const helpers_1 = require("../../shared/tiny-big/helpers");
|
|
30
|
+
describe('ether-to-wei', () => {
|
|
31
|
+
it('happy path', () => {
|
|
32
|
+
expect((0, index_1.etherToWei)('100').toString()).toStrictEqual('100000000000000000000');
|
|
33
|
+
expect((0, index_1.etherToWei)(100).toString()).toStrictEqual('100000000000000000000');
|
|
34
|
+
expect((0, index_1.etherToWei)('1000.0').toString()).toStrictEqual('1000000000000000000000');
|
|
35
|
+
expect((0, index_1.etherToWei)(1000).toString()).toStrictEqual('1000000000000000000000');
|
|
36
|
+
expect((0, index_1.etherToWei)('1000.0').toNumber()).toStrictEqual(1000000000000000000000);
|
|
37
|
+
expect((0, index_1.etherToWei)((0, index_1.tinyBig)(1000)).toString()).toStrictEqual('1000000000000000000000');
|
|
38
|
+
expect((0, index_1.etherToWei)((0, index_1.tinyBig)('1000.0')).toNumber()).toStrictEqual(1000000000000000000000);
|
|
39
|
+
expect((0, index_1.etherToWei)((0, big_js_1.default)(1000)).toString()).toStrictEqual('1000000000000000000000');
|
|
40
|
+
expect((0, index_1.etherToWei)((0, big_js_1.default)('1000.0')).toNumber()).toStrictEqual(1000000000000000000000);
|
|
41
|
+
});
|
|
42
|
+
it('matches ethers and web3 toString', () => {
|
|
43
|
+
expect((0, index_1.etherToWei)('-09999.0').toString()).toStrictEqual(ethers.utils.parseEther('-09999.0').toString());
|
|
44
|
+
expect((0, index_1.etherToWei)('-09999.0').toString()).toStrictEqual(web3_1.default.utils.toWei('-09999.0', 'ether'));
|
|
45
|
+
});
|
|
46
|
+
it('matches ethers and web3 toNumber', () => {
|
|
47
|
+
/* easy */
|
|
48
|
+
expect((0, index_1.etherToWei)('9').toNumber()).toStrictEqual(Number(ethers.utils.parseEther('9')));
|
|
49
|
+
// web3 responds with scientific notation
|
|
50
|
+
expect((0, index_1.etherToWei)('9').toNumber()).toStrictEqual(Number(web3_1.default.utils.toWei('9', 'ether')));
|
|
51
|
+
/* harder */
|
|
52
|
+
expect((0, index_1.etherToWei)('-0999999.90').toNumber()).toStrictEqual(Number(ethers.utils.parseEther('-0999999.90')) /* overflow error if you use the ".toNumber()", */);
|
|
53
|
+
// web3 responds with scientific notation
|
|
54
|
+
expect((0, index_1.etherToWei)('-0999999.9').toNumber()).toStrictEqual(Number((0, helpers_1.scientificStrToDecimalStr)(web3_1.default.utils.toWei('-0999999.9', 'ether'))));
|
|
55
|
+
});
|
|
56
|
+
it('wrong types', () => {
|
|
57
|
+
expect(() => {
|
|
58
|
+
// @ts-expect-error should not accept boolean
|
|
59
|
+
(0, index_1.etherToWei)(false);
|
|
60
|
+
}).toThrow();
|
|
61
|
+
expect(() => {
|
|
62
|
+
// @ts-expect-error should not accept array
|
|
63
|
+
(0, index_1.etherToWei)([1, 2, 3]);
|
|
64
|
+
}).toThrow();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|