private String convert(BigInteger value) { if (value != null) { return Numeric.encodeQuantity(value); } else { return null; } } }
private static String convert(BigInteger value) { if (value != null) { return Numeric.encodeQuantity(value); } else { return null; // we don't want the field to be encoded if not present } } }
@Override @JsonValue public String getValue() { return Numeric.encodeQuantity(blockNumber); }
@Override public Request<?, EthBlock> ethGetUncleByBlockHashAndIndex( String blockHash, BigInteger transactionIndex) { return new Request<>( "eth_getUncleByBlockHashAndIndex", Arrays.asList( blockHash, Numeric.encodeQuantity(transactionIndex)), web3jService, EthBlock.class); }
@Override public Request<?, EthTransaction> ethGetTransactionByBlockHashAndIndex( String blockHash, BigInteger transactionIndex) { return new Request<>( "eth_getTransactionByBlockHashAndIndex", Arrays.asList( blockHash, Numeric.encodeQuantity(transactionIndex)), web3jService, EthTransaction.class); }
@Override public Request<?, EthGetStorageAt> ethGetStorageAt( String address, BigInteger position, DefaultBlockParameter defaultBlockParameter) { return new Request<>( "eth_getStorageAt", Arrays.asList( address, Numeric.encodeQuantity(position), defaultBlockParameter.getValue()), web3jService, EthGetStorageAt.class); }
@Override public Request<?, EthBlock> ethGetUncleByBlockNumberAndIndex( DefaultBlockParameter defaultBlockParameter, BigInteger uncleIndex) { return new Request<>( "eth_getUncleByBlockNumberAndIndex", Arrays.asList( defaultBlockParameter.getValue(), Numeric.encodeQuantity(uncleIndex)), web3jService, EthBlock.class); }
@Override public Request<?, EthTransaction> ethGetTransactionByBlockNumberAndIndex( DefaultBlockParameter defaultBlockParameter, BigInteger transactionIndex) { return new Request<>( "eth_getTransactionByBlockNumberAndIndex", Arrays.asList( defaultBlockParameter.getValue(), Numeric.encodeQuantity(transactionIndex)), web3jService, EthTransaction.class); }
@Test public void testQuantityEncode() { assertThat(Numeric.encodeQuantity(BigInteger.valueOf(0)), is("0x0")); assertThat(Numeric.encodeQuantity(BigInteger.valueOf(1)), is("0x1")); assertThat(Numeric.encodeQuantity(BigInteger.valueOf(1024)), is("0x400")); assertThat(Numeric.encodeQuantity( BigInteger.valueOf(Long.MAX_VALUE)), is("0x7fffffffffffffff")); assertThat(Numeric.encodeQuantity( new BigInteger("204516877000845695339750056077105398031")), is("0x99dc848b94efc27edfad28def049810f")); }
@Test(expected = MessageEncodingException.class) public void testQuantityEncodeNegative() { Numeric.encodeQuantity(BigInteger.valueOf(-1)); }
private EthBlock createBlockWithTransactions(int blockNumber, List<Transaction> transactions) { EthBlock ethBlock = new EthBlock(); EthBlock.Block block = new EthBlock.Block(); block.setNumber(Numeric.encodeQuantity(BigInteger.valueOf(blockNumber))); List<EthBlock.TransactionResult> transactionResults = transactions.stream() .map(it -> (EthBlock.TransactionResult<Transaction>) () -> it) .collect(Collectors.toList()); block.setTransactions(transactionResults); ethBlock.setResult(block); return ethBlock; }
private void configureLatestBlock(long timestamp) throws IOException { EthBlock.Block block = new EthBlock.Block(); block.setTimestamp(Numeric.encodeQuantity(BigInteger.valueOf(timestamp))); EthBlock ethBlock = new EthBlock(); ethBlock.setResult(block); when(web3jService.send(any(Request.class), eq(EthBlock.class))) .thenReturn(ethBlock); }
private EthBlock createBlock(int number) { EthBlock ethBlock = new EthBlock(); EthBlock.Block block = new EthBlock.Block(); block.setNumber(Numeric.encodeQuantity(BigInteger.valueOf(number))); ethBlock.setResult(block); return ethBlock; }
Mockito.when(web3j.ethBlockNumber().sendAsync()).thenReturn(supplyAsync(() -> { EthBlockNumber ethBlockNumber = new EthBlockNumber(); ethBlockNumber.setResult(Numeric.encodeQuantity(blockNumber)); return ethBlockNumber; })); Mockito.when(web3j.netPeerCount().sendAsync()).thenReturn(supplyAsync(() -> { NetPeerCount netPeerCount = new NetPeerCount(); netPeerCount.setResult(Numeric.encodeQuantity(netPeer)); return netPeerCount; }));