【区块链】以太坊 web3j for java 使用

来源:互联网 发布:淘宝男装 编辑:程序博客网 时间:2024/06/06 10:06

web3j 部署合约


环境:mac os & android studio

官网:here

以太坊 web3j for java 配置和使用<1>: here

以太坊 web3j for java 使用 - 为用户创建凭证credentials <2>:here

一、编译合约

solc <contract>.sol --bin --abi --optimize -o <output-dir>/

====>生产 .abi 和 .bin 文件

例如,在Hello.sol文件目录下输入:solc Hello.sol --bin --abi --optimize -o /Users/pro/Desktop

二、生成 .java文件

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.name

例如:

然后将生成的 .java文件导入到安卓项目中。

三、部署合约

YourSmartContract contract = YourSmartContract.deploy(    <web3j>, <credentials>, GAS_PRICE, GAS_LIMIT,    [<initialValue>,]    <param1>, ..., <paramN>).send();

或者可以加载已经部署在私有链上面的合约:

YourSmartContract contract = YourSmartContract.load(    "0x<address>|<ensName>", web3j, credentials, GAS_PRICE, GAS_LIMIT);

例如:

AllBikeShared contract = AllBikeShared.load("0x055a864a5f39f9eecd17e729e2cfc4fb6b12c19b", web3, credentials, GAS_PRICE, GAS_LIMIT);

四、调用合约

1.call 的方式,不改变智能合约的状态

    Type result = contract.someMethod(<param1>, ...).send();

例如:

    BigInteger result = contract.getUserCount().send();
阅读全文
0 0
原创粉丝点击