val function =when(methodName){
"swapEthForExactTokens" -> {
val amountOut =map["amountOut"] as Int
val address =Gson().fromJson<String>(map["address"] as JsonArray,object :TypeToken<List<String>>(){}.type).map { Address(it.toString()) }
val toAddress =map["to"] as String
val deadline =map["deadline"] as Int
Function( methodName, listOf(Uint(amountOut.toBigInteger()), DynamicArray(address), Address(toAddress), Uint(deadline.toBigInteger())),listOf(object : TypeReference<Type<*>?>() {}))
}
else ->Function(methodName, listOf(Address(approveToAddress), Uint256(num.toPlainString().toBigInteger())), listOf(object : TypeReference<Type<*>?>() {}))
}
//创建RawTransaction交易对象
val rawTransaction = RawTransaction.createTransaction(nonce.toBigInteger(), gasPrice.toBigInteger(), gasLimit.toBigInteger(), contractAddress, FunctionEncoder.encode(function))
//签名Transaction
val signMessage = TransactionEncoder.signMessage(rawTransaction, Credentials.create(fromPrivateKey))
return Numeric.toHexString(signMessage)
UniSwap 使用Web3j 生成原生币兑换代币。
最新推荐文章于 2023-05-24 21:54:57 发布