web3j contract 使用方法

2 篇文章 1 订阅
2 篇文章 0 订阅

web3j contract 使用方法

solidity

solc环境
npm install -g solc
web3j环境
brew tap web3j/web3j
brew install web3j
  • 第二步编译合约。得到abi bin。
solc <contract>.sol --bin --abi --optimize -o <output-dir>/

example:
solc erc20_example.sol --bin --abi --optimize -o .
  • 第三步生成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

example:
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();
            //etc..
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
使用 Java 调用智能合约,需要使用 web3j 库。web3j 是一个用于与以太坊网络进行交互的轻量级 Java 库。下面是使用 web3j 调用智能合约的一些基本步骤: 1.在项目中添加 web3j 库的依赖: ``` <dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>4.5.14</version> </dependency> <dependency> <groupId>org.web3j</groupId> <artifactId>crypto</artifactId> <version>4.5.14</version> </dependency> <dependency> <groupId>org.web3j</groupId> <artifactId>abi</artifactId> <version>4.5.14</version> </dependency> ``` 2.创建一个与以太坊节点进行连接的 Web3j 对象: ``` Web3j web3j = Web3j.build(new HttpService("http://localhost:8545")); ``` 3.构建一个与智能合约进行交互的 Credentials 对象: ``` Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile"); ``` 4.使用智能合约的地址和 ABI 构建一个 Contract 对象: ``` String contractAddress = "0x123456..."; String contractABI = "..."; Contract contract = Contract.load(contractAddress, web3j, credentials, new DefaultGasProvider(), contractABI); ``` 5.调用智能合约的方法: ``` TransactionReceipt transactionReceipt = contract.methodName(param1, param2).send(); ``` 其中,methodName 是智能合约中定义的方法名,param1 和 param2 是该方法的参数。send() 方法用于发送交易并等待交易被打包,并返回交易回执。 以上是使用 web3j 调用智能合约的基本步骤,具体的实现需要根据实际情况进行调整和优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值