Skip to content

Commit

Permalink
Merge pull request #3377 from Sifchain/feature/query-add-liquidity
Browse files Browse the repository at this point in the history
Add endpoint for querying estimated pool share
  • Loading branch information
canercandan authored Nov 15, 2022
2 parents e3a5d02 + f378dd8 commit e982b3d
Show file tree
Hide file tree
Showing 9 changed files with 1,244 additions and 228 deletions.
20 changes: 20 additions & 0 deletions docs/proposals/asymmetric-adds.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ y'' = y + g.(x - x')

Since the add liquidity process involves swapping then the Liquidity protection procedure must be applied.

## CLI

### Get Estimation of pool share

```bash
sifnoded query clp estimate-pool-share \
--externalAmount=0 \
--nativeAmount=1000000000000000000 \
--symbol ceth \
--output json
```

```json
{
"percentage": "0.183227703974514619",
"native_asset_amount": "549683111923543857",
"external_asset_amount": "366455407949029238"
}
```

## References

Detailed derivation of formulas https://hackmd.io/NjvaZY1qQiS17s_uEgZmTw?both
68 changes: 57 additions & 11 deletions docs/tutorials/asymmetric-adds.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ returns:
}
```

3. Query akasha balances:
4. Query akasha balances:

```bash
sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
Expand All @@ -76,7 +76,21 @@ sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
ceth: 500000000000000000000000
rowan: 500000000000000000000000

3. Add liquidity asymmetrically from akasha account to the ceth pool
5. Query pool share estimation:

```
sifnoded query clp estimate-pool-share --externalAmount=0 --nativeAmount=1000000000000000000 --symbol ceth --output json
```

```json
{
"percentage": "0.183227703974514619",
"native_asset_amount": "549683111923543857",
"external_asset_amount": "366455407949029238"
}
```

6. Add liquidity asymmetrically from akasha account to the ceth pool

```bash
sifnoded tx clp add-liquidity \
Expand All @@ -90,7 +104,7 @@ sifnoded tx clp add-liquidity \
-y
```

4. Query akasha balances:
7. Query akasha balances:

```
sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
Expand All @@ -100,13 +114,13 @@ ceth: 500000000000000000000000
rowan: 499998900000000000000000


4. Query ceth lps:
8. Query ceth lps:

```bash
sifnoded q clp lplist ceth
```

4. Remove the liquidity added by akasha in the previous step
9. Remove the liquidity added by akasha in the previous step

```bash
sifnoded tx clp remove-liquidity \
Expand All @@ -120,7 +134,7 @@ sifnoded tx clp remove-liquidity \
-y
```

5. Query akasha balances:
10. Query akasha balances:

```bash
sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
Expand All @@ -129,13 +143,43 @@ sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
ceth: 500000366455407949029238
rowan: 499999349683111923543856

# Confirm share estimation calculation

We'll confirm the pool share estimation in step five was correct. The estimated pool share was given as:

```json
{
"percentage": "0.183227703974514619",
"native_asset_amount": "549683111923543857",
"external_asset_amount": "366455407949029238"
}
```

Before removing liquidity (but after adding liquidity) akasha's balances were:

ceth: 500000000000000000000000
rowan: 499998900000000000000000

After removing liquidity akasha's balances were:

ceth: 500000366455407949029238
rowan: 499999349683111923543856

The gain in ceth after removing liquidity equals, 500000366455407949029238 - 500000000000000000000000 = 366455407949029238. This equals the estimated
external asset amount.

The gain in rowan after removing liquidity equals, 499999349683111923543856 - 499998900000000000000000 = 449683111923543856. 100000000000000000rowan was spent on tx fees, so if we add this to the difference, 449683111923543856 + 100000000000000000 = 549683111923543856. This equals the estimated
external asset amount.

# Confirm correct pool accounting

akasha started with 500000000000000000000000rowan and now has 499999349683111923543856rowan. So akasha has
500000000000000000000000rowan - 499999349683111923543856rowan = 650316888076456144rowan less rowan.
200000000000000000rowan was spent on tx fees. So 650316888076456144rowan - 200000000000000000rowan = 450316888076456144rowan
was given to the pool by akasha. In return akasha has gained 500000366455407949029238 - 500000000000000000000000 = 366455407949029238ceth
from the pool.

