合约代码

先准备好一份合约代码如下:

合约的功能是实现简单的用户管理,包括

  • 注册功能
  • 登录功能
  • 修改密码
  • 获得用户信息
  • 获得系统用户总数
pragma solidity >= 0.5.0;
contract UserManagerment {// 用户结构体struct User {address ethAddr;string userName;bytes32 password;}// 合约部署者地址address host;// 保存所有用户User[] userInfos;// 用户名到是否注册的映射mapping(string => bool) registerPool;// 用户名到用户的映射mapping(string => User) userPool;// 构造函数 设置合约拥有者地址constructor() public {host = msg.sender;}// 登录功能function doLogin(string memory userName, string memory password)  public view returns (bool) {return userPool[userName].password == keccak256(abi.encode(password));}// 注册检测function checkRegister(string memory userName)  public view returns (bool) {return registerPool[userName];}// 用户注册function register(address ethAddr, string memory  userName, string memory password) public {// 检查用户是否注册 当 require() 中的条件值为假时抛出异常,异常信息为:“用户已经注册”require(!checkRegister(userName),"用户已经注册!");// 保存注册信息userPool[userName] = User(ethAddr, userName, keccak256(abi.encode(password)));// 设置用户名为已经注册registerPool[userName] = true;// 添加到用户数组中userInfos.push(userPool[userName]);}// 更新密码function updatePassword(string memory userName, string memory newPwd) public {// keccak256加密userPool[userName].password = keccak256(abi.encode(newPwd));}// 获得用户信息function getUserInfoByUserName(string memory userName) public view returns (address,string memory){require(registerPool[userName],"未查到该用户信息");User storage user = userPool[userName];return (user.ethAddr,user.userName);}// 获得所有用户信息function getAllUserInfos(uint index)public view returns (address,string memory){// 检查下表范围require(index < userInfos.length,"下标越界");// 检查用户权限,只有合约创建者才能执行该操作require(msg.sender == host,"非法访问");// 取出用户信息User storage user = userInfos[index];// 返回数据return (user.ethAddr,user.userName);}// 获得系统用户数function getTotalUserNum() public view returns (uint){// 检查用户权限,只有合约创建者才能执行该操作require(msg.sender == host,"非法访问");return userInfos.length;}
}

编译合约

由于使用的VS Code编辑器,安装的插件solidity0.0.72可以直接按F5编译

编译后,在文件夹根目录生成了bin文件夹。里头的内容如下:

java打包合约

  1. 下载web3j命令行工具

    下载连接:点击直达

2. 解压工具包

注:可以将bin文件夹路径添加到系统环境变量中,这样省得之后进入bin目录使用命令

3. 进入文件夹bin目录

输入如下命令:

web3j solidity generate /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name

说明:

  • /path/to/.bin 智能合约编译后的生成物
  • /path/to/.abi 智能合约编译后的生成物
  • -o /path/to/src/main/java 输出路径
  • -p com.your.organisation.name 打包的包名

举例:

web3j solidity generate E:\BlockChain\Solidity\bin\UserManagerment.bin E:\BlockChain\Solidity\bin\UserManagerment.abi  -o E:\Web3j -p com.my.contract

输出结果:

修改Java代码

  1. 上述操作生成的UserManagerment.java内容如下:

package com.my.contract;import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;/*** <p>* Auto generated code.* <p>* <strong>Do not modify!</strong>* <p>* Please use the <a href="https://docs.web3j.io/command_line.html">web3j* command line tools</a>, or the* org.web3j.codegen.SolidityFunctionWrapperGenerator in the* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen* module</a> to update.** <p>* Generated with web3j version 3.6.0.*/
public class UserManagerment extends Contract {private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";public static final String FUNC_CHECKREGISTER = "checkRegister";public static final String FUNC_DOLOGIN = "doLogin";public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";public static final String FUNC_REGISTER = "register";public static final String FUNC_UPDATEPASSWORD = "updatePassword";@Deprecatedprotected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice,BigInteger gasLimit) {super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);}protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {super(BINARY, contractAddress, web3j, credentials, contractGasProvider);}@Deprecatedprotected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);}protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);}public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");}public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");}public RemoteCall<TransactionReceipt> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> doLogin(String userName, String password) {final Function function = new Function(FUNC_DOLOGIN,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getAllUserInfos(BigInteger index) {final Function function = new Function(FUNC_GETALLUSERINFOS,Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getTotalUserNum() {final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getUserInfoByUserName(String userName) {final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {final Function function = new Function(FUNC_REGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {final Function function = new Function(FUNC_UPDATEPASSWORD,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(newPwd)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}@Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);}@Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);}public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);}public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);}
}

