Skip to content

Commit 33dd963

Browse files
committed
chore(solana): remove sol4k, use Rpc20Driver for latest blockhash
sol4k was used in only two places: RpcUrl.MAINNNET.value for the mainnet endpoint and a single Connection.getLatestBlockhash() call. Replace both with the existing Rpc20Driver — a new getLatestBlockhash() extension mirroring the other RPC calls, plus a named MAINNET_RPC_URL constant — and drop the org.sol4k:sol4k dependency. This also removes the library's only kotlin-stdlib 2.4.0 requirement, unblocking it from the pending Kotlin 2.4 upgrade. Behavior is unchanged: same mainnet endpoint and finalized blockhash commitment. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 6cf2895 commit 33dd963

5 files changed

Lines changed: 38 additions & 11 deletions

File tree

apps/flipcash/shared/onramp/deeplinks/src/main/kotlin/com/flipcash/app/onramp/PhantomWalletController.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import com.getcode.solana.keys.Signature
2222
import com.getcode.solana.keys.base58
2323
import com.getcode.solana.rpc.RpcConfig
2424
import com.getcode.solana.rpc.RpcException
25-
import com.getcode.solana.rpc.SolanaConnection
2625
import com.getcode.solana.rpc.getBalance
2726
import com.getcode.solana.rpc.getAccountData
27+
import com.getcode.solana.rpc.getLatestBlockhash
2828
import com.getcode.solana.rpc.getTokenAccountBalance
2929
import com.getcode.solana.rpc.sendTransaction
3030
import com.getcode.solana.rpc.simulateTransaction
@@ -57,7 +57,6 @@ class PhantomWalletController @Inject constructor(
5757
private val phantomSdk: PhantomSdk,
5858
private val phantomConnector: PhantomWalletConnector,
5959
) {
60-
private val connection by lazy { SolanaConnection(rpcConfig.rpcUrl) }
6160
private val driver by lazy { Rpc20Driver(rpcConfig.rpcUrl, rpcConfig.networkDriver) }
6261

6362
suspend fun connectWallet(
@@ -226,7 +225,7 @@ class PhantomWalletController @Inject constructor(
226225
val owner = requireNotNull(userManager.accountCluster) { "Owner is null" }
227226

228227
val swapId = SwapId.generate()
229-
val recentBlockhash = connection.getLatestBlockhash()
228+
val recentBlockhash = driver.getLatestBlockhash().getOrThrow()
230229

231230
val liquidityPool = userFlags.resolvedFlags.value.usdcOnRampLiquidityPool.effectiveValue
232231
val swapPool = when (liquidityPool) {
@@ -279,7 +278,7 @@ class PhantomWalletController @Inject constructor(
279278
): Result<SolanaTransaction> {
280279
return withContext(Dispatchers.IO) {
281280
try {
282-
val recentBlockhash = connection.getLatestBlockhash()
281+
val recentBlockhash = driver.getLatestBlockhash().getOrThrow()
283282

284283
val poolAddress = FundSwapPool.CoinbaseStableSwapper.poolAddress
285284
val poolData = driver.getAccountData(poolAddress).getOrThrow()

libs/crypto/solana/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ dependencies {
2323
implementation(libs.okhttp)
2424
implementation(libs.okhttp.logging.interceptor)
2525

26-
implementation("org.sol4k:sol4k:0.5.17")
2726
api("com.solanamobile:web3-solana:0.2.5")
2827
api("com.solanamobile:rpc-core:0.2.9")
2928
implementation("com.solanamobile:rpc-okiodriver:0.2.9")

libs/crypto/solana/src/main/kotlin/com/getcode/solana/inject/SolanaModule.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import dagger.Provides
88
import dagger.hilt.InstallIn
99
import dagger.hilt.components.SingletonComponent
1010
import okhttp3.OkHttpClient
11-
import org.sol4k.RpcUrl
1211
import javax.inject.Named
1312
import javax.inject.Singleton
1413

1514
@Module
1615
@InstallIn(SingletonComponent::class)
1716
object SolanaModule {
1817

18+
private const val MAINNET_RPC_URL = "https://api.mainnet-beta.solana.com"
19+
1920
@Singleton
2021
@Provides
2122
fun providesOkRpcDriver(
@@ -24,7 +25,7 @@ object SolanaModule {
2425

2526
@Provides
2627
@Named("solana-rpc-url")
27-
fun providesSolanaRpcUrl(): String = RpcUrl.MAINNNET.value
28+
fun providesSolanaRpcUrl(): String = MAINNET_RPC_URL
2829

2930
@Provides
3031
@Singleton

libs/crypto/solana/src/main/kotlin/com/getcode/solana/rpc/Calls.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@ import kotlinx.serialization.json.jsonArray
88
import kotlinx.serialization.json.jsonObject
99
import kotlinx.serialization.json.jsonPrimitive
1010
import kotlinx.serialization.json.long
11-
import org.sol4k.Connection
1211
import android.util.Base64
1312

14-
class SolanaConnection(rpcUrl: String,) {
15-
private val connection = Connection(rpcUrl)
16-
fun getLatestBlockhash(): String = connection.getLatestBlockhash()
13+
/**
14+
* Returns the most recent blockhash as a base58 string.
15+
*/
16+
suspend fun Rpc20Driver.getLatestBlockhash(): Result<String> {
17+
val response = runCatching {
18+
makeRequest(
19+
request = GetLatestBlockhash(),
20+
resultSerializer = JsonElement.serializer()
21+
)
22+
}.getOrElse { return Result.failure(it) }
23+
response.error?.let { return Result.failure(RpcException(it.code, it.message)) }
24+
25+
val blockhash = response.result
26+
?.jsonObject?.get("value")
27+
?.jsonObject?.get("blockhash")
28+
?.jsonPrimitive?.content
29+
?: return Result.failure(Throwable("Missing blockhash"))
30+
31+
return Result.success(blockhash)
1732
}
1833

1934
/**

libs/crypto/solana/src/main/kotlin/com/getcode/solana/rpc/Requests.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ internal class GetTokenAccountBalance(
6464
id = requestId
6565
)
6666

67+
internal class GetLatestBlockhash(
68+
commitment: String = "finalized",
69+
requestId: String = "1",
70+
): JsonRpc20Request(
71+
method = "getLatestBlockhash",
72+
params = buildJsonArray {
73+
addJsonObject {
74+
put("commitment", commitment)
75+
}
76+
},
77+
id = requestId
78+
)
79+
6780
internal class SimulateTransaction(
6881
encodedTransaction: String,
6982
commitment: String = "finalized",

0 commit comments

Comments
 (0)