This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from fracek/paths-endpoint
Add new paths endpoints
- Loading branch information
Showing
6 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
stellar-dotnet-sdk-test/requests/PathStrictReceiveRequestBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using stellar_dotnet_sdk; | ||
using stellar_dotnet_sdk_test.responses; | ||
|
||
namespace stellar_dotnet_sdk_test.requests | ||
{ | ||
[TestClass] | ||
public class PathStrictReceiveRequestBuilderTest | ||
{ | ||
[TestMethod] | ||
public void TestUriBuilder() | ||
{ | ||
using (var server = new Server("https://horizon-testnet.stellar.org")) | ||
{ | ||
var destinationAsset = | ||
Asset.CreateNonNativeAsset("USD", "GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V"); | ||
|
||
// Technically not a valid request since it contains both a source account and assets | ||
var req = server.PathStrictReceive | ||
.SourceAccount("GARSFJNXJIHO6ULUBK3DBYKVSIZE7SC72S5DYBCHU7DKL22UXKVD7MXP") | ||
.SourceAssets(new[] {new AssetTypeNative(), destinationAsset}) | ||
.DestinationAccount("GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V") | ||
.DestinationAsset(destinationAsset) | ||
.DestinationAmount("10.1"); | ||
|
||
Assert.AreEqual("https://horizon-testnet.stellar.org/path/strict-receive?" + | ||
"source_account=GARSFJNXJIHO6ULUBK3DBYKVSIZE7SC72S5DYBCHU7DKL22UXKVD7MXP&" + | ||
"source_assets=native,USD:GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V&" + | ||
"destination_account=GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V&" + | ||
"destination_asset_type=credit_alphanum4&" + | ||
"destination_asset_code=USD&" + | ||
"destination_asset_issuer=GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V&" + | ||
"destination_amount=10.1", | ||
req.BuildUri().ToString()); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestExecute() | ||
{ | ||
var jsonResponse = File.ReadAllText(Path.Combine("testdata", "pathsPage.json")); | ||
var fakeHttpClient = FakeHttpClient.CreateFakeHttpClient(jsonResponse); | ||
|
||
using (var server = new Server("https://horizon-testnet.stellar.org", fakeHttpClient)) | ||
{ | ||
var destinationAsset = | ||
Asset.CreateNonNativeAsset("USD", "GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V"); | ||
|
||
var assets = await server.PathStrictReceive | ||
.SourceAccount("GARSFJNXJIHO6ULUBK3DBYKVSIZE7SC72S5DYBCHU7DKL22UXKVD7MXP") | ||
.DestinationAccount("GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V") | ||
.DestinationAsset(destinationAsset) | ||
.DestinationAmount("10.1") | ||
.Execute(); | ||
|
||
PathsPageDeserializerTest.AssertTestData(assets); | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
stellar-dotnet-sdk-test/requests/PathStrictSendRequestBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using stellar_dotnet_sdk; | ||
using stellar_dotnet_sdk_test.responses; | ||
|
||
namespace stellar_dotnet_sdk_test.requests | ||
{ | ||
[TestClass] | ||
public class PathStrictSendRequestBuilderTest | ||
{ | ||
[TestMethod] | ||
public void TestUriBuilder() | ||
{ | ||
using (var server = new Server("https://horizon-testnet.stellar.org")) | ||
{ | ||
var sourceAsset = | ||
Asset.CreateNonNativeAsset("USD", "GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V"); | ||
|
||
// Technically not a valid request since it contains both a destination account and assets | ||
var req = server.PathStrictSend | ||
.SourceAmount("10.1") | ||
.SourceAsset(sourceAsset) | ||
.DestinationAccount("GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V") | ||
.DestinationAssets(new [] {new AssetTypeNative(), sourceAsset}); | ||
|
||
Assert.AreEqual("https://horizon-testnet.stellar.org/path/strict-send?" + | ||
"source_amount=10.1&" + | ||
"source_asset_type=credit_alphanum4&" + | ||
"source_asset_code=USD&" + | ||
"source_asset_issuer=GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V&" + | ||
"destination_account=GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V&" + | ||
"destination_assets=native,USD:GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V", | ||
req.BuildUri().ToString()); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestExecute() | ||
{ | ||
var jsonResponse = File.ReadAllText(Path.Combine("testdata", "pathsPage.json")); | ||
var fakeHttpClient = FakeHttpClient.CreateFakeHttpClient(jsonResponse); | ||
|
||
using (var server = new Server("https://horizon-testnet.stellar.org", fakeHttpClient)) | ||
{ | ||
var sourceAsset = | ||
Asset.CreateNonNativeAsset("USD", "GAEDTJ4PPEFVW5XV2S7LUXBEHNQMX5Q2GM562RJGOQG7GVCE5H3HIB4V"); | ||
|
||
var assets = await server.PathStrictSend | ||
.SourceAmount("10.1") | ||
.SourceAsset(sourceAsset) | ||
.DestinationAssets(new [] {new AssetTypeNative(), sourceAsset}) | ||
.Execute(); | ||
|
||
PathsPageDeserializerTest.AssertTestData(assets); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace stellar_dotnet_sdk | ||
{ | ||
public static class AssetExtensions | ||
{ | ||
/// <summary> | ||
/// Encode the asset to a string used in query parameters. The native assets is encoded as "native", while | ||
/// credit assets are encoded as "CODE:ISSUER". | ||
/// </summary> | ||
/// <param name="asset">The asset</param> | ||
/// <returns></returns> | ||
public static string ToQueryParameterEncodedString(this Asset asset) | ||
{ | ||
if (asset is AssetTypeCreditAlphaNum) | ||
{ | ||
var creditAsset = (AssetTypeCreditAlphaNum) asset; | ||
return $"{creditAsset.Code}:{creditAsset.Issuer}"; | ||
} | ||
return asset.GetType(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
stellar-dotnet-sdk/requests/PathStrictReceiveRequestBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using stellar_dotnet_sdk.responses; | ||
|
||
namespace stellar_dotnet_sdk.requests | ||
{ | ||
public class | ||
PathStrictReceiveRequestBuilder : RequestBuilderExecutePageable<PathStrictReceiveRequestBuilder, PathResponse> | ||
{ | ||
public PathStrictReceiveRequestBuilder(Uri serverUri, HttpClient httpClient) | ||
: base(serverUri, "path/strict-receive", httpClient) | ||
{ | ||
} | ||
|
||
public PathStrictReceiveRequestBuilder SourceAccount(string account) | ||
{ | ||
UriBuilder.SetQueryParam("source_account", account); | ||
return this; | ||
} | ||
|
||
public PathStrictReceiveRequestBuilder DestinationAccount(string account) | ||
{ | ||
UriBuilder.SetQueryParam("destination_account", account); | ||
return this; | ||
} | ||
|
||
public PathStrictReceiveRequestBuilder DestinationAmount(string amount) | ||
{ | ||
UriBuilder.SetQueryParam("destination_amount", amount); | ||
return this; | ||
} | ||
|
||
public PathStrictReceiveRequestBuilder DestinationAsset(Asset asset) | ||
{ | ||
UriBuilder.SetQueryParam("destination_asset_type", asset.GetType()); | ||
|
||
if (asset is AssetTypeCreditAlphaNum) | ||
{ | ||
AssetTypeCreditAlphaNum creditAlphaNumAsset = (AssetTypeCreditAlphaNum) asset; | ||
UriBuilder.SetQueryParam("destination_asset_code", creditAlphaNumAsset.Code); | ||
UriBuilder.SetQueryParam("destination_asset_issuer", creditAlphaNumAsset.Issuer); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public PathStrictReceiveRequestBuilder SourceAssets(IEnumerable<Asset> sources) | ||
{ | ||
var encodedAssets = sources.Select(a => a.ToQueryParameterEncodedString()); | ||
UriBuilder.SetQueryParam("source_assets", String.Join(",", encodedAssets)); | ||
return this; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
stellar-dotnet-sdk/requests/PathStrictSendRequestBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using stellar_dotnet_sdk.responses; | ||
|
||
namespace stellar_dotnet_sdk.requests | ||
{ | ||
public class | ||
PathStrictSendRequestBuilder : RequestBuilderExecutePageable<PathStrictSendRequestBuilder, PathResponse> | ||
{ | ||
public PathStrictSendRequestBuilder(Uri serverUri, HttpClient httpClient) | ||
: base(serverUri, "path/strict-send", httpClient) | ||
{ | ||
} | ||
|
||
public PathStrictSendRequestBuilder DestinationAccount(string account) | ||
{ | ||
UriBuilder.SetQueryParam("destination_account", account); | ||
return this; | ||
} | ||
|
||
public PathStrictSendRequestBuilder SourceAmount(string amount) | ||
{ | ||
UriBuilder.SetQueryParam("source_amount", amount); | ||
return this; | ||
} | ||
|
||
public PathStrictSendRequestBuilder SourceAsset(Asset asset) | ||
{ | ||
UriBuilder.SetQueryParam("source_asset_type", asset.GetType()); | ||
|
||
if (asset is AssetTypeCreditAlphaNum) | ||
{ | ||
AssetTypeCreditAlphaNum creditAlphaNumAsset = (AssetTypeCreditAlphaNum) asset; | ||
UriBuilder.SetQueryParam("source_asset_code", creditAlphaNumAsset.Code); | ||
UriBuilder.SetQueryParam("source_asset_issuer", creditAlphaNumAsset.Issuer); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public PathStrictSendRequestBuilder DestinationAssets(IEnumerable<Asset> sources) | ||
{ | ||
var encodedAssets = sources.Select(a => a.ToQueryParameterEncodedString()); | ||
UriBuilder.SetQueryParam("destination_assets", String.Join(",", encodedAssets)); | ||
return this; | ||
} | ||
} | ||
} |