代码内容介绍

  • 合约二进制串

private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";
  • 我们定义的function名称
 public static final String FUNC_CHECKREGISTER = "checkRegister";public static final String FUNC_DOLOGIN = "doLogin";public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";public static final String FUNC_REGISTER = "register";public static final String FUNC_UPDATEPASSWORD = "updatePassword";
  • 合约部署函数
  public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");}public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");}
  • 合约加载函数
 @Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);}@Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);}public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);}public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);}
  • 自定义的函数
public RemoteCall<TransactionReceipt> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> doLogin(String userName, String password) {final Function function = new Function(FUNC_DOLOGIN,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getAllUserInfos(BigInteger index) {final Function function = new Function(FUNC_GETALLUSERINFOS,Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getTotalUserNum() {final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> getUserInfoByUserName(String userName) {final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {final Function function = new Function(FUNC_REGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {final Function function = new Function(FUNC_UPDATEPASSWORD,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(newPwd)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}

自定义的函数介绍

举例说明:

 public RemoteCall<TransactionReceipt> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}

这是自动生成的Java代码,但是好像有些问题。因为我们在合约中定义的函数是有返回值的。

RemoteCall

<> 中是函数的返回类型>

TransactionReceipt 表示返回的是区块的信息

Function()中有三个参数

函数名称、输入参数、返回值

return executeRemoteCallTransaction(function)

表示执行远程调用,返回的是 TransactionReceipt

开始修改

举例:

 public RemoteCall<TransactionReceipt> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}

合约中我们定义的是返回Bool值,所以要修改如下几处内容:

修改后内容:

 public RemoteCall<Bool> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));return executeRemoteCallSingleValueReturn(function);}

修改后完整代码

package com.mao.contract;import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;public class UserManagerment extends Contract {private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";public static final String FUNC_CHECKREGISTER = "checkRegister";public static final String FUNC_DOLOGIN = "doLogin";public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";public static final String FUNC_REGISTER = "register";public static final String FUNC_UPDATEPASSWORD = "updatePassword";@Deprecatedprotected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice,BigInteger gasLimit) {super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);}protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {super(BINARY, contractAddress, web3j, credentials, contractGasProvider);}@Deprecatedprotected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);}protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);}public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");}public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");}@Deprecatedpublic static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");}/**************************************** 自定义的函数 ********************************************************/public RemoteCall<Bool> checkRegister(String userName) {final Function function = new Function(FUNC_CHECKREGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));return executeRemoteCallSingleValueReturn(function);}public RemoteCall<Bool> doLogin(String userName, String password) {final Function function = new Function(FUNC_DOLOGIN,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));return executeRemoteCallSingleValueReturn(function);}public RemoteCall<List<Type>> getAllUserInfos(int index) {final Function function = new Function(FUNC_GETALLUSERINFOS,Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Utf8String>() {}));return executeRemoteCallMultipleValueReturn(function);}public RemoteCall<Uint256> getTotalUserNum() {final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));return executeRemoteCallSingleValueReturn(function);}public RemoteCall<List<Type>> getUserInfoByUserName(String userName) {final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Utf8String>() {}));return executeRemoteCallMultipleValueReturn(function);}public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {final Function function = new Function(FUNC_REGISTER,Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(password)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {final Function function = new Function(FUNC_UPDATEPASSWORD,Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),new org.web3j.abi.datatypes.Utf8String(newPwd)),Collections.<TypeReference<?>>emptyList());return executeRemoteCallTransaction(function);}/**************************************** END ********************************************************/@Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);}@Deprecatedpublic static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);}public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);}public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,ContractGasProvider contractGasProvider) {return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);}
}

