生活随笔
收集整理的这篇文章主要介绍了
web3j contract 使用方法
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
web3j contract 使用方法
solidity
solc环境
npm
install -g solc
web3j环境
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文件 创建、调用合约
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();}}
}
总结
以上是生活随笔为你收集整理的web3j contract 使用方法的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。