forked from piomin/sample-spring-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
docker run -d --name ethereum -p 8545:8545 -p 30303:30303 ethereum/client-go --rpc --rpcaddr "0.0.0.0" --rpcapi="db,eth,net,web3,personal" --rpccorsdomain "*" --testnet --fast | ||
docker exec -it ethereum geth attach ipc:/root/.ethereum/testnet/geth.ipc | ||
## Introduction to Blockchain with Java using Ethereum, web3j and Spring Boot [![Twitter](https://img.shields.io/twitter/follow/piotr_minkowski.svg?style=social&logo=twitter&label=Follow%20Me)](https://twitter.com/piotr_minkowski) | ||
|
||
https://claudiodangelis.com/ethereum/2018/02/19/exploring-ethereum-platform-accounts.html | ||
|
||
|
||
https://github.com/matthiaszimmermann/web3j_demo/blob/master/src/main/java/org/matthiaszimmermann/web3j/util/Web3jConstants.java | ||
Detailed description can be found here: [Introduction to Blockchain with Java using Ethereum, web3j and Spring Boot](https://piotrminkowski.wordpress.com/2018/06/22/introduction-to-blockchain-with-java-using-ethereum-web3j-and-spring-boot/) |
41 changes: 41 additions & 0 deletions
41
src/main/java/pl/piomin/service/blockchain/BlockchainApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,54 @@ | ||
package pl.piomin.service.blockchain; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.core.DefaultBlockParameterName; | ||
import org.web3j.protocol.core.methods.request.Transaction; | ||
import org.web3j.protocol.core.methods.response.EthAccounts; | ||
import org.web3j.protocol.core.methods.response.EthCoinbase; | ||
import org.web3j.protocol.core.methods.response.EthGetTransactionCount; | ||
import org.web3j.protocol.core.methods.response.EthSendTransaction; | ||
import pl.piomin.service.blockchain.service.BlockchainService; | ||
import rx.Subscription; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
|
||
@SpringBootApplication | ||
public class BlockchainApp { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BlockchainService.class); | ||
|
||
@Autowired | ||
Web3j web3j; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(BlockchainApp.class, args); | ||
} | ||
|
||
@PostConstruct | ||
public void listen() { | ||
Subscription subscription = web3j.transactionObservable().subscribe(tx -> { | ||
LOGGER.info("New tx: id={}, block={}, from={}, to={}, value={}", tx.getHash(), tx.getBlockHash(), tx.getFrom(), tx.getTo(), tx.getValue().intValue()); | ||
try { | ||
EthCoinbase coinbase = web3j.ethCoinbase().send(); | ||
EthGetTransactionCount transactionCount = web3j.ethGetTransactionCount(tx.getFrom(), DefaultBlockParameterName.LATEST).send(); | ||
LOGGER.info("Tx count: {}", transactionCount.getTransactionCount().intValue()); | ||
if (transactionCount.getTransactionCount().intValue() % 10 == 0) { | ||
EthGetTransactionCount tc = web3j.ethGetTransactionCount(coinbase.getAddress(), DefaultBlockParameterName.LATEST).send(); | ||
Transaction transaction = Transaction.createEtherTransaction(coinbase.getAddress(), tc.getTransactionCount(), tx.getValue(), BigInteger.valueOf(21_000), tx.getFrom(), tx.getValue()); | ||
web3j.ethSendTransaction(transaction).send(); | ||
} | ||
} catch (IOException e) { | ||
LOGGER.error("Error getting transactions", e); | ||
} | ||
}); | ||
LOGGER.info("Subscribed"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/pl/piomin/service/blockchain/model/BlockchainTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package pl.piomin.service.blockchain.model; | ||
|
||
public class BlockchainTransaction { | ||
|
||
private String id; | ||
private int fromId; | ||
private int toId; | ||
private long value; | ||
private boolean accepted; | ||
|
||
public BlockchainTransaction() { | ||
|
||
} | ||
|
||
public BlockchainTransaction(int fromId, int toId, long value) { | ||
this.fromId = fromId; | ||
this.toId = toId; | ||
this.value = value; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public int getFromId() { | ||
return fromId; | ||
} | ||
|
||
public void setFromId(int fromId) { | ||
this.fromId = fromId; | ||
} | ||
|
||
public int getToId() { | ||
return toId; | ||
} | ||
|
||
public void setToId(int toId) { | ||
this.toId = toId; | ||
} | ||
|
||
public long getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(long value) { | ||
this.value = value; | ||
} | ||
|
||
public boolean isAccepted() { | ||
return accepted; | ||
} | ||
|
||
public void setAccepted(boolean accepted) { | ||
this.accepted = accepted; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
spring: | ||
application: | ||
name: transaction-service | ||
server: | ||
port: ${PORT:8090} | ||
web3j: | ||
|