Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SPL Token 2022 program #1544

Merged
merged 1 commit into from
Jan 7, 2025
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
76 changes: 48 additions & 28 deletions data/src/main/kotlin/com/vultisig/wallet/data/api/SolanaApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -232,39 +232,57 @@ internal class SolanaApiImp @Inject constructor(
)
}.body()

override suspend fun getSPLTokens(walletAddress: String): List<SplResponseAccountJson>? {
try {
val payload = RpcPayload(
jsonrpc = "2.0",
method = "getTokenAccountsByOwner",
params = buildJsonArray {
add(walletAddress)
addJsonObject {
put("programId", PROGRAM_ID_SPL_REQUEST_PARAM)
override suspend fun getSPLTokens(walletAddress: String): List<SplResponseAccountJson>? =
coroutineScope {
try {
val payload = getSplRpcPayload(walletAddress, PROGRAM_ID_SPL_REQUEST_PARAM)
val payloadToken2022 = getSplRpcPayload(walletAddress, TOKEN_PROGRAM_ID_2022)

val response = async {
httpClient.post(rpcEndpoint) {
setBody(payload)
}
addJsonObject {
put("encoding", ENCODING_SPL_REQUEST_PARAM)
}

val responseToken2022 = async {
httpClient.post(rpcEndpoint) {
setBody(payloadToken2022)
}
},
id = 1,
)
val response = httpClient.post(rpcEndpoint) {
setBody(payload)
}
val responseContent = response.bodyAsText()
Timber.d(responseContent)
val rpcResp = response.body<SplResponseJson>()
}

if (rpcResp.error != null) {
Timber.d("get spl token addresses error: ${rpcResp.error}")
return null
listOf(response, responseToken2022)
.awaitAll()
.mapNotNull { it.body<SplResponseJson>().result?.accounts }
.flatten()

} catch (e: Exception) {
Timber.e(e)
null
}
return rpcResp.result?.accounts
} catch (e: Exception) {
Timber.e(e)
return null
}
}


private fun getSplRpcPayload(address: String, programId: String) = RpcPayload(
jsonrpc = "2.0",
method = "getTokenAccountsByOwner",
params = buildJsonArray {
add(address)
addJsonObject {
put(
"programId",
programId
)
}
addJsonObject {
put(
"encoding",
ENCODING_SPL_REQUEST_PARAM
)
}
},
id = 1,
)


override suspend fun getSPLTokenBalance(walletAddress: String, coinAddress: String): String? {
try {
Expand Down Expand Up @@ -335,6 +353,8 @@ internal class SolanaApiImp @Inject constructor(
companion object {
private const val PROGRAM_ID_SPL_REQUEST_PARAM =
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
private const val TOKEN_PROGRAM_ID_2022 =
"TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
private const val ENCODING_SPL_REQUEST_PARAM = "jsonParsed"
private const val DATA_LENGTH_MINIMUM_BALANCE_FOR_RENT_EXEMPTION = 165
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal class AccountsRepositoryImpl @Inject constructor(
solanaCoins: List<Coin>,
vault: Vault
): List<Coin> {
if (solanaCoins.isNotEmpty()) return emptyList()
if (solanaCoins.any { !it.isNativeToken }) return emptyList()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val solanaAddress = solanaCoins.firstOrNull()?.address
val newSPLTokens = mutableListOf<Coin>()
solanaAddress?.let {
Expand Down
Loading