Skip to content

Commit 0b3897a

Browse files
committed
fix(services/opencode): add CreateIdempotent for sender USDC ATA in swap instructions
Prevents "Invalid account owner" on the UsdfProgram_Swap instruction when the sender's USDC ATA is closed or reaped between simulation and submission (e.g. while the user is signing in their external wallet). Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 42169fb commit 0b3897a

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

services/opencode/src/main/kotlin/com/getcode/opencode/solana/swap/UsdcToUsdfSwapInstructions.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.getcode.opencode.solana.swap
22

3-
import com.getcode.opencode.internal.solana.extensions.deriveAssociatedAccount
43
import com.getcode.opencode.internal.solana.extensions.timelockSwapAccounts
54
import com.getcode.opencode.internal.solana.model.LiquidityPool
65
import com.getcode.opencode.internal.solana.model.SwapId
@@ -33,7 +32,8 @@ internal fun buildUsdcToUsdfSwapInstructions(
3332
mint = Mint.usdf,
3433
)
3534

36-
val usdcAta = PublicKey.deriveAssociatedAccount(
35+
val createUsdcAta = AssociatedTokenProgram_CreateIdempotent(
36+
subsidizer = sender,
3737
owner = sender,
3838
mint = Mint.usdc,
3939
)
@@ -55,14 +55,17 @@ internal fun buildUsdcToUsdfSwapInstructions(
5555
).instruction()
5656
)
5757

58-
// 5. Memo:Memo
58+
// 5. AssociatedTokenAccount::CreateIdempotent (USDC ATA)
59+
add(createUsdcAta.instruction())
60+
61+
// 6. Memo:Memo
5962
add(
6063
MemoProgram_Memo(
6164
message = swapId.publicKey.base58()
6265
).instruction()
6366
)
6467

65-
// 6. Usdf::Swap (USDC ATA -> USDF ATA)
68+
// 7. Usdf::Swap (USDC ATA -> USDF ATA)
6669
add(
6770
UsdfProgram_Swap(
6871
amount = amount,
@@ -72,11 +75,11 @@ internal fun buildUsdcToUsdfSwapInstructions(
7275
usdfVault = pool.usdfVault,
7376
otherVault = pool.otherVault,
7477
userUsdfToken = createUsdfAta.address,
75-
userOtherToken = usdcAta.publicKey,
78+
userOtherToken = createUsdcAta.address,
7679
).instruction()
7780
)
7881

79-
// 7. Token::Transfer (USDF ATA -> USDF Swap PDA)
82+
// 8. Token::Transfer (USDF ATA -> USDF Swap PDA)
8083
add(
8184
TokenProgram_Transfer(
8285
amount = amount,

0 commit comments

Comments
 (0)