生活随笔
收集整理的這篇文章主要介紹了
web3j contract 使用方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
web3j contract 使用方法
solidity
solc環(huán)境
npm
install -g solc
web3j環(huán)境
brew tap web3j/web3j
brew
install web3j
<>
. -- -- -- - <
->
. -- -- -- - .
web3j solidity generate [--javaTypes|--solidityTypes] /path/to/<smart-contract>
.bin /path/to/<smart-contract>
.abi -o /path/to/src/main/java -p
com.your.organisation.nameexample:
web3j solidity generate --javaTypes TokenERC20
.bin TokenERC20
.abi -o . -p TokenERC20
.java
- 第四步使用上一步生成的java文件 創(chuàng)建、調(diào)用合約
public class SolSample {public static void main(String[] args) {deploy();use();}
private static void deploy() {Web3j web3j = Web3j.build(
new HttpService(Environment.RPC_URL));Credentials credentials =
null;RemoteCall<TokenERC20> deploy = TokenERC20.deploy(web3j, credentials,Convert.toWei(
"10", Convert.Unit.GWEI).toBigInteger(),BigInteger.valueOf(
3000000),BigInteger.valueOf(
5201314),
"my token",
"mt");
try {TokenERC20 tokenERC20 = deploy.send();tokenERC20.isValid();}
catch (Exception e) {e.printStackTrace();}}
private static void use() {Web3j web3j = Web3j.build(
new HttpService(Environment.RPC_URL));String contractAddress =
null;Credentials credentials =
null;TokenERC20 contract = TokenERC20.load(contractAddress, web3j, credentials,Convert.toWei(
"10", Convert.Unit.GWEI).toBigInteger(),BigInteger.valueOf(
100000));String myAddress =
null;String toAddress =
null;BigInteger amount = BigInteger.ONE;
try {BigInteger balance = contract.balanceOf(myAddress).send();TransactionReceipt receipt = contract.transfer(toAddress, amount).send();}
catch (Exception e) {e.printStackTrace();}}
}
本文根據(jù)web3j.io文檔編寫
本文項(xiàng)目地址
總結(jié)
以上是生活随笔為你收集整理的web3j contract 使用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。