Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
Challenge Task
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OST
BlCh
Challenge Task
Commits
2db0b7f4
Commit
2db0b7f4
authored
4 months ago
by
David Hintermann
Browse files
Options
Downloads
Patches
Plain Diff
feat(pool): implement rebalance swap
parent
9870a943
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!9
feat(pool): correct pool price
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Frontend/Functions/LiquidityFunction.cs
+9
-0
9 additions, 0 deletions
Frontend/Functions/LiquidityFunction.cs
Frontend/PoolV3Client.cs
+35
-21
35 additions, 21 deletions
Frontend/PoolV3Client.cs
with
44 additions
and
21 deletions
Frontend/Functions/LiquidityFunction.cs
0 → 100644
+
9
−
0
View file @
2db0b7f4
using
Nethereum.ABI.FunctionEncoding.Attributes
;
using
Nethereum.Contracts
;
namespace
Frontend.Functions
;
[
Function
(
"liquiditry"
,
"uint128"
)]
public
class
LiquidityFunction
:
FunctionMessage
{
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Frontend/PoolV3Client.cs
+
35
−
21
View file @
2db0b7f4
...
...
@@ -93,12 +93,16 @@ public class PoolV3Client
}
public
async
Task
<
decimal
>
GetPairRatio
()
{
return
(
decimal
)
SqrtPriceX96ToPrice
(
await
GetSqrtPriceX96
());
}
public
async
Task
<
BigInteger
>
GetSqrtPriceX96
()
{
var
handler
=
_web3
.
Eth
.
GetContractQueryHandler
<
Slot0Funtion
>();
var
func
=
new
Slot0Funtion
();
var
slot0Result
=
await
handler
.
QueryDeserializingToObjectAsync
<
Slot0OutputDTO
>(
func
,
_pool
.
Address
);
var
sqrtPriceX96
=
slot0Result
.
SqrtPriceX96
;
return
(
decimal
)
SqrtPriceX96ToPrice
(
sqrtPriceX96
);
return
slot0Result
.
SqrtPriceX96
;
}
public
double
SqrtPriceX96ToPrice
(
BigInteger
sqrtPriceX96
)
...
...
@@ -138,7 +142,7 @@ public class PoolV3Client
return
_token1Address
;
}
public
async
Task
<
uint
>
get
PoolFee
Tier
()
public
async
Task
<
uint
>
Query
PoolFee
()
{
if
(
null
==
_poolFeeTier
)
{
...
...
@@ -150,6 +154,12 @@ public class PoolV3Client
return
(
uint
)
_poolFeeTier
;
}
public
async
Task
<
BigInteger
>
QueryLiquidity
(){
var
handler
=
_web3
.
Eth
.
GetContractQueryHandler
<
LiquidityFunction
>();
var
func
=
new
LiquidityFunction
();
return
await
handler
.
QueryAsync
<
BigInteger
>(
_pool
.
Address
,
func
);
}
/*
this function should swap the tokens in the pool
...
...
@@ -181,7 +191,7 @@ public class PoolV3Client
{
TokenIn
=
fromTokenAddress
,
TokenOut
=
toTokenAddress
,
Fee
=
await
get
PoolFee
Tier
(),
Fee
=
await
Query
PoolFee
(),
Recipient
=
_account
.
Address
,
AmountIn
=
Web3
.
Convert
.
ToWei
(
amount
),
AmountOutMinimum
=
Web3
.
Convert
.
ToWei
(
amountOutMinimum
),
// Set your minimum amount out
...
...
@@ -201,26 +211,30 @@ public class PoolV3Client
/// This function should swap the tokens in the pool to create a desired quote
/// </summary>
/// <param name="desiredQuote">the ration between token1/token0</param>
/// <returns></returns>
/// <returns>
Quote after the swap
</returns>
public
async
Task
<
decimal
>
PerformSwapToCreatDesiredQuote
(
decimal
desiredQuote
)
{
var
currentQuote
=
await
GetPairRatio
();
throw
new
NotImplementedException
(
);
var
amount
=
0.0M
;
if
(
currentQuote
<
desiredQuote
)
{
// we sell some of token1 to buy token0
amount
=
(
desiredQuote
-
currentQuote
)
/
desiredQuote
;
throw
new
NotImplementedException
();
await
SwapAsync
(
Token1Address
,
Token0Address
,
amount
);
}
else
{
amount
=
1
;
throw
new
NotImplementedException
();
a
wait
SwapAsync
(
Token1Address
,
Token0Address
,
amount
);
var
sqrtPriceX96
=
await
GetSqrtPriceX96
();
var
desiredSqrtPriceX96
=
PriceToSqrtPriceX96
((
double
)
desiredQuote
);
string
[]
tokenAddresses
=[
Token0Address
,
Token1Address
]
;
var
fromTokenIndex
=
0
;
var
amount
=
0.0
;
var
liquidity
=
await
QueryLiquidity
();
if
(
desiredSqrtPriceX96
>
sqrtPriceX96
){
// we have to by token0 to increase demand and drive the price up
fromTokenIndex
=
1
;
// we want to swap from token1 to token0
// Taken from getNextSqrtPriceFromAmount1RoundingDown see https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/SqrtPriceMath.sol
amount
=(
double
)(
liquidity
*(
desiredSqrtPriceX96
-
sqrtPriceX96
));
}
else
{
// whe have to sell token0 to lower the price
// Taken from getNextSqrtPriceFromAmount0ReoundingUp see https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/SqrtPriceMath.sol
a
mount
=(
double
)(
liquidity
*(
sqrtPriceX96
-
desiredSqrtPriceX96
))/(
double
)(
desiredSqrtPriceX96
*
sqrtPriceX96
);
}
throw
new
NotImplementedException
();
amount
*=
1
+
(
int
)
await
QueryPoolFee
()/
1000.0
;
// add the fees
var
receipt
=
await
SwapAsync
(
tokenAddresses
[
fromTokenIndex
],
tokenAddresses
[(
fromTokenIndex
+
1
)%
2
],
Web3
.
Convert
.
FromWei
(
new
BigInteger
(
amount
)));
return
await
GetPairRatio
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment