@Test public void testEthCall() throws Exception { EthCall ethCall = web3j.ethCall(config.buildTransaction(), DefaultBlockParameter.valueOf("latest")).send(); assertThat(DefaultBlockParameterName.LATEST.getValue(), is("latest")); assertThat(ethCall.getValue(), is("0x")); }
public String getValue() { return getResult(); } }
@Override public String getContractAddressById(String fromAddress, int chainId, String contractId) { List<Type> arguments = new ArrayList<>(); List<TypeReference<?>> returnValues = new ArrayList<>(); returnValues.add(new TypeReference<Address>() { }); arguments.add(stringToBytes32(contractId)); Function getContractAddressById = new Function("getContractAddressById", arguments, returnValues); String encodedFunction = FunctionEncoder.encode(getContractAddressById); Transaction ethCallTransaction = createEthCallTransaction(fromAddress, proxyContractAddressProvider.getProxyContractAddress(chainId), encodedFunction); try { EthCall rawResponse = web3jProvider.get(chainId) .ethCall(ethCallTransaction, DefaultBlockParameterName.LATEST) .send(); if (!rawResponse.hasError()) { List<Type> response = FunctionReturnDecoder.decode(rawResponse.getValue(), getContractAddressById.getOutputParameters()); return ((Address) response.get(0)).getValue(); } else { throw new RuntimeException(mapErrorToMessage(rawResponse.getError())); } } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testEthCall() { buildResponse( "{\n" + " \"id\":1,\n" + " \"jsonrpc\": \"2.0\",\n" + " \"result\": \"0x\"\n" + "}" ); EthCall ethCall = deserialiseResponse(EthCall.class); assertThat(ethCall.getValue(), is("0x")); }
@Test public void testCallMultipleValue() throws Exception { EthCall ethCall = new EthCall(); ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000037" + "0000000000000000000000000000000000000000000000000000000000000007"); prepareCall(ethCall); assertThat(contract.callMultipleValue().send(), equalTo(Arrays.asList( new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.valueOf(7))))); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); } }
@Test public void testResolve() throws Exception { configureSyncing(false); configureLatestBlock(System.currentTimeMillis() / 1000); // block timestamp is in seconds NetVersion netVersion = new NetVersion(); netVersion.setResult(Byte.toString(ChainId.MAINNET)); String resolverAddress = "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a"; String contractAddress = "0x00000000000000000000000019e03255f667bdfd50a32722df860b1eeaf4d635"; EthCall resolverAddressResponse = new EthCall(); resolverAddressResponse.setResult(resolverAddress); EthCall contractAddressResponse = new EthCall(); contractAddressResponse.setResult(contractAddress); when(web3jService.send(any(Request.class), eq(NetVersion.class))) .thenReturn(netVersion); when(web3jService.send(any(Request.class), eq(EthCall.class))) .thenReturn(resolverAddressResponse); when(web3jService.send(any(Request.class), eq(EthCall.class))) .thenReturn(contractAddressResponse); assertThat(ensResolver.resolve("web3j.eth"), is("0x19e03255f667bdfd50a32722df860b1eeaf4d635")); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
@Test public void testCallSingleValue() throws Exception { // Example taken from FunctionReturnDecoderTest EthCall ethCall = new EthCall(); ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000000"); prepareCall(ethCall); assertThat(contract.callSingleValue().send(), equalTo(new Utf8String(""))); }
/** * Execute constant function call - i.e. a call that does not change state of the contract * * @param function to call * @return {@link List} of values returned by function call */ private List<Type> executeCall( Function function) throws IOException { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall ethCall = web3j.ethCall( Transaction.createEthCallTransaction( transactionManager.getFromAddress(), contractAddress, encodedFunction), defaultBlockParameter) .send(); String value = ethCall.getValue(); return FunctionReturnDecoder.decode(value, function.getOutputParameters()); }
@Test public void testReverseResolve() throws Exception { configureSyncing(false); configureLatestBlock(System.currentTimeMillis() / 1000); // block timestamp is in seconds NetVersion netVersion = new NetVersion(); netVersion.setResult(Byte.toString(ChainId.MAINNET)); String resolverAddress = "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a"; String contractName = "0x0000000000000000000000000000000000000000000000000000000000000020" + TypeEncoder.encode(new Utf8String("web3j.eth")); System.err.println(contractName); EthCall resolverAddressResponse = new EthCall(); resolverAddressResponse.setResult(resolverAddress); EthCall contractNameResponse = new EthCall(); contractNameResponse.setResult(contractName); when(web3jService.send(any(Request.class), eq(NetVersion.class))) .thenReturn(netVersion); when(web3jService.send(any(Request.class), eq(EthCall.class))) .thenReturn(resolverAddressResponse); when(web3jService.send(any(Request.class), eq(EthCall.class))) .thenReturn(contractNameResponse); assertThat(ensResolver.reverseResolve("0x19e03255f667bdfd50a32722df860b1eeaf4d635"), is("web3j.eth")); }
@InvokeOnHeader(Web3jConstants.ETH_CALL) void ethCall(Message message) throws IOException { String fromAddress = message.getHeader(Web3jConstants.FROM_ADDRESS, configuration::getFromAddress, String.class); String toAddress = message.getHeader(Web3jConstants.TO_ADDRESS, configuration::getToAddress, String.class); BigInteger nonce = message.getHeader(Web3jConstants.NONCE, configuration::getNonce, BigInteger.class); BigInteger gasPrice = message.getHeader(Web3jConstants.GAS_PRICE, configuration::getGasPrice, BigInteger.class); BigInteger gasLimit = message.getHeader(Web3jConstants.GAS_LIMIT, configuration::getGasLimit, BigInteger.class); BigInteger value = message.getHeader(Web3jConstants.VALUE, configuration::getValue, BigInteger.class); String data = message.getHeader(Web3jConstants.DATA, configuration::getData, String.class); DefaultBlockParameter atBlock = toBlockParameter(message.getHeader(Web3jConstants.AT_BLOCK, configuration::getAtBlock, Object.class)); org.web3j.protocol.core.methods.request.Transaction transaction = new org.web3j.protocol.core.methods.request.Transaction( fromAddress, nonce, gasPrice, gasLimit, toAddress, value, data); Request<?, EthCall> request = web3j.ethCall(transaction, atBlock); setRequestId(message, request); EthCall response = request.send(); boolean hasError = checkForError(message, response); if (!hasError) { message.setBody(response.getValue()); } }
private String callSmartContractFunction( org.web3j.abi.datatypes.Function function, String contractAddress, Wallet wallet) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction(wallet.address, contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = mWeb3j.ethCall( Transaction.createEthCallTransaction( mAddress, contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
/** * 查询代币精度 * * @param web3j * @param contractAddress * @return */ public static int getTokenDecimals(Web3j web3j, String contractAddress) { String methodName = "decimals"; String fromAddr = emptyAddress; int decimal = 0; List<Type> inputParameters = new ArrayList<>(); List<TypeReference<?>> outputParameters = new ArrayList<>(); TypeReference<Uint8> typeReference = new TypeReference<Uint8>() { }; outputParameters.add(typeReference); Function function = new Function(methodName, inputParameters, outputParameters); String data = FunctionEncoder.encode(function); Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data); EthCall ethCall; try { ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get(); List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters()); decimal = Integer.parseInt(results.get(0).getValue().toString()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return decimal; }
/** * 查询代币符号 * * @param web3j * @param contractAddress * @return */ public static String getTokenSymbol(Web3j web3j, String contractAddress) { String methodName = "symbol"; String symbol = null; String fromAddr = emptyAddress; List<Type> inputParameters = new ArrayList<>(); List<TypeReference<?>> outputParameters = new ArrayList<>(); TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() { }; outputParameters.add(typeReference); Function function = new Function(methodName, inputParameters, outputParameters); String data = FunctionEncoder.encode(function); Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data); EthCall ethCall; try { ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get(); List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters()); symbol = results.get(0).getValue().toString(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return symbol; }
/** * 查询代币名称 * * @param web3j * @param contractAddress * @return */ public static String getTokenName(Web3j web3j, String contractAddress) { String methodName = "name"; String name = null; String fromAddr = emptyAddress; List<Type> inputParameters = new ArrayList<>(); List<TypeReference<?>> outputParameters = new ArrayList<>(); TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() { }; outputParameters.add(typeReference); Function function = new Function(methodName, inputParameters, outputParameters); String data = FunctionEncoder.encode(function); Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data); EthCall ethCall; try { ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get(); List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters()); name = results.get(0).getValue().toString(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return name; }