Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions libs/crypto/solana/src/main/kotlin/com/getcode/solana/rpc/Calls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class SolanaConnection(rpcUrl: String,) {
* Returns the SOL balance (in lamports) for the given public key.
*/
suspend fun Rpc20Driver.getBalance(publicKey: PublicKey): Result<Long> {
val response = makeRequest(
request = GetBalance(publicKey),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = GetBalance(publicKey),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
if (error != null) {
return Result.failure(RpcException(error.code, error.message))
Expand All @@ -42,10 +44,12 @@ suspend fun Rpc20Driver.getBalance(publicKey: PublicKey): Result<Long> {
* Returns 0 if the account does not exist.
*/
suspend fun Rpc20Driver.getTokenAccountBalance(tokenAccount: PublicKey): Result<Long> {
val response = makeRequest(
request = GetTokenAccountBalance(tokenAccount),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = GetTokenAccountBalance(tokenAccount),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
if (error != null) {
// Account not found — treat as zero balance
Expand Down Expand Up @@ -77,10 +81,12 @@ suspend fun Rpc20Driver.getTokenAccountBalance(tokenAccount: PublicKey): Result<
* (e.g., network issue, account not found, or RPC error).
*/
suspend fun Rpc20Driver.doesAccountExist(publicKey: PublicKey): Result<Unit> {
val response = makeRequest(
request = GetAccountInfo(publicKey),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = GetAccountInfo(publicKey),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
if (error != null) {
return Result.failure(RpcException(error.code, error.message))
Expand All @@ -98,10 +104,12 @@ suspend fun Rpc20Driver.doesAccountExist(publicKey: PublicKey): Result<Unit> {
* Returns the raw account data for the given public key, base64-decoded.
*/
suspend fun Rpc20Driver.getAccountData(publicKey: PublicKey): Result<ByteArray> {
val response = makeRequest(
request = GetAccountInfo(publicKey),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = GetAccountInfo(publicKey),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
if (error != null) {
return Result.failure(RpcException(error.code, error.message))
Expand All @@ -127,10 +135,12 @@ suspend fun Rpc20Driver.getAccountData(publicKey: PublicKey): Result<ByteArray>
*/
suspend fun Rpc20Driver.sendTransaction(encodedTransaction: String): Result<String> {
println("sending transaction on the blockchain => $encodedTransaction")
val response = makeRequest(
request = SendTransaction(encodedTransaction),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = SendTransaction(encodedTransaction),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
if (error != null) {
return Result.failure(RpcException(error.code, error.message))
Expand Down Expand Up @@ -163,10 +173,12 @@ suspend fun Rpc20Driver.simulateTransaction(
commitment: String = "confirmed",
): Result<String> {
println("simulating transaction on the blockchain => $encodedTransaction")
val response = makeRequest(
request = SimulateTransaction(encodedTransaction, commitment),
resultSerializer = JsonElement.serializer()
)
val response = runCatching {
makeRequest(
request = SimulateTransaction(encodedTransaction, commitment),
resultSerializer = JsonElement.serializer()
)
}.getOrElse { return Result.failure(it) }
val error = response.error
val errorDetails = response.result?.jsonObject?.get("err")?.toString()
if (error != null || errorDetails != null) {
Expand Down
14 changes: 12 additions & 2 deletions libs/logging/src/main/kotlin/com/getcode/utils/ErrorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.TimeoutCancellationException
import timber.log.Timber
import java.net.ConnectException
import java.net.SocketException
import java.net.UnknownHostException
import java.util.concurrent.TimeoutException
import javax.net.ssl.SSLException

object ErrorUtils {
private var isDisplayErrors = false
Expand All @@ -30,6 +32,8 @@ object ErrorUtils {
TimeoutCancellationException::class,
CancellationException::class,
ConnectException::class,
SSLException::class,
SocketException::class,
)

fun handleError(throwable: Throwable) {
Expand Down Expand Up @@ -76,7 +80,11 @@ object ErrorUtils {
throwable is TimeoutException ||
throwable.cause is TimeoutException ||
throwable is UnknownHostException ||
throwable.cause is UnknownHostException
throwable.cause is UnknownHostException ||
throwable is SSLException ||
throwable.cause is SSLException ||
throwable is SocketException ||
throwable.cause is SocketException

private val gmsTransientMessages = setOf("SERVICE_NOT_AVAILABLE", "FIS_AUTH_ERROR")

Expand Down Expand Up @@ -122,7 +130,9 @@ object ErrorUtils {
fun Throwable.isNetworkError(): Boolean =
this is UnknownHostException || cause is UnknownHostException ||
this is ConnectException || cause is ConnectException ||
this is TimeoutException || cause is TimeoutException
this is TimeoutException || cause is TimeoutException ||
this is SSLException || cause is SSLException ||
this is SocketException || cause is SocketException

data class SuppressibleException(override val message: String, override val cause: Throwable? = null) : Throwable(message, cause) {
constructor(cause: Throwable) : this(cause.message.orEmpty(), cause)
Expand Down
Loading