-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8071edc
commit 913cfa1
Showing
7 changed files
with
203 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
solanaclient/src/commonMain/kotlin/com/solana/rpc/AccountInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.solana.rpc | ||
|
||
import com.solana.publickey.SolanaPublicKey | ||
import com.solana.serializers.BorshAsBase64JsonArraySerializer | ||
import com.solana.serializers.SolanaResponseSerializer | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AccountInfo<D>( | ||
val data: D?, | ||
val executable: Boolean, | ||
val lamports: ULong, | ||
val owner: SolanaPublicKey, | ||
val rentEpoch: ULong, | ||
val size: ULong? = null | ||
) | ||
|
||
fun <A> SolanaAccountSerializer(serializer: KSerializer<A>) = | ||
SolanaResponseSerializer( | ||
AccountInfo.serializer( | ||
BorshAsBase64JsonArraySerializer(serializer) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
solanaclient/src/commonMain/kotlin/com/solana/serializers/SolanaResponseSerializer.kt
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
solanaclient/src/commonMain/kotlin/com/solana/serializers/SolanaResponseSerializers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.solana.serializers | ||
|
||
import com.funkatronics.encoders.Base64 | ||
import com.funkatronics.kborsh.BorshDecoder | ||
import com.funkatronics.kborsh.BorshEncoder | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.SerializationException | ||
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) | ||
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 object ByteArrayAsBase64JsonArraySerializer: KSerializer<ByteArray> { | ||
private val delegateSerializer = ListSerializer(String.serializer()) | ||
override val descriptor: SerialDescriptor = delegateSerializer.descriptor | ||
|
||
override fun serialize(encoder: Encoder, value: ByteArray) = | ||
encoder.encodeSerializableValue(delegateSerializer, listOf( | ||
Base64.encodeToString(value), "base64" | ||
)) | ||
|
||
override fun deserialize(decoder: Decoder): ByteArray { | ||
decoder.decodeSerializableValue(delegateSerializer).apply { | ||
if (contains("base64")) first { it != "base64" }.apply { | ||
return Base64.decode(this) | ||
} | ||
else throw(SerializationException("Not Base64")) | ||
} | ||
} | ||
} | ||
|
||
internal class BorshAsBase64JsonArraySerializer<T>(private val dataSerializer: KSerializer<T>): KSerializer<T?> { | ||
private val delegateSerializer = ByteArrayAsBase64JsonArraySerializer | ||
override val descriptor: SerialDescriptor = dataSerializer.descriptor | ||
|
||
override fun serialize(encoder: Encoder, value: T?) = | ||
encoder.encodeSerializableValue(delegateSerializer, | ||
value?.let { | ||
BorshEncoder().apply { | ||
encodeSerializableValue(dataSerializer, value) | ||
}.borshEncodedBytes | ||
} ?: byteArrayOf() | ||
) | ||
|
||
override fun deserialize(decoder: Decoder): T? = | ||
decoder.decodeSerializableValue(delegateSerializer).run { | ||
if (this.isEmpty()) return null | ||
BorshDecoder(this).decodeSerializableValue(dataSerializer) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
solanaclient/src/commonTest/kotlin/com/solana/rpc/RpcClientTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters