Skip to content

Commit 29d66e2

Browse files
🐛 Improvement & Bug Fixes (#25)
* 🐛 Improvement & Bug Fixes * 🐛 Fix open/increase/decrease/collect/close with closed atas Fix open/increase/decrease/collect/close with closed atas and added tests for native mint * 🐛 Fix not deterministic bug with liquidity * 🐛 Add fee payer * 🐛 Remove test delay * 🐛 Fix ATA & Set release to 0.1 * 🐛 Fix race condition in unit tests
1 parent 093e620 commit 29d66e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2762
-722
lines changed

.config/dotnet-tools.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
"commands": [
1414
"dotnet-format"
1515
]
16+
},
17+
"dotnet-reportgenerator-globaltool": {
18+
"version": "5.1.15",
19+
"commands": [
20+
"reportgenerator"
21+
]
1622
}
1723
}
1824
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,4 @@ riderModule.iml
615615
/_ReSharper.Caches/
616616
_site/
617617
api/
618+
test-ledger/

src/Solana.Unity.Dex/IDex.cs

Lines changed: 130 additions & 75 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Numerics;
2+
3+
namespace Solana.Unity.Dex.Math;
4+
5+
public class DecimalUtil
6+
{
7+
public static double FromUlong(ulong value, int shift = 0)
8+
{
9+
return value / System.Math.Pow(10, shift);
10+
}
11+
12+
public static double FromBigInteger(BigInteger value, int shift = 0)
13+
{
14+
return FromUlong((ulong)value, shift);
15+
}
16+
17+
public static ulong ToUlong(double value, int shift = 0)
18+
{
19+
return (ulong)(value * System.Math.Pow(10, shift));
20+
}
21+
22+
public static ulong ToUlong(float value, int shift = 0)
23+
{
24+
return ToUlong((double)value, shift);
25+
}
26+
}

src/Solana.Unity.Dex/Models/Pool.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Solana.Unity.Wallet;
2+
using System.Numerics;
3+
4+
namespace Solana.Unity.Dex.Models;
5+
6+
public class Pool
7+
{
8+
public PublicKey Address { get; set; }
9+
10+
public PublicKey TokenMintA { get; set; }
11+
public PublicKey TokenMintB { get; set; }
12+
13+
public BigInteger Liquidity { get; set; }
14+
public ushort Fee { get; set; }
15+
public ushort TickSpacing { get; set; }
16+
}

src/Solana.Unity.Dex/Orca/Math/SwapMath.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ bool aToB
127127
)
128128
{
129129
return (aToB == amountSpecifiedIsInput)
130-
? TokenMath.GetAmountDeltaB(currentSqrtPrice, targetSqrtPrice, currentLiquidity, amountSpecifiedIsInput)
131-
: TokenMath.GetAmountDeltaA(currentSqrtPrice, targetSqrtPrice, currentLiquidity, amountSpecifiedIsInput);
130+
? TokenMath.GetAmountDeltaB(currentSqrtPrice, targetSqrtPrice, currentLiquidity, !amountSpecifiedIsInput)
131+
: TokenMath.GetAmountDeltaA(currentSqrtPrice, targetSqrtPrice, currentLiquidity, !amountSpecifiedIsInput);
132132
}
133133
}
134134
}

src/Solana.Unity.Dex/Orca/Math/TokenMath.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ bool roundUp
2121
BigInteger numerator = currentLiquidity * sqrtPriceDiff << 64;
2222
BigInteger denominator = sqrtPriceLower * sqrtPriceUpper;
2323

24-
BigInteger quotient = numerator / (denominator);
25-
BigInteger remainder = numerator % (denominator);
24+
BigInteger quotient = numerator / denominator;
25+
BigInteger remainder = numerator % denominator;
2626

2727
var result = roundUp && remainder != 0 ? quotient + BigInteger.One : quotient;
2828

@@ -59,8 +59,8 @@ BigInteger sqrtPrice1
5959
)
6060
{
6161
return (sqrtPrice0 > sqrtPrice1) ?
62-
Tuple.Create<BigInteger, BigInteger>(sqrtPrice1, sqrtPrice0) :
63-
Tuple.Create<BigInteger, BigInteger>(sqrtPrice0, sqrtPrice1);
62+
Tuple.Create(sqrtPrice1, sqrtPrice0) :
63+
Tuple.Create(sqrtPrice0, sqrtPrice1);
6464
}
6565

6666
public static BigInteger GetNextSqrtPrice(
@@ -108,7 +108,7 @@ bool amountSpecifiedIsInput
108108

109109
BigInteger denominator = amountSpecifiedIsInput
110110
? currLiquidityShiftLeft + p
111-
: currLiquidityShiftLeft + p;
111+
: currLiquidityShiftLeft - p;
112112

113113
BigInteger price = BitMath.DivRoundUp(numerator, denominator);
114114

0 commit comments

Comments
 (0)