@@ -8,7 +8,9 @@ import com.getcode.opencode.internal.extensions.toPublicKey
88import com.getcode.opencode.internal.transactors.GiveBillTransactor.GiveTransactorError
99import com.getcode.opencode.model.accounts.AccountCluster
1010import com.getcode.opencode.model.core.OpenCodePayload
11+ import com.getcode.opencode.model.core.PayloadKind
1112import com.getcode.opencode.model.transactions.TransactionMetadata
13+ import com.getcode.solana.keys.PublicKey
1214import com.getcode.utils.CodeServerError
1315import com.getcode.utils.ErrorUtils
1416import kotlinx.coroutines.CoroutineScope
@@ -35,6 +37,35 @@ internal class GrabBillTransactor(
3537 val data = payload
3638 ? : return logAndFail(GrabTransactorError .Other (message = " No payload available. Did you call with() first?" ))
3739
40+ val kind = payload?.kind
41+ ? : return logAndFail(GrabTransactorError .Other (message = " No payload kind available. Did you call with() first?" ))
42+
43+
44+ return when (kind) {
45+ PayloadKind .Unknown -> logAndFail(GrabTransactorError .Other (message = " Unknown payload kind" ))
46+ PayloadKind .Cash -> handleLegacyScan(ownerKey, data)
47+ PayloadKind .MultiMintCash -> handleMultiMintScan(ownerKey, data)
48+ }
49+ }
50+
51+ fun dispose () {
52+ owner = null
53+ payload = null
54+
55+ scope.cancel()
56+ }
57+
58+ private suspend fun handleLegacyScan (
59+ ownerKey : AccountCluster ,
60+ data : OpenCodePayload
61+ ): Result <TransactionMetadata .PublicPayment > {
62+ return requestGrab(ownerKey, data)
63+ }
64+
65+ private suspend fun handleMultiMintScan (
66+ ownerKey : AccountCluster ,
67+ data : OpenCodePayload
68+ ): Result <TransactionMetadata .PublicPayment > {
3869 // 1. Wait for the give request from the sender so we can determine what mint we are operating on
3970 val (messageId, giveRequestMint) = messagingController.pollForGiveRequest(data.rendezvous)
4071 .getOrNull()
@@ -59,32 +90,50 @@ internal class GrabBillTransactor(
5990
6091 // 4. Send the grab request to the recipient with the correct vault
6192 return userAccountResult.map {
62- messagingController.sendRequestToGrabBill(
63- destination = tokenizedCluster.vaultPublicKey,
64- payload = data
93+ requestGrab(tokenizedCluster, data)
94+ messagingController.sendRequestToGrabBill(
95+ destination = tokenizedCluster.vaultPublicKey,
96+ payload = data
97+ )
98+ }.fold(
99+ onSuccess = {
100+ // 5. Wait for confirmation
101+ transactionController.pollIntentMetadata(
102+ owner = tokenizedCluster.authority.keyPair,
103+ intentId = data.rendezvous.toPublicKey(),
104+ debugLogs = true
65105 )
66- }.fold(
67- onSuccess = {
68- // 5. Wait for confirmation
69- transactionController.pollIntentMetadata(
70- owner = tokenizedCluster.authority.keyPair,
71- intentId = data.rendezvous.toPublicKey(),
72- debugLogs = true
73- )
74- }, onFailure = {
75- if (it !is GrabTransactorError ) {
76- ErrorUtils .handleError(it)
77- }
78- logAndFail(it)
106+ }, onFailure = {
107+ if (it !is GrabTransactorError ) {
108+ ErrorUtils .handleError(it)
79109 }
80- )
110+ logAndFail(it)
111+ }
112+ )
81113 }
82114
83- fun dispose () {
84- owner = null
85- payload = null
86-
87- scope.cancel()
115+ private suspend fun requestGrab (
116+ owner : AccountCluster ,
117+ data : OpenCodePayload
118+ ): Result <TransactionMetadata .PublicPayment > {
119+ return messagingController.sendRequestToGrabBill(
120+ destination = owner.vaultPublicKey,
121+ payload = data
122+ ).fold(
123+ onSuccess = {
124+ // 5. Wait for confirmation
125+ transactionController.pollIntentMetadata(
126+ owner = owner.authority.keyPair,
127+ intentId = data.rendezvous.toPublicKey(),
128+ debugLogs = true
129+ )
130+ }, onFailure = {
131+ if (it !is GrabTransactorError ) {
132+ ErrorUtils .handleError(it)
133+ }
134+ logAndFail(it)
135+ }
136+ )
88137 }
89138}
90139
0 commit comments