Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ocp/rpc/transaction/stateful_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ func (s *transactionServer) handleStablecoinStatefulSwap(
return handleStatefulSwapError(streamer, err)
}

if initiateStablecoinSwapReq.SwapVersion != requiredCoinbaseSwapVersion {
return handleStatefulSwapError(streamer, NewSwapValidationErrorf("swap version must be %d", requiredCoinbaseSwapVersion))
}

if !common.IsCoreMint(fromMint) {
return handleStatefulSwapError(streamer, NewSwapValidationError("source mint must be the core mint"))
}
Expand Down Expand Up @@ -1035,9 +1039,6 @@ func (s *transactionServer) handleStablecoinStatefulSwap(
selectedNonce.ReleaseIfNotReserved(ctx)
}()

coinbaseSwapVersion := selectCoinbaseSwapVersion(initiateStablecoinSwapReq.SwapVersion)
log = log.With(zap.String("coinbase_swap_version", coinbaseSwapVersion.String()))

transactionVersion := selectSwapTransactionVersion(initiateReq.TransactionVersion)
log = log.With(zap.String("selected_transaction_version", transactionVersion.String()))

Expand All @@ -1052,7 +1053,6 @@ func (s *transactionServer) handleStablecoinStatefulSwap(
initiateStablecoinSwapReq.FeeAmount,
selectedNonce,
transactionVersion,
coinbaseSwapVersion,
)

txn, err := makeStatefulSwapTransaction(ctx, swapHandler)
Expand Down
14 changes: 5 additions & 9 deletions ocp/rpc/transaction/stateless_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (s *transactionServer) handleStablecoinStatelessSwap(
// Section: Validation
//

if initiateStablecoinSwapReq.SwapVersion != requiredCoinbaseSwapVersion {
return handleStatelessSwapError(streamer, NewSwapValidationErrorf("swap version must be %d", requiredCoinbaseSwapVersion))
}

if fromMint.PublicKey().ToBase58() != usdc.Mint {
return handleStatelessSwapError(streamer, NewSwapValidationError("source mint must be usdc"))
}
Expand Down Expand Up @@ -242,11 +246,7 @@ func (s *transactionServer) handleStablecoinStatelessSwap(
return handleStatelessSwapError(streamer, err)
}

coinbaseSwapVersion := selectCoinbaseSwapVersion(initiateStablecoinSwapReq.SwapVersion)
log = log.With(zap.String("coinbase_swap_version", coinbaseSwapVersion.String()))

coinbaseSwapIxn, err := transaction_util.MakeCoinbaseSwapInstruction(
coinbaseSwapVersion,
coinbaseSwapIxn := transaction_util.MakeCoinbaseSwapInstruction(
coinbaseAccounts,
owner.PublicKey().ToBytes(),
fromMint.PublicKey().ToBytes(),
Expand All @@ -256,10 +256,6 @@ func (s *transactionServer) handleStablecoinStatelessSwap(
initiateStablecoinSwapReq.SwapAmount,
initiateStablecoinSwapReq.SwapAmount,
)
if err != nil {
log.With(zap.Error(err)).Warn("failure building coinbase swap instruction")
return handleStatelessSwapError(streamer, err)
}

feeRecipient, err := common.NewAccountFromPublicKeyBytes(coinbaseAccounts.FeeRecipient)
if err != nil {
Expand Down
32 changes: 10 additions & 22 deletions ocp/rpc/transaction/swap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,18 +1380,13 @@ func (h *ReserveTreasuryFundedCreateAndBuySwapHandler) MakeInstructions(ctx cont
return ixns, nil
}

// selectCoinbaseSwapVersion picks the CoinbaseStableSwapper swap instruction
// version from the client's requested swap_version. Clients rebuild and sign
// the transaction locally, so the server must match what the client builds.
// Only an explicit 2 selects v2. Older clients leave it unset and build v1.
func selectCoinbaseSwapVersion(requested uint32) transaction_util.CoinbaseSwapVersion {
if requested == 2 {
return transaction_util.CoinbaseSwapV2
}
return transaction_util.CoinbaseSwapV1
}

const (
// The only supported CoinbaseStableSwapper swap instruction version is
// swap_v2. Clients rebuild and sign the transaction locally, so a request
// for any other version, including older clients that leave it unset and
// build the deprecated whitelist swap, is rejected.
requiredCoinbaseSwapVersion = 2

// Measured on mainnet at 62k CU with an existing destination ATA. Creating
// the destination ATA adds about 9k, and the swap authority ATA, destination
// ATA and VM swap PDA derivations each cost 1,500 CU per bump below 255, for
Expand All @@ -1418,9 +1413,8 @@ type CoinbaseStableSwapperSwapHandler struct {
computeUnitPrice uint64
memoValue string

feeDestination *common.Account
coinbaseSwapVersion transaction_util.CoinbaseSwapVersion
coinbaseAccounts *transaction_util.CoinbaseSwapAccounts
feeDestination *common.Account
coinbaseAccounts *transaction_util.CoinbaseSwapAccounts
}

func NewCoinbaseStableSwapperSwapHandler(
Expand All @@ -1434,7 +1428,6 @@ func NewCoinbaseStableSwapperSwapHandler(
feeAmount uint64,
selectedNonce *transaction_util.Nonce,
transactionVersion solana.MessageVersion,
coinbaseSwapVersion transaction_util.CoinbaseSwapVersion,
) *CoinbaseStableSwapperSwapHandler {
return &CoinbaseStableSwapperSwapHandler{
data: data,
Expand All @@ -1453,8 +1446,7 @@ func NewCoinbaseStableSwapperSwapHandler(
computeUnitPrice: coinbaseStatefulSwapComputeUnitPrice,
memoValue: swapMemoValue("coinbase_stable_swapper", transactionVersion),

feeDestination: common.CoreMintFeesAccount,
coinbaseSwapVersion: coinbaseSwapVersion,
feeDestination: common.CoreMintFeesAccount,
}
}

Expand Down Expand Up @@ -1564,8 +1556,7 @@ func (h *CoinbaseStableSwapperSwapHandler) MakeInstructions(ctx context.Context)
},
)

coinbaseSwapIxn, err := transaction_util.MakeCoinbaseSwapInstruction(
h.coinbaseSwapVersion,
coinbaseSwapIxn := transaction_util.MakeCoinbaseSwapInstruction(
coinbaseAccounts,
h.swapAuthority.PublicKey().ToBytes(),
h.fromMint.PublicKey().ToBytes(),
Expand All @@ -1575,9 +1566,6 @@ func (h *CoinbaseStableSwapperSwapHandler) MakeInstructions(ctx context.Context)
h.swapAmount,
h.swapAmount,
)
if err != nil {
return nil, err
}

closeSwapAuthorityFromMintAtaIxn := token.CloseAccount(
swapAuthorityFromMintAta,
Expand Down
Loading
Loading