6. Check akash's gains/losses are reflected in the pool balances
11. Check akash's gains/losses are reflected in the pool balances

```
sifnoded q clp pool ceth
Expand All @@ -158,17 +202,19 @@ external_asset_balance = original_balance - amount_gained_by_akasha

Which equals the queried external asset pool balance.

# Equivalence with swapping

Has akasha had a "cheap" swap? How much would akasha have if instead of adding and removing from the pool
they had simply swapped 450316888076456144rowan for ceth?

7. Reset the chain
12. Reset the chain

```bash
make init
make run
```

8. Recreate the ceth pool
13. Recreate the ceth pool

```bash
sifnoded tx clp create-pool \
Expand All @@ -182,7 +228,7 @@ sifnoded tx clp create-pool \
-y
```

9. Swap 450316888076456144rowan for ceth from akasha:
14. Swap 450316888076456144rowan for ceth from akasha:

```bash
sifnoded tx clp swap \
Expand All @@ -197,7 +243,7 @@ sifnoded tx clp swap \
-y
```

5. Query akasha balances:
15. Query akasha balances:

```bash
sifnoded q bank balances $(sifnoded keys show akasha -a --keyring-backend=test)
Expand Down
30 changes: 30 additions & 0 deletions proto/sifnode/clp/v1/querier.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ service Query {
rpc GetSwapFeeParams(SwapFeeParamsReq) returns (SwapFeeParamsRes) {
option (google.api.http).get = "/sifchain/clp/v1/swap_fee_rate";
};
rpc GetPoolShareEstimate(PoolShareEstimateReq) returns (PoolShareEstimateRes) {
option (google.api.http).get = "/sifchain/clp/v1/pool_share_estimate";
};
}

message PoolReq {
Expand Down Expand Up @@ -201,4 +204,31 @@ message SwapFeeParamsRes {
(gogoproto.nullable) = false
];
repeated SwapFeeTokenParams token_params = 2;
}

message PoolShareEstimateReq {
sifnode.clp.v1.Asset external_asset = 1;
string native_asset_amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
string external_asset_amount = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}

message PoolShareEstimateRes {
string percentage = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string native_asset_amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
string external_asset_amount = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}
52 changes: 52 additions & 0 deletions x/clp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package cli
import (
"context"
"fmt"
"log"
"strings"

"github.com/Sifchain/sifnode/x/clp/types"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"

//"github.com/Sifchain/sifnode/x/clp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func GetQueryCmd(queryRoute string) *cobra.Command {
Expand All @@ -36,6 +39,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
GetCmdLiquidityProtectionParams(queryRoute),
GetCmdProviderDistributionParams(queryRoute),
GetCmdSwapFeeParams(queryRoute),
GetCmdPoolShareEstimate(queryRoute),
)
return clpQueryCmd
}
Expand Down Expand Up @@ -397,3 +401,51 @@ func GetCmdSwapFeeParams(queryRoute string) *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func GetCmdPoolShareEstimate(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "estimate-pool-share",
Short: "Estimate the pool share of an add liquidity action",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

externalAsset := types.NewAsset(viper.GetString(FlagAssetSymbol))
externalAmount := viper.GetString(FlagExternalAssetAmount)
nativeAmount := viper.GetString(FlagNativeAssetAmount)

result, err := queryClient.GetPoolShareEstimate(context.Background(), &types.PoolShareEstimateReq{
ExternalAsset: &externalAsset,
NativeAssetAmount: sdk.NewUintFromString(nativeAmount),
ExternalAssetAmount: sdk.NewUintFromString(externalAmount),
})
if err != nil {
return err
}

return clientCtx.PrintProto(result)
},
}

cmd.Flags().AddFlagSet(FsAssetSymbol)
cmd.Flags().AddFlagSet(FsExternalAssetAmount)
cmd.Flags().AddFlagSet(FsNativeAssetAmount)
if err := cmd.MarkFlagRequired(FlagAssetSymbol); err != nil {
log.Println("MarkFlagRequired failed: ", err.Error())
}
if err := cmd.MarkFlagRequired(FlagExternalAssetAmount); err != nil {
log.Println("MarkFlagRequired failed: ", err.Error())
}

if err := cmd.MarkFlagRequired(FlagNativeAssetAmount); err != nil {
log.Println("MarkFlagRequired failed: ", err.Error())
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit e982b3d

Please sign in to comment.