Skip to content

Commit

Permalink
feat: convert deposit address
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Feb 2, 2024
1 parent 45d2bba commit 435b21d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
namespace SwappyBot.Commands.Swap
{
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

public class DepositAddress
{
public string Address { get; }

public double IssuedBlock { get; }

public double ChannelId { get; }

private DepositAddress(
AssetInfo asset,
DepositAddressResult result)
{
Address = asset.AddressConverter(result.Address);
IssuedBlock = result.IssuedBlock;
ChannelId = result.ChannelId;
}

public static async Task<DepositAddress?> GetDepositAddressAsync(
IHttpClientFactory httpClientFactory,
AssetInfo assetFrom,
AssetInfo assetTo,
string destinationAddress,
int commissionBps)
{
using var client = httpClientFactory.CreateClient("Deposit");

var response = await client.PostAsJsonAsync(
string.Empty,
new DepositAddressRequest(assetFrom, assetTo, destinationAddress, commissionBps));

var result = await response.Content.ReadFromJsonAsync<DepositAddressResponse>();

return new DepositAddress(
assetFrom,
result.Result);
}
}

public class DepositAddressResponse
{
[JsonPropertyName("result")]
public DepositAddressResult Result { get; set; }
Expand Down
20 changes: 2 additions & 18 deletions swappy-bot/Commands/Swap/Swap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ await Context.Channel.SendMessageAsync(

swapState.SwapAccepted = DateTimeOffset.UtcNow;

var deposit = await GetDepositChannelAsync(
var deposit = await DepositAddress.GetDepositAddressAsync(
_httpClientFactory,
assetFrom,
assetTo,
swapState.DestinationAddress,
Expand Down Expand Up @@ -856,23 +857,6 @@ private static MessageComponent BuildSelectedAssetSelect(
.WithSelectMenu(assetsSelect)
.Build();
}

private async Task<DepositAddressResult?> GetDepositChannelAsync(
AssetInfo assetFrom,
AssetInfo assetTo,
string destinationAddress,
int commissionBps)
{
using var client = _httpClientFactory.CreateClient("Deposit");

var response = await client.PostAsJsonAsync(
string.Empty,
new DepositAddressRequest(assetFrom, assetTo, destinationAddress, commissionBps));

var result = await response.Content.ReadFromJsonAsync<DepositAddress>();

return result.Result;
}
}

public class DummyModal : IModal
Expand Down

0 comments on commit 435b21d

Please sign in to comment.