Skip to content

Commit

Permalink
AND-5521 fixed solana crash when unexpected api response
Browse files Browse the repository at this point in the history
  • Loading branch information
kozarezvlad committed Dec 9, 2023
1 parent 86cd036 commit 5653b95
Showing 1 changed file with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,21 @@ import kotlinx.coroutines.withContext
import org.p2p.solanaj.core.PublicKey
import org.p2p.solanaj.programs.Program
import org.p2p.solanaj.rpc.Cluster
import org.p2p.solanaj.rpc.RpcException
import org.p2p.solanaj.rpc.types.AccountInfo
import org.p2p.solanaj.rpc.types.FeesInfo
import org.p2p.solanaj.rpc.types.SignatureStatuses
import org.p2p.solanaj.rpc.types.TokenAccountInfo
import org.p2p.solanaj.rpc.types.TokenResultObjects
import org.p2p.solanaj.rpc.types.TransactionResult
import org.p2p.solanaj.rpc.types.*
import org.p2p.solanaj.rpc.types.config.Commitment
import java.math.BigDecimal

/**
* Created by Anton Zhilenkov on 26/01/2022.
*/
class SolanaNetworkService(
private val provider: RpcClient
): NetworkProvider {
private val provider: RpcClient,
) : NetworkProvider {

override val baseUrl: String = provider.host

suspend fun getMainAccountInfo(
account: PublicKey
account: PublicKey,
): Result<SolanaMainAccountInfo> = withContext(Dispatchers.IO) {
val accountInfo = accountInfo(account).successOr { return@withContext it }
val tokenAccounts = accountTokensInfo(account).successOr { return@withContext it }
Expand All @@ -47,15 +41,17 @@ class SolanaNetworkService(
}.associateBy { it.mint }

val txsInProgress = getTransactionsInProgressInfo(account).successOr { listOf() }
Result.Success(SolanaMainAccountInfo(
value = accountInfo.value,
tokensByMint = tokensByMint,
txsInProgress = txsInProgress
))
Result.Success(
SolanaMainAccountInfo(
value = accountInfo.value,
tokensByMint = tokensByMint,
txsInProgress = txsInProgress
)
)
}

private suspend fun getTransactionsInProgressInfo(
account: PublicKey
account: PublicKey,
): Result<List<TransactionInfo>> = withContext(Dispatchers.IO) {
try {
val allSignatures = provider.api.getSignaturesForAddress(account.toBase58(), Commitment.CONFIRMED, 20)
Expand All @@ -72,38 +68,38 @@ class SolanaNetworkService(
}
}
Result.Success(txInProgress)
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}

suspend fun getSignatureStatuses(
signatures: List<String>
signatures: List<String>,
): Result<SignatureStatuses> = withContext(Dispatchers.IO) {
try {
Result.Success(provider.api.getSignatureStatuses(signatures, true))
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}

suspend fun accountInfo(account: PublicKey): Result<AccountInfo> = withContext(Dispatchers.IO) {
try {
Result.Success(provider.api.getAccountInfo(account, Commitment.FINALIZED.toMap()))
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}

suspend fun accountTokensInfo(
account: PublicKey
account: PublicKey,
): Result<List<TokenAccountInfo.Value>> = withContext(Dispatchers.IO) {
try {
val params = mutableMapOf<String, Any>("programId" to Program.Id.token)
params.addCommitment(Commitment.RECENT)
val tokensAccountsInfo = provider.api.getTokenAccountsByOwner(account, params, mutableMapOf())
Result.Success(tokensAccountsInfo.value)
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}
Expand All @@ -118,7 +114,7 @@ class SolanaNetworkService(
try {
val splAccountInfo = provider.api.getSplTokenAccountInfo(associatedTokenAddress)
Result.Success(SolanaSplAccountInfo(splAccountInfo.value, associatedTokenAddress))
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}
Expand All @@ -127,7 +123,7 @@ class SolanaNetworkService(
try {
val params = provider.api.getFees(Commitment.FINALIZED)
Result.Success(params)
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}
Expand All @@ -137,10 +133,11 @@ class SolanaNetworkService(
Result.Success(info.accountExist)
}

suspend fun isSplTokenAccountExist(account: PublicKey, mint: PublicKey): Result<Boolean> = withContext(Dispatchers.IO) {
val info = splAccountInfo(account, mint).successOr { return@withContext it }
Result.Success(info.accountExist)
}
suspend fun isSplTokenAccountExist(account: PublicKey, mint: PublicKey): Result<Boolean> =
withContext(Dispatchers.IO) {
val info = splAccountInfo(account, mint).successOr { return@withContext it }
Result.Success(info.accountExist)
}

fun mainAccountCreationFee(): BigDecimal = accountRentFeeByEpoch(1)

Expand All @@ -163,7 +160,7 @@ class SolanaNetworkService(
try {
val rent = provider.api.getMinimumBalanceForRentExemption(dataLength)
Result.Success(rent.toBigDecimal())
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}
Expand All @@ -172,15 +169,15 @@ class SolanaNetworkService(
try {
val result = provider.api.sendSignedTransaction(signedTransaction)
Result.Success(result)
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}

suspend fun getRecentBlockhash(commitment: Commitment? = null): Result<String> = withContext(Dispatchers.IO) {
try {
Result.Success(provider.api.getRecentBlockhash(commitment))
} catch (ex: RpcException) {
} catch (ex: Exception) {
Result.Failure(Solana.Api(ex))
}
}
Expand Down Expand Up @@ -208,7 +205,7 @@ private val AccountInfo.requireValue
data class SolanaMainAccountInfo(
val value: AccountInfo.Value?,
val tokensByMint: Map<String, SolanaTokenAccountInfo>,
val txsInProgress: List<TransactionInfo>
val txsInProgress: List<TransactionInfo>,
) {
val balance: Long
get() = value?.lamports ?: 0L
Expand All @@ -231,7 +228,6 @@ data class SolanaSplAccountInfo(
get() = value!!
}


data class SolanaTokenAccountInfo(
val value: TokenAccountInfo.Value,
val address: String,
Expand All @@ -242,7 +238,7 @@ data class SolanaTokenAccountInfo(
data class TransactionInfo(
val signature: String,
val fee: Long, // in lamports
val instructions: List<TransactionResult.Instruction>
val instructions: List<TransactionResult.Instruction>,
)

private fun MutableMap<String, Any>.addCommitment(commitment: Commitment): MutableMap<String, Any> {
Expand Down

0 comments on commit 5653b95

Please sign in to comment.