Magician-ContractsTools智能合约工具包

联合创作 · 2023-09-18 15:00

 Magician-ContractsTools 是一个用于调用智能合约的工具包,你可以非常容易地在Java程序中调用智能合约进行查询和写入操作。

有三个内置的标准合约模板,分别是ERC20、ERC721和ERC1155,如果你需要调用这三个合约中的标准函数,可以帮助你非常快速地完成工作。除了内置的合同模板外,如果你需要调用自定义的合同函数也是很容易的,以后我们还会继续增加标准模板。

此外,还有InputData解码和ETH查询和转移的工具

计划支持三种链,ETH(BSC、POLYGON等)、SOL和TRON

导入依赖

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician-ContractsTools</artifactId>
    <version>1.0.1</version>
</dependency>

<!-- This is the logging package, you must have it or the console will not see anything, any logging package that can bridge with slf4j is supported -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

合约查询 以及 写入

初始化合约工具对象

String privateKey = ""; // 私钥
Web3j web3j = Web3j.build(new HttpService("https://data-seed-prebsc-1-s1.binance.org:8545/")); // 链的RPC地址
String contractAddress = "";

EthContractUtil ethContractUtil = EthContractUtil.builder(web3j);

查询合约

// 查询
List<Type> result = ethContractUtil.select(
            contractAddress, // 合约地址
            EthAbiCodecTool.getInputData(
                    "balanceOf", // 要调用的方法名称
                    new Address(toAddress) // 方法的参数,如果有多个,可以继续传入下一个参数
            ),  // 要调用的方法的inputData
            new TypeReference<Uint256>() {} // 方法的返回类型,如果有多个返回值,可以继续传入下一个参数
        );

 写入合约

// 往合约里写入数据
SendResultModel sendResultModel = ethContractUtil.sendRawTransaction(
                SendModel.builder()
                        .setSenderAddress("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84") // 调用者的地址
                        .setPrivateKey("")// senderAddress的私钥
                        .setToAddress("0x428862f821b1A5eFff5B258583572451229eEeA6") // 合约地址
                        .setValue(new BigInteger("1000000000")) // 主链币数量,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasPrice(new BigInteger("1000")) // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasLimit(new BigInteger("800000")) // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
                EthAbiCodecTool.getInputData(
                        "transfer", // 要调用的方法名称
                        new Address(toAddress), // 方法的参数,如果有多个,可以继续传入下一个参数
                        new Uint256(new BigInteger("1000000000000000000")) // 方法的参数,如果有多个,可以继续传入下一个参数
                ) // 要调用的方法的inputData
            );

sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

合约模板

目前只有三种模板,后面会继续增加,为了节约篇幅 所以这里只展示ERC20的用法,详情可以看官网文档

调用ERC20合约

初始化合约模板

Web3j web3j = Web3j.build(new HttpService("https://data-seed-prebsc-2-s1.binance.org:8545"));

String contractAddress = "";

ERC20Contract erc20Contract = ERC20Contract.builder(web3j, contractAddress);

查询

// 调用合约的 totalSupply 函数
BigInteger total = erc20Contract.totalSupply();

// 调用合约的 balanceOf 函数
BigInteger amount = erc20Contract.balanceOf("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84");

// 调用合约的 allowance 函数
BigInteger amount = erc20Contract.allowance("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", "0x552115849813d334C58f2757037F68E2963C4c5e");

写入

// 调用合约的 transfer 函数
SendResultModel sendResultModel = erc20Contract.transfer(
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 转账接收人
                new BigInteger("1000000000000000000"), // 转账金额
                SendModel.builder()
                        .setSenderAddress("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84") // 调用者的地址
                        .setPrivateKey("")// senderAddress的私钥
                        .setValue(new BigInteger("1000000000")) // 主链币数量,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasPrice(new BigInteger("1000")) // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasLimit(new BigInteger("800000")) // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

// 调用合约的 transferFrom 函数
SendResultModel sendResultModel = erc20Contract.transferFrom(
                "0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", // 转账付款人
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 转账接收人
                new BigInteger("1000000000000000000"), // 转账金额
                SendModel.builder()
                        .setSenderAddress("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84") // 调用者的地址
                        .setPrivateKey("")// senderAddress的私钥
                        .setValue(new BigInteger("1000000000")) // 主链币数量,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasPrice(new BigInteger("1000")) // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasLimit(new BigInteger("800000")) // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

// 调用合约的 approve 函数
SendResultModel sendResultModel = erc20Contract.approve(
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 被授权人
                new BigInteger("1000000000000000000"), // 授权金额
                SendModel.builder()
                        .setSenderAddress("0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84") // 调用者的地址
                        .setPrivateKey("")// senderAddress的私钥
                        .setValue(new BigInteger("1000000000")) // 主链币数量,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasPrice(new BigInteger("1000")) // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                        .setGasLimit(new BigInteger("800000")) // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

InputData 编解码

// 编码
String inputData = EthAbiCodecTool.getInputData(
            "transfer", // 方法名
            new Address(toAddress), // 参数1
            new Uint256(new BigInteger("1000000000000000000")) // 参数2,如果还有其他参数,可以继续传入下一个
    );

// 解码
List<Type> result = EthAbiCodecTool.decoderInputData(
            "0x" + inputData.substring(10), // 去除方法签名的inputData
            new TypeReference<Address>() {}, // 被编码的方法的参数1 类型
            new TypeReference<Uint256>() {} // 被编码的方法的参数2 类型, 如果还有其他参数,可以继续传入下一个
    );

for(Type type : result){
    System.out.println(type.getValue());
}

// 获取方法签名,其实就是inputData的前十位
String functionCode = EthAbiCodecTool.getFunAbiCode(
            "transfer", // 方法名
            new Address(toAddress), // 参数1,值随意传,反正我们要的方法签名,不是完整的inputData
            new Uint256(new BigInteger("1000000000000000000")) // 参数2,值随意传,反正我们要的方法签名,不是完整的inputData,如果还有其他参数,可以继续传入下一个
    );

文档地址

https://magician-io.com/chain

 

 

 

浏览 5
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报