1. Create a springboot project and introduce the jar package of web3j
<!-- web3j --> <dependency> <groupId>org.web3j</groupId> <artifactId>web3j-spring-boot-starter</artifactId> <version>1.6.0</version> </dependency>
2 、 ill 配置
client-address can create a node by yourself, or you can go to https://infura.io/ to register to get it. The following is the client address of the test chain node.
web3j: client-address: https://rinkeby.infura.io/key admin-client: true httpTimeoutSeconds: 600
3. Code dry goods
First, let's talk about web3j to create a wallet address
File file = new File( "/home/wallet" ); //Specify a directory
WalletUtils. generateNewWalletFile ("Specify the password of the wallet", file, true ); //The method returns the file name of the wallet created
As for the address, haha, you can see it in the file name, or use the method to authenticate the wallet, as follows
Credentials credentials = WalletUtils.loadCredentials ( "password", wallet file);
returnAddress = credentials.getAddress();
Let's talk about the program code for transferring a certain token between two eth addresses:
Credentials credentials = WalletUtils.loadCredentials ( "Password to Outgoing Address", wallet file); String fromAddress = credentials.getAddress(); EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); Address address = new Address(toAddress); Uint256 value = new Uint256(amount); List<Type> parametersList = new ArrayList<>(); parametersList.add(address); parametersList.add(value); List<TypeReference<?>> outList = new ArrayList<>(); Function function = new Function("transfer", parametersList, outList); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction ( nonce, Gas unit price , Gas maximum quantity , contract address , encodedFunction); byte [] signedMessage = TransactionEncoder. signMessage (rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j .ethSendRawTransaction (hexValue) .sendAsync (). Get (); transactionHash = ethSendTransaction.getTransactionHash();
The last thing I got is this is the txHash of the transfer. You can check the transfer status (very easy) through Json-rpc.
Json-rpc description document address: https://github.com/ethereum/wiki/wiki/JSON-RPC
There are many methods provided, try it, Sao Nian!
4. If you join a company and have its own ERC20 virtual currency, you can use this java method to transfer money, haha, if you like it, please pay attention! brother
Original, please do not reprint, thank you!