From f20b02606df31191f010331680c06caac0b6ef9a Mon Sep 17 00:00:00 2001 From: jeffyanta Date: Mon, 6 Apr 2026 10:33:46 -0400 Subject: [PATCH] StatefulSwap now validates positive sell fee --- ocp/rpc/transaction/server.go | 8 ++++++-- ocp/rpc/transaction/swap.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ocp/rpc/transaction/server.go b/ocp/rpc/transaction/server.go index 5fab058..9457d9e 100644 --- a/ocp/rpc/transaction/server.go +++ b/ocp/rpc/transaction/server.go @@ -13,6 +13,7 @@ import ( "github.com/code-payments/ocp-server/ocp/antispam" auth_util "github.com/code-payments/ocp-server/ocp/auth" "github.com/code-payments/ocp-server/ocp/common" + currency_util "github.com/code-payments/ocp-server/ocp/currency" ocp_data "github.com/code-payments/ocp-server/ocp/data" "github.com/code-payments/ocp-server/ocp/data/nonce" "github.com/code-payments/ocp-server/ocp/transaction" @@ -23,7 +24,8 @@ type transactionServer struct { log *zap.Logger - data ocp_data.Provider + data ocp_data.Provider + mintDataProvider *currency_util.MintDataProvider auth *auth_util.RPCSignatureVerifier @@ -46,6 +48,7 @@ type transactionServer struct { func NewTransactionServer( log *zap.Logger, data ocp_data.Provider, + mintDataProvider *currency_util.MintDataProvider, submitIntentIntegration SubmitIntentIntegration, antispamGuard *antispam.Guard, amlGuard *aml.Guard, @@ -86,7 +89,8 @@ func NewTransactionServer( log: log, - data: data, + data: data, + mintDataProvider: mintDataProvider, auth: auth_util.NewRPCSignatureVerifier(log, data), diff --git a/ocp/rpc/transaction/swap.go b/ocp/rpc/transaction/swap.go index 7cb8aea..18f28e2 100644 --- a/ocp/rpc/transaction/swap.go +++ b/ocp/rpc/transaction/swap.go @@ -28,6 +28,7 @@ import ( "github.com/code-payments/ocp-server/ocp/vm" "github.com/code-payments/ocp-server/protoutil" "github.com/code-payments/ocp-server/solana" + "github.com/code-payments/ocp-server/solana/currencycreator" ) func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_StatefulSwapServer) error { @@ -233,6 +234,24 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat return handleStatefulSwapError(streamer, NewSwapDeniedError("mint is being initialized")) } + if !initializesMint && !common.IsCoreMint(fromMint) { + liveReserveState, err := s.mintDataProvider.GetLiveReserveState(ctx, fromMint) + if err != nil { + log.With(zap.Error(err)).Warn("failure getting live reserve state") + return handleStatefulSwapError(streamer, err) + } + + _, estimatedFees := currencycreator.EstimateSell(¤cycreator.EstimateSellArgs{ + CurrentSupplyInQuarks: liveReserveState.SupplyFromBonding, + SellAmountInQuarks: initiateReserveSwapReq.Amount, + ValueMintDecimals: uint8(common.CoreMintDecimals), + SellFeeBps: currencyMetadataRecord.SellFeeBps, + }) + if estimatedFees == 0 { + return handleStatefulSwapError(streamer, NewSwapDeniedError("swap would not generate a sell fee")) + } + } + var destinationVmAuthority *common.Account if !initializesMint { if owner.PublicKey().ToBase58() == swapAuthority.PublicKey().ToBase58() {