Skip to content

Commit

Permalink
feat: try to respect slices
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Sep 27, 2024
1 parent 9cf91c8 commit 7b3176b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
13 changes: 13 additions & 0 deletions chainflip-lp/Infrastructure/ToHexNumericExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ChainflipLp.Infrastructure
{
using Nethereum.Hex.HexTypes;

public static class ToHexNumericExtension
{
public static string ToHexNumeric(this double amount)
{
var nativeAmount = Nethereum.Util.UnitConversion.Convert.ToWei(amount, 6);
return new HexBigInteger(nativeAmount).HexValue;
}
}
}
86 changes: 62 additions & 24 deletions chainflip-lp/Model/OrderManager/PlaceBuyOrders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ private async Task PlaceBuyOrders(
{
// We have USDC, figure out where to place a buy order
// Check each pool their slice compared to the total balance
// Grab the pool with the biggest difference and throw the balance in there
// Fill up the pools where possible until the USDC runs out

var poolToUse = _configuration.Pools[0];
var maxPoolDifference = 0d;
var poolDifferences = new Dictionary<PoolConfiguration, double>();
foreach (var pool in _configuration.Pools)
{
var expectedPoolSlice = totalBalance / 100 * pool.Slice.Value;
Expand All @@ -69,30 +68,69 @@ private async Task PlaceBuyOrders(
currentPoolSlice.ToString(Constants.DollarString),
poolDifference.ToString(Constants.DollarString));

if (poolDifference <= maxPoolDifference)
continue;

maxPoolDifference = poolDifference;
poolToUse = pool;
poolDifferences.Add(pool, poolDifference);
}

_logger.LogWarning(
"Placing {Asset}/{Chain} buy order for ${Balance} USDC",
poolToUse.Asset,
poolToUse.Chain,
usdcBalance.ToNumeric().ToString(Constants.DollarString));

await PlaceBuyOrder(
poolToUse,
usdcBalance,
client,
cancellationToken);
var poolOrder = poolDifferences
.OrderByDescending(x => x.Value)
.Select(x => new
{
Pool = x.Key,
Difference = x.Value
})
.ToList();

var remainingBalance = usdcBalance.ToNumeric();
foreach (var pool in poolOrder)
{
if (remainingBalance <= 0)
continue;

if (pool.Difference < remainingBalance)
{
// Fill up the pool, subtract from remainingBalance and carry on
_logger.LogWarning(
"Placing {Asset}/{Chain} buy order for ${Balance} USDC",
pool.Pool.Asset,
pool.Pool.Chain,
pool.Difference.ToString(Constants.DollarString));

await NotifyTelegram(
telegramClient,
$"Placed {poolToUse.Asset}/{poolToUse.Chain} buy order for {usdcBalance.ToNumeric().ToString(Constants.DollarString)} USDC",
true,
cancellationToken);
await PlaceBuyOrder(
pool.Pool,
pool.Difference.ToHexNumeric(),
client,
cancellationToken);

await NotifyTelegram(
telegramClient,
$"Placed {pool.Pool.Asset}/{pool.Pool.Chain} buy order for {pool.Difference.ToString(Constants.DollarString)} USDC",
true,
cancellationToken);

remainingBalance -= pool.Difference;
}
else
{
// Dump the rest of the balance in this pool
_logger.LogWarning(
"Placing {Asset}/{Chain} buy order for ${Balance} USDC",
pool.Pool.Asset,
pool.Pool.Chain,
remainingBalance.ToString(Constants.DollarString));

await PlaceBuyOrder(
pool.Pool,
remainingBalance.ToHexNumeric(),
client,
cancellationToken);

await NotifyTelegram(
telegramClient,
$"Placed {pool.Pool.Asset}/{pool.Pool.Chain} buy order for {remainingBalance.ToString(Constants.DollarString)} USDC",
true,
cancellationToken);
}
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions chainflip-lp/chainflip-lp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Nethereum.Hex" Version="4.25.0" />
<PackageReference Include="Nethereum.Util" Version="4.25.0" />
<PackageReference Include="Polly" Version="8.4.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.0" />
Expand Down

0 comments on commit 7b3176b

Please sign in to comment.