├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── images ├── blocks.png ├── contract.png ├── transaction.png └── transactions.png ├── settings.gradle └── src ├── main ├── java │ └── com │ │ └── web3labs │ │ └── quorum │ │ └── token │ │ └── TokenApplication.java ├── resources │ └── logback.xml └── solidity │ └── web3labs │ └── Token.sol └── test └── java └── com └── web3labs └── quorum └── token └── TokenApplicationIT.java /.gitignore: -------------------------------------------------------------------------------- 1 | ### Java template 2 | *.class 3 | 4 | # Package Files # 5 | *.jar 6 | *.war 7 | *.ear 8 | 9 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 10 | hs_err_pid* 11 | ### Gradle template 12 | .gradle 13 | /build 14 | /out 15 | */build/ 16 | */out/ 17 | 18 | # Ignore Gradle GUI config 19 | gradle-app.setting 20 | 21 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 22 | !gradle-wrapper.jar 23 | 24 | # Cache of project 25 | .gradletasknamecache 26 | 27 | # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 28 | # gradle/wrapper/gradle-wrapper.properties 29 | 30 | .idea 31 | *.iml 32 | *.ipr 33 | *.iws 34 | 35 | # OS X 36 | .DS_Store 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quorum Token Java Sample 2 | 3 | This project demonstrates the creation and management of a private token on a Quorum network. 4 | 5 | Quorum privacy is used, only certain members of the network are privy to the 6 | token that has been created. 7 | 8 | It is written in Java using [web3j](https://web3j.io) which is maintained by 9 | [Web3 Labs](https://www.web3labs.com). 10 | 11 | 12 | ## Prerequisites 13 | 14 | A Quorum network running with at least 3 transaction nodes. Check next section for an example of a ready to use network. 15 | 16 | You will need the following details for each node: 17 | - Node URL, `http://:` 18 | - Transaction enclave address, `` 19 | 20 | ## Quorum-dev-quickstart 21 | 22 | As an example of a Quorum network that can be used with this sample project, we have the [Quorum-dev-quickstart](https://docs.goquorum.consensys.net/en/stable/Tutorials/Quorum-Dev-Quickstart/). 23 | This, will spin up a network of 3 members with their private transactions managers and will let you demo privacy groups and how they work. 24 | 25 | ## Running the application 26 | 27 | You will need to update the 28 | [TokenApplication](src/main/java/com/web3labs/quorum/token/TokenApplication.java#L33) 29 | class with details of the URL for each of your transaction nodes and their associated public keys. 30 | 31 | ```java 32 | // FIXME: Add node URL and transaction node keys here 33 | Node nodeA = createAndUnlockAccount("nodeA", "http://", ""); 34 | Node nodeB = createAndUnlockAccount("nodeB", "http://", ""); 35 | Node nodeZ = createAndUnlockAccount("nodeZ", "http://", ""); 36 | ``` 37 | 38 | Where your node url would be similar to `https://..com:3200/{}` and 39 | the nodekey would be a base64 encoded public key such as 40 | `V4pb2lVRMwLZXGGmm/Ee3Y2U7BTlQ+BO8abrktMbSSQ=`. 41 | 42 | Then to run the application, simply type: 43 | 44 | ```bash 45 | ./gradlew run 46 | ``` 47 | 48 | The application logs the different activities it completes, which are as follows: 49 | 50 | 1. Create an Ethereum account on nodes A, B and Z. 51 | 1. Deploy a Quorum Token (symbol QT) contract that is visible only to nodes A, and B but not Z. 52 | 1. Transfer QT to accounts associated with nodes A, B, Z. 53 | 1. Display all account balances. 54 | 1. Demonstrate that node Z cannot see the QT assigned to its account. 55 | 1. Decrease the supply of QT. 56 | 1. Display all account balances. 57 | 58 | There are also a couple of 59 | [integration tests](src/test/java/com/web3labs/quorum/token/TokenApplicationIT.java) 60 | you can use to test the application. 61 | 62 | ## Viewing contracts and transactions 63 | 64 | The Web3 Labs blockchain explorer provides an easy to use UI for browsing transaction 65 | and contract details. 66 | 67 | ```bash 68 | git clone https://github.com/blk-io/epirus-free.git 69 | cd epirus-free 70 | NODE_ENDPOINT=http:// docker-compose up 71 | ``` 72 | 73 | You may access the blockchain explorer via http://localhost. 74 | 75 | ![Latest blocks](images/blocks.png) 76 | 77 | You can then browse the smart contract that was created by obtaining the token contract address, 78 | which is logged as follows: 79 | 80 | ```bash 81 | 19:53:10.853 [main] INFO c.w.quorum.token.TokenApplication - Quorum Token (QT) created at contract address 0x, by account 0x 82 | ``` 83 | 84 | You can view the contract itself via the url `http://localhost:5000/contract/0x`: 85 | 86 | ![Contract](images/contract.png) 87 | 88 | You can also view the private transactions: 89 | 90 | ![Private transactions](images/transactions.png) 91 | 92 | And details of those transactions: 93 | 94 | ![Transaction details](images/transaction.png) 95 | 96 | If you wish to learn more about our Epirus contract registry and blockchain explorer, 97 | including our production-ready SaaS offerings which include features such as authentication 98 | and BI integrations please [contact us](mailto:hi@web3labs.com). 99 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.web3j' version '4.8.4' 4 | id 'application' 5 | } 6 | 7 | ext { 8 | web3jQuorumVersion = '4.8.4' 9 | logbackVersion = '1.2.3' 10 | 11 | //test dependencies 12 | junitVersion = '5.3.2' 13 | } 14 | 15 | group 'com.web3labs.quorum' 16 | version '0.1.0' 17 | 18 | sourceCompatibility = 1.8 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | implementation "org.web3j:quorum:$web3jQuorumVersion", 26 | "ch.qos.logback:logback-core:$logbackVersion", 27 | "ch.qos.logback:logback-classic:$logbackVersion" 28 | testImplementation "org.junit.jupiter:junit-jupiter-engine:$junitVersion" 29 | } 30 | 31 | web3j { 32 | generatedPackageName = 'com.web3labs.{0}' 33 | includedContracts = ['Token'] 34 | } 35 | 36 | import org.web3j.solidity.gradle.plugin.OutputComponent 37 | solidity { 38 | outputComponents = [OutputComponent.BIN, OutputComponent.ABI, OutputComponent.METADATA] 39 | } 40 | 41 | application { 42 | mainClassName = 'com.web3labs.quorum.token.TokenApplication' 43 | } 44 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/web3labs/quorum-sample/c51a2b554308147e14f13578dafa2299732c071f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Mar 12 15:25:40 GMT 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /images/blocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/web3labs/quorum-sample/c51a2b554308147e14f13578dafa2299732c071f/images/blocks.png -------------------------------------------------------------------------------- /images/contract.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/web3labs/quorum-sample/c51a2b554308147e14f13578dafa2299732c071f/images/contract.png -------------------------------------------------------------------------------- /images/transaction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/web3labs/quorum-sample/c51a2b554308147e14f13578dafa2299732c071f/images/transaction.png -------------------------------------------------------------------------------- /images/transactions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/web3labs/quorum-sample/c51a2b554308147e14f13578dafa2299732c071f/images/transactions.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'token' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/web3labs/quorum/token/TokenApplication.java: -------------------------------------------------------------------------------- 1 | package com.web3labs.quorum.token; 2 | 3 | import java.math.BigInteger; 4 | import java.security.SecureRandom; 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.stream.Collectors; 9 | 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | import org.web3j.protocol.admin.Admin; 13 | import org.web3j.protocol.admin.methods.response.NewAccountIdentifier; 14 | import org.web3j.protocol.core.methods.response.TransactionReceipt; 15 | import org.web3j.protocol.http.HttpService; 16 | import org.web3j.quorum.Node; 17 | import org.web3j.quorum.Quorum; 18 | import org.web3j.quorum.tx.ClientTransactionManager; 19 | import org.web3j.tx.TransactionManager; 20 | import org.web3j.tx.exceptions.ContractCallException; 21 | import org.web3j.tx.gas.DefaultGasProvider; 22 | 23 | import com.web3labs.token.Token; 24 | 25 | /** 26 | * Demonstration Quorum token application. 27 | */ 28 | public class TokenApplication { 29 | 30 | private static final Logger log = LoggerFactory.getLogger(TokenApplication.class); 31 | 32 | public static void main(String[] args) throws Exception { 33 | // FIXME: Add node URL and transaction node keys here 34 | Node nodeA = createAndUnlockAccount("nodeA", "http://", ""); 35 | Node nodeB = createAndUnlockAccount("nodeB", "http://", ""); 36 | Node nodeZ = createAndUnlockAccount("nodeZ", "http://", ""); 37 | 38 | new TokenApplication().run(nodeA, nodeB, nodeZ); 39 | } 40 | 41 | public static Node createAndUnlockAccount( 42 | String name, String url, String publicKey) throws Exception { 43 | 44 | Admin admin = Admin.build(new HttpService(url)); 45 | String password = createPassword(16); 46 | NewAccountIdentifier accountId = admin.personalNewAccount(password).send(); 47 | 48 | log.info("{} account: {} created with password: {}", 49 | name, accountId.getAccountId(), password); 50 | 51 | // Unlock account for maximum duration 52 | admin.personalUnlockAccount(accountId.getAccountId(), password, BigInteger.ZERO).send(); 53 | log.info("{} account {} unlocked", name, accountId.getAccountId()); 54 | 55 | return new Node(accountId.getAccountId(), Collections.singletonList(publicKey), url); 56 | } 57 | 58 | public void run(Node nodeA, Node nodeB, Node nodeZ) { 59 | 60 | try { 61 | String tokenName = "Quorum Token"; 62 | String tokenSymbol = "QT"; 63 | 64 | // Create token that is private to nodes A, B 65 | Token token = createToken(tokenName, tokenSymbol, 8, nodeA, nodeB); 66 | 67 | log.info( 68 | "{} ({}) created at contract address {}, by account {}\n", 69 | tokenName, tokenSymbol, token.getContractAddress(), nodeA.getAddress()); 70 | 71 | logSupply(token); 72 | 73 | logBalances(token, nodeA, nodeB, nodeZ); 74 | 75 | // Allocate tokens to nodes B and Z 76 | transferToken(token, nodeB.getAddress(), 100_000); 77 | transferToken(token, nodeZ.getAddress(), 50_000); 78 | 79 | logBalances(token, nodeA, nodeB, nodeZ); 80 | 81 | // Although Node Z has been allocated tokens, it cannot see this as it is not privy to 82 | // the underlying smart contract - it wasn't included as a participant 83 | try { 84 | log.info("Getting token balances from nodeZ ({})", nodeZ.getUrl()); 85 | getBalanceByNode(token.getContractAddress(), nodeZ); 86 | throw new Exception("It should not be possible for nodeZ to see it's balance"); 87 | } catch (ContractCallException e) { 88 | log.info("Exception: NodeZ unable to view its balance as not included in " + 89 | "token contract creation\n"); 90 | } 91 | 92 | // Burn tokens 93 | long burnQty = 499_999; 94 | log.info("Decreasing available supply by {}", burnQty); 95 | decreaseTokenSupply(token, burnQty); 96 | 97 | logBalances(token, nodeA, nodeB, nodeZ); 98 | 99 | } catch (Exception e) { 100 | log.error("Error performing operation", e); 101 | } 102 | } 103 | 104 | private void logBalances( 105 | Token token, Node nodeA, Node nodeB, Node nodeZ) throws Exception { 106 | log.info("Getting token balances from nodeA ({})", nodeA.getUrl()); 107 | log.info("NodeA balance: {}", token.balanceOf(nodeA.getAddress()).send().longValue()); 108 | log.info("NodeB balance: {}", token.balanceOf(nodeB.getAddress()).send().longValue()); 109 | log.info("NodeZ balance: {}\n", token.balanceOf(nodeZ.getAddress()).send().longValue()); 110 | } 111 | 112 | private void logSupply(Token token) throws Exception { 113 | log.info("Available supply: {}\n", token.totalSupply().send().longValue()); 114 | } 115 | 116 | public Token createToken( 117 | String tokenName, String symbol, long decimals, 118 | Node creatorNode, Node... participantNodes) throws Exception { 119 | 120 | Quorum quorum = Quorum.build(new HttpService(creatorNode.getUrl())); 121 | 122 | ClientTransactionManager transactionManager = createTransactionManager( 123 | quorum, creatorNode, participantNodes); 124 | 125 | return Token.deploy(quorum, transactionManager, new DefaultGasProvider(), 126 | BigInteger.valueOf(1_000_000), tokenName, symbol, BigInteger.valueOf(decimals)) 127 | .send(); 128 | } 129 | 130 | public TransactionReceipt transferToken( 131 | Token token, String destinationAddress, long value) throws Exception { 132 | 133 | return token.transfer( 134 | destinationAddress, BigInteger.valueOf(value)) 135 | .send(); 136 | } 137 | 138 | public TransactionReceipt decreaseTokenSupply( 139 | Token token, long quantity) throws Exception { 140 | return token.burn( 141 | BigInteger.valueOf(quantity)) 142 | .send(); 143 | } 144 | 145 | public long getBalance(Token token, String address) throws Exception { 146 | return token.balanceOf(address).send().longValue(); 147 | } 148 | 149 | public long getBalanceByNode(String contractAddress, Node node) throws Exception { 150 | Quorum quorum = Quorum.build(new HttpService(node.getUrl())); 151 | 152 | TransactionManager transactionManager = createTransactionManager(quorum, node); 153 | Token token = Token.load( 154 | contractAddress, quorum, transactionManager, new DefaultGasProvider()); 155 | return token.balanceOf(node.getAddress()).send().longValue(); 156 | } 157 | 158 | private static ClientTransactionManager createTransactionManager( 159 | Quorum quorum, Node creatorNode, Node... participantNodes) { 160 | 161 | List publicKeys = Arrays.stream(participantNodes) 162 | .flatMap(n -> n.getPublicKeys().stream()) 163 | .collect(Collectors.toList()); 164 | 165 | return new ClientTransactionManager( 166 | quorum, 167 | creatorNode.getAddress(), 168 | creatorNode.getPublicKeys().get(0), 169 | publicKeys); 170 | } 171 | 172 | // Simple account password generator 173 | private static final String CHARS = 174 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!+<>[]%,(){}.&@^?*$-"; 175 | private static final SecureRandom RND = new SecureRandom(); 176 | 177 | private static String createPassword(int length){ 178 | StringBuilder sb = new StringBuilder(length); 179 | for (int i = 0; i < length; i++) { 180 | sb.append(CHARS.charAt(RND.nextInt(CHARS.length()))); 181 | } 182 | return sb.toString(); 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/solidity/web3labs/Token.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.7.0; 2 | 3 | import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; 4 | import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol"; 5 | 6 | contract Token is ERC20, ERC20Burnable { 7 | 8 | constructor( 9 | uint totalSupply, 10 | string memory name, 11 | string memory symbol, 12 | uint8 decimals) ERC20(name, symbol) { 13 | _setupDecimals(decimals); 14 | _mint(msg.sender, totalSupply); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/web3labs/quorum/token/TokenApplicationIT.java: -------------------------------------------------------------------------------- 1 | package com.web3labs.quorum.token; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.web3j.protocol.Web3j; 7 | import org.web3j.protocol.http.HttpService; 8 | 9 | /** 10 | * Simple integration test to demonstrate Token contract. 11 | */ 12 | public class TokenApplicationIT { 13 | 14 | private static final Logger log = LoggerFactory.getLogger(TokenApplicationIT.class); 15 | 16 | @Test 17 | public void testNodeConnections() throws Exception { 18 | testConnection(""); 19 | // duplicate for multiple nodes ... 20 | } 21 | 22 | private void testConnection(String nodeUrl) throws Exception { 23 | Web3j web3j = Web3j.build(new HttpService(nodeUrl)); 24 | log.info(web3j.web3ClientVersion().send().getWeb3ClientVersion()); 25 | } 26 | 27 | @Test 28 | public void testLifeCycle() throws Exception { 29 | TokenApplication.main(new String[]{ }); 30 | } 31 | } 32 | --------------------------------------------------------------------------------