Skip to content

Commit

Permalink
Add commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
jszmltr committed Oct 2, 2023
1 parent bf4cf2e commit 2db33c9
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.avianlabs.solana

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import net.avianlabs.solana.client.RpcInvocation
Expand All @@ -9,10 +10,12 @@ public class SolanaClient(
private val client: RpcKtorClient,
) {

@OptIn(ExperimentalSerializationApi::class)
internal val json: Json = Json {
ignoreUnknownKeys = true
isLenient = true
allowSpecialFloatingPointValues = true
explicitNulls = false
}

internal suspend inline fun <reified T> invoke(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.avianlabs.solana.domain.core


/**
* [Commitment.Finalized] - the node will query the most recent block confirmed by supermajority of the cluster as having reached maximum lockout, meaning the cluster has recognized this block as finalized
* [Commitment.Confirmed] - the node will query the most recent block that has been voted on by supermajority of the cluster.
* It incorporates votes from gossip and replay.
* It does not count votes on descendants of a block, only direct votes on that block.
* This confirmation level also upholds "optimistic confirmation" guarantees in release 1.3 and onwards.
* [Commitment.Processed] - the node will query its most recent block. Note that the block may still be skipped by the cluster.
*
* @see <a href="https://docs.solana.com/api/http#configuring-state-commitment">Configuring state commitment</a>
*/
public enum class Commitment(public val value: String) {
Finalized("finalized"),
Confirmed("confirmed"),
Processed("processed"),
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package net.avianlabs.solana.methods

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.client.RpcResponse.RPC
import net.avianlabs.solana.domain.core.Commitment
import net.avianlabs.solana.domain.core.PublicKey

public suspend fun SolanaClient.getBalance(
account: PublicKey,
commitment: Commitment? = null,
): Long {
val result = invoke<RPC<Long>>("getBalance", params(account))
val result = invoke<RPC<Long>>("getBalance", params(account, commitment))
return result!!.value!!
}

private fun SolanaClient.params(
account: PublicKey,
) = JsonArray(listOf(json.encodeToJsonElement(account)))
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(account))
commitment?.let { add(json.encodeToJsonElement(BalanceParams(it.value))) }
})

@Serializable
internal data class BalanceParams(
val commitment: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package net.avianlabs.solana.methods

import io.ktor.util.*
import io.ktor.util.encodeBase64
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.client.RpcResponse.RPC
import net.avianlabs.solana.domain.core.Commitment

public suspend fun SolanaClient.getFeeForMessage(message: ByteArray): Long {
val result = invoke<RPC<Long>>("getFeeForMessage", params(message))
public suspend fun SolanaClient.getFeeForMessage(
message: ByteArray,
commitment: Commitment? = null
): Long {
val result = invoke<RPC<Long>>("getFeeForMessage", params(message, commitment))
return result!!.value!!
}

private fun SolanaClient.params(
message: ByteArray,
) = JsonArray(listOf(json.encodeToJsonElement(message.encodeBase64())))
commitment: Commitment?,
) = JsonArray(buildList {
add(json.encodeToJsonElement(message.encodeBase64()))
commitment?.let { add(json.encodeToJsonElement(FeeForMessageParams(it.value))) }
})

@Serializable
internal data class FeeForMessageParams(
val commitment: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.domain.core.Commitment

public suspend fun SolanaClient.getMinimumBalanceForRentExemption(
dataLength: Long,
commitment: String = "confirmed",
commitment: Commitment? = null,
): Long {
val result = invoke<Long>("getMinimumBalanceForRentExemption", params(dataLength, commitment))
return result!!
}

private fun SolanaClient.params(
dataLength: Long,
commitment: String,
) = JsonArray(
listOf(
json.encodeToJsonElement(dataLength),
json.encodeToJsonElement(GetMinimumBalanceForRentExemptionParams(commitment))
)
)
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(dataLength))
commitment?.let { add(json.encodeToJsonElement(GetMinimumBalanceForRentExemptionParams(it.value))) }
})

@Serializable
internal data class GetMinimumBalanceForRentExemptionParams(
val commitment: String,
val commitment: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.client.RpcResponse
import net.avianlabs.solana.domain.core.Commitment

public suspend fun SolanaClient.getRecentBlockhash(
commitment: String? = null,
commitment: Commitment? = null,
): RecentBlockHash {
val rpc = invoke<RpcResponse.RPC<RecentBlockHash>>("getRecentBlockhash", params(commitment))
return rpc!!.value!!
}

private fun SolanaClient.params(
commitment: String? = null,
) = JsonArray(listOf(json.encodeToJsonElement(commitment)))
commitment: Commitment?,
) = JsonArray(buildList {
commitment?.let { add(json.encodeToJsonElement(it.value)) }
})

