Skip to content

Commit 5a8e4a7

Browse files
Alex Vargaspivovarit
authored andcommitted
Adding web3j contract deployment example (eugenp#3911)
Committer: Alex Vargas <alexvargas1425@gmail.com>
1 parent 30eeb09 commit 5a8e4a7

6 files changed

Lines changed: 140 additions & 0 deletions

File tree

‎ethereumj/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
<version>1.5.0-RELEASE</version>
4949
</dependency>
5050

51+
<!-- Web3j -->
52+
<dependency>
53+
<groupId>org.web3j</groupId>
54+
<artifactId>core</artifactId>
55+
<version>3.3.1</version>
56+
</dependency>
57+
5158
<!-- JSTL/JSP -->
5259
<dependency>
5360
<groupId>javax.servlet</groupId>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.web3j;
2+
3+
import com.baeldung.web3j.contracts.Greeting;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.web3j.crypto.Credentials;
7+
import org.web3j.crypto.WalletUtils;
8+
import org.web3j.protocol.Web3j;
9+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
10+
import org.web3j.protocol.http.HttpService;
11+
import org.web3j.tx.Contract;
12+
import org.web3j.tx.ManagedTransaction;
13+
14+
public class Template {
15+
16+
private Logger l = LoggerFactory.getLogger(Template.class);
17+
18+
private void deployContract() throws Exception{
19+
20+
Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/<your_token>"));
21+
22+
Credentials credentials =
23+
WalletUtils.loadCredentials(
24+
"<password>",
25+
"/path/to/<walletfile>");
26+
27+
Greeting contract = Greeting.deploy(
28+
web3j, credentials,
29+
ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT,
30+
"Hello blockchain world!").send();
31+
32+
String contractAddress = contract.getContractAddress();
33+
l.debug("Smart contract deployed to address "+ contractAddress);
34+
35+
l.debug("Value stored in remote smart contract: "+ contract.greet().send());
36+
37+
TransactionReceipt transactionReceipt = contract.setGreeting("Well hello again").send();
38+
39+
l.debug("New value stored in remote smart contract: "+ contract.greet().send());
40+
}
41+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.web3j.contracts;
2+
3+
import java.math.BigInteger;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import org.web3j.abi.FunctionEncoder;
7+
import org.web3j.abi.TypeReference;
8+
import org.web3j.abi.datatypes.Function;
9+
import org.web3j.abi.datatypes.Type;
10+
import org.web3j.abi.datatypes.Utf8String;
11+
import org.web3j.crypto.Credentials;
12+
import org.web3j.protocol.Web3j;
13+
import org.web3j.protocol.core.RemoteCall;
14+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
15+
import org.web3j.tx.Contract;
16+
import org.web3j.tx.TransactionManager;
17+
18+
/**
19+
* <p>Auto generated code.
20+
* <p><strong>Do not modify!</strong>
21+
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
22+
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
23+
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
24+
*
25+
* <p>Generated with web3j version 3.3.1.
26+
*/
27+
public class Greeting extends Contract {
28+
private static final String BINARY = "6060604052341561000f57600080fd5b6040516103cc3803806103cc833981016040528080519091019050600181805161003d92916020019061005f565b505060008054600160a060020a03191633600160a060020a03161790556100fa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a057805160ff19168380011785556100cd565b828001600101855582156100cd579182015b828111156100cd5782518255916020019190600101906100b2565b506100d99291506100dd565b5090565b6100f791905b808211156100d957600081556001016100e3565b90565b6102c3806101096000396000f30060606040526004361061004b5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663a41368628114610050578063cfae3217146100a3575b600080fd5b341561005b57600080fd5b6100a160046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061012d95505050505050565b005b34156100ae57600080fd5b6100b6610144565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f25780820151838201526020016100da565b50505050905090810190601f16801561011f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60018180516101409291602001906101ed565b5050565b61014c61026b565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061022e57805160ff191683800117855561025b565b8280016001018555821561025b579182015b8281111561025b578251825591602001919060010190610240565b5061026792915061027d565b5090565b60206040519081016040526000815290565b6101ea91905b8082111561026757600081556001016102835600a165627a7a723058206cfb726ed213c2fe842a4c886c8089e918b6de9c6cdfb372fa459eca4840c5740029";
29+
30+
protected Greeting(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
31+
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
32+
}
33+
34+
protected Greeting(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
35+
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
36+
}
37+
38+
public RemoteCall<TransactionReceipt> setGreeting(String _message) {
39+
final Function function = new Function(
40+
"setGreeting",
41+
Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_message)),
42+
Collections.<TypeReference<?>>emptyList());
43+
return executeRemoteCallTransaction(function);
44+
}
45+
46+
public RemoteCall<String> greet() {
47+
final Function function = new Function("greet",
48+
Arrays.<Type>asList(),
49+
Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
50+
return executeRemoteCallSingleValueReturn(function, String.class);
51+
}
52+
53+
public static RemoteCall<Greeting> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String _message) {
54+
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_message)));
55+
return deployRemoteCall(Greeting.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
56+
}
57+
58+
public static RemoteCall<Greeting> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String _message) {
59+
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_message)));
60+
return deployRemoteCall(Greeting.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
61+
}
62+
63+
public static Greeting load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
64+
return new Greeting(contractAddress, web3j, credentials, gasPrice, gasLimit);
65+
}
66+
67+
public static Greeting load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
68+
return new Greeting(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
69+
}
70+
}
71+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pragma solidity ^0.4.0;
2+
3+
contract Greeting {
4+
address creator;
5+
string message;
6+
7+
function Greeting(string _message) {
8+
message = _message;
9+
creator = msg.sender;
10+
}
11+
12+
function greet() constant returns (string) {
13+
return message;
14+
}
15+
16+
function setGreeting(string _message) {
17+
message = _message;
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6060604052341561000f57600080fd5b6040516103cc3803806103cc833981016040528080519091019050600181805161003d92916020019061005f565b505060008054600160a060020a03191633600160a060020a03161790556100fa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a057805160ff19168380011785556100cd565b828001600101855582156100cd579182015b828111156100cd5782518255916020019190600101906100b2565b506100d99291506100dd565b5090565b6100f791905b808211156100d957600081556001016100e3565b90565b6102c3806101096000396000f30060606040526004361061004b5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663a41368628114610050578063cfae3217146100a3575b600080fd5b341561005b57600080fd5b6100a160046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061012d95505050505050565b005b34156100ae57600080fd5b6100b6610144565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f25780820151838201526020016100da565b50505050905090810190601f16801561011f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60018180516101409291602001906101ed565b5050565b61014c61026b565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061022e57805160ff191683800117855561025b565b8280016001018555821561025b579182015b8281111561025b578251825591602001919060010190610240565b5061026792915061027d565b5090565b60206040519081016040526000815290565b6101ea91905b8082111561026757600081556001016102835600a165627a7a723058206cfb726ed213c2fe842a4c886c8089e918b6de9c6cdfb372fa459eca4840c5740029

0 commit comments

Comments
 (0)