Skip to content

Commit 783cc78

Browse files
committed
Splitting init to small files
1 parent ffc8c55 commit 783cc78

File tree

9 files changed

+645
-606
lines changed

9 files changed

+645
-606
lines changed

lib/account.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
const querystring = require('querystring');
2+
module.exports = function(getRequest, apiKey) {
3+
return {
4+
/**
5+
* Returns the amount of Tokens a specific account owns.
6+
* @param {string} address - Contract address
7+
* @param {string} tokenname - Name of the token
8+
* @param {string} contractaddress - Contract address
9+
* @example
10+
* var supply = api.account.tokenbalance(
11+
* '0x4366ddc115d8cf213c564da36e64c8ebaa30cdbd',
12+
* 'DGD',
13+
* ''
14+
* );
15+
* @returns {Promise.<object>}
16+
*/
17+
tokenbalance(address, tokenname, contractaddress) {
18+
19+
const module = 'account';
20+
const action = 'tokenbalance';
21+
const tag = 'latest';
22+
23+
var queryObject = {
24+
module, action, apiKey, tag
25+
};
26+
27+
if (contractaddress) {
28+
queryObject.contractaddress = contractaddress;
29+
}
30+
31+
if (tokenname) {
32+
queryObject.tokenname = tokenname;
33+
}
34+
35+
if (address) {
36+
queryObject.address = address;
37+
}
38+
39+
var query = querystring.stringify(queryObject);
40+
return getRequest(query);
41+
},
42+
/**
43+
* Returns the balance of a sepcific account
44+
* @param {string} address - Address
45+
* @example
46+
* var balance = api.account.balance('0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae');
47+
* @returns {Promise.<object>}
48+
*/
49+
balance(address) {
50+
const module = 'account';
51+
let action = 'balance';
52+
const tag = 'latest';
53+
54+
if (typeof address !== 'string' && address && address.length) {
55+
address = address.join(',');
56+
action = 'balancemulti';
57+
}
58+
59+
var query = querystring.stringify({
60+
module, action, tag, address, apiKey
61+
});
62+
return getRequest(query);
63+
},
64+
/**
65+
* Get a list of internal transactions
66+
* @param {string} txhash - Transaction hash. If specified then address will be ignored
67+
* @param {string} address - Transaction address
68+
* @param {string} startblock - start looking here
69+
* @param {string} endblock - end looking there
70+
* @param {string} sort - Sort asc/desc
71+
* @example
72+
* var txlist = api.account.txlistinternal('0x40eb908387324f2b575b4879cd9d7188f69c8fc9d87c901b9e2daaea4b442170');
73+
* @returns {Promise.<object>}
74+
*/
75+
txlistinternal(txhash, address, startblock, endblock, sort) {
76+
const module = 'account';
77+
const action = 'txlistinternal';
78+
79+
var queryObject = {
80+
module,
81+
action,
82+
apiKey
83+
};
84+
85+
if (!startblock) {
86+
startblock = 0;
87+
}
88+
89+
if (!endblock) {
90+
endblock = 'latest';
91+
}
92+
93+
if (!sort) {
94+
sort = 'asc';
95+
}
96+
97+
if (txhash) {
98+
queryObject.txhash = txhash;
99+
} else {
100+
queryObject.address = address;
101+
}
102+
103+
queryObject.txhash = txhash;
104+
105+
return getRequest(querystring.stringify(queryObject));
106+
},
107+
/**
108+
* Get a list of transactions for a specfic address
109+
* @param {string} address - Transaction address
110+
* @param {string} startblock - start looking here
111+
* @param {string} endblock - end looking there
112+
* @param {string} sort - Sort asc/desc
113+
* @example
114+
* var txlist = api.account.txlist('0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae', 1, 'latest', 'asc');
115+
* @returns {Promise.<object>}
116+
*/
117+
txlist(address, startblock, endblock, sort) {
118+
const module = 'account';
119+
const action = 'txlist';
120+
121+
if (!startblock) {
122+
startblock = 0;
123+
}
124+
125+
if (!endblock) {
126+
endblock = 'latest';
127+
}
128+
129+
if (!sort) {
130+
sort = 'asc';
131+
}
132+
133+
var query = querystring.stringify({
134+
module, action, startblock, endblock, sort, address, apiKey
135+
});
136+
137+
return getRequest(query);
138+
},
139+
/**
140+
* Get a list of blocks that a specific account has mineds
141+
* @example
142+
* var txlist = api.account.getminedblocks('0x9dd134d14d1e65f84b706d6f205cd5b1cd03a46b');
143+
* @param {string} address - Transaction hash
144+
*/
145+
getminedblocks(address) {
146+
const module = 'account';
147+
const action = 'getminedblocks';
148+
var query = querystring.stringify({
149+
module, action, address, apiKey
150+
});
151+
return getRequest(query);
152+
}
153+
};
154+
};

