Skip to content

Commit

Permalink
normalize use of context
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Jun 15, 2024
1 parent 16d8d6a commit dceef5d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
33 changes: 18 additions & 15 deletions solanaclient/src/commonMain/kotlin/com/solana/rpc/AccountInfo.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.solana.rpc

import com.solana.publickey.SolanaPublicKey
import com.solana.serialization.deserializer
import com.solana.serialization.wrappedSerializer
import com.solana.serializers.BorshAsAsEncodedDataArrayDeserializer
import com.solana.serializers.BorshAsBase64JsonArraySerializer
import com.solana.serializers.ByteArrayAsEncodedDataArrayDeserializer
import com.solana.serializers.SolanaResponseSerializer
import com.solana.serializers.SolanaResponseDeserializer
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -30,14 +29,14 @@ data class AccountInfoWithPublicKey<P>(
)

fun <A> SolanaAccountDeserializer(deserializer: DeserializationStrategy<A>) =
SolanaResponseSerializer(
SolanaResponseDeserializer(
AccountInfo.serializer(
BorshAsBase64JsonArraySerializer(deserializer.wrappedSerializer())
)
).deserializer()
)

fun MultipleAccountsDeserializer() =
SolanaResponseSerializer(
SolanaResponseDeserializer(
ListSerializer(
AccountInfo.serializer(
ByteArrayAsEncodedDataArrayDeserializer.wrappedSerializer()
Expand All @@ -46,24 +45,28 @@ fun MultipleAccountsDeserializer() =
)

fun <A> MultipleAccountsDeserializer(deserializer: DeserializationStrategy<A>) =
SolanaResponseSerializer(
SolanaResponseDeserializer(
ListSerializer(
AccountInfo.serializer(
BorshAsAsEncodedDataArrayDeserializer(deserializer).wrappedSerializer()
).nullable
)
).deserializer()
)

fun ProgramAccountsDeserializer() =
ListSerializer(
AccountInfoWithPublicKey.serializer(
ByteArrayAsEncodedDataArrayDeserializer.wrappedSerializer()
SolanaResponseDeserializer(
ListSerializer(
AccountInfoWithPublicKey.serializer(
ByteArrayAsEncodedDataArrayDeserializer.wrappedSerializer()
)
)
).deserializer()
)

fun <A> ProgramAccountsDeserializer(deserializer: DeserializationStrategy<A>) =
ListSerializer(
AccountInfoWithPublicKey.serializer(
BorshAsAsEncodedDataArrayDeserializer(deserializer).wrappedSerializer()
SolanaResponseDeserializer(
ListSerializer(
AccountInfoWithPublicKey.serializer(
BorshAsAsEncodedDataArrayDeserializer(deserializer).wrappedSerializer()
)
)
).deserializer()
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.solana.networking.HttpNetworkDriver
import com.solana.networking.Rpc20Driver
import com.solana.publickey.SolanaPublicKey
import com.solana.rpccore.RpcRequest
import com.solana.serializers.SolanaResponseSerializer
import com.solana.serializers.SolanaResponseDeserializer
import com.solana.transaction.Transaction
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
Expand Down Expand Up @@ -59,7 +59,7 @@ class SolanaRpcClient(
requestId: String? = null
) = makeRequest(
BalanceRequest(address, commitment, requestId),
SolanaResponseSerializer(Long.serializer())
SolanaResponseDeserializer(Long.serializer())
)

suspend fun getLatestBlockhash(
Expand All @@ -68,7 +68,7 @@ class SolanaRpcClient(
requestId: String? = null
) = makeRequest(
LatestBlockhashRequest(commitment, minContextSlot, requestId),
SolanaResponseSerializer(BlockhashResponse.serializer())
SolanaResponseDeserializer(BlockhashResponse.serializer())
)

suspend fun getMinBalanceForRentExemption(
Expand Down Expand Up @@ -148,7 +148,7 @@ class SolanaRpcClient(
requestId: String? = null
) = makeRequest(
SignatureStatusesRequest(signatures, searchTransactionHistory, requestId),
SolanaResponseSerializer(ListSerializer(SignatureStatus.serializer().nullable))
SolanaResponseDeserializer(ListSerializer(SignatureStatus.serializer().nullable))
)

suspend fun sendTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ProgramAccountsRequest(
method = "getProgramAccounts",
params = { add(program.base58()) },
configuration = {
put("withContext", false)
put("withContext", true)
put("commitment", commitment?.serialName())
put("minContextSlot", minContextSlot)
put("dataSlice", buildJsonObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.solana.rpc
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject

@Serializable
class SolanaResponse<V>(val context: Context, val value: V?)

@Serializable
class Context(val apiVersion: String, val slot: ULong)

@Serializable
class BlockhashResponse(
val blockhash: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@ package com.solana.serializers

import com.funkatronics.kborsh.Borsh
import com.solana.rpc.Encoding
import com.solana.rpc.SolanaResponse
import com.solana.serialization.wrappedSerializer
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class SolanaResponseSerializer<R>(dataSerializer: KSerializer<R>)
: KSerializer<R?> {
private val serializer = WrappedValue.serializer(dataSerializer)
class SolanaResponseDeserializer<R>(dataSerializer: DeserializationStrategy<R>)
: DeserializationStrategy<R?> {
private val serializer = SolanaResponse.serializer(dataSerializer.wrappedSerializer())
override val descriptor: SerialDescriptor = serializer.descriptor

override fun serialize(encoder: Encoder, value: R?) =
encoder.encodeSerializableValue(serializer, WrappedValue(value))

override fun deserialize(decoder: Decoder): R? =
decoder.decodeSerializableValue(serializer).value
}

@Serializable
private class WrappedValue<V>(val value: V?)

internal class ByteArrayAsEncodedDataArraySerializer(val encoding: Encoding) : SerializationStrategy<ByteArray> {
private val delegateSerializer = ListSerializer(String.serializer())
override val descriptor: SerialDescriptor = delegateSerializer.descriptor
Expand Down
31 changes: 17 additions & 14 deletions solanaclient/src/commonTest/kotlin/com/solana/rpc/RpcClientTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,23 @@ class RpcClientTests {
val mockedResponse = """
{
"jsonrpc":"2.0",
"result":[{
"account": {
"data":[
"${Base64.encodeToString(testDataBorsh)}",
"base64"
]
"executable":true,
"lamports":1,
"owner":"11111111111111111111111111111111",
"rentEpoch":1234567890,
"space":${testDataBorsh.size}
},
"pubkey": "11111111111111111111111111111111"
}]
"result":{
"context":{"apiVersion":"apiVer","slot":123456789},
"value":[{
"account": {
"data":[
"${Base64.encodeToString(testDataBorsh)}",
"base64"
]
"executable":true,
"lamports":1,
"owner":"11111111111111111111111111111111",
"rentEpoch":1234567890,
"space":${testDataBorsh.size}
},
"pubkey": "11111111111111111111111111111111"
}]
}
"id":"requestId"
}
""".trimIndent()
Expand Down

0 comments on commit dceef5d

Please sign in to comment.