@Serializable
public data class RecentBlockHash(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.domain.core.Commitment
import net.avianlabs.solana.domain.core.PublicKey

public suspend fun SolanaClient.getSignaturesForAddress(
account: PublicKey,
commitment: String?,
commitment: Commitment? = null,
): List<SignatureInformation> {
val rpcParams = SignaturesForAddress(
commitment = commitment,
val result = invoke<List<SignatureInformation>>(
"getSignaturesForAddress",
params(account, commitment)
)
val result =
invoke<List<SignatureInformation>>("getSignaturesForAddress", params(account, rpcParams))
return result!!
}

private fun SolanaClient.params(
account: PublicKey,
rpcParams: SignaturesForAddress,
) = JsonArray(listOf(json.encodeToJsonElement(account), json.encodeToJsonElement(rpcParams)))
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(account))
commitment?.let { add(json.encodeToJsonElement(SignaturesForAddressParams(commitment = it.value))) }
})

@Serializable
internal data class SignaturesForAddress(
internal data class SignaturesForAddressParams(
val limit: Long? = null,
val before: String? = null,
val until: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.client.RpcResponse.RPC
import net.avianlabs.solana.domain.core.Commitment
import net.avianlabs.solana.domain.core.PublicKey

public suspend fun SolanaClient.getTokenAccountBalance(
tokenAccount: PublicKey,
commitment: Commitment? = null,
): TokenAmountInfo {
val result = invoke<RPC<TokenAmountInfo>>("getTokenAccountBalance", params(tokenAccount))
val result = invoke<RPC<TokenAmountInfo>>("getTokenAccountBalance", params(tokenAccount, commitment))
return result!!.value!!
}

private fun SolanaClient.params(
account: PublicKey,
) = JsonArray(listOf(json.encodeToJsonElement(account)))
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(account))
commitment?.let { json.encodeToJsonElement(TokenAccountBalanceParams(it.value)) }
})

@Serializable
internal data class TokenAccountBalanceParams(
val commitment: String? = null
)

@Serializable
public data class TokenAmountInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.domain.core.Commitment

public suspend fun SolanaClient.getTransaction(
signature: String,
commitment: String? = null,
commitment: Commitment? = null,
): TransactionResponse? {
val rpcParams = GetTransactionParams(
commitment = commitment,
)
return invoke<TransactionResponse?>("getTransaction", params(signature, rpcParams))
return invoke<TransactionResponse?>("getTransaction", params(signature, commitment))
}

private fun SolanaClient.params(
signature: String,
rpcParams: GetTransactionParams,
) = JsonArray(listOf(json.encodeToJsonElement(signature), json.encodeToJsonElement(rpcParams)))
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(signature))
commitment?.let { add(json.encodeToJsonElement(GetTransactionParams(it.value))) }
})

@Serializable
internal data class GetTransactionParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.client.RpcResponse.RPC
import net.avianlabs.solana.domain.core.Commitment

public suspend fun SolanaClient.isBlockHashValid(
blockHash: String,
commitment: String? = null,
commitment: Commitment? = null,
minContextSlot: Long? = null,
): Boolean {
val result =
Expand All @@ -18,14 +19,14 @@ public suspend fun SolanaClient.isBlockHashValid(

private fun SolanaClient.params(
blockHash: String,
commitment: String? = null,
minContextSlot: Long? = null,
) = JsonArray(
listOf(
json.encodeToJsonElement(blockHash),
json.encodeToJsonElement(IsBlockHashValidParams(commitment, minContextSlot))
)
)
commitment: Commitment?,
minContextSlot: Long?,
) = JsonArray(buildList {
add(json.encodeToJsonElement(blockHash))
if (commitment != null || minContextSlot != null) {
add(json.encodeToJsonElement(IsBlockHashValidParams(commitment?.value, minContextSlot)))
}
})

@Serializable
internal data class IsBlockHashValidParams(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package net.avianlabs.solana.methods

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.encodeToJsonElement
import net.avianlabs.solana.SolanaClient
import net.avianlabs.solana.domain.core.Commitment
import net.avianlabs.solana.domain.core.PublicKey

public suspend fun SolanaClient.requestAirdrop(
publicKey: PublicKey,
lamports: Long,
commitment: Commitment? = null
): String {
val result = invoke<String>("requestAirdrop", params(publicKey, lamports))
val result = invoke<String>("requestAirdrop", params(publicKey, lamports, commitment))
return result!!
}

private fun SolanaClient.params(
publicKey: PublicKey,
lamports: Long,
) = JsonArray(listOf(json.encodeToJsonElement(publicKey), json.encodeToJsonElement(lamports)))
commitment: Commitment?
) = JsonArray(buildList {
add(json.encodeToJsonElement(publicKey))
add(json.encodeToJsonElement(lamports))
commitment?.let { json.encodeToJsonElement(RequestAirdropParams(it.value)) }
})

@Serializable
internal data class RequestAirdropParams(
val commitment: String? = null,
)

0 comments on commit 2db33c9

Please sign in to comment.