|
@@ -0,0 +1,85 @@
|
|
|
+package com.ichaoj.ams.script.test.nft;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
+import com.ichaoj.ams.script.IScript;
|
|
|
+import com.ichaoj.ams.script.SResult;
|
|
|
+import com.ichaoj.ams.script.annotation.Script;
|
|
|
+import com.ichaoj.ams.script.annotation.ScriptParam;
|
|
|
+import com.ichaoj.ams.script.model.AirdropParam;
|
|
|
+import com.ichaoj.ams.script.model.AirdropWallet;
|
|
|
+import com.ichaoj.ams.script.util.Web3Util;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.web3j.abi.FunctionEncoder;
|
|
|
+import org.web3j.abi.datatypes.Function;
|
|
|
+import org.web3j.abi.datatypes.generated.Uint256;
|
|
|
+import org.web3j.crypto.Credentials;
|
|
|
+import org.web3j.crypto.RawTransaction;
|
|
|
+import org.web3j.crypto.TransactionEncoder;
|
|
|
+import org.web3j.protocol.Web3j;
|
|
|
+import org.web3j.protocol.core.DefaultBlockParameterName;
|
|
|
+import org.web3j.protocol.core.Response;
|
|
|
+import org.web3j.protocol.core.methods.request.Transaction;
|
|
|
+import org.web3j.protocol.core.methods.response.EthSendTransaction;
|
|
|
+import org.web3j.protocol.http.HttpService;
|
|
|
+import org.web3j.utils.Convert;
|
|
|
+import org.web3j.utils.Numeric;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wren
|
|
|
+ */
|
|
|
+@Script(name = "test-script",
|
|
|
+ params = {
|
|
|
+ @ScriptParam(name = "quantity", note = "铸币量", defaultValue = "1")
|
|
|
+ }
|
|
|
+)
|
|
|
+public class Erc21TestScript extends IScript {
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public SResult run(Map<String, AirdropParam> params, AirdropWallet airdropWallet) {
|
|
|
+ Web3j web3j = Web3j.build(new HttpService("https://eth-sepolia.g.alchemy.com/v2/QR461hv0kEkKn91VlTAlIx6RhnnfPl-m"));
|
|
|
+ Function mint = new Function("mint", ListUtil.of(new Uint256(Long.parseLong(params.get("quantity").getValue()))), ListUtil.empty());
|
|
|
+ BigInteger nonce = Web3Util.getNonce(web3j, airdropWallet.getAddress());
|
|
|
+ BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
|
|
|
+ String mintEncoder = FunctionEncoder.encode(mint);
|
|
|
+ Transaction callTransaction = Transaction.createFunctionCallTransaction(
|
|
|
+ airdropWallet.getAddress(),
|
|
|
+ nonce,
|
|
|
+ gasPrice,
|
|
|
+ BigInteger.valueOf(0),
|
|
|
+ "0xA786221716dd8892003B9e8e029D7bF602e429b1",
|
|
|
+ mintEncoder);
|
|
|
+ BigInteger gasLimit = Web3Util.getTransactionGasLimit(web3j, callTransaction);
|
|
|
+ RawTransaction rawTransaction = RawTransaction.createTransaction(
|
|
|
+ nonce,
|
|
|
+ gasPrice,
|
|
|
+ gasLimit,
|
|
|
+ "0xA786221716dd8892003B9e8e029D7bF602e429b1",
|
|
|
+ BigInteger.valueOf(0),
|
|
|
+ mintEncoder);
|
|
|
+
|
|
|
+ Credentials credentials = Web3Util.getCredentials(airdropWallet);
|
|
|
+ byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
|
|
|
+ String hexValue = Numeric.toHexString(signMessage);
|
|
|
+ //发送交易
|
|
|
+ EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();
|
|
|
+ Response.Error error = ethSendTransaction.getError();
|
|
|
+ if (error != null) {
|
|
|
+ throw new RuntimeException(error.getMessage());
|
|
|
+ }
|
|
|
+ org.web3j.protocol.core.methods.response.Transaction transaction = web3j
|
|
|
+ .ethGetTransactionByHash(ethSendTransaction.getTransactionHash())
|
|
|
+ .send()
|
|
|
+ .getTransaction()
|
|
|
+ .get();
|
|
|
+ BigInteger gasAmount = transaction.getGas().multiply(transaction.getGasPrice());
|
|
|
+ SResult sResult = new SResult();
|
|
|
+ BigInteger balance = web3j.ethGetBalance(airdropWallet.getAddress(), DefaultBlockParameterName.LATEST).send().getBalance();
|
|
|
+ sResult.setGas(gasAmount.toString());
|
|
|
+ sResult.setTxId(transaction.getHash());
|
|
|
+ sResult.setCurrentBalance(balance.toString());
|
|
|
+ return sResult;
|
|
|
+ }
|
|
|
+}
|