Skip to content

Commit

Permalink
Solana regroup services and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
furenster committed Feb 17, 2025
1 parent 828d57e commit 125421a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SolanaSignerPreloader(
val senderTokenAddressJob = async { accountsService.getTokenAccountByOwner(params.from.address, tokenId) }
val recipientTokenAddressJob = async { accountsService.getTokenAccountByOwner(params.destination.address, tokenId) }
val tokenProgramJob = async {
val owner = networkInfoService.getTokenInfo(tokenId)
val owner = accountsService.getTokenInfo(tokenId)

if (owner != null) {
SolanaTokenProgramId.entries.firstOrNull {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.gemwallet.android.blockchain.clients.solana

import com.gemwallet.android.blockchain.clients.GetTokenClient
import com.gemwallet.android.blockchain.clients.solana.services.SolanaRpcClient
import com.gemwallet.android.blockchain.rpc.model.JSONRpcRequest
import com.gemwallet.android.blockchain.clients.solana.services.SolanaAccountsService
import com.gemwallet.android.blockchain.clients.solana.services.createAccountInfoRequest
import com.wallet.core.primitives.Asset
import com.wallet.core.primitives.AssetId
import com.wallet.core.primitives.AssetType
Expand All @@ -11,35 +11,20 @@ import wallet.core.jni.Base58

class SolanaTokenClient(
private val chain: Chain,
private val rpcClient: SolanaRpcClient,
private val accountsService: SolanaAccountsService,
) : GetTokenClient {

override suspend fun getTokenData(tokenId: String): Asset? {
val metadataKey = uniffi.gemstone.solanaDeriveMetadataPda(tokenId)
val tokenInfo = rpcClient.getAccountInfoSpl(
JSONRpcRequest(
SolanaMethod.GetAccountInfo.value,
params = listOf(
tokenId,
mapOf(
"encoding" to "jsonParsed"
),
)
)
).getOrNull()?.result?.value?.data?.parsed?.info ?: return null

val base64 = rpcClient.getAccountInfoMpl(
JSONRpcRequest(
SolanaMethod.GetAccountInfo.value,
params = listOf(
metadataKey,
mapOf(
"encoding" to "jsonParsed"
),
)
)
).getOrNull()?.result?.value?.data?.first() ?: return null

val tokenInfo = accountsService.getAccountInfoSpl(accountsService.createAccountInfoRequest(tokenId))
.getOrNull()?.result?.value?.data?.parsed?.info ?: return null

val base64 = accountsService.getAccountInfoMpl(accountsService.createAccountInfoRequest(metadataKey))
.getOrNull()?.result?.value?.data?.first() ?: return null

val metadata = uniffi.gemstone.solanaDecodeMetadata(base64)

return Asset(
id = AssetId(chain = chain, tokenId = tokenId),
name = metadata.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.gemwallet.android.blockchain.clients.solana.models.SolanaArrayData
import com.gemwallet.android.blockchain.clients.solana.models.SolanaInfo
import com.gemwallet.android.blockchain.clients.solana.models.SolanaParsedData
import com.gemwallet.android.blockchain.clients.solana.models.SolanaParsedSplTokenInfo
import com.gemwallet.android.blockchain.clients.solana.models.SolanaTokenOwner
import com.gemwallet.android.blockchain.rpc.model.JSONRpcRequest
import com.gemwallet.android.blockchain.rpc.model.JSONRpcResponse
import com.wallet.core.blockchain.solana.models.SolanaBalanceValue
Expand All @@ -28,6 +29,9 @@ interface SolanaAccountsService {

@POST("/")
suspend fun getAccountInfoMpl(@Body request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<SolanaValue<SolanaArrayData<String>>>>

@POST("/")
suspend fun getTokenInfo(@Body request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<SolanaValue<SolanaTokenOwner>>>
}

suspend fun SolanaAccountsService.getTokenAccountByOwner(owner: String, tokenId: String): String? {
Expand All @@ -40,4 +44,20 @@ suspend fun SolanaAccountsService.getTokenAccountByOwner(owner: String, tokenId:
)
)
return getTokenAccountByOwner(accountRequest).getOrNull()?.result?.value?.firstOrNull()?.pubkey
}
}

suspend fun SolanaAccountsService.getTokenInfo(tokenId: String): String? {
return getTokenInfo(createAccountInfoRequest(tokenId)).getOrNull()?.result?.value?.owner
}

fun SolanaAccountsService.createAccountInfoRequest(tokenId: String): JSONRpcRequest<List<Any>> {
return JSONRpcRequest.create(
SolanaMethod.GetAccountInfo,
params = listOf(
tokenId,
mapOf(
"encoding" to "jsonParsed"
),
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ import retrofit2.http.Body
import retrofit2.http.POST

interface SolanaNetworkInfoService {
@POST("/")
suspend fun getTokenInfo(@Body request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<SolanaValue<SolanaTokenOwner>>>

@POST("/")
suspend fun getBlockhash(@Body request: JSONRpcRequest<List<String>>): Result<JSONRpcResponse<SolanaBlockhashResult>>
}

suspend fun SolanaNetworkInfoService.getTokenInfo(tokenId: String): String? {
return getTokenInfo(
JSONRpcRequest(
SolanaMethod.GetAccountInfo.value,
params = listOf(
tokenId,
mapOf(
"encoding" to "jsonParsed"
),
)
)
).getOrNull()?.result?.value?.owner
}

suspend fun SolanaNetworkInfoService.getBlockhash(): String {
val blockhash = getBlockhash(JSONRpcRequest.create(SolanaMethod.GetLatestBlockhash, emptyList()))
.getOrNull()?.result?.value?.blockhash
Expand Down

0 comments on commit 125421a

Please sign in to comment.