Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single offer endpoint #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions StellarDotnetSdk.Tests/Requests/OffersRequestBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public async Task TestOffersWithBuyingCreditAsset()
OfferPageDeserializerTest.AssertTestData(offerResponsePage);
}

[TestMethod]
public async Task TestSingleOffer()
{
using var server = await Utils.CreateTestServerWithJson("Responses/offer.json");
var offer = await server.Offers.Offer("GAHEAZQD6K7QBBPGLQZQOHCA5L3Z7ZNFBDU5XCCEI7QYVEFIRUF3IW27");
Assert.AreEqual("100.0000000", offer.Amount);
Assert.AreEqual("1", offer.PagingToken);
Assert.AreEqual(0, offer.Buying.CompareTo(new AssetTypeNative()));
Assert.AreEqual("GAHEAZQD6K7QBBPGLQZQOHCA5L3Z7ZNFBDU5XCCEI7QYVEFIRUF3IW27", offer.Seller);
Assert.AreEqual(0, offer.Selling.CompareTo(Asset.CreateNonNativeAsset("IOM", "GBL37ZFYU3FRBKTFO4YPIAELTO2PN6YEIF3ZS6FKOEAM5SOWALX5IP44")));
Assert.AreEqual("1", offer.Id);
Assert.AreEqual("https://horizon-testnet.stellar.org/offers/1", offer.Links.Self.Href);
Assert.AreEqual("https://horizon-testnet.stellar.org/accounts/GAHEAZQD6K7QBBPGLQZQOHCA5L3Z7ZNFBDU5XCCEI7QYVEFIRUF3IW27", offer.Links.OfferMaker.Href);
Assert.AreEqual(new Price(1, 2), offer.PriceRatio);
Assert.AreEqual("0.5000000", offer.Price);
Assert.AreEqual(380, offer.LastModifiedLedger);
Assert.AreEqual("2024-12-10T17:50:08Z", offer.LastModifiedTime);

}
private async Task<Server> CreateServer()
{
return await Utils.CreateTestServerWithJson("Responses/offerPage.json");
Expand Down
3 changes: 3 additions & 0 deletions StellarDotnetSdk.Tests/StellarDotnetSdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@
<None Update="TestData\Responses\transactionFeeBump.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\Responses\offer.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Compile Remove="KeyPairBIP39Tests.cs"/>
Expand Down
29 changes: 29 additions & 0 deletions StellarDotnetSdk.Tests/TestData/Responses/offer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/offers/1"
},
"offer_maker": {
"href": "https://horizon-testnet.stellar.org/accounts/GAHEAZQD6K7QBBPGLQZQOHCA5L3Z7ZNFBDU5XCCEI7QYVEFIRUF3IW27"
}
},
"id": "1",
"paging_token": "1",
"seller": "GAHEAZQD6K7QBBPGLQZQOHCA5L3Z7ZNFBDU5XCCEI7QYVEFIRUF3IW27",
"selling": {
"asset_type": "credit_alphanum4",
"asset_code": "IOM",
"asset_issuer": "GBL37ZFYU3FRBKTFO4YPIAELTO2PN6YEIF3ZS6FKOEAM5SOWALX5IP44"
},
"buying": {
"asset_type": "native"
},
"amount": "100.0000000",
"price_r": {
"n": 1,
"d": 2
},
"price": "0.5000000",
"last_modified_ledger": 380,
"last_modified_time": "2024-12-10T17:50:08Z"
}
28 changes: 24 additions & 4 deletions StellarDotnetSdk/Requests/OffersRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using StellarDotnetSdk.Assets;
using StellarDotnetSdk.Responses;

Expand All @@ -13,6 +14,17 @@ public OffersRequestBuilder(Uri serverUri, HttpClient httpClient) :
{
}

/// <summary>
/// Requests specific uri and returns OfferResponse
/// </summary>
public async Task<OfferResponse> Offer(Uri uri)
{
var responseHandler = new ResponseHandler<OfferResponse>();

var response = await HttpClient.GetAsync(uri);
return await responseHandler.HandleResponse(response);
}

/// <summary>
/// Builds request to GET /accounts/{account}/offers
/// See: https://www.stellar.org/developers/horizon/reference/offers-for-account.html
Expand All @@ -34,10 +46,7 @@ public OffersRequestBuilder ForAccount(string account)
/// <exception cref="ArgumentNullException"></exception>
public OffersRequestBuilder Offers(OffersRequestOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);

if (options.Seller != null)
{
Expand Down Expand Up @@ -102,6 +111,17 @@ public OffersRequestBuilder WithBuyingAsset(Asset buying)
return Offers(options => options.Buying = buying);
}

/// <summary>
/// Requests <code>GET /offers/{offerId}</code>
/// <a href="https://developers.stellar.org/docs/data/horizon/api-reference/get-offer-by-offer-id">Retrieve an Offer</a>
/// </summary>
/// <param name="offerId">ID of the offer to fetch.</param>
public async Task<OfferResponse> Offer(string offerId)
{
SetSegments("offers", offerId);
return await Offer(BuildUri());
}

private void AddAssetFilterQueryParam(string side, Asset asset)
{
UriBuilder.SetQueryParam($"{side}_asset_type", asset.Type);
Expand Down
Loading