Skip to content

Commit 7f872f7

Browse files
committed
feat: add slippage protection
1 parent e53e5c4 commit 7f872f7

File tree

7 files changed

+207
-3
lines changed

7 files changed

+207
-3
lines changed

swappy-bot/Commands/DepositAddressProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ public static async Task<Result<DepositAddressResponse>> GetDepositAddressAsync(
1818
AssetInfo assetFrom,
1919
AssetInfo assetTo,
2020
string destinationAddress,
21-
string refundAddress)
21+
string refundAddress,
22+
decimal minPrice)
2223
{
2324
using var client = httpClientFactory.CreateClient("Broker");
2425

25-
// TODO: Add minimumPrice, 2% slippage
26+
var slippage = minPrice * (98 / 100m);
27+
2628
var swapRequest =
2729
$"swap" +
2830
$"?amount={amount}" +
2931
$"&sourceAsset={assetFrom.Id}" +
3032
$"&destinationAsset={assetTo.Id}" +
3133
$"&destinationAddress={destinationAddress}" +
34+
$"&minimumPrice={slippage}" +
3235
$"&refundAddress={refundAddress}" +
3336
$"&retryDurationInBlocks=150" +
3437
$"&apiKey={configuration.BrokerApiKey}";

swappy-bot/Commands/QuoteProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@ public class QuoteResponse
6868

6969
[JsonPropertyName("egressAmount")]
7070
public decimal EgressAmount { get; set; }
71+
72+
[JsonPropertyName("estimatedPrice")]
73+
public decimal EstimatedPrice { get; set; }
7174
}
7275
}

swappy-bot/Commands/Swap/Swap.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ await Context.Channel.SendMessageAsync(
404404
swapState.QuoteTime = quoteTime;
405405
swapState.QuoteDeposit = quoteDeposit;
406406
swapState.QuoteReceive = quoteReceive;
407+
swapState.QuoteMinPrice = quote.EstimatedPrice;
407408
swapState.QuoteRate = quoteRate;
408409

409410
await Context.Channel.SendMessageAsync(
@@ -677,6 +678,7 @@ await Context.Channel.SendMessageAsync(
677678
swapState.QuoteTime = quoteTime;
678679
swapState.QuoteDeposit = quoteDeposit;
679680
swapState.QuoteReceive = quoteReceive;
681+
swapState.QuoteMinPrice = quote.EstimatedPrice;
680682
swapState.QuoteRate = quoteRate;
681683
}
682684

@@ -785,6 +787,7 @@ await Context.Channel.SendMessageAsync(
785787
DepositAddressResponse deposit;
786788
try
787789
{
790+
var swapStateQuoteMinPrice = swapState.QuoteMinPrice;
788791
var depositResult = await DepositAddressProvider.GetDepositAddressAsync(
789792
_logger,
790793
_configuration,
@@ -793,7 +796,8 @@ await Context.Channel.SendMessageAsync(
793796
assetFrom,
794797
assetTo,
795798
swapState.DestinationAddress,
796-
swapState.RefundAddress);
799+
swapState.RefundAddress,
800+
swapStateQuoteMinPrice.Value);
797801

798802
if (depositResult.IsFailed)
799803
throw new Exception("Failed generating deposit address.");

swappy-bot/EntityFramework/SwapState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SwapState
2121

2222
public decimal? QuoteDeposit { get; set; }
2323
public decimal? QuoteReceive { get; set; }
24+
public decimal? QuoteMinPrice { get; set; }
2425
public string? QuoteRate { get; set; }
2526
public decimal? QuotePlatformFee { get; set; }
2627
public decimal? QuoteChainflipFee { get; set; }
@@ -66,6 +67,7 @@ public override void Configure(EntityTypeBuilder<SwapState> builder)
6667

6768
builder.Property(x => x.QuoteDeposit).IsRequired(false).HasPrecision(27, 18);
6869
builder.Property(x => x.QuoteReceive).IsRequired(false).HasPrecision(27, 18);
70+
builder.Property(x => x.QuoteMinPrice).IsRequired(false).HasPrecision(27, 18);
6971
builder.Property(x => x.QuoteRate).IsRequired(false).HasMaxLength(200);
7072
builder.Property(x => x.QuotePlatformFee).IsRequired(false).HasPrecision(27, 18);
7173
builder.Property(x => x.QuoteChainflipFee).IsRequired(false).HasPrecision(27, 18);

swappy-bot/Migrations/20250606130303_AddQuoteMinPrice.Designer.cs

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace SwappyBot.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class AddQuoteMinPrice : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.AddColumn<decimal>(
14+
name: "quoteminprice",
15+
table: "swap_state",
16+
type: "numeric(27,18)",
17+
precision: 27,
18+
scale: 18,
19+
nullable: true);
20+
}
21+
22+
/// <inheritdoc />
23+
protected override void Down(MigrationBuilder migrationBuilder)
24+
{
25+
migrationBuilder.DropColumn(
26+
name: "quoteminprice",
27+
table: "swap_state");
28+
}
29+
}
30+
}

swappy-bot/Migrations/BotContextModelSnapshot.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7878
.HasColumnType("numeric(27,18)")
7979
.HasColumnName("quotedeposit");
8080

81+
b.Property<decimal?>("QuoteMinPrice")
82+
.HasPrecision(27, 18)
83+
.HasColumnType("numeric(27,18)")
84+
.HasColumnName("quoteminprice");
85+
8186
b.Property<decimal?>("QuotePlatformFee")
8287
.HasPrecision(27, 18)
8388
.HasColumnType("numeric(27,18)")

0 commit comments

Comments
 (0)