使用web3j工具生成java版本的智能合约

这里需要使用的环境 web3j,nodejs

  • 安装编译sol工具

1

$ npm install -g solc

  • 保存为hello.sol文件到本地

1

2

3

4

5

6

7

8

pragma solidity 0.4.19

contract hello { 

function main(uint a) constant returns (uint b)  

    

        uint result = a * 8

        return result; 

    

}

  • 编译sol文件

1

2

3

4

$ solcjs <sol文件目录>   --optimize  --bin --abi --output-dir <输出目录>

 

//demo

$ solcjs F:\\hello.sol   --optimize  --bin --abi --output-dir F:\\

出现这种错误

1

2

Failed to write F:\F_\hello_sol_hello.bin: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.bin'

Failed to write F:\F_\hello_sol_hello.abi: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.abi'

这里有个坑,就是使用solcjs 编译智能合约文件输出到目录会有一个文件夹,这个需要手动创建,我这里输出目录到F:\\ 但是它还是要输出到F:\\F_\ 下,这里的F_文件夹需要我们创建!

  • 编译成功,记录下bin 和 abi后缀的文件
  • 使用web3j工具生成java版本的智能合约
  • web3j 命令行工具下载

1

2

3

$ web3j solidity generate <编译的bin文件地址> <编译的abi文件地址> -o <输出目录> -p <java包名>

//demo

$ web3j solidity generate F:\F_\hello_sol_hello.bin F:\F_\hello_sol_hello.abi -o E:\etheth\src\main\java -p xyz.lihang.demo.eth.sol

 PS:使用web3j命令,需要进入https://github.com/web3j/web3j/releases/tag/v3.3.1网站,下载web3j-3.3.1.tar,并解压。

   进入目录bin下,在此目录命令行执行web3j,否则web3j bash命令不存在

  • 生成成功

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

package xyz.lihang.demo.eth.sol;

 

import java.math.BigInteger;

import java.util.Arrays;

import org.web3j.abi.TypeReference;

import org.web3j.abi.datatypes.Function;

import org.web3j.abi.datatypes.Type;

import org.web3j.abi.datatypes.generated.Uint256;

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol.core.RemoteCall;

import org.web3j.tx.Contract;

import org.web3j.tx.TransactionManager;

 

/**

 * <p>Auto generated code.

 * <p><strong>Do not modify!</strong>

 * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,

 * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the

 * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.

 *

 * <p>Generated with web3j version 3.2.0.

 */

public class Hello_sol_hello extends Contract {

    private static final String BINARY = "60606040523415600e57600080fd5b609a8061001c6000396000f300606060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ab3ae25581146043575b600080fd5b3415604d57600080fd5b60566004356068565b60405190815260200160405180910390f35b600802905600a165627a7a723058200cc51f5dad45190b24189d9f8ff836d704bcebc9862cfd669e054b8c8f19f66c0029";

 

    protected Hello_sol_hello(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

        super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);

    }

 

    protected Hello_sol_hello(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

        super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);

    }

 

    public RemoteCall<BigInteger> main(BigInteger a) {

        Function function = new Function("main",

                Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(a)),

                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));

        return executeRemoteCallSingleValueReturn(function, BigInteger.class);

    }

 

    public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

        return deployRemoteCall(Hello_sol_hello.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");

    }

 

    public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

        return deployRemoteCall(Hello_sol_hello.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");

    }

 

    public static Hello_sol_hello load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

        return new Hello_sol_hello(contractAddress, web3j, credentials, gasPrice, gasLimit);

    }

 

    public static Hello_sol_hello load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

        return new Hello_sol_hello(contractAddress, web3j, transactionManager, gasPrice, gasLimit);

    }

}

一定要注意合约调用后进行挖矿,这样才能网络确认,才能使用合约

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERC20是以太坊上的代币标准,Java可以通过以太坊的API(如web3j)来与以太坊进行交互,实现授权转账的代码如下: 1. 授权 ```java //导入web3j相关的类 import org.web3j.abi.datatypes.Address; import org.web3j.abi.datatypes.generated.Uint256; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.tx.Contract; import org.web3j.tx.ManagedTransaction; import org.web3j.tx.TransactionManager; import org.web3j.tx.gas.DefaultGasProvider; //代币合约地址 String contractAddress = "0x12345..."; //代币转账地址 String fromAddress = "0xabcdef..."; //代币接收地址 String toAddress = "0x123456..."; //代币授权数量 BigInteger tokenAmount = BigInteger.valueOf(10000); //创建web3j实例 Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/v3/your-project-id")); //创建交易管理器 TransactionManager transactionManager = new RawTransactionManager(web3j, credentials); //创建代币合约实例 ERC20Token contract = ERC20Token.load(contractAddress, web3j, transactionManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT); //授权代币合约 TransactionReceipt approveReceipt = contract.approve(new Address(toAddress), new Uint256(tokenAmount)).send(); ``` 2. 转账 ```java //代币转账数量 BigInteger transferAmount = BigInteger.valueOf(100); //调用代币合约的transferFrom方法实现转账 TransactionReceipt transferReceipt = contract.transferFrom(new Address(fromAddress), new Address(toAddress), new Uint256(transferAmount)).send(); ``` 以上代码中,`ERC20Token`是自定义的Java类,用于与代币合约进行交互,需要根据代币合约的ABI文件生成。具体生成方法可以参考web3j的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值