Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #178 from fracek/fix-177
Browse files Browse the repository at this point in the history
Deserialize ManageBuyOffer operation response
  • Loading branch information
elucidsoft authored Jun 22, 2019
2 parents 98aa13b + 3089bfe commit c734042
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 25 deletions.
46 changes: 41 additions & 5 deletions stellar-dotnet-sdk-test/responses/OperationDeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void AssertPaymentOperationTestData(PaymentOperationResponse opera
Assert.AreEqual(operation.To, "GDWNY2POLGK65VVKIH5KQSH7VWLKRTQ5M6ADLJAYC2UEHEBEARCZJWWI");
Assert.AreEqual(operation.Amount, "100.0");
Assert.AreEqual(operation.Asset, new AssetTypeNative());

Assert.IsFalse(operation.TransactionSuccessful);
}

Expand Down Expand Up @@ -312,12 +312,48 @@ public void TestSerializeDeserializeManageOfferOperation()
private static void AssertManageOfferData(OperationResponse instance)
{
//There is a JsonConverter called OperationDeserializer that instantiates the type based on the json type_i element...
Assert.IsTrue(instance is ManageOfferOperationResponse);
var operation = (ManageOfferOperationResponse) instance;
Assert.IsTrue(instance is ManageSellOfferOperationResponse);
var operation = (ManageSellOfferOperationResponse) instance;

Assert.AreEqual(operation.OfferId, 96052902);
Assert.AreEqual(operation.Amount, "243.7500000");
Assert.AreEqual(operation.Price, "8.0850240");
Assert.AreEqual(operation.SellingAsset,
Asset.CreateNonNativeAsset("USD", "GDSRCV5VTM3U7Y3L6DFRP3PEGBNQMGOWSRTGSBWX6Z3H6C7JHRI4XFJP"));
Assert.AreEqual(operation.BuyingAsset, new AssetTypeNative());
}

[TestMethod]
public void TestDeserializeManageBuyOfferOperation()
{
var json = File.ReadAllText(Path.Combine("testdata", "operationManageBuyOffer.json"));
var instance = JsonSingleton.GetInstance<OperationResponse>(json);

AssertManageBuyOfferData(instance);
}

[TestMethod]
public void TestSerializeDeserializeManageBuyOfferOperation()
{
var json = File.ReadAllText(Path.Combine("testdata", "operationManageBuyOffer.json"));
var instance = JsonSingleton.GetInstance<OperationResponse>(json);
var serialized = JsonConvert.SerializeObject(instance);
var back = JsonConvert.DeserializeObject<OperationResponse>(serialized);

AssertManageBuyOfferData(back);
}

private static void AssertManageBuyOfferData(OperationResponse instance)
{
//There is a JsonConverter called OperationDeserializer that instantiates the type based on the json type_i element...
Assert.IsTrue(instance is ManageBuyOfferOperationResponse);
var operation = (ManageBuyOfferOperationResponse) instance;

Assert.AreEqual(operation.OfferId, 0);
Assert.AreEqual(operation.Amount, "100.0");
Assert.AreEqual(operation.BuyingAsset, Asset.CreateNonNativeAsset("CNY", "GAZWSWPDQTBHFIPBY4FEDFW2J6E2LE7SZHJWGDZO6Q63W7DBSRICO2KN"));
Assert.AreEqual(operation.Amount, "50000.0000000");
Assert.AreEqual(operation.Price, "0.0463000");
Assert.AreEqual(operation.BuyingAsset,
Asset.CreateNonNativeAsset("RMT", "GDEGOXPCHXWFYY234D2YZSPEJ24BX42ESJNVHY5H7TWWQSYRN5ZKZE3N"));
Assert.AreEqual(operation.SellingAsset, new AssetTypeNative());
}

Expand Down
3 changes: 3 additions & 0 deletions stellar-dotnet-sdk-test/stellar-dotnet-sdk-test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,8 @@
<None Update="testdata\transactionTransactionWithoutMemo.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="testdata\operationManageBuyOffer.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
38 changes: 38 additions & 0 deletions stellar-dotnet-sdk-test/testdata/operationManageBuyOffer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"_links": {
"self": {
"href": "https://horizon.stellar.org/operations/105020235379187713"
},
"transaction": {
"href": "https://horizon.stellar.org/transactions/35b08c8e7fc739ab07fd8eeb9564c59b4f1e85099aa2d3b1d37ed86a6cb5c438"
},
"effects": {
"href": "https://horizon.stellar.org/operations/105020235379187713/effects"
},
"succeeds": {
"href": "https://horizon.stellar.org/effects?order=desc&cursor=105020235379187713"
},
"precedes": {
"href": "https://horizon.stellar.org/effects?order=asc&cursor=105020235379187713"
}
},
"id": "105020235379187713",
"paging_token": "105020235379187713",
"transaction_successful": true,
"source_account": "GDID4M4PAN3C2JO4PJPCZ4Z6PSYUTW2I32E5EBORECQYIJDGTIZO3N5D",
"type": "manage_buy_offer",
"type_i": 12,
"created_at": "2019-06-21T08:24:48Z",
"transaction_hash": "35b08c8e7fc739ab07fd8eeb9564c59b4f1e85099aa2d3b1d37ed86a6cb5c438",
"amount": "50000.0000000",
"price": "0.0463000",
"price_r": {
"n": 463,
"d": 10000
},
"buying_asset_type": "credit_alphanum4",
"buying_asset_code": "RMT",
"buying_asset_issuer": "GDEGOXPCHXWFYY234D2YZSPEJ24BX42ESJNVHY5H7TWWQSYRN5ZKZE3N",
"selling_asset_type": "native",
"offer_id": 0
}
38 changes: 23 additions & 15 deletions stellar-dotnet-sdk-test/testdata/operationManageOffer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
{
"_links": {
"self": {
"href": "http://horizon-testnet.stellar.org/operations/3320426331639809"
"href": "https://horizon.stellar.org/operations/105020235379122177"
},
"transaction": {
"href": "http://horizon-testnet.stellar.org/transactions/1f8fc03b26110e917d124381645d7dcf85927f17e46d8390d254a0bd99cfb0ad"
"href": "https://horizon.stellar.org/transactions/a76f49631300ec2949716d521da8791fce7db59639b1608a5fcf4e8aff520464"
},
"effects": {
"href": "http://horizon-testnet.stellar.org/operations/3320426331639809/effects"
"href": "https://horizon.stellar.org/operations/105020235379122177/effects"
},
"succeeds": {
"href": "http://horizon-testnet.stellar.org/effects?order=desc&cursor=3320426331639809"
"href": "https://horizon.stellar.org/effects?order=desc&cursor=105020235379122177"
},
"precedes": {
"href": "http://horizon-testnet.stellar.org/effects?order=asc&cursor=3320426331639809"
"href": "https://horizon.stellar.org/effects?order=asc&cursor=105020235379122177"
}
},
"id": "3320426331639809",
"paging_token": "3320426331639809",
"source_account": "GCR6QXX7IRIJVIM5WA5ASQ6MWDOEJNBW3V6RTC5NJXEMOLVTUVKZ725X",
"id": "105020235379122177",
"paging_token": "105020235379122177",
"transaction_successful": true,
"source_account": "GBX5SNYZYZJQ2JKO43QI3I6K3QGNA4ARRIUSZ2EMIGXLELO3UZG4APNR",
"type": "manage_offer",
"type_i": 3,
"offer_id": 0,
"amount": "100.0",
"buying_asset_type": "credit_alphanum4",
"buying_asset_code": "CNY",
"buying_asset_issuer": "GAZWSWPDQTBHFIPBY4FEDFW2J6E2LE7SZHJWGDZO6Q63W7DBSRICO2KN",
"selling_asset_type": "native"
}
"created_at": "2019-06-21T08:24:48Z",
"transaction_hash": "a76f49631300ec2949716d521da8791fce7db59639b1608a5fcf4e8aff520464",
"amount": "243.7500000",
"price": "8.0850240",
"price_r": {
"n": 5054660,
"d": 625188
},
"buying_asset_type": "native",
"selling_asset_type": "credit_alphanum4",
"selling_asset_code": "USD",
"selling_asset_issuer": "GDSRCV5VTM3U7Y3L6DFRP3PEGBNQMGOWSRTGSBWX6Z3H6C7JHRI4XFJP",
"offer_id": 96052902
}
4 changes: 3 additions & 1 deletion stellar-dotnet-sdk/responses/OperationDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static OperationResponse CreateResponse(int type)
case 2:
return new PathPaymentOperationResponse();
case 3:
return new ManageOfferOperationResponse();
return new ManageSellOfferOperationResponse();
case 4:
return new CreatePassiveOfferOperationResponse();
case 5:
Expand All @@ -53,6 +53,8 @@ private static OperationResponse CreateResponse(int type)
return new ManageDataOperationResponse();
case 11:
return new BumpSequenceOperationResponse();
case 12:
return new ManageBuyOfferOperationResponse();
default:
throw new JsonSerializationException($"Invalid operation 'type_i'='{type}'");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace stellar_dotnet_sdk.responses.operations
{
public class ManageBuyOfferOperationResponse : ManageOfferOperationResponse
{
public override int TypeId => 12;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ public ManageOfferOperationResponse(int offerId, string amount, string price, st
SellingAssetIssuer = sellingAssetIssuer;
}

public override int TypeId => 3;

[JsonProperty(PropertyName = "offer_id")]
public int OfferId { get; private set; }

[JsonProperty(PropertyName = "amount")]
public string Amount { get; private set; }

// Price is not implemented yet in horizon

[JsonProperty(PropertyName = "price")]
public string Price { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace stellar_dotnet_sdk.responses.operations
{
public class ManageSellOfferOperationResponse : ManageOfferOperationResponse
{
public override int TypeId => 3;
}
}

0 comments on commit c734042

Please sign in to comment.