web3j 合约方法调用源码分析

文章目录

调用方法流程
  1. 方法包括方法名,参数 返回值 (Function)
  2. 对方法进行编码(FunctionEncoder.encode)
  3. 根据none pirce limit address 方法编码 创建交易信息(RawTransaction.createTransaction)
  4. 签名交易信息 (TransactionEncoder.signMessage)
  5. 并转成16进制数据 (Numeric.toHexString)
  6. 发送交易
  7. 通过交易原数据和签名拿到hash(TransactionUtils.generateTransactionHashHexEncoded)
Function
java 复制代码
  public Function(String name, List<Type> inputParameters, List<TypeReference<?>> outputParameters) {
        this.name = name;
        this.inputParameters = inputParameters;
        this.outputParameters = Utils.convert(outputParameters);
    }
RawTransaction
java 复制代码
  protected RawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data, BigInteger gasPremium, BigInteger feeCap) {
        this.nonce = nonce;
        this.gasPrice = gasPrice;
        this.gasLimit = gasLimit;
        this.to = to;
        this.value = value;
        this.data = data != null ? Numeric.cleanHexPrefix(data) : null;
        this.gasPremium = gasPremium;
        this.feeCap = feeCap;
    }
Credentials
java 复制代码
 public static Credentials create(ECKeyPair ecKeyPair) {
        String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair));
        return new Credentials(ecKeyPair, address);
    }
signMessage
java 复制代码
 public static byte[] signMessage(RawTransaction rawTransaction, Credentials credentials) {
        byte[] encodedTransaction = encode(rawTransaction);
        Sign.SignatureData signatureData = Sign.signMessage(encodedTransaction, credentials.getEcKeyPair());
        return encode(rawTransaction, signatureData);
    }
generateTransactionHash
java 复制代码
 public static byte[] generateTransactionHash(RawTransaction rawTransaction, Credentials credentials) {
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        return Hash.sha3(signedMessage);
    }
toHexString
java 复制代码
 public static String toHexString(byte[] input) {
        return toHexString(input, 0, input.length, true);
    }
    
    
    public static String toHexString(byte[] input, int offset, int length, boolean withPrefix) {
        StringBuilder stringBuilder = new StringBuilder();
        if (withPrefix) {
            stringBuilder.append("0x");
        }

        for(int i = offset; i < offset + length; ++i) {
            stringBuilder.append(String.format("%02x", input[i] & 255));
        }

        return stringBuilder.toString();
    }
RawTransactionManager
java 复制代码
 public EthSendTransaction sendTransaction(BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value, boolean constructor) throws IOException {
        BigInteger nonce = this.getNonce();
        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);
        return this.signAndSend(rawTransaction);
    }
合约执行流程
java 复制代码
   //1
   this.executeTransaction(function);
   //2
   this.executeTransaction(function, BigInteger.ZERO);
   //3
   this.executeTransaction(FunctionEncoder.encode(function), weiValue, function.getName());
   //4 weiValue如果是转eth就是数量 如果是调用合约方法就是data
   this.executeTransaction(data, weiValue, funcName, false);
   //5
    TransactionReceipt receipt = this.send(this.contractAddress, data, weiValue, this.gasProvider.getGasPrice(funcName), this.gasProvider.getGasLimit(funcName), constructor);
    //6       
    this.transactionManager.executeTransaction(gasPrice, gasLimit, to, data, value, constructor);
    //7
    this.sendTransaction(gasPrice, gasLimit, to, data, value, constructor);
FastRawTransactionManager

维护了一个nonce 避免每次发送请求都区获取nonce

可以最大限度地减少向节点发送RPC请求的次数,从而提高交易发送的响应速度。

NoOpProcessor

使用NoOpProcessor的一个常见场景是,当我们只需要发送交易,而不关心区块事件或其他通知时,可以将其设置为事件处理器,避免不必要的事件处理开销。

这允许调用方对提交到网络的交易拥有交易哈希。

java 复制代码
     ///使用7
     public  static TransactionManager getTxManager(Credentials credentials, Web3j web3j){
        NoOpProcessor processor = new NoOpProcessor(web3j);
        return new FastRawTransactionManager(web3j, credentials, processor);
    }
    ///使用
    this.sendTransaction(gasPrice, gasLimit, to, FunctionEncoder.encode(function), BigInteger.ZERO, false);
    
    
     public  static String sendEthTransaction(Credentials credentials, Web3j web3j,BigInteger weiValue,BigInteger gasPrice, BigInteger gasLimit, String to){
        try {
           return getTxManager(credentials,web3j).sendTransaction(gasPrice, gasLimit, to, "", weiValue, false).getTransactionHash();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
代码
kotlin 复制代码
@Throws(
    IOException::class,
    ExecutionException::class,
    InterruptedException::class
)
fun signTokenTransaction(
    amount: String,
    to: String,
    privateKey: String,
    coinAddress: String,
    decimals: Int,
    nonce: BigInteger
): Pair<String, String> {
    //支付的矿工费
    val gasPrice = getWeb3j().ethGasPrice().send().gasPrice
    val gasLimit = BigInteger("60000")
    val credentials = Credentials.create(privateKey)
    val amountWei =
        BigDecimal.TEN.pow(decimals).multiply(BigDecimal(amount)).toBigInteger()
    //封装转账交易
    val function = Function(
        "transfer",
        listOf<Type<*>>(
            Address(to),
            Uint256(amountWei)
        ), emptyList()
    )
    val data = FunctionEncoder.encode(function)
    //签名交易
    val rawTransaction = RawTransaction.createTransaction(
        nonce,
        gasPrice,
        gasLimit,
        coinAddress,
        data
    )
    val signMessage = TransactionEncoder.signMessage(rawTransaction, credentials)

    val hexValue = Numeric.toHexString(signMessage)
    val hash = TransactionUtils.generateTransactionHashHexEncoded(
        rawTransaction,
        Credentials.create(privateKey)
    )
    return hexValue to hash

    //广播交易
//    return getWeb3j().ethSendRawTransaction(Numeric.toHexString(signMessage)).sendAsync().get()
//        .transactionHash
}
相关推荐
黑客老陈14 小时前
新手小白如何挖掘cnvd通用漏洞之存储xss漏洞(利用xss钓鱼)
运维·服务器·前端·网络·安全·web3·xss
小树苗1933 天前
DePIN潜力项目Spheron解读:激活闲置硬件,赋能Web3与AI
人工智能·web3
CESS_Cloud3 天前
CESS 出席华盛顿区块链政策峰会:参与国家安全与数据隐私保护专题讨论
安全·阿里云·web3·去中心化·区块链
我可是千机伞4 天前
BOB.meme已于12月18日正式部署于BNB Chain
web3
TianXuan_Chain4 天前
web3跨链桥协议-Nomad
web3·区块链·智能合约·跨链桥
黑客老陈5 天前
蓝队HW初级面试题总结
服务器·网络·安全·小程序·web3
TinTin Land5 天前
Electric Capital 2024 年开发者报告 | 洞见未来 Web3 生态发展方向
web3
TechubNews6 天前
IntoTheBlock 联创:Web3 基础设施正在被过度建设,我们正在盲目行事
web3·区块链
Roun36 天前
探索Web3的核心原则:去中心化与用户控制
web3·去中心化·区块链
dingzd956 天前
Web3 时代:技术变革与未来展望
web3·去中心化·区块链·互联网