diff --git a/ethereumj/pom.xml b/ethereumj/pom.xml index 9676106e38da..8b6d3677c971 100644 --- a/ethereumj/pom.xml +++ b/ethereumj/pom.xml @@ -48,6 +48,13 @@ 1.5.0-RELEASE + + + org.web3j + core + 3.3.1 + + javax.servlet diff --git a/ethereumj/src/main/java/com/baeldung/web3j/Template.java b/ethereumj/src/main/java/com/baeldung/web3j/Template.java new file mode 100644 index 000000000000..7d4867bf2140 --- /dev/null +++ b/ethereumj/src/main/java/com/baeldung/web3j/Template.java @@ -0,0 +1,41 @@ +package com.baeldung.web3j; + +import com.baeldung.web3j.contracts.Greeting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.web3j.crypto.Credentials; +import org.web3j.crypto.WalletUtils; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.protocol.http.HttpService; +import org.web3j.tx.Contract; +import org.web3j.tx.ManagedTransaction; + +public class Template { + + private Logger l = LoggerFactory.getLogger(Template.class); + + private void deployContract() throws Exception{ + + Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/")); + + Credentials credentials = + WalletUtils.loadCredentials( + "", + "/path/to/"); + + Greeting contract = Greeting.deploy( + web3j, credentials, + ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, + "Hello blockchain world!").send(); + + String contractAddress = contract.getContractAddress(); + l.debug("Smart contract deployed to address "+ contractAddress); + + l.debug("Value stored in remote smart contract: "+ contract.greet().send()); + + TransactionReceipt transactionReceipt = contract.setGreeting("Well hello again").send(); + + l.debug("New value stored in remote smart contract: "+ contract.greet().send()); + } +} diff --git a/ethereumj/src/main/java/com/baeldung/web3j/contracts/Greeting.java b/ethereumj/src/main/java/com/baeldung/web3j/contracts/Greeting.java new file mode 100644 index 000000000000..f4f4f90760e5 --- /dev/null +++ b/ethereumj/src/main/java/com/baeldung/web3j/contracts/Greeting.java @@ -0,0 +1,71 @@ +package com.baeldung.web3j.contracts; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import org.web3j.abi.FunctionEncoder; +import org.web3j.abi.TypeReference; +import org.web3j.abi.datatypes.Function; +import org.web3j.abi.datatypes.Type; +import org.web3j.abi.datatypes.Utf8String; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.RemoteCall; +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.tx.Contract; +import org.web3j.tx.TransactionManager; + +/** + *

Auto generated code. + *

Do not modify! + *

Please use the web3j command line tools, + * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the + * codegen module to update. + * + *

Generated with web3j version 3.3.1. + */ +public class Greeting extends Contract { + private static final String BINARY = "6060604052341561000f57600080fd5b6040516103cc3803806103cc833981016040528080519091019050600181805161003d92916020019061005f565b505060008054600160a060020a03191633600160a060020a03161790556100fa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a057805160ff19168380011785556100cd565b828001600101855582156100cd579182015b828111156100cd5782518255916020019190600101906100b2565b506100d99291506100dd565b5090565b6100f791905b808211156100d957600081556001016100e3565b90565b6102c3806101096000396000f30060606040526004361061004b5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663a41368628114610050578063cfae3217146100a3575b600080fd5b341561005b57600080fd5b6100a160046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061012d95505050505050565b005b34156100ae57600080fd5b6100b6610144565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f25780820151838201526020016100da565b50505050905090810190601f16801561011f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60018180516101409291602001906101ed565b5050565b61014c61026b565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061022e57805160ff191683800117855561025b565b8280016001018555821561025b579182015b8281111561025b578251825591602001919060010190610240565b5061026792915061027d565b5090565b60206040519081016040526000815290565b6101ea91905b8082111561026757600081556001016102835600a165627a7a723058206cfb726ed213c2fe842a4c886c8089e918b6de9c6cdfb372fa459eca4840c5740029"; + + protected Greeting(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + protected Greeting(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + public RemoteCall setGreeting(String _message) { + final Function function = new Function( + "setGreeting", + Arrays.asList(new org.web3j.abi.datatypes.Utf8String(_message)), + Collections.>emptyList()); + return executeRemoteCallTransaction(function); + } + + public RemoteCall greet() { + final Function function = new Function("greet", + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + return executeRemoteCallSingleValueReturn(function, String.class); + } + + public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String _message) { + String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(new org.web3j.abi.datatypes.Utf8String(_message))); + return deployRemoteCall(Greeting.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor); + } + + public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String _message) { + String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(new org.web3j.abi.datatypes.Utf8String(_message))); + return deployRemoteCall(Greeting.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor); + } + + public static Greeting load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + return new Greeting(contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + public static Greeting load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { + return new Greeting(contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } +} + diff --git a/ethereumj/src/main/resources/solidity/Greeting.sol b/ethereumj/src/main/resources/solidity/Greeting.sol new file mode 100644 index 000000000000..6a2861274a9b --- /dev/null +++ b/ethereumj/src/main/resources/solidity/Greeting.sol @@ -0,0 +1,19 @@ +pragma solidity ^0.4.0; + +contract Greeting { + address creator; + string message; + + function Greeting(string _message) { + message = _message; + creator = msg.sender; + } + + function greet() constant returns (string) { + return message; + } + + function setGreeting(string _message) { + message = _message; + } +} diff --git a/ethereumj/src/main/resources/solidity/build/Greeting.abi b/ethereumj/src/main/resources/solidity/build/Greeting.abi new file mode 100644 index 000000000000..9cb196c6659c --- /dev/null +++ b/ethereumj/src/main/resources/solidity/build/Greeting.abi @@ -0,0 +1 @@ +[{"constant":false,"inputs":[{"name":"_message","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_message","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}] \ No newline at end of file diff --git a/ethereumj/src/main/resources/solidity/build/Greeting.bin b/ethereumj/src/main/resources/solidity/build/Greeting.bin new file mode 100644 index 000000000000..05c12a02acd2 --- /dev/null +++ b/ethereumj/src/main/resources/solidity/build/Greeting.bin @@ -0,0 +1 @@ +6060604052341561000f57600080fd5b6040516103cc3803806103cc833981016040528080519091019050600181805161003d92916020019061005f565b505060008054600160a060020a03191633600160a060020a03161790556100fa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a057805160ff19168380011785556100cd565b828001600101855582156100cd579182015b828111156100cd5782518255916020019190600101906100b2565b506100d99291506100dd565b5090565b6100f791905b808211156100d957600081556001016100e3565b90565b6102c3806101096000396000f30060606040526004361061004b5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663a41368628114610050578063cfae3217146100a3575b600080fd5b341561005b57600080fd5b6100a160046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061012d95505050505050565b005b34156100ae57600080fd5b6100b6610144565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f25780820151838201526020016100da565b50505050905090810190601f16801561011f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60018180516101409291602001906101ed565b5050565b61014c61026b565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061022e57805160ff191683800117855561025b565b8280016001018555821561025b579182015b8281111561025b578251825591602001919060010190610240565b5061026792915061027d565b5090565b60206040519081016040526000815290565b6101ea91905b8082111561026757600081556001016102835600a165627a7a723058206cfb726ed213c2fe842a4c886c8089e918b6de9c6cdfb372fa459eca4840c5740029 \ No newline at end of file