-
Notifications
You must be signed in to change notification settings - Fork 21
feat: onramp settlement flow #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e346931
bcb7a78
5d4c378
6f97549
1dab913
a1032df
59ccd89
4da3133
d95634f
22a22a6
637fe96
1c248b3
0e27784
e3850bc
867189e
bf7f489
289a456
10a4237
721769d
d89e09c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,14 @@ func (ctrl *ProfileController) UpdateSenderProfile(ctx *gin.Context) { | |
| return | ||
| } | ||
|
|
||
| if payload.WebhookVersion != "" && payload.WebhookVersion != "1" && payload.WebhookVersion != "2" { | ||
| u.APIResponse(ctx, http.StatusBadRequest, "error", "Failed to validate payload", []types.ErrorData{{ | ||
| Field: "WebhookVersion", | ||
| Message: "Must be \"1\" or \"2\"", | ||
| }}) | ||
| return | ||
| } | ||
|
|
||
| // Get sender profile from the context | ||
| senderCtx, ok := ctx.Get("sender") | ||
| if !ok { | ||
|
|
@@ -91,6 +99,10 @@ func (ctrl *ProfileController) UpdateSenderProfile(ctx *gin.Context) { | |
| update.SetWebhookURL(payload.WebhookURL) | ||
| } | ||
|
|
||
| if payload.WebhookVersion != "" && payload.WebhookVersion != sender.WebhookVersion { | ||
| update.SetWebhookVersion(payload.WebhookVersion) | ||
| } | ||
|
|
||
| if payload.DomainWhitelist != nil { | ||
| update.SetDomainWhitelist(payload.DomainWhitelist) | ||
| } | ||
|
|
@@ -355,21 +367,68 @@ func (ctrl *ProfileController) UpdateProviderProfile(ctx *gin.Context) { | |
| continue | ||
| } | ||
|
|
||
| // Calculate rate from tokenPayload based on conversion type | ||
| var rate decimal.Decimal | ||
| if tokenPayload.ConversionRateType == providerordertoken.ConversionRateTypeFixed { | ||
| rate = tokenPayload.FixedConversionRate | ||
| } else { | ||
| rate = currency.MarketRate.Add(tokenPayload.FloatingConversionRate) | ||
| // Validate buy/sell rates - at least one direction must be configured | ||
| hasBuyRate := !tokenPayload.FixedBuyRate.IsZero() || !tokenPayload.FloatingBuyDelta.IsZero() | ||
| hasSellRate := !tokenPayload.FixedSellRate.IsZero() || !tokenPayload.FloatingSellDelta.IsZero() | ||
| if !hasBuyRate && !hasSellRate { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("At least one rate (buy or sell) must be configured for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| // Validate buy_rate >= sell_rate when both are configured (provider profitability) | ||
| var buyRate, sellRate decimal.Decimal | ||
| if !tokenPayload.FixedBuyRate.IsZero() { | ||
| buyRate = tokenPayload.FixedBuyRate | ||
| } else if !tokenPayload.FloatingBuyDelta.IsZero() && !currency.MarketBuyRate.IsZero() { | ||
| buyRate = currency.MarketBuyRate.Add(tokenPayload.FloatingBuyDelta) | ||
| } | ||
| if !tokenPayload.FixedSellRate.IsZero() { | ||
| sellRate = tokenPayload.FixedSellRate | ||
| } else if !tokenPayload.FloatingSellDelta.IsZero() && !currency.MarketSellRate.IsZero() { | ||
| sellRate = currency.MarketSellRate.Add(tokenPayload.FloatingSellDelta) | ||
| } | ||
| if !buyRate.IsZero() && buyRate.LessThanOrEqual(decimal.Zero) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Buy rate must be positive for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
| if !sellRate.IsZero() && sellRate.LessThanOrEqual(decimal.Zero) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Sell rate must be positive for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
| if !buyRate.IsZero() && !sellRate.IsZero() && buyRate.LessThan(sellRate) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Buy rate must be >= sell rate for profitability (%s)", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
|
|
||
| // Validate rate deviation for floating rates | ||
| if tokenPayload.ConversionRateType == providerordertoken.ConversionRateTypeFloating { | ||
| percentDeviation := u.AbsPercentageDeviation(currency.MarketRate, rate) | ||
| // Validate rate deviation only when rate was derived from floating (market + delta), not when using fixed rate | ||
| if tokenPayload.FixedBuyRate.IsZero() && !tokenPayload.FloatingBuyDelta.IsZero() && !currency.MarketBuyRate.IsZero() { | ||
| percentDeviation := u.AbsPercentageDeviation(currency.MarketBuyRate, buyRate) | ||
| if percentDeviation.GreaterThan(orderConf.PercentDeviationFromMarketRate) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Buy rate is too far from market rate for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
| } | ||
| if tokenPayload.FixedSellRate.IsZero() && !tokenPayload.FloatingSellDelta.IsZero() && !currency.MarketSellRate.IsZero() { | ||
| percentDeviation := u.AbsPercentageDeviation(currency.MarketSellRate, sellRate) | ||
| if percentDeviation.GreaterThan(orderConf.PercentDeviationFromMarketRate) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Rate is too far from market rate for %s", tokenPayload.Symbol), | ||
| Message: fmt.Sprintf("Sell rate is too far from market rate for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
|
|
@@ -384,12 +443,32 @@ func (ctrl *ProfileController) UpdateProviderProfile(ctx *gin.Context) { | |
| Message: fmt.Sprintf("Rate slippage cannot be less than 0.1%% for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } else if rate.Mul(tokenPayload.RateSlippage.Div(decimal.NewFromFloat(100))).GreaterThan(currency.MarketRate.Mul(decimal.NewFromFloat(0.05))) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Rate slippage is too high for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } else { | ||
| // Cap slippage percentage so fixed rates (e.g. rateRef=1) cannot bypass validation | ||
| const maxSlippagePercent = 5.0 | ||
| if tokenPayload.RateSlippage.GreaterThan(decimal.NewFromFloat(maxSlippagePercent)) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Rate slippage is too high for %s (max %.1f%%)", tokenPayload.Symbol, maxSlippagePercent), | ||
| }) | ||
| continue | ||
| } | ||
| // Check slippage against market rates (use sell rate as reference for offramp) | ||
| marketRef := currency.MarketSellRate | ||
| if marketRef.IsZero() { | ||
| marketRef = currency.MarketBuyRate | ||
| } | ||
| rateRef := sellRate | ||
| if rateRef.IsZero() { | ||
| rateRef = buyRate | ||
| } | ||
| if !marketRef.IsZero() && !rateRef.IsZero() && rateRef.Mul(tokenPayload.RateSlippage.Div(decimal.NewFromFloat(100))).GreaterThan(marketRef.Mul(decimal.NewFromFloat(0.05))) { | ||
| validationErrors = append(validationErrors, types.ErrorData{ | ||
| Field: "Tokens", | ||
| Message: fmt.Sprintf("Rate slippage is too high for %s", tokenPayload.Symbol), | ||
| }) | ||
| continue | ||
| } | ||
chibie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Check if token already exists for provider | ||
|
|
@@ -443,11 +522,20 @@ func (ctrl *ProfileController) UpdateProviderProfile(ctx *gin.Context) { | |
| tokenPayload.RateSlippage = existingToken.RateSlippage | ||
| } | ||
|
|
||
| // Choose a representative conversion rate for bucket calculations (offramp = sell side) | ||
| conversionRate := sellRate | ||
| if conversionRate.IsZero() { | ||
| conversionRate = buyRate | ||
| } | ||
| if conversionRate.IsZero() { | ||
| conversionRate = decimal.NewFromInt(1) | ||
| } | ||
|
Comment on lines
+525
to
+532
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fallback When both buy and sell rates are zero (e.g., floating deltas configured but market rates not yet available), falling back to Consider either skipping bucket assignment for this token or logging a warning so the misconfiguration is visible. 🤖 Prompt for AI Agents |
||
|
|
||
| tokenOperations = append(tokenOperations, TokenOperation{ | ||
| TokenPayload: tokenPayload, | ||
| ProviderToken: providerToken, | ||
| Currency: currency, | ||
| Rate: rate, | ||
| Rate: conversionRate, | ||
| IsUpdate: isUpdate, | ||
| ExistingToken: existingToken, | ||
| }) | ||
|
|
@@ -586,9 +674,10 @@ func (ctrl *ProfileController) UpdateProviderProfile(ctx *gin.Context) { | |
| SetSettlementAddress(op.TokenPayload.SettlementAddress). | ||
| SetNetwork(op.TokenPayload.Network). | ||
| SetRateSlippage(op.TokenPayload.RateSlippage). | ||
| SetConversionRateType(op.TokenPayload.ConversionRateType). | ||
| SetFixedConversionRate(op.TokenPayload.FixedConversionRate). | ||
| SetFloatingConversionRate(op.TokenPayload.FloatingConversionRate). | ||
| SetFixedBuyRate(op.TokenPayload.FixedBuyRate). | ||
| SetFixedSellRate(op.TokenPayload.FixedSellRate). | ||
| SetFloatingBuyDelta(op.TokenPayload.FloatingBuyDelta). | ||
| SetFloatingSellDelta(op.TokenPayload.FloatingSellDelta). | ||
| SetMaxOrderAmount(op.TokenPayload.MaxOrderAmount). | ||
| SetMinOrderAmount(op.TokenPayload.MinOrderAmount). | ||
| SetMaxOrderAmountOtc(op.TokenPayload.MaxOrderAmountOTC). | ||
|
|
@@ -610,9 +699,10 @@ func (ctrl *ProfileController) UpdateProviderProfile(ctx *gin.Context) { | |
| // Create new token | ||
| _, err = tx.ProviderOrderToken. | ||
| Create(). | ||
| SetConversionRateType(op.TokenPayload.ConversionRateType). | ||
| SetFixedConversionRate(op.TokenPayload.FixedConversionRate). | ||
| SetFloatingConversionRate(op.TokenPayload.FloatingConversionRate). | ||
| SetFixedBuyRate(op.TokenPayload.FixedBuyRate). | ||
| SetFixedSellRate(op.TokenPayload.FixedSellRate). | ||
| SetFloatingBuyDelta(op.TokenPayload.FloatingBuyDelta). | ||
| SetFloatingSellDelta(op.TokenPayload.FloatingSellDelta). | ||
| SetMaxOrderAmount(op.TokenPayload.MaxOrderAmount). | ||
| SetMinOrderAmount(op.TokenPayload.MinOrderAmount). | ||
| SetMaxOrderAmountOtc(op.TokenPayload.MaxOrderAmountOTC). | ||
|
|
@@ -829,12 +919,17 @@ func (ctrl *ProfileController) GetSenderProfile(ctx *gin.Context) { | |
| kybRejectionComment = kybProfile.KybRejectionComment | ||
| } | ||
|
|
||
| webhookVersion := sender.WebhookVersion | ||
| if webhookVersion == "" { | ||
| webhookVersion = "1" | ||
| } | ||
| response := &types.SenderProfileResponse{ | ||
| ID: sender.ID, | ||
| FirstName: user.FirstName, | ||
| LastName: user.LastName, | ||
| Email: user.Email, | ||
| WebhookURL: sender.WebhookURL, | ||
| WebhookVersion: webhookVersion, | ||
| DomainWhitelist: sender.DomainWhitelist, | ||
| Tokens: tokensPayload, | ||
| APIKey: *apiKey, | ||
|
|
@@ -958,17 +1053,18 @@ func (ctrl *ProfileController) GetProviderProfile(ctx *gin.Context) { | |
| tokensPayload := make([]types.ProviderOrderTokenPayload, len(orderTokens)) | ||
| for i, orderToken := range orderTokens { | ||
| payload := types.ProviderOrderTokenPayload{ | ||
| Symbol: orderToken.Edges.Token.Symbol, | ||
| ConversionRateType: orderToken.ConversionRateType, | ||
| FixedConversionRate: orderToken.FixedConversionRate, | ||
| FloatingConversionRate: orderToken.FloatingConversionRate, | ||
| MaxOrderAmount: orderToken.MaxOrderAmount, | ||
| MinOrderAmount: orderToken.MinOrderAmount, | ||
| MaxOrderAmountOTC: orderToken.MaxOrderAmountOtc, | ||
| MinOrderAmountOTC: orderToken.MinOrderAmountOtc, | ||
| RateSlippage: orderToken.RateSlippage, | ||
| SettlementAddress: orderToken.SettlementAddress, | ||
| Network: orderToken.Network, | ||
| Symbol: orderToken.Edges.Token.Symbol, | ||
| FixedBuyRate: orderToken.FixedBuyRate, | ||
| FixedSellRate: orderToken.FixedSellRate, | ||
| FloatingBuyDelta: orderToken.FloatingBuyDelta, | ||
| FloatingSellDelta: orderToken.FloatingSellDelta, | ||
| MaxOrderAmount: orderToken.MaxOrderAmount, | ||
| MinOrderAmount: orderToken.MinOrderAmount, | ||
| MaxOrderAmountOTC: orderToken.MaxOrderAmountOtc, | ||
| MinOrderAmountOTC: orderToken.MinOrderAmountOtc, | ||
| RateSlippage: orderToken.RateSlippage, | ||
| SettlementAddress: orderToken.SettlementAddress, | ||
| Network: orderToken.Network, | ||
| } | ||
| tokensPayload[i] = payload | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.