diff --git a/Artboard.png b/Artboard.png new file mode 100644 index 0000000..a8f50fc Binary files /dev/null and b/Artboard.png differ diff --git a/file1 b/file1 index 9daeafb..3ce5b46 100644 --- a/file1 +++ b/file1 @@ -1 +1,2 @@ test +done diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..64b0c01 --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + org.web3j.sample + sample-project-maven + 1.0.0-SNAPSHOT + + + 3.6.0 + 1.2.3 + 0.3.7 + 4.12 + 1.6.0 + 1.8 + 1.8 + + + + + org.web3j + core + ${web3j.version} + + + ch.qos.logback + logback-classic + ${logback-classic.version} + runtime + + + junit + junit + ${junit.version} + + + + + + + org.web3j + web3j-maven-plugin + ${web3j-maven-plugin.version} + + + src/main/solidity + + *.sol + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin} + + org.web3j.sample.Application + + + + + + diff --git a/src/main/java/org/web3j/sample/Application.java b/src/main/java/org/web3j/sample/Application.java new file mode 100644 index 0000000..5abb0f6 --- /dev/null +++ b/src/main/java/org/web3j/sample/Application.java @@ -0,0 +1,113 @@ +package org.web3j.sample; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.web3j.crypto.Credentials; +import org.web3j.crypto.WalletUtils; +import org.web3j.model.Greeter; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.protocol.http.HttpService; +import org.web3j.tx.Transfer; +import org.web3j.tx.gas.ContractGasProvider; +import org.web3j.tx.gas.DefaultGasProvider; +import org.web3j.utils.Convert; +import org.web3j.utils.Numeric; + +import java.math.BigDecimal; + +/** + * A simple web3j application that demonstrates a number of core features of web3j: + * + *
    + *
  1. Connecting to a node on the Ethereum network
  2. + *
  3. Loading an Ethereum wallet file
  4. + *
  5. Sending Ether from one address to another
  6. + *
  7. Deploying a smart contract to the network
  8. + *
  9. Reading a value from the deployed smart contract
  10. + *
  11. Updating a value in the deployed smart contract
  12. + *
  13. Viewing an event logged by the smart contract
  14. + *
+ * + *

To run this demo, you will need to provide: + * + *

    + *
  1. Ethereum client (or node) endpoint. The simplest thing to do is + * request a free access token from Infura
  2. + *
  3. A wallet file. This can be generated using the web3j + * command line tools
  4. + *
  5. Some Ether. This can be requested from the + * Rinkeby Faucet
  6. + *
+ * + *

For further background information, refer to the project README. + */ +public class Application { + + private static final Logger log = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) throws Exception { + new Application().run(); + } + + private void run() throws Exception { + + // We start by creating a new web3j instance to connect to remote nodes on the network. + // Note: if using web3j Android, use Web3jFactory.build(... + Web3j web3j = Web3j.build(new HttpService( + "https://rinkeby.infura.io/")); // FIXME: Enter your Infura token here; + log.info("Connected to Ethereum client version: " + + web3j.web3ClientVersion().send().getWeb3ClientVersion()); + + // We then need to load our Ethereum wallet file + // FIXME: Generate a new wallet file using the web3j command line tools https://docs.web3j.io/command_line.html + Credentials credentials = + WalletUtils.loadCredentials( + "", + "/path/to/"); + log.info("Credentials loaded"); + + // FIXME: Request some Ether for the Rinkeby test network at https://www.rinkeby.io/#faucet + log.info("Sending 1 Wei (" + + Convert.fromWei("1", Convert.Unit.ETHER).toPlainString() + " Ether)"); + TransactionReceipt transferReceipt = Transfer.sendFunds( + web3j, credentials, + "0x19e03255f667bdfd50a32722df860b1eeaf4d635", // you can put any address here + BigDecimal.ONE, Convert.Unit.WEI) // 1 wei = 10^-18 Ether + .send(); + log.info("Transaction complete, view it at https://rinkeby.etherscan.io/tx/" + + transferReceipt.getTransactionHash()); + + // Now lets deploy a smart contract + log.info("Deploying smart contract"); + ContractGasProvider contractGasProvider = new DefaultGasProvider(); + Greeter contract = Greeter.deploy( + web3j, + credentials, + contractGasProvider, + "test" + ).send(); + + String contractAddress = contract.getContractAddress(); + log.info("Smart contract deployed to address " + contractAddress); + log.info("View contract at https://rinkeby.etherscan.io/address/" + contractAddress); + + log.info("Value stored in remote smart contract: " + contract.greet().send()); + + // Lets modify the value in our smart contract + TransactionReceipt transactionReceipt = contract.newGreeting("Well hello again").send(); + + log.info("New value stored in remote smart contract: " + contract.greet().send()); + + // Events enable us to log specific events happening during the execution of our smart + // contract to the blockchain. Index events cannot be logged in their entirety. + // For Strings and arrays, the hash of values is provided, not the original value. + // For further information, refer to https://docs.web3j.io/filters.html#filters-and-events + for (Greeter.ModifiedEventResponse event : contract.getModifiedEvents(transactionReceipt)) { + log.info("Modify event fired, previous value: " + event.oldGreeting + + ", new value: " + event.newGreeting); + log.info("Indexed event previous value: " + Numeric.toHexString(event.oldGreetingIdx) + + ", new value: " + Numeric.toHexString(event.newGreetingIdx)); + } + } +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..82f5336 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/src/main/solidity/Greeter.sol b/src/main/solidity/Greeter.sol new file mode 100644 index 0000000..d6b5a13 --- /dev/null +++ b/src/main/solidity/Greeter.sol @@ -0,0 +1,40 @@ +pragma solidity ^0.4.24; + +// Modified Greeter contract. Based on example at https://www.ethereum.org/greeter. + +contract Mortal { + /* Define variable owner of the type address*/ + address owner; + + /* this function is executed at initialization and sets the owner of the contract */ + constructor () public { owner = msg.sender; } + + /* Function to recover the funds on the contract */ + function kill() public { if (msg.sender == owner) selfdestruct(owner); } +} + +contract Greeter is Mortal { + /* define variable greeting of the type string */ + string greeting; + + /* this runs when the contract is executed */ + constructor (string _greeting) public { + greeting = _greeting; + } + + function newGreeting(string _greeting) public { + emit Modified(greeting, _greeting, greeting, _greeting); + greeting = _greeting; + } + + /* main function */ + function greet() public constant returns (string) { + return greeting; + } + + /* we include indexed events to demonstrate the difference that can be + captured versus non-indexed */ + event Modified( + string indexed oldGreetingIdx, string indexed newGreetingIdx, + string oldGreeting, string newGreeting); +} diff --git a/src/main/test/java/org/web3j/sample/GreeterContractIT.java b/src/main/test/java/org/web3j/sample/GreeterContractIT.java new file mode 100644 index 0000000..31c6307 --- /dev/null +++ b/src/main/test/java/org/web3j/sample/GreeterContractIT.java @@ -0,0 +1,14 @@ +package org.web3j.sample; + +import org.junit.Test; + +/** + * Integration test to run our main application. + */ +public class GreeterContractIT { + + @Test + public void testGreeterContract() throws Exception { + Application.main(new String[]{ }); + } +}