Skip to content

Commit f484187

Browse files
authored
StatefulSwap now validates positive sell fee (#168)
1 parent c8b7f63 commit f484187

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

ocp/rpc/transaction/server.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/code-payments/ocp-server/ocp/antispam"
1414
auth_util "github.com/code-payments/ocp-server/ocp/auth"
1515
"github.com/code-payments/ocp-server/ocp/common"
16+
currency_util "github.com/code-payments/ocp-server/ocp/currency"
1617
ocp_data "github.com/code-payments/ocp-server/ocp/data"
1718
"github.com/code-payments/ocp-server/ocp/data/nonce"
1819
"github.com/code-payments/ocp-server/ocp/transaction"
@@ -23,7 +24,8 @@ type transactionServer struct {
2324

2425
log *zap.Logger
2526

26-
data ocp_data.Provider
27+
data ocp_data.Provider
28+
mintDataProvider *currency_util.MintDataProvider
2729

2830
auth *auth_util.RPCSignatureVerifier
2931

@@ -46,6 +48,7 @@ type transactionServer struct {
4648
func NewTransactionServer(
4749
log *zap.Logger,
4850
data ocp_data.Provider,
51+
mintDataProvider *currency_util.MintDataProvider,
4952
submitIntentIntegration SubmitIntentIntegration,
5053
antispamGuard *antispam.Guard,
5154
amlGuard *aml.Guard,
@@ -86,7 +89,8 @@ func NewTransactionServer(
8689

8790
log: log,
8891

89-
data: data,
92+
data: data,
93+
mintDataProvider: mintDataProvider,
9094

9195
auth: auth_util.NewRPCSignatureVerifier(log, data),
9296

ocp/rpc/transaction/swap.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/code-payments/ocp-server/ocp/vm"
2929
"github.com/code-payments/ocp-server/protoutil"
3030
"github.com/code-payments/ocp-server/solana"
31+
"github.com/code-payments/ocp-server/solana/currencycreator"
3132
)
3233

3334
func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_StatefulSwapServer) error {
@@ -233,6 +234,24 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat
233234
return handleStatefulSwapError(streamer, NewSwapDeniedError("mint is being initialized"))
234235
}
235236

237+
if !initializesMint && !common.IsCoreMint(fromMint) {
238+
liveReserveState, err := s.mintDataProvider.GetLiveReserveState(ctx, fromMint)
239+
if err != nil {
240+
log.With(zap.Error(err)).Warn("failure getting live reserve state")
241+
return handleStatefulSwapError(streamer, err)
242+
}
243+
244+
_, estimatedFees := currencycreator.EstimateSell(&currencycreator.EstimateSellArgs{
245+
CurrentSupplyInQuarks: liveReserveState.SupplyFromBonding,
246+
SellAmountInQuarks: initiateReserveSwapReq.Amount,
247+
ValueMintDecimals: uint8(common.CoreMintDecimals),
248+
SellFeeBps: currencyMetadataRecord.SellFeeBps,
249+
})
250+
if estimatedFees == 0 {
251+
return handleStatefulSwapError(streamer, NewSwapDeniedError("swap would not generate a sell fee"))
252+
}
253+
}
254+
236255
var destinationVmAuthority *common.Account
237256
if !initializesMint {
238257
if owner.PublicKey().ToBase58() == swapAuthority.PublicKey().ToBase58() {

0 commit comments

Comments
 (0)