-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.js
63 lines (55 loc) · 1.84 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const ETHERSCAN_API_KEY = 'H9Q2BDA55J85PISTNG8BDM5IKD4MGTUVW4';
const ALCHEMY_API_KEY = 'FY6BwiO9_hzVN4N2Fx8Ti-BeukyI2XiM';
const _web3Instance = {};
let _contractInstance;
async function dataFetchAbi(contractAddress, network) {
if (!contractAddress) {
throw new Error('Contract address is empty.');
}
if (!contractAddress.startsWith('0x')) {
throw new Error('Contract address does not start with 0x.');
}
if (contractAddress.length != 42) {
throw new Error('Contract address should be 20 bytes.');
}
const abiReq = await _fetchJson(
`https://api.etherscan.io/api?module=contract&action=getabi&address=${contractAddress}&apikey=${ETHERSCAN_API_KEY}`
);
if (abiReq.status === '1' && abiReq.result) {
return JSON.parse(abiReq.result);
} else {
throw new Error('ABI not found.');
}
}
function dataEncodeFunctionSignature(abiField, network) {
return _getWeb3(network).eth.abi.encodeFunctionSignature(abiField);
}
function dataInitializeContractInstance(contractAddress, network, abi) {
_contractInstance = new (_getWeb3(network).eth.Contract)(abi, contractAddress);
}
async function dataQueryFunction(abiField, inputs, blockNumber, from) {
return _contractInstance.methods[abiField.name](...inputs).call();
}
function dataValidateType(type, value) {
if (value === '') {
throw new Error('Value is empty.');
}
if (type === 'address') {
if (!value.startsWith('0x')) {
throw new Error('Value does not start with 0x.');
}
if (value.length != 42) {
throw new Error('Value should be 20 bytes.');
}
}
}
function _getWeb3(network) {
if (!_web3Instance[network]) {
_web3Instance[network] = new Web3(`https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_API_KEY}`);
}
return _web3Instance[network];
}
async function _fetchJson(url) {
const res = await fetch(url);
return await res.json();
}