diff --git a/chainflip-lp/Infrastructure/ToHexNumericExtension.cs b/chainflip-lp/Infrastructure/ToHexNumericExtension.cs new file mode 100644 index 0000000..c524a84 --- /dev/null +++ b/chainflip-lp/Infrastructure/ToHexNumericExtension.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/chainflip-lp/Model/OrderManager/PlaceBuyOrders.cs b/chainflip-lp/Model/OrderManager/PlaceBuyOrders.cs index e7bdb36..5365bb5 100644 --- a/chainflip-lp/Model/OrderManager/PlaceBuyOrders.cs +++ b/chainflip-lp/Model/OrderManager/PlaceBuyOrders.cs @@ -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(); foreach (var pool in _configuration.Pools) { var expectedPoolSlice = totalBalance / 100 * pool.Slice.Value; @@ -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 { diff --git a/chainflip-lp/chainflip-lp.csproj b/chainflip-lp/chainflip-lp.csproj index a5afad8..cd8b89e 100644 --- a/chainflip-lp/chainflip-lp.csproj +++ b/chainflip-lp/chainflip-lp.csproj @@ -63,6 +63,8 @@ + +