|
| 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 | +}; |
0 commit comments