lib/block.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const querystring = require('querystring');
2+
module.exports = function(getRequest, apiKey) {
3+
return {
4+
/**
5+
* Find the block reward for a given address and block
6+
* @param {string} address - Address of the block
7+
* @param {string} blockno - Block number
8+
* @returns {Promise.<object>}
9+
*/
10+
getblockreward(address, blockno) {
11+
const module = 'block';
12+
const action = 'getblockreward';
13+
if (!blockno) {
14+
blockno = 0;
15+
}
16+
var query = querystring.stringify({
17+
module, action, address, blockno, apiKey
18+
});
19+
return getRequest(query);
20+
}
21+
};
22+
};

lib/contract.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const querystring = require('querystring');
2+
module.exports = function(getRequest, apiKey) {
3+
return {
4+
/**
5+
* Returns the ABI/Interface of a given contract
6+
* @param {string} address - Contract address
7+
* @example
8+
* api.contract
9+
* .getabi('0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359')
10+
* .at('0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359')
11+
* .memberId('0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359')
12+
* .then(console.log)
13+
* @returns {Promise.<object>}
14+
*/
15+
getabi(address) {
16+
const module = 'contract';
17+
const action = 'getabi';
18+
19+
var query = querystring.stringify({
20+
module, action, address, apiKey
21+
});
22+
23+
return getRequest(query);
24+
}
25+
};
26+
};

lib/get-request.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
const axios = require('axios');
3+
/**
4+
* @param {string} chain
5+
* @returns {string}
6+
*/
7+
function pickChainUrl(chain) {
8+
if (!chain || !TESTNET_API_URL_MAP[chain]) {
9+
return MAIN_API_URL;
10+
}
11+
12+
return TESTNET_API_URL_MAP[chain];
13+
}
14+
15+
16+
const MAIN_API_URL = 'https://api.etherscan.io';
17+
const TESTNET_API_URL_MAP = {
18+
ropsten: 'http://api-ropsten.etherscan.io',
19+
kovan: 'http://api-kovan.etherscan.io',
20+
rinkeby: 'https://api-rinkeby.etherscan.io',
21+
homestead: 'https://api.etherscan.io'
22+
};
23+
24+
module.exports = function(chain) {
25+
var client = axios.create({
26+
baseURL: pickChainUrl(chain),
27+
timeout: 3000
28+
});
29+
30+
/**
31+
* @param query
32+
* @returns {Promise<any>}
33+
*/
34+
function getRequest(query) {
35+
return new Promise(function(resolve, reject) {
36+
client.get('/api?' + query).then(function(response) {
37+
var data = response.data;
38+
39+
if (data.status && data.status != 1) {
40+
return reject(data.message);
41+
}
42+
43+
if (data.error) {
44+
return reject(new Error(data.error));
45+
}
46+
resolve(data);
47+
}).catch(function(error) {
48+
return reject(new Error(error));
49+
});
50+
});
51+
}
52+
53+
return getRequest;
54+
};

0 commit comments

Comments
 (0)