Skip to content

Commit

Permalink
add data slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Jun 14, 2024
1 parent 913cfa1 commit 3b76413
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ class SolanaRpcClient(
publicKey: SolanaPublicKey,
commitment: Commitment? = null,
minContextSlot: Long? = null,
dataSlice: AccountInfoRequest.DataSlice? = null,
requestId: String? = null
) = makeRequest(
AccountInfoRequest(publicKey, commitment, minContextSlot, requestId),
AccountInfoRequest(publicKey, commitment, minContextSlot, dataSlice, requestId),
SolanaAccountSerializer(serializer)
)

suspend inline fun <reified D> getAccountInfo(
publicKey: SolanaPublicKey,
commitment: Commitment? = null,
minContextSlot: Long? = null,
dataSlice: AccountInfoRequest.DataSlice? = null,
requestId: String? = null
) = getAccountInfo<D>(serializer(), publicKey, commitment, minContextSlot, requestId)
) = getAccountInfo<D>(serializer(), publicKey, commitment, minContextSlot, dataSlice, requestId)

suspend fun getBalance(
address: SolanaPublicKey,
Expand Down Expand Up @@ -157,6 +159,7 @@ class SolanaRpcClient(
publicKey: SolanaPublicKey,
commitment: Commitment? = null,
minContextSlot: Long? = null,
dataSlice: DataSlice? = null,
requestId: String? = null
) : SolanaRpcRequest(
method = "getAccountInfo",
Expand All @@ -166,11 +169,19 @@ class SolanaRpcClient(
put("encoding", Encoding.base64.getEncoding())
commitment?.let { put("commitment", commitment.value) }
minContextSlot?.let { put("minContextSlot", minContextSlot) }
// TODO: support data slicing
dataSlice?.let {
putJsonObject("dataSlice") {
put("length", dataSlice.length)
put("offset", dataSlice.offset)
}
}
}
},
requestId
)
) {
@Serializable
data class DataSlice(val length: Long, val offset: Long)
}

class AirdropRequest(
address: SolanaPublicKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ class RpcClientTests {
assertEquals(expectedAccountData, response.result!!.data!!.decodeToString())
}

@Test
fun `getAccountInfo with data slicing returns AccountInfo object with sliced data`() = runTest {
// given
val dataSlice = SolanaRpcClient.AccountInfoRequest.DataSlice(8, 2)
val expectedAccountData = "stem_pro"
val rpcClient = SolanaRpcClient(TestConfig.RPC_URL, KtorNetworkDriver())

// when
val response = rpcClient.getAccountInfo(
ByteStringSerializer(expectedAccountData.length),
SolanaPublicKey.from("11111111111111111111111111111111"),
dataSlice = dataSlice
)

// then
assertNull(response.error)
assertNotNull(response.result)
assertEquals(expectedAccountData, response.result!!.data!!.decodeToString())
}

@Test
fun `getAccountInfo deserializes account data struct`() = runTest {
// given
Expand Down

0 comments on commit 3b76413

Please sign in to comment.