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 #336 from elucidsoft/fix-liquidity-pool-operations…
Browse files Browse the repository at this point in the history
…-responses

Fix for AMM Protocol
  • Loading branch information
elucidsoft committed Oct 16, 2021
2 parents a64fde2 + 918b597 commit 83d0623
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void TestSerializeDeserialize()
[TestMethod]
public void TestReserveEquality()
{
LiquidityPoolResponse.Reserve a = new LiquidityPoolResponse.Reserve("2000.0000000", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
LiquidityPoolResponse.Reserve b = new LiquidityPoolResponse.Reserve("2000.0000000", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
LiquidityPoolResponse.Reserve c = new LiquidityPoolResponse.Reserve("1000.0000005", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
LiquidityPoolResponse.Reserve d = new LiquidityPoolResponse.Reserve("2000.0000000", Asset.Create("EURT:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
Reserve a = new Reserve("2000.0000000", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
Reserve b = new Reserve("2000.0000000", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
Reserve c = new Reserve("1000.0000005", Asset.Create("PHP:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));
Reserve d = new Reserve("2000.0000000", Asset.Create("EURT:GAP5LETOV6YIE62YAM56STDANPRDO7ZFDBGSNHJQIYGGKSMOZAHOOS2S"));

Assert.AreEqual(a, b);
Assert.AreNotEqual(a, c);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations/1508315204960257"
},
"transaction": {
"href": "https://horizon-testnet.stellar.org/transactions/618930125efa20b6da220d8750c975580efeed18cf19a928500866fa78dd5d27"
},
"effects": {
"href": "https://horizon-testnet.stellar.org/operations/1508315204960257/effects"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=1508315204960257"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=1508315204960257"
}
},
"id": "1508315204960257",
"paging_token": "1508315204960257",
"transaction_successful": false,
"source_account": "GAIWHTRJ2ISBQRYSNRPAUMFXC66QIPS34R7YMTES7QYG6DYF25IRTSER",
"type": "liquidity_pool_deposit",
"type_i": 22,
"created_at": "2021-10-06T18:04:24Z",
"transaction_hash": "618930125efa20b6da220d8750c975580efeed18cf19a928500866fa78dd5d27",
"liquidity_pool_id": "b26c0d6545349ad7f44ba758b7c705459537201583f2e524635be04aff84bc69",
"reserves_max": [
{
"amount": "1000.0000000"
},
{
"amount": "1.0000000"
}
],
"min_price": "1.0000000",
"min_price_r": {
"n": 1,
"d": 1
},
"max_price": "100000000.0000000",
"max_price_r": {
"n": 100000000,
"d": 1
},
"reserves_deposited": [
{
"amount": "0.0000000"
},
{
"amount": "0.0000000"
}
],
"shares_received": "0.0000000"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using stellar_dotnet_sdk;
using stellar_dotnet_sdk.responses;
using stellar_dotnet_sdk.responses.operations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace stellar_dotnet_sdk_test.responses.operations
{
[TestClass]
public class LiquidityPoolDepositOperationResponseTest
{
[JsonProperty("liquidity_pool_id")]
public LiquidityPoolID LiquidityPoolID { get; set; }

[JsonProperty("reserves_max")]
public List<Reserve> ReservesMax { get; set; }

[JsonProperty("min_price")]
public string MinPrice { get; set; }

[JsonProperty("max_price")]
public string MaxPrice { get; set; }

[JsonProperty("reserves_deposited")]
public List<Reserve> ReservesDeposited { get; set; }

[JsonProperty("shares_received")]
public string SharesReceived { get; set; }

//Allow Trust
[TestMethod]
public void TestDeserialize()
{
var json = File.ReadAllText(Path.Combine("responses/operations/LiquidityPoolDepositOperationResponse", "Data.json"));
var instance = (LiquidityPoolDepositOperationResponse)JsonSingleton.GetInstance<OperationResponse>(json);

AssertData(instance);
}

[TestMethod]
public void TestSerializeDeserialize()
{
var json = File.ReadAllText(Path.Combine("responses/operations/LiquidityPoolDepositOperationResponse", "Data.json"));
var instance = JsonSingleton.GetInstance<OperationResponse>(json);
var serialized = JsonConvert.SerializeObject(instance);
var back = (LiquidityPoolDepositOperationResponse)JsonConvert.DeserializeObject<OperationResponse>(serialized);

AssertData(back);
}

public void AssertData(LiquidityPoolDepositOperationResponse instance)
{
Assert.AreEqual(new LiquidityPoolID("b26c0d6545349ad7f44ba758b7c705459537201583f2e524635be04aff84bc69"), instance.LiquidityPoolID);
Assert.AreEqual("1508315204960257", instance.PagingToken);

Assert.AreEqual("1.0000000", instance.MinPrice);
Assert.AreEqual("100000000.0000000", instance.MaxPrice);

Assert.AreEqual("1000.0000000", instance.ReservesMax[0].Amount);
Assert.AreEqual(null, instance.ReservesMax[0].Asset);

Assert.AreEqual("1.0000000", instance.ReservesMax[1].Amount);
Assert.AreEqual(null, instance.ReservesMax[1].Asset);

Assert.AreEqual("0.0000000", instance.ReservesDeposited[0].Amount);
Assert.AreEqual("0.0000000", instance.ReservesDeposited[1].Amount);

Assert.AreEqual("0.0000000", instance.SharesReceived);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations/1508641622462465"
},
"transaction": {
"href": "https://horizon-testnet.stellar.org/transactions/e8332b8dcada55e2801438c01d20f5abdeac60791260b24eb2f07be01cf57f2a"
},
"effects": {
"href": "https://horizon-testnet.stellar.org/operations/1508641622462465/effects"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=1508641622462465"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=1508641622462465"
}
},
"id": "1508641622462465",
"paging_token": "1508641622462465",
"transaction_successful": true,
"source_account": "GBCB56ASGYTRLYGIQPQMCR7HICUJYXXPTZAMD2CWG3UFB7JPD6YXC66Q",
"type": "liquidity_pool_withdraw",
"type_i": 23,
"created_at": "2021-10-06T18:10:59Z",
"transaction_hash": "e8332b8dcada55e2801438c01d20f5abdeac60791260b24eb2f07be01cf57f2a",
"liquidity_pool_id": "b26c0d6545349ad7f44ba758b7c705459537201583f2e524635be04aff84bc69",
"reserves_min": [
{
"asset": "native",
"amount": "0.0000000"
},
{
"asset": "USDC:GAKMOAANQHJKF5735OYVSQZL6KC3VMFL4LP4ZYY2LWK256TSUG45IEFB",
"amount": "0.0000000"
}
],
"shares": "1000.0000000",
"reserves_received": [
{
"asset": "native",
"amount": "1000.0000000"
},
{
"asset": "USDC:GAKMOAANQHJKF5735OYVSQZL6KC3VMFL4LP4ZYY2LWK256TSUG45IEFB",
"amount": "1000.0000000"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using stellar_dotnet_sdk;
using stellar_dotnet_sdk.responses;
using stellar_dotnet_sdk.responses.operations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace stellar_dotnet_sdk_test.responses.operations
{
[TestClass]
public class LiquidityPoolWithdrawOperationResponseTest
{
[JsonProperty("liquidity_pool_id")]
public LiquidityPoolID LiquidityPoolID { get; set; }

[JsonProperty("reserves_max")]
public List<Reserve> ReservesMax { get; set; }

[JsonProperty("min_price")]
public string MinPrice { get; set; }

[JsonProperty("max_price")]
public string MaxPrice { get; set; }

[JsonProperty("reserves_deposited")]
public List<Reserve> ReservesDeposited { get; set; }

[JsonProperty("shares_received")]
public string SharesReceived { get; set; }

//Allow Trust
[TestMethod]
public void TestDeserialize()
{
var json = File.ReadAllText(Path.Combine("responses/operations/LiquidityPoolWithdrawOperationResponse", "Data.json"));
var instance = (LiquidityPoolWithdrawOperationResponse)JsonSingleton.GetInstance<OperationResponse>(json);

AssertData(instance);
}

[TestMethod]
public void TestSerializeDeserialize()
{
var json = File.ReadAllText(Path.Combine("responses/operations/LiquidityPoolWithdrawOperationResponse", "Data.json"));
var instance = JsonSingleton.GetInstance<OperationResponse>(json);
var serialized = JsonConvert.SerializeObject(instance);
var back = (LiquidityPoolWithdrawOperationResponse)JsonConvert.DeserializeObject<OperationResponse>(serialized);

AssertData(back);
}

public void AssertData(LiquidityPoolWithdrawOperationResponse instance)
{
Assert.AreEqual(new LiquidityPoolID("b26c0d6545349ad7f44ba758b7c705459537201583f2e524635be04aff84bc69"), instance.LiquidityPoolID);
Assert.AreEqual("1508641622462465", instance.PagingToken);

Assert.AreEqual("0.0000000", instance.ReservesMin[0].Amount);
Assert.AreEqual("native", instance.ReservesMin[0].Asset.CanonicalName());

Assert.AreEqual("0.0000000", instance.ReservesMin[1].Amount);
Assert.AreEqual("USDC:GAKMOAANQHJKF5735OYVSQZL6KC3VMFL4LP4ZYY2LWK256TSUG45IEFB", instance.ReservesMin[1].Asset.CanonicalName());

Assert.AreEqual("1000.0000000", instance.Shares);

Assert.AreEqual("1000.0000000", instance.ReservesReceived[0].Amount);
Assert.AreEqual("native", instance.ReservesReceived[0].Asset.CanonicalName());

Assert.AreEqual("1000.0000000", instance.ReservesReceived[1].Amount);
Assert.AreEqual("USDC:GAKMOAANQHJKF5735OYVSQZL6KC3VMFL4LP4ZYY2LWK256TSUG45IEFB", instance.ReservesReceived[1].Asset.CanonicalName());
}
}
}
6 changes: 6 additions & 0 deletions stellar-dotnet-sdk-test/stellar-dotnet-sdk-test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<None Update="responses\LiquidityPoolResponse\Data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="responses\operations\LiquidityPoolDepositOperationResponse\Data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="responses\operations\LiquidityPoolWithdrawOperationResponse\Data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="testdata\account.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
45 changes: 45 additions & 0 deletions stellar-dotnet-sdk/Reserve.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Newtonsoft.Json;
using stellar_dotnet_sdk.converters;
using System;
using System.Collections.Generic;
using System.Text;

namespace stellar_dotnet_sdk
{
[JsonConverter(typeof(ReserveJsonConverter))]
public class Reserve
{
[JsonProperty(PropertyName = "amount")]
public string Amount { get; set; }

[JsonProperty(PropertyName = "asset")]
public Asset Asset { get; set; }

public Reserve() { }

public Reserve(string amount, Asset asset)
{
Amount = amount ?? throw new ArgumentNullException(nameof(amount), "amount cannot be null");
Asset = asset ?? throw new ArgumentNullException(nameof(amount), "asset cannot be null");
}

public override bool Equals(object obj)
{
if (!(obj is Reserve))
{
return false;
}

Reserve other = (Reserve)obj;
return Equals(Asset, other.Asset) && Equals(Amount, other.Amount);
}

public override int GetHashCode()
{
int hashCode = 1588693772;
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Amount);
hashCode = (hashCode * -1521134295) + EqualityComparer<Asset>.Default.GetHashCode(Asset);
return hashCode;
}
}
}

This file was deleted.

Loading

0 comments on commit 83d0623

Please sign in to comment.