Skip to content
Open

Done #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Artboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions file1
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
done
66 changes: 66 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.web3j.sample</groupId>
<artifactId>sample-project-maven</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<web3j.version>3.6.0</web3j.version>
<logback-classic.version>1.2.3</logback-classic.version>
<web3j-maven-plugin.version>0.3.7</web3j-maven-plugin.version>
<junit.version>4.12</junit.version>
<exec-maven-plugin>1.6.0</exec-maven-plugin>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>${web3j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.web3j</groupId>
<artifactId>web3j-maven-plugin</artifactId>
<version>${web3j-maven-plugin.version}</version>
<configuration>
<soliditySourceFiles>
<directory>src/main/solidity</directory>
<includes>
<include>*.sol</include>
</includes>
</soliditySourceFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin}</version>
<configuration>
<mainClass>org.web3j.sample.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
113 changes: 113 additions & 0 deletions src/main/java/org/web3j/sample/Application.java
Original file line number Diff line number Diff line change
@@ -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:
*
* <ol>
* <li>Connecting to a node on the Ethereum network</li>
* <li>Loading an Ethereum wallet file</li>
* <li>Sending Ether from one address to another</li>
* <li>Deploying a smart contract to the network</li>
* <li>Reading a value from the deployed smart contract</li>
* <li>Updating a value in the deployed smart contract</li>
* <li>Viewing an event logged by the smart contract</li>
* </ol>
*
* <p>To run this demo, you will need to provide:
*
* <ol>
* <li>Ethereum client (or node) endpoint. The simplest thing to do is
* <a href="https://infura.io/register.html">request a free access token from Infura</a></li>
* <li>A wallet file. This can be generated using the web3j
* <a href="https://docs.web3j.io/command_line.html">command line tools</a></li>
* <li>Some Ether. This can be requested from the
* <a href="https://www.rinkeby.io/#faucet">Rinkeby Faucet</a></li>
* </ol>
*
* <p>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/<your token>")); // 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(
"<password>",
"/path/to/<walletfile>");
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));
}
}
}
16 changes: 16 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- Change to DEBUG log protocol messages -->
<logger name="org.web3j.protocol" level="INFO"/>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
40 changes: 40 additions & 0 deletions src/main/solidity/Greeter.sol
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 14 additions & 0 deletions src/main/test/java/org/web3j/sample/GreeterContractIT.java
Original file line number Diff line number Diff line change
@@ -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[]{ });
}
}