0

为什么交易没有出现在主网上?我正在尝试通过向自己发送零 eths 来存储数据。下面的代码在 ropsten 上运行良好,但在主网上却不行。汽油价格和限额高于我使用 MetaMask 成功手动设置数据时使用的价格。我的钱包里有 0.0137 ETH。我尝试过长时间超时(10 分钟)的 sendAsync。该代码与 web3j 的示例代码http://docs.web3j.io/latest/getting_started/interacting_with_node/#send-ether非常相似。下面的代码在 Kotlin 中。

fun writeData(dataToStore: String, publicKey: String, privateKey: String): String {
    val nonce =  web3j
        .ethGetTransactionCount(publicKey, DefaultBlockParameterName.LATEST)
        .send()
        .transactionCount

    val dataAsByteArray = dataToStore.toByteArray(Charsets.UTF_8)
    val dataAsHexString: String = Numeric.toHexString(dataAsByteArray)
    val toAddress = publicKey
    val gasPrice = BigInteger.valueOf(140_000_000_000L)
    val gasLimit = BigInteger.valueOf(150_000L)

    val rawTransaction = RawTransaction.createTransaction(
        nonce,
        gasPrice,
        gasLimit,
        toAddress,
        dataAsHexString
    )
    val credentials = Credentials.create(privateKey)
    val signedMessage: ByteArray = TransactionEncoder
        .signMessage(rawTransaction, credentials)
    val hexValue = Numeric.toHexString(signedMessage)

    return web3j.ethSendRawTransaction(hexValue).send().transactionHash
        ?: throw Exception("Something went wrong")
}
4

0 回答 0