|
| 1 | +package io.api.core.impl; |
| 2 | + |
| 3 | +import io.api.core.IProxyProvider; |
| 4 | +import io.api.executor.IHttpExecutor; |
| 5 | +import io.api.manager.IQueueManager; |
| 6 | +import io.api.model.proxy.BlockProxy; |
| 7 | +import io.api.model.proxy.TxProxy; |
| 8 | +import io.api.model.proxy.utility.BlockProxyTO; |
| 9 | +import io.api.model.proxy.utility.StringProxyTO; |
| 10 | +import io.api.model.proxy.utility.TxProxyTO; |
| 11 | +import io.api.util.BasicUtils; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | + |
| 14 | +import java.math.BigInteger; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +/** |
| 18 | + * ! NO DESCRIPTION ! |
| 19 | + * |
| 20 | + * @author GoodforGod |
| 21 | + * @since 28.10.2018 |
| 22 | + */ |
| 23 | +public class ProxyProvider extends BasicProvider implements IProxyProvider { |
| 24 | + |
| 25 | + private static final String ACT_BLOCKNO_PARAM = ACT_PREFIX + "eth_blockNumber"; |
| 26 | + private static final String ACT_BY_BLOCKNO_PARAM = ACT_PREFIX + "eth_getBlockByNumber"; |
| 27 | + private static final String ACT_UNCLE_BY_BLOCKNOINDEX_PARAM = ACT_PREFIX + "eth_getUncleByBlockNumberAndIndex"; |
| 28 | + private static final String ACT_BLOCKTX_COUNT_PARAM = ACT_PREFIX + "eth_getBlockTransactionCountByNumber"; |
| 29 | + private static final String ACT_TX_BY_HASH_PARAM = ACT_PREFIX + "eth_getTransactionByHash"; |
| 30 | + private static final String ACT_TX_BY_BLOCKNOINDEX_PARAM = ACT_PREFIX + "eth_getTransactionByBlockNumberAndIndex"; |
| 31 | + private static final String ACT_TX_COUNT_PARAM = ACT_PREFIX + "eth_getTransactionCount"; |
| 32 | + private static final String ACT_SEND_RAW_TX_PARAM = ACT_PREFIX + "eth_sendRawTransaction"; |
| 33 | + private static final String ACT_TX_RECEIPT_PARAM = ACT_PREFIX + "eth_getTransactionReceipt"; |
| 34 | + private static final String ACT_CALL_PARAM = ACT_PREFIX + "eth_call"; |
| 35 | + private static final String ACT_CODE_PARAM = ACT_PREFIX + "eth_getCode"; |
| 36 | + private static final String ACT_STORAGEAT_PARAM = ACT_PREFIX + "eth_getStorageAt"; |
| 37 | + private static final String ACT_GASPRICE_PARAM = ACT_PREFIX + "eth_gasPrice"; |
| 38 | + private static final String ACT_ESTIMATEGAS_PARAM = ACT_PREFIX + "eth_estimateGas"; |
| 39 | + |
| 40 | + private static final String BOOLEAN_PARAM = "&boolean=true"; |
| 41 | + private static final String TAG_LAST_PARAM = "&tag=lastest"; |
| 42 | + private static final String GASPRICE_PARAM = "&gasPrice="; |
| 43 | + private static final String POSITION_PARAM = "&position="; |
| 44 | + private static final String ADDRESS_PARAM = "&address="; |
| 45 | + private static final String TXHASH_PARAM = "&txhash="; |
| 46 | + private static final String INDEX_PARAM = "&index="; |
| 47 | + private static final String VALUE_PARAM = "&value="; |
| 48 | + private static final String DATA_PARAM = "&data="; |
| 49 | + private static final String GAS_PARAM = "&gas="; |
| 50 | + private static final String TAG_PARAM = "&tag="; |
| 51 | + private static final String HEX_PARAM = "&hex="; |
| 52 | + private static final String TO_PARAM = "&to="; |
| 53 | + |
| 54 | + ProxyProvider(final IQueueManager queue, |
| 55 | + final String baseUrl, |
| 56 | + final IHttpExecutor executor) { |
| 57 | + super(queue, "proxy", baseUrl,executor); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public long blockNoLast() { |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + @NotNull |
| 66 | + @Override |
| 67 | + public Optional<BlockProxy> block(final long blockNo) { |
| 68 | + final long compBlockNo = BasicUtils.compensateMinBlock(blockNo); |
| 69 | + |
| 70 | + final String urlParams = ACT_BY_BLOCKNO_PARAM + TAG_PARAM + compBlockNo; |
| 71 | + final BlockProxyTO response = getRequest(urlParams, BlockProxyTO.class); |
| 72 | + return Optional.ofNullable(response.getResult()); |
| 73 | + } |
| 74 | + |
| 75 | + @NotNull |
| 76 | + @Override |
| 77 | + public Optional<BlockProxy> blockUncle(final long blockNo, final long index) { |
| 78 | + final long compBlockNo = BasicUtils.compensateMinBlock(blockNo); |
| 79 | + final long compIndex = BasicUtils.compensateMinBlock(index); |
| 80 | + |
| 81 | + final String urlParams = ACT_UNCLE_BY_BLOCKNOINDEX_PARAM + TAG_PARAM + compBlockNo + INDEX_PARAM + compIndex; |
| 82 | + final BlockProxyTO response = getRequest(urlParams, BlockProxyTO.class); |
| 83 | + return Optional.ofNullable(response.getResult()); |
| 84 | + } |
| 85 | + |
| 86 | + @NotNull |
| 87 | + @Override |
| 88 | + public Optional<TxProxy> tx(final String txhash) { |
| 89 | + BasicUtils.validateTxHash(txhash); |
| 90 | + |
| 91 | + final String urlParams = ACT_TX_BY_HASH_PARAM + TXHASH_PARAM + txhash; |
| 92 | + final TxProxyTO response = getRequest(urlParams, TxProxyTO.class); |
| 93 | + return Optional.ofNullable(response.getResult()); |
| 94 | + } |
| 95 | + |
| 96 | + @NotNull |
| 97 | + @Override |
| 98 | + public Optional<TxProxy> tx(final long blockNo, final long index) { |
| 99 | + final long compBlockNo = BasicUtils.compensateMinBlock(blockNo); |
| 100 | + final long compIndex = BasicUtils.compensateMinBlock(index); |
| 101 | + |
| 102 | + final String urlParams = ACT_TX_BY_BLOCKNOINDEX_PARAM + TAG_PARAM + compBlockNo + INDEX_PARAM + compIndex; |
| 103 | + final TxProxyTO response = getRequest(urlParams, TxProxyTO.class); |
| 104 | + return Optional.ofNullable(response.getResult()); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int txCount(final long blockNo) { |
| 109 | + final long compensatedBlockNo = BasicUtils.compensateMinBlock(blockNo); |
| 110 | + final String urlParams = ACT_TX_COUNT_PARAM + TAG_PARAM + compensatedBlockNo; |
| 111 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 112 | + return Integer.valueOf(response.getResult()); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int txSendCount(final String address) { |
| 117 | + BasicUtils.validateAddress(address); |
| 118 | + |
| 119 | + final String urlParams = ACT_TX_BY_HASH_PARAM + ADDRESS_PARAM + address + TAG_LAST_PARAM; |
| 120 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 121 | + return (int) BasicUtils.parseHex(response.getResult()); |
| 122 | + } |
| 123 | + |
| 124 | + //TODO need postRequest executor implementation |
| 125 | + @Override |
| 126 | + public boolean txSendRaw(final String hexEncodedTx) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + @NotNull |
| 131 | + @Override |
| 132 | + public String call(final String address, final String data) { |
| 133 | + BasicUtils.validateAddress(address); |
| 134 | + |
| 135 | + final String urlParams = ACT_CALL_PARAM + TO_PARAM + address + DATA_PARAM + data + TAG_LAST_PARAM; |
| 136 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 137 | + return (BasicUtils.isEmpty(response.getResult())) |
| 138 | + ? "" |
| 139 | + : response.getResult(); |
| 140 | + } |
| 141 | + |
| 142 | + @NotNull |
| 143 | + @Override |
| 144 | + public String code(final String address) { |
| 145 | + BasicUtils.validateAddress(address); |
| 146 | + |
| 147 | + final String urlParams = ACT_CODE_PARAM + ADDRESS_PARAM + address + TAG_LAST_PARAM; |
| 148 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 149 | + return (BasicUtils.isEmpty(response.getResult())) |
| 150 | + ? "" |
| 151 | + : response.getResult(); |
| 152 | + } |
| 153 | + |
| 154 | + @NotNull |
| 155 | + @Override |
| 156 | + public String storageAt(final String address, final long position) { |
| 157 | + BasicUtils.validateAddress(address); |
| 158 | + final long compPosition = BasicUtils.compensateMinBlock(position); |
| 159 | + |
| 160 | + final String urlParams = ACT_STORAGEAT_PARAM + ADDRESS_PARAM + address + POSITION_PARAM + compPosition + TAG_LAST_PARAM; |
| 161 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 162 | + return (BasicUtils.isEmpty(response.getResult())) |
| 163 | + ? "" |
| 164 | + : response.getResult(); |
| 165 | + } |
| 166 | + |
| 167 | + @NotNull |
| 168 | + @Override |
| 169 | + public BigInteger gasPrice() { |
| 170 | + final StringProxyTO response = getRequest(ACT_GASPRICE_PARAM, StringProxyTO.class); |
| 171 | + return (BasicUtils.isEmpty(response.getResult())) |
| 172 | + ? BigInteger.valueOf(-1) |
| 173 | + : BigInteger.valueOf(BasicUtils.parseHex(response.getResult())); |
| 174 | + } |
| 175 | + |
| 176 | + @NotNull |
| 177 | + @Override |
| 178 | + public BigInteger gasEstimated() { |
| 179 | + final String urlParams = ACT_STORAGEAT_PARAM + VALUE_PARAM + "0xff22" |
| 180 | + + GAS_PARAM + "0xffffff" + GASPRICE_PARAM + "0x151da038cc"; |
| 181 | + final StringProxyTO response = getRequest(urlParams, StringProxyTO.class); |
| 182 | + return (BasicUtils.isEmpty(response.getResult())) |
| 183 | + ? BigInteger.valueOf(-1) |
| 184 | + : BigInteger.valueOf(BasicUtils.parseHex(response.getResult())); |
| 185 | + } |
| 186 | +} |
0 commit comments