diff --git a/ocp/rpc/transaction/swap.go b/ocp/rpc/transaction/swap.go index 49c68de..3b3c2fe 100644 --- a/ocp/rpc/transaction/swap.go +++ b/ocp/rpc/transaction/swap.go @@ -144,6 +144,10 @@ func (s *transactionServer) StatefulSwap(streamer transactionpb.Transaction_Stat return handleStatefulSwapError(streamer, NewSwapValidationError("owner cannot be swap authority")) } + if !common.IsCoreMint(fromMint) && !common.IsCoreMint(toMint) { + return handleStatefulSwapError(streamer, NewSwapDeniedError("swap must involve core mint")) + } + if bytes.Equal(fromMint.PublicKey().ToBytes(), toMint.PublicKey().ToBytes()) { return handleStatefulSwapError(streamer, NewSwapValidationError("must swap between two different mints")) } diff --git a/ocp/worker/swap/util.go b/ocp/worker/swap/util.go index 7a4e912..8d1faf9 100644 --- a/ocp/worker/swap/util.go +++ b/ocp/worker/swap/util.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "database/sql" "fmt" + "math/big" "slices" "time" @@ -170,7 +171,7 @@ func (p *runtime) updateBalancesForFinalizedSwap(ctx context.Context, swapRecord return 0, errors.New("delta quarks into destination vm omnibus is not positive") } - var usdMarketValue float64 + var usdMarketValueWithoutFees float64 switch swapRecord.FundingSource { case swap.FundingSourceSubmitIntent: fundingIntentRecord, err := p.data.GetIntent(ctx, swapRecord.FundingId) @@ -182,7 +183,7 @@ func (p *runtime) updateBalancesForFinalizedSwap(ctx context.Context, swapRecord return 0, errors.New("unexpected intent type") } - usdMarketValue = fundingIntentRecord.SendPublicPaymentMetadata.UsdMarketValue + usdMarketValueWithoutFees = fundingIntentRecord.SendPublicPaymentMetadata.UsdMarketValue case swap.FundingSourceExternalWallet: if !common.IsCoreMint(fromMint) { return 0, errors.New("unexpected source mint") @@ -192,7 +193,7 @@ func (p *runtime) updateBalancesForFinalizedSwap(ctx context.Context, swapRecord return 0, errors.New("core mint is not a usd stable coin") } - usdMarketValue, err = currency_util.CalculateUsdMarketValueFromTokenAmount(ctx, p.data, common.CoreMintAccount, swapRecord.Amount, time.Now()) + usdMarketValueWithoutFees, err = currency_util.CalculateUsdMarketValueFromTokenAmount(ctx, p.data, common.CoreMintAccount, swapRecord.Amount, time.Now()) if err != nil { return 0, err } @@ -200,6 +201,14 @@ func (p *runtime) updateBalancesForFinalizedSwap(ctx context.Context, swapRecord return 0, errors.New("unsupported funding source") } + usdMarketValue := usdMarketValueWithoutFees + if !common.IsCoreMint(fromMint) { + usdMarketValue, _ = new(big.Float).Mul( + big.NewFloat(0.99).SetPrec(128), + big.NewFloat(usdMarketValue).SetPrec(128), + ).Float64() + } + err = p.data.ExecuteInTx(ctx, sql.LevelDefault, func(ctx context.Context) error { // For transaction history intentRecord := &intent.Record{ @@ -304,7 +313,10 @@ func (p *runtime) notifySwapFinalized(ctx context.Context, swapRecord *swap.Reco valueReceived := nativeAmount if !common.IsCoreMint(fromMint) { - valueReceived = 0.99 * valueReceived + valueReceived, _ = new(big.Float).Mul( + big.NewFloat(0.99).SetPrec(128), + big.NewFloat(valueReceived).SetPrec(128), + ).Float64() } return p.integration.OnSwapFinalized(ctx, owner, isBuy, targetCurrencyMetadataRecord.Name, currencyCode, valueReceived)