With this toolkit, it is easier to call smart contracts in Java

Magician-ContractsTools is a toolkit for calling smart contracts. You can easily call smart contracts in Java programs for query and write operations.

There are three built-in standard contract templates, namely ERC20, ERC721 and ERC1155. If you need to call the standard functions in these three contracts, it can help you complete the work very quickly. In addition to the built-in contract templates, it is also easy to call custom contract functions if you need to, and we will continue to add standard templates in the future.

In addition, there are tools for InputData decoding and ETH query and transfer

Plan to support three chains, ETH (BSC, POLYGON, etc.), SOL and TRON

import dependencies

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician-ContractsTools</artifactId>
    <version>1.0.0</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>
复制代码

Contract query and write

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>() {} // 方法的返回类型,如果有多个返回值,可以继续传入下一个参数
        );

// 往合约里写入数据
// gasPrice,gasLimit 两个参数,如果想用默认值可以不传,或者传null
// 如果不传的话,两个参数都必须不传,要传就一起传, 如果设置为null的话,可以一个为null,一个有值
SendResultModel sendResultModel = ethContractUtil.sendRawTransaction(
                    senderAddress, // 调用者的地址
                    contractAddress, // 合约地址
                    privateKey, // senderAddress的私钥
                    new BigInteger("1200000"), // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
                    new BigInteger("800000"), // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
                    EthAbiCodecTool.getInputData(
                            "transfer", // 要调用的方法名称
                            new Address(toAddress), // 方法的参数,如果有多个,可以继续传入下一个参数
                            new Uint256(new BigInteger("1000000000000000000")) // 方法的参数,如果有多个,可以继续传入下一个参数
                    ) // 要调用的方法的inputData
            );

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

contract template

Currently there are only three templates, which will continue to be added later. In order to save space, only ERC20 is shown here. For details, please visit the official website.

Call ERC20 contract

Inquire

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

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

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

to write

// 调用合约的 transfer 函数
SendResultModel sendResultModel = erc20Contract.transfer(
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 转账接收人
                new BigInteger("1000000000000000000"), // 转账金额
                "0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", // 调用者的地址
                "", // 调用者的私钥
                null, // gasPrice,如果传null,自动使用默认值
                null // gasLimit,如果传null,自动使用默认值
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

// 调用合约的 transferFrom 函数
SendResultModel sendResultModel = erc20Contract.transferFrom(
                "0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", // 转账付款人
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 转账接收人
                new BigInteger("1000000000000000000"), // 转账金额
                "0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", // 调用者的地址
                "", // 调用者的私钥
                null, // gasPrice,如果传null,自动使用默认值
                null // gasLimit,如果传null,自动使用默认值
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果

// 调用合约的 approve 函数
SendResultModel sendResultModel = erc20Contract.approve(
                "0x552115849813d334C58f2757037F68E2963C4c5e", // 被授权人
                new BigInteger("1000000000000000000"), // 授权金额
                "0xb4e32492E9725c3215F1662Cf28Db1862ed1EE84", // 调用者的地址
                "", // 调用者的私钥
                null, // gasPrice,如果传null,自动使用默认值
                null // gasLimit,如果传null,自动使用默认值
        );
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果
复制代码

InputData codec

// 编码
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,如果还有其他参数,可以继续传入下一个
    );
复制代码

Main chain currency query and transfer

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

// 这种方式是单例的
EthHelper ethHelper =  MagicianWeb3.getEthBuilder().getEth(web3j);
// 如果你想创建多个EthHelper对象,可以用这种方式
EthHelper ethHelper = EthHelper.builder(web3j);

// 余额查询
BigInteger balance = ethHelper.balanceOf(fromAddress);

// 转账
TransactionReceipt transactionReceipt = ethHelper.transfer(
            toAddress,
            privateKey, 
            BigDecimal.valueOf(1),
            Convert.Unit.ETHER
);
复制代码

Official website address

https://magician-io.com

Guess you like

Origin blog.csdn.net/sherlockholmes11/article/details/128069521