部署和调用

启动私链

(不再介绍,看之前记录)

部署合约

Consts.java

public class Consts {// GAS价格public static BigInteger GAS_PRICE = BigInteger.valueOf(2000_000_000L);// GAS上限public static BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000L);// 交易费用public static BigInteger GAS_VALUE = BigInteger.valueOf(100_000L);;// 账户密码public static String PASSWORD = "123";// 账户文件路径public static String PATH = "E:/BlockChain/node5/data/keystore/UTC--2020-04-11T07-54-54.678177700Z--12d4e53d6f017a2a62807876ec41fc97a0f60a71";// 合约地址,第一次部署之后记录下来public static String ADDRESS = "0x2e2d63186093b543d9f84a3f8343c59a3047718f";// chain id,在创世区块中定义的public static byte CHAINID = (byte) 666;
}

DeployedContract.java

public class DeployedContract {public static void main(String[] args) throws Exception {// 1. 默认连接到 http://localhost:8545/Web3j web3j = Web3j.build(new HttpService());// 2. 获取凭证Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);// 3.部署合约UserManagerment contract = UserManagerment.deploy(web3j, credentials, Consts.GAS_PRICE, Consts.GAS_LIMIT).send();// 4.获得合约地址System.out.println(contract.getContractAddress());}}

调用合约

Main.java

public class Main {public static void main(String[] args) throws Exception {// 1.默认连接到 http://localhost:8545/Web3j web3j = Web3j.build(new HttpService());// 2.获取凭证Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);// 3.加载合约UserManagerment contract = UserManagerment.load(Consts.ADDRESS, web3j, credentials, Consts.GAS_PRICE,Consts.GAS_LIMIT);// 4.调用合约// 检查注册Boolean isRegister = contract.checkRegister("张三").send().getValue();System.out.println("用户名‘张三’是否被注册:" + isRegister);// 注册contract.register("0x2e2d63186093b543d9f84a3f8343c59a3047718f", "张三", "123").send();isRegister = contract.checkRegister("张三").send().getValue();System.out.println("用户名‘张三’是否被注册:" + isRegister);// 登录Boolean canLogin = contract.doLogin("张三", "123").send().getValue();if (canLogin) {System.out.println("登录成功");} else {System.out.println("登录失败");}// 获得用户信息System.out.println("张三信息如下:");List<Type> userInfo = contract.getUserInfoByUserName("张三").send();for (Type info : userInfo) {System.out.println(info.toString());}// 系统用户人数int userNum = contract.getTotalUserNum().send().getValue().intValue();System.out.println("总共的用户数:" + userNum);// 输出所有用户信息for (int i = 0; i < userNum; i++) {List<Type> user = contract.getAllUserInfos(i).send();for (Type info : user) {System.out.println(info.toString());}}// 5.关闭连接web3j.shutdown();}
}

运行结果:

https://blog.csdn.net/maohuihua123/article/details/105455683

将solidity智能合约打包成Java代码相关推荐

  1. 以太坊solidity智能合约-生成随机数

    Solidity随机数生成 在以太坊的只能合约中,没有提供像其他面向对象编程一样的生成随机数的工具类或方法.其实,所谓的随机数也是伪随机的,没有哪一种语言能够真正的生成随机数. 对于solidity来 ...

  2. web3j用于solidity智能合约maven插件

    web3j maven插件用于基于solidity智能合约文件创建java类. 用法 插件的基本配置将从src/main/resources获取solidity文件,并将java类生成到src/mai ...

  3. Solidity智能合约库:区块链工程师的随身工具箱

    编者荐语: Solidity使用起来不如其他语言那般丝滑顺畅?安全事故难以避免?社区迎来适用于FISCO BCOS的Solidity智能合约库,轻松破解合约的各种小难题,让合约开发更加快速高效.省时省 ...

  4. 微众银行Solidity智能合约库:区块链工程师的随身工具箱

    区块链技术在经历了十余年的发展后,渐呈"燎原之势",不断在各行业落地生根.但同时,从技术的角度看,区块链应用开发仍然有着较高的门槛,存在不少痛点.为了提升应用开发各环节的用户体验, ...

  5. solidity智能合约implicit conversion异常

    问题场景 在使用^0.5.10版本的solidity时,如果使用this关键字会出现以下问题. 代码: require(tokenContract.balanceOf(this) >= _num ...

  6. Solidity 智能合约入门

    Solidity 智能合约入门 存储合约示例 将一个数据放置在链上 // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.16 &l ...

  7. 基于以太坊的次高价盲拍solidity智能合约(二)

    基于以太坊的次高价盲拍solidity智能合约(二) 4.揭标 5.第三方仲裁人终结拍卖 4.揭标 揭标的过程应该是本智能合约中最复杂且具有灵魂的关键步骤. 当每个发起过竞标的用户,利用该标的隐式价格 ...

  8. Solidity智能合约开发 — 1-以太坊开发工具和部署

    Solidity简介 solidity 是为实现智能合约而创建的一个高阶编程语言.也是以太坊虚拟机(EVM)智能合约的语言. Solidity开发工具remix remix是以太坊官方推荐的在线开发工 ...

  9. 蚂蚁区块链第12课 如何使用命令行编译工具solcjs编译Solidity智能合约?

    1,摘要 蚂蚁区块链合约平台支持 Solidity 智能合约,针对合约源代码的编译,可以直接通过蚂蚁区块链 Cloud IDE 合约开发环境进行合约编译.部署.测试和调试. 本文介绍由蚂蚁区块链平台提 ...

  10. Kotlin代码转换成Java代码

    一.Kotlin代码与Java代码对比 二.转换方法 三.参考资料 一.Kotlin代码与Java代码对比 //Kotlin当中的单例模式 object PrinterDriver{init{prin ...

最新文章

  1. 浅析js中的arguments
  2. 在SAP BW中使用ABAP
  3. Elasticsearch: 权威指南 » 聚合 » Doc Values and Fielddata » 聚合与分析
  4. 安卓开发笔记(二十六):Splash实现首页快速开屏功能
  5. NLP:自然语言处理技术近十年发展技术更迭的简介、案例之详细攻略(持续更新)
  6. arm 流水线和pc值
  7. Memcache简介
  8. 修改goods对ECshop的url路径进行优化
  9. 你不一定知道的vb6(2)
  10. centos下安装go环境两种方法
  11. 关于APP 内涉及用户个人敏感信息/权限的进一步整改
  12. 用python刷微信投票_微信投票知道 微信刷票能否python抓取微信投票_大师网络投票刷票网...
  13. 带有资源混淆的打补丁过程
  14. Android欢迎页面以及引导页面
  15. 帧定格(用于定格画面添加字幕或者图片)
  16. 通过Windows10管理AD域控
  17. AT指令(中文详解版)
  18. Pytorch 安装(CPU)
  19. Polar vector and axial vector(极矢量和轴向矢量)
  20. 520男生送什么礼物特别、2022特别礼物合集

热门文章

  1. 实现DEDE转跳属性文档在模板上调用出转跳地址
  2. 该学学数据结构了,不会数据结构真是寸步难行啊。。。。。
  3. iOS开发_UI_AutoLayout
  4. 今日看了一下广告收入,心里拔凉拔凉的。
  5. Tomcat内存设置方法(转载并实践)
  6. 高一信息技术 计算机配件的真伪辨别,高一信息技术组PPT.ppt
  7. 拓端tecdat|Excel实例:排序和筛选2
  8. 拓端tecdat|R语言时间序列TAR阈值模型分析
  9. linux work有关的命令,Linux执行后台work相关
  10. java表示非法参数的异常是_JAVA 的异常那些事