Skip to content

Commit

Permalink
Merge pull request #15 from solana-mobile/fix-sendtx-api
Browse files Browse the repository at this point in the history
Fix sendTransaction API
  • Loading branch information
Funkatronics authored Jun 10, 2024
2 parents a6ea146 + 8415ae3 commit 8071edc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
ktor-client-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" }
multimult = { group = "io.github.funkatronics", name = "multimult", version = "0.2.1" }
multimult = { group = "io.github.funkatronics", name = "multimult", version = "0.2.2" }
web3-solana = { group = "com.solanamobile", name = "web3-solana", version = "0.3.0-beta3" }

[plugins]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package com.solana.networking
import com.solana.rpccore.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

class Rpc20Driver(private val url: String,
private val httpDriver: HttpNetworkDriver,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.solana.rpc

import com.funkatronics.encoders.Base58
import com.funkatronics.encoders.Base64
import com.solana.networking.HttpNetworkDriver
import com.solana.networking.Rpc20Driver
import com.solana.publickey.SolanaPublicKey
Expand Down Expand Up @@ -69,14 +70,14 @@ class SolanaRpcClient(

suspend fun sendTransaction(
transaction: Transaction,
skipPreflight: Boolean = false,
options: TransactionOptions = defaultTransactionOptions,
requestId: String? = null
) = makeRequest(SendTransactionRequest(transaction, skipPreflight, requestId), String.serializer())
) = makeRequest(SendTransactionRequest(transaction, options, requestId), String.serializer())

suspend fun sendAndConfirmTransaction(
transaction: Transaction,
options: TransactionOptions = defaultTransactionOptions
) = sendTransaction(transaction).apply {
) = sendTransaction(transaction, options).apply {
result?.let { confirmTransaction(it, options) }
}

Expand Down Expand Up @@ -181,14 +182,19 @@ class SolanaRpcClient(

class SendTransactionRequest(
transaction: Transaction,
skipPreflight: Boolean = false,
options: TransactionOptions,
requestId: String? = null
) : SolanaRpcRequest(
method = "sendTransaction",
params = buildJsonArray {
add(Base58.encodeToString(transaction.serialize()))
add(when (options.encoding) {
Encoding.base58 -> Base58.encodeToString(transaction.serialize())
Encoding.base64 -> Base64.encodeToString(transaction.serialize())
})
addJsonObject {
put("skipPreflight", skipPreflight)
put("encoding", options.encoding.getEncoding())
put("skipPreflight", options.skipPreflight)
put("preflightCommitment", options.preflightCommitment.toString())
}
},
requestId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import kotlin.time.Duration.Companion.seconds

enum class Encoding(private val enc: String) {
base64("base64"),
base58("base58"),
jsonParsed("jsonParsed");
base58("base58");
fun getEncoding(): String {
return enc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class RpcClientTests {
}

@Test
fun `sendTransaction returns transaction signature`() = runTest {
fun `sendTransaction with base58 encoding returns transaction signature`() = runTest {
// given
val keyPair = Ed25519.generateKeyPair()
val pubkey = SolanaPublicKey(keyPair.publicKey)
Expand All @@ -106,7 +106,49 @@ class RpcClientTests {
Transaction(listOf(sig), this)
}

val response = rpc.sendTransaction(transaction, skipPreflight = true)
val response = rpc.sendTransaction(transaction,
TransactionOptions(
commitment = Commitment.CONFIRMED,
encoding = Encoding.base58,
skipPreflight = true
)
)

// then
assertNull(airdropResponse.error)
assertNotNull(airdropResponse.result)
assertNull(response.error)
assertNotNull(response.result)
assertTrue { response.result!!.isNotEmpty() }
}

@Test
fun `sendTransaction with base64 encoding returns transaction signature`() = runTest {
// given
val keyPair = Ed25519.generateKeyPair()
val pubkey = SolanaPublicKey(keyPair.publicKey)
val rpc = SolanaRpcClient(TestConfig.RPC_URL, KtorNetworkDriver())
val message = "hello solana!"

// when
val airdropResponse = rpc.requestAirdrop(pubkey, 0.1f)
val blockhashResponse = rpc.getLatestBlockhash()

val transaction = Message.Builder()
.setRecentBlockhash(blockhashResponse.result!!.blockhash)
.addInstruction(buildMemoTransaction(pubkey, message))
.build().run {
val sig = Ed25519.sign(keyPair, serialize())
Transaction(listOf(sig), this)
}

val response = rpc.sendTransaction(transaction,
TransactionOptions(
commitment = Commitment.CONFIRMED,
encoding = Encoding.base64,
skipPreflight = true
)
)

// then
assertNull(airdropResponse.error)
Expand Down

0 comments on commit 8071edc

Please sign in to comment.