Skip to content

Commit 32a2d75

Browse files
committed
fix(ocp): properly provide verifiedState for buys; fix remote sends missing verified state
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 99cfd51 commit 32a2d75

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

services/opencode/src/main/kotlin/com/getcode/opencode/RepositoryFactory.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ object RepositoryFactory {
3939
val transactionApi = TransactionApi(module.provideManagedChannel(context, config))
4040
val service = AccountService(api)
4141
val transactionMetadataMapper = TransactionMetadataMapper()
42-
val verifiedStateManager = ManagerFactory.createVerifiedStateManager()
4342
val swapFunding = SwapFunding(transactionApi)
44-
val transactionService = TransactionService(transactionApi, transactionMetadataMapper, swapFunding, verifiedStateManager)
43+
val transactionService = TransactionService(transactionApi, transactionMetadataMapper, swapFunding)
4544
return module.providesAccountRepository(service, transactionService)
4645
}
4746

@@ -97,9 +96,8 @@ object RepositoryFactory {
9796

9897
val api = TransactionApi(module.provideManagedChannel(context, config))
9998
val transactionMetadataMapper = TransactionMetadataMapper()
100-
val verifiedStateManager = ManagerFactory.createVerifiedStateManager()
10199
val swapFunding = SwapFunding(api)
102-
val service = TransactionService(api, transactionMetadataMapper, swapFunding, verifiedStateManager)
100+
val service = TransactionService(api, transactionMetadataMapper, swapFunding)
103101
return module.providesTransactionRepository(service)
104102
}
105103

services/opencode/src/main/kotlin/com/getcode/opencode/controllers/TransactionController.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,18 @@ class TransactionController @Inject constructor(
169169
rendezvous: PublicKey,
170170
scope: CoroutineScope = this.scope,
171171
): Result<IntentType> {
172+
val verifiedState =
173+
verifiedStateManager.getVerifiedStateFor(amount.rate.currency, token.address)
174+
?: return Result.failure(SwapError.Other(IllegalStateException("No verified state found")))
175+
172176
val intent = IntentRemoteSend.create(
173177
amount = amount,
174178
token = token,
175179
owner = owner,
176180
source = source,
177181
giftCard = giftCard,
178-
rendezvous = rendezvous
182+
rendezvous = rendezvous,
183+
verifiedState = verifiedState,
179184
)
180185

181186
return submitIntent(scope, intent, owner.authority.keyPair)
@@ -260,6 +265,9 @@ class TransactionController @Inject constructor(
260265
Result.success(Unit)
261266
}
262267

268+
val verifiedState = verifiedStateManager.getVerifiedStateFor(amount.rate.currency, Mint.usdf)
269+
?: return Result.failure(SwapError.Other(IllegalStateException("No verified state found")))
270+
263271
return accountResult.fold(
264272
onSuccess = {
265273
repository.buy(
@@ -269,7 +277,9 @@ class TransactionController @Inject constructor(
269277
amount = amount,
270278
of = of,
271279
source = source,
272-
fund = fund
280+
fund = fund,
281+
verifiedState = verifiedState,
282+
273283
)
274284
},
275285
onFailure = { error ->
@@ -288,15 +298,27 @@ class TransactionController @Inject constructor(
288298
owner: AccountCluster,
289299
amount: LocalFiat,
290300
of: Token,
291-
): Result<Unit> = repository.sell(scope = scope, owner = owner, amount = amount, of = of)
292-
.onFailure { error ->
293-
trace(
294-
tag = "TransactionController",
295-
message = error.message.orEmpty(),
296-
type = TraceType.Error,
297-
error = error
298-
)
299-
}
301+
): Result<Unit> {
302+
val verifiedState =
303+
verifiedStateManager.getVerifiedStateFor(amount.rate.currency, of.address)
304+
?: return Result.failure(SwapError.Other(IllegalStateException("No verified state found")))
305+
306+
return repository.sell(
307+
scope = scope,
308+
owner = owner,
309+
amount = amount,
310+
of = of,
311+
verifiedState = verifiedState
312+
)
313+
.onFailure { error ->
314+
trace(
315+
tag = "TransactionController",
316+
message = error.message.orEmpty(),
317+
type = TraceType.Error,
318+
error = error
319+
)
320+
}
321+
}
300322

301323
internal suspend fun submitIntent(
302324
scope: CoroutineScope,

services/opencode/src/main/kotlin/com/getcode/opencode/internal/domain/repositories/InternalTransactionRepository.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.getcode.opencode.internal.domain.repositories
22

33
import com.getcode.ed25519.Ed25519
4-
import com.getcode.opencode.internal.network.executors.IntentExecutor
4+
import com.getcode.opencode.internal.manager.VerifiedState
55
import com.getcode.opencode.internal.network.services.TransactionService
66
import com.getcode.opencode.internal.solana.model.SwapId
77
import com.getcode.opencode.model.accounts.AccountCluster
@@ -12,7 +12,6 @@ import com.getcode.opencode.model.transactions.AirdropType
1212
import com.getcode.opencode.model.transactions.ExchangeData
1313
import com.getcode.opencode.model.transactions.SwapFundingSource
1414
import com.getcode.opencode.model.transactions.SwapRequest
15-
import com.getcode.opencode.model.transactions.SwapResult
1615
import com.getcode.opencode.model.transactions.TransactionMetadata
1716
import com.getcode.opencode.model.transactions.WithdrawalAvailability
1817
import com.getcode.opencode.repositories.TransactionRepository
@@ -63,14 +62,31 @@ internal class InternalTransactionRepository @Inject constructor(
6362
amount: LocalFiat,
6463
of: Token,
6564
swapId: SwapId?,
65+
verifiedState: VerifiedState,
6666
source: SwapFundingSource,
6767
fund: (suspend (SwapRequest) -> Result<Unit>)?
68-
): Result<Unit> = service.buy(scope, swapId, amount, of, owner, source, fund)
68+
): Result<Unit> = service.buy(
69+
scope = scope,
70+
swapId = swapId,
71+
amount = amount,
72+
of = of,
73+
owner = owner,
74+
verifiedState = verifiedState,
75+
source = source,
76+
fund = fund
77+
)
6978

7079
override suspend fun sell(
7180
scope: CoroutineScope,
7281
owner: AccountCluster,
7382
amount: LocalFiat,
74-
of: Token
75-
): Result<Unit> = service.sell(scope, amount, of, owner)
83+
of: Token,
84+
verifiedState: VerifiedState,
85+
): Result<Unit> = service.sell(
86+
scope = scope,
87+
amount = amount,
88+
of = of,
89+
owner = owner,
90+
verifiedState = verifiedState
91+
)
7692
}

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/api/intents/IntentRemoteSend.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getcode.opencode.internal.network.api.intents
22

33
import com.codeinc.opencode.gen.transaction.v1.TransactionService
4+
import com.getcode.opencode.internal.manager.VerifiedState
45
import com.getcode.opencode.internal.network.api.intents.actions.ActionOpenAccount
56
import com.getcode.opencode.internal.network.api.intents.actions.ActionPublicTransfer
67
import com.getcode.opencode.internal.network.api.intents.actions.ActionPublicWithdraw
@@ -32,6 +33,7 @@ internal class IntentRemoteSend(
3233
giftCard: GiftCardAccount,
3334
amount: LocalFiat,
3435
token: Token,
36+
verifiedState: VerifiedState,
3537
): IntentRemoteSend {
3638
// 1. Open gift card account
3739
val openGiftCardAccount = ActionOpenAccount.createGiftCard(giftCard.cluster, token.address)
@@ -71,7 +73,8 @@ internal class IntentRemoteSend(
7173
amount = amount,
7274
mint = token.address,
7375
isRemoteSend = true,
74-
isWithdrawal = false
76+
isWithdrawal = false,
77+
verifiedState = verifiedState,
7578
),
7679
actionGroup = actions
7780
)

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/extensions/LocalToProtobuf.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ internal fun TransactionMetadata.asProtobufMetadata(): TransactionService.Metada
146146
.setSource(source.asSolanaAccountId())
147147
.setMint(exchangeData.mint.asSolanaAccountId())
148148
.apply {
149+
println("verifiedExchangeData: $verifiedExchangeData")
149150
if (verifiedExchangeData != null) {
150151
setClientExchangeData(verifiedExchangeData.asProtobufExchangeData())
151152
}

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/services/TransactionService.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.getcode.ed25519.Ed25519
66
import com.getcode.ed25519.Ed25519.KeyPair
77
import com.getcode.opencode.internal.domain.mapping.TransactionMetadataMapper
88
import com.getcode.opencode.internal.manager.VerifiedProtoManager
9+
import com.getcode.opencode.internal.manager.VerifiedState
910
import com.getcode.opencode.internal.network.api.TransactionApi
1011
import com.getcode.opencode.internal.network.executors.IntentExecutor
1112
import com.getcode.opencode.internal.network.executors.SwapExecutor
@@ -45,7 +46,6 @@ internal class TransactionService @Inject constructor(
4546
private val api: TransactionApi,
4647
private val transactionMetadataMapper: TransactionMetadataMapper,
4748
private val swapFunding: SwapFunding,
48-
private val verifiedStateManager: VerifiedProtoManager,
4949
) {
5050
suspend fun submitIntent(
5151
scope: CoroutineScope,
@@ -195,12 +195,10 @@ internal class TransactionService @Inject constructor(
195195
amount: LocalFiat,
196196
of: Token,
197197
owner: AccountCluster,
198+
verifiedState: VerifiedState,
198199
source: SwapFundingSource = SwapFundingSource.SubmitIntent(),
199200
fund: (suspend (SwapRequest) -> Result<Unit>)?,
200201
): Result<Unit> {
201-
val verifiedState = verifiedStateManager.getVerifiedStateFor(amount.rate.currency, of.address)
202-
?: return Result.failure(SwapError.Other(IllegalStateException("No verified state found")))
203-
204202
val request = SwapRequest(
205203
owner = owner,
206204
swapAuthority = Ed25519.createKeyPair(),
@@ -226,10 +224,9 @@ internal class TransactionService @Inject constructor(
226224
of: Token,
227225
owner: AccountCluster,
228226
source: SwapFundingSource = SwapFundingSource.SubmitIntent(),
227+
verifiedState: VerifiedState,
229228
): Result<Unit> {
230229
val tokenCluster = owner.withTimelockForToken(of)
231-
val verifiedState = verifiedStateManager.getVerifiedStateFor(amount.rate.currency, of.address)
232-
?: return Result.failure(SwapError.Other(IllegalStateException("No verified state found")))
233230

234231
val request = SwapRequest(
235232
owner = tokenCluster,

services/opencode/src/main/kotlin/com/getcode/opencode/repositories/TransactionRepository.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getcode.opencode.repositories
22

33
import com.getcode.ed25519.Ed25519.KeyPair
4+
import com.getcode.opencode.internal.manager.VerifiedState
45
import com.getcode.opencode.internal.solana.model.SwapId
56
import com.getcode.opencode.model.accounts.AccountCluster
67
import com.getcode.opencode.model.financial.Limits
@@ -57,6 +58,7 @@ interface TransactionRepository {
5758
amount: LocalFiat,
5859
of: Token,
5960
swapId: SwapId? = null,
61+
verifiedState: VerifiedState,
6062
source: SwapFundingSource = SwapFundingSource.SubmitIntent(),
6163
fund: (suspend (SwapRequest) -> Result<Unit>)? = null,
6264
): Result<Unit>
@@ -65,6 +67,7 @@ interface TransactionRepository {
6567
scope: CoroutineScope,
6668
owner: AccountCluster,
6769
amount: LocalFiat,
68-
of: Token
70+
of: Token,
71+
verifiedState: VerifiedState,
6972
): Result<Unit>
7073
}

0 commit comments

Comments
 (0)