Skip to content

Commit

Permalink
more fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Jun 7, 2024
1 parent e08bbde commit 1f05bc6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ class SolanaRpcClient(
) : SolanaRpcRequest(
method = "sendTransaction",
params = buildJsonArray {
add(
if(options.encoding == Encoding.base58)
Base58.encodeToString(transaction.serialize())
else Base64.encodeToString(transaction.serialize())
)
add(when (options.encoding) {
Encoding.base58 -> Base58.encodeToString(transaction.serialize())
Encoding.base64 -> Base64.getEncoder(true)
.encodeToString(transaction.serialize())
})
addJsonObject {
put("encoding", options.encoding.getEncoding())
put("skipPreflight", options.skipPreflight)
put("preflightCommitment", options.preflightCommitment.value)
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 @@ -109,6 +109,43 @@ class RpcClientTests {
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
)
)
Expand Down

0 comments on commit 1f05bc6

Please sign in to comment.