Skip to content

Commit 82b1c5a

Browse files
Add and send PathFindCloseRequest
1 parent 01a67bb commit 82b1c5a

File tree

5 files changed

+105
-27
lines changed

5 files changed

+105
-27
lines changed

src/Services/XummOrderService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public async Task<string> GetRefundRedirectUrlAsync(Guid orderGuid, decimal amou
188188
Flags = XrplPaymentFlags.tfNoDirectRipple
189189
};
190190

191-
var pathFindRequest = new PathFindRequest
191+
var pathFindRequest = new PathFindCreateRequest
192192
{
193193
SourceAccount = _xummPaymentSettings.XrplAddress,
194194
DestinationAccount = paymentPayload.Response.Account

src/WebSocket/IXrplWebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace Nop.Plugin.Payments.Xumm.WebSocket;
88
public interface IXrplWebSocket
99
{
1010
Task<List<AccountTrustLine>> GetAccountTrustLines(string account, bool throwError = false);
11-
Task<(decimal?,XrplPaymentPathSpecification[][]?)> GetDestinationAmountAndPathsAsync(PathFindRequest pathFindRequest, bool hasCounterParty);
11+
Task<(decimal?,XrplPaymentPathSpecification[][]?)> GetDestinationAmountAndPathsAsync(PathFindCreateRequest pathFindRequest, bool hasCounterParty);
1212
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Nop.Plugin.Payments.Xumm.WebSocket.Models;
2+
3+
public class PathFindCloseRequest : BaseRequest
4+
{
5+
public PathFindCloseRequest() : base("path_find")
6+
{
7+
SubCommand = "close";
8+
}
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Globalization;
2+
using System.Text.Json.Serialization;
3+
using XUMM.NET.SDK.Extensions;
4+
using XUMM.NET.SDK.Models.Payload.XRPL;
5+
6+
namespace Nop.Plugin.Payments.Xumm.WebSocket.Models;
7+
8+
public class PathFindCreateRequest : BaseRequest
9+
{
10+
public PathFindCreateRequest() : base("path_find")
11+
{
12+
SubCommand = "create";
13+
}
14+
15+
/// <summary>
16+
///
17+
/// </summary>
18+
[JsonPropertyName("source_account")]
19+
public string SourceAccount { get; set; }
20+
21+
/// <summary>
22+
///
23+
/// </summary>
24+
[JsonPropertyName("destination_account")]
25+
public string DestinationAccount { get; set; }
26+
27+
/// <summary>
28+
///
29+
/// </summary>
30+
[JsonPropertyName("destination_amount")]
31+
public object? DestinationAmount { get; set; }
32+
33+
[JsonPropertyName("send_max")]
34+
public object? SendMax { get; set; }
35+
36+
public void SetDestinationToXrp()
37+
{
38+
DestinationAmount = "-1";
39+
}
40+
41+
public void SetDestinationAmountToCounterParty(string currency, string issuer)
42+
{
43+
DestinationAmount = new XrplTransactionCurrencyAmount
44+
{
45+
Currency = currency,
46+
Value = "-1",
47+
Issuer = issuer
48+
};
49+
}
50+
public void SetSendMaxAmount(decimal amount)
51+
{
52+
SendMax = amount.XrpToDropsString();
53+
}
54+
55+
public void SetSendMaxAmount(string currency, decimal amount, string issuer)
56+
{
57+
SendMax = new XrplTransactionCurrencyAmount
58+
{
59+
Currency = currency,
60+
Value = amount.ToString(CultureInfo.InvariantCulture),
61+
Issuer = issuer
62+
};
63+
}
64+
}

src/WebSocket/XrplWebSocket.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public XrplWebSocket(ILogger logger)
4444

4545
#region Methods
4646

47-
public async Task<(decimal?, XrplPaymentPathSpecification[][]?)> GetDestinationAmountAndPathsAsync(PathFindRequest pathFindRequest, bool hasCounterParty)
47+
public async Task<(decimal?, XrplPaymentPathSpecification[][]?)> GetDestinationAmountAndPathsAsync(PathFindCreateRequest pathFindRequest, bool hasCounterParty)
4848
{
4949
decimal? destinationAmount = null;
5050
XrplPaymentPathSpecification[][]? paths = null;
@@ -84,7 +84,8 @@ public XrplWebSocket(ILogger logger)
8484

8585
var jsonElement = JsonSerializer.Deserialize<JsonElement>(ms);
8686

87-
87+
// Wait until we receive the best path for the current ledger
88+
// https://xrpl.org/path_find.html#asynchronous-follow-ups
8889
if (!jsonElement.TryGetProperty("full_reply", out var fullReplyElement) || !fullReplyElement.GetBoolean() ||
8990
!jsonElement.TryGetProperty("alternatives", out var alternativesElement))
9091
{
@@ -96,37 +97,41 @@ public XrplWebSocket(ILogger logger)
9697
if (alternative.TryGetProperty("paths_computed", out var pathsComputedElement))
9798
{
9899
paths = pathsComputedElement.Deserialize<XrplPaymentPathSpecification[][]>();
99-
100-
if (paths != null)
100+
101+
if (paths == null)
101102
{
102-
if (alternative.TryGetProperty("destination_amount", out var destinationAmountElement))
103-
{
104-
if (hasCounterParty)
105-
{
106-
var currencyAmount = destinationAmountElement.Deserialize<XrplTransactionCurrencyAmount>();
107-
destinationAmount = currencyAmount?.Value.XrplStringNumberToDecimal();
108-
}
109-
else
110-
{
111-
var currencyAmount = destinationAmountElement.GetString();
112-
if (currencyAmount != null)
113-
{
114-
destinationAmount = currencyAmount.XrpDropsToDecimal();
115-
}
116-
}
117-
}
103+
continue;
104+
}
118105

119-
if (destinationAmount == null)
106+
if (alternative.TryGetProperty("destination_amount", out var destinationAmountElement))
107+
{
108+
if (hasCounterParty)
120109
{
121-
// Couldn't determine the amount to deliver for the found paths
122-
paths = null;
110+
var currencyAmount = destinationAmountElement.Deserialize<XrplTransactionCurrencyAmount>();
111+
destinationAmount = currencyAmount?.Value.XrplStringNumberToDecimal();
123112
}
124113
else
125114
{
126-
source.Cancel();
127-
break;
115+
var currencyAmount = destinationAmountElement.GetString();
116+
if (currencyAmount != null)
117+
{
118+
destinationAmount = currencyAmount.XrpDropsToDecimal();
119+
}
128120
}
129121
}
122+
123+
if (destinationAmount == null)
124+
{
125+
// Couldn't determine the amount to deliver for the found paths
126+
paths = null;
127+
}
128+
else
129+
{
130+
await SendMessageAsync(webSocket, new PathFindCloseRequest());
131+
132+
source.Cancel();
133+
break;
134+
}
130135
}
131136
}
132137
}

0 commit comments

Comments
 (0)