@@ -25,8 +25,6 @@ import com.getcode.opencode.model.financial.Token
2525import com.getcode.opencode.model.financial.usdf
2626import com.getcode.opencode.model.transactions.SwapFundingSource
2727import com.getcode.solana.keys.base58
28- import com.getcode.utils.network.pollUntil
29- import com.getcode.vendor.Base58
3028import com.flipcash.app.core.AppRoute
3129import com.flipcash.app.onramp.internal.CoinbaseOnRampWebError
3230import com.getcode.utils.CodeServerError
@@ -46,9 +44,7 @@ import kotlinx.serialization.Serializable
4644import kotlinx.serialization.json.Json
4745import kotlinx.serialization.json.JsonIgnoreUnknownKeys
4846import retrofit2.HttpException
49- import java.security.SecureRandom
5047import javax.inject.Inject
51- import kotlin.time.Duration.Companion.seconds
5248
5349typealias OrderWithPaymentLink = Pair <String , OnRampPurchaseResponse .PaymentLink >
5450
@@ -79,15 +75,15 @@ class CoinbaseOnRampController @Inject constructor(
7975 _pendingNavigation .tryEmit(route)
8076 }
8177
82- fun startPayment (order : OnrampOrder , token : Token , amount : VerifiedFiat ) {
83- _state .value = CoinbaseOnRampState .Paying (order, token, amount)
78+ fun startPayment (order : OnrampOrder , token : Token , amount : VerifiedFiat , swapId : SwapId ) {
79+ _state .value = CoinbaseOnRampState .Paying (order, token, amount, swapId )
8480 }
8581
8682 fun onPaymentSuccess (orderId : String ) {
8783 val current = _state .value
8884 if (current is CoinbaseOnRampState .Paying ) {
8985 _state .update {
90- CoinbaseOnRampState .Processing (orderId , current.token, current.amount)
86+ CoinbaseOnRampState .Completed (current.swapId , current.token, current.amount)
9187 }
9288 }
9389 }
@@ -118,56 +114,20 @@ class CoinbaseOnRampController @Inject constructor(
118114 }
119115
120116 return placeOrderInclusiveOfFees(amount)
121- .map { (orderId, paymentLink) ->
122- val order = OnrampOrder (orderId, paymentLink.url)
123- startPayment(order, token, verifiedFiat)
124- }
125- }
126-
127- suspend fun processPayment (): Result <SwapId > {
128- val current = _state .value
129- if (current !is CoinbaseOnRampState .Processing ) {
130- return Result .failure(IllegalStateException (" Not in Processing state" ))
131- }
132-
133- return pollUntil(
134- call = { lookupOrder(current.orderId).getOrThrow() },
135- required = { order -> order.txHash != null },
136- maxAttempts = 100 ,
137- interval = 3 .seconds,
138- tag = " CoinbaseOrderPoller" ,
139- ).mapCatching { order ->
140- order.txHash ? : throw IllegalStateException (" No hash provided from provider" )
141- }.mapCatching { txHash ->
117+ .mapCatching { (orderId, paymentLink) ->
142118 val owner = userManager.accountCluster
143119 ? : throw IllegalStateException (" No account cluster" )
144120
145- transactionController.buy(
121+ val swapId = transactionController.buy(
146122 owner = owner,
147- amount = current.amount,
148- of = current.token,
149- source = SwapFundingSource .ExternalWallet (
150- transactionSignature = runCatching { Base58 .decode(txHash) }
151- .getOrElse { ByteArray (64 ).also { SecureRandom ().nextBytes(it) } }
152- .toList()
153- ),
123+ amount = verifiedFiat,
124+ of = token,
125+ source = SwapFundingSource .CoinbaseOnramp (orderId = orderId),
154126 fund = { Result .success(Unit ) }
155127 ).getOrThrow()
156- }
157- .onSuccess { swapId ->
158- _state .update { CoinbaseOnRampState .Completed (swapId, current.token, current.amount) }
159- }
160- .onFailure { error ->
161- trace(
162- message = " Payment processing failed" ,
163- tag = " OnRamp" ,
164- metadata = {
165- " orderId" to current.orderId
166- " errorType" to error::class .simpleName.orEmpty()
167- },
168- error = error,
169- )
170- reset()
128+
129+ val order = OnrampOrder (orderId, paymentLink.url)
130+ startPayment(order, token, verifiedFiat, swapId)
171131 }
172132 }
173133
0 commit comments