Magician-ContractsTools はスマート コントラクトを呼び出すためのツールキットで、Java プログラムでスマート コントラクトを簡単に呼び出して、クエリや書き込み操作を行うことができます。
ERC20、ERC721、ERC1155 という 3 つの標準契約テンプレートが組み込まれており、これら 3 つの契約の標準関数を呼び出す必要がある場合、作業を迅速に完了するのに役立ちます。組み込みの契約テンプレートに加えて、必要に応じてカスタム コントラクト関数を簡単に呼び出すこともできます。今後も標準テンプレートを追加し続ける予定です。
さらに、InputData デコードおよび ETH クエリと転送のためのツールもあります
ETH(BSC、POLYGONなど)、SOL、TRONの3つのチェーンをサポート予定
依存関係をインポートする
<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>
复制代码
契約の問い合わせと書き込み
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(); // 交易成功上链后的结果
复制代码
契約書テンプレート
現在テンプレートは 3 つしかありませんが、今後も追加されていく予定です(スペース節約のため、ここでは ERC20 のみを示しています。詳しくは公式 Web サイトをご覧ください)。
ERC20契約に電話をかける
お問い合わせ
// 调用合约的 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"), // 转账金额
"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(); // 交易成功上链后的结果
复制代码
入力データ コーデック
// 编码
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,如果还有其他参数,可以继续传入下一个
);
复制代码
メインチェーンのコインのクエリと転送
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
);
复制代码