Skip to content

Commit

Permalink
Merge pull request #3385 from Sifchain/feature/pool-share-query-swap-…
Browse files Browse the repository at this point in the history
…info

Feature/pool share query swap info
  • Loading branch information
joneskm authored Nov 18, 2022
2 parents d658edf + 71f009b commit cd1431a
Show file tree
Hide file tree
Showing 10 changed files with 635 additions and 105 deletions.
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const releaseVersion = "1.1.0-beta.rc3"
const releaseVersion = "1.1.0-beta.rc4"

func SetupHandlers(app *SifchainApp) {
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm m.VersionMap) (m.VersionMap, error) {
Expand Down
11 changes: 10 additions & 1 deletion docs/proposals/asymmetric-adds.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,19 @@ sifnoded query clp estimate-pool-share \
{
"percentage": "0.183227703974514619",
"native_asset_amount": "549683111923543857",
"external_asset_amount": "366455407949029238"
"external_asset_amount": "366455407949029238",
"swap_info": {
"status": "SELL_NATIVE",
"fee": "1102674246586848",
"fee_rate": "0.003000000000000000",
"amount": "450316888076456143",
"result": "366455407949029237"
}
}
```

Where `swap_info` `swap_status` is one of `NO_SWAP`, `SELL_NATIVe`, `BUY_NATIVE`.

## References

Detailed derivation of formulas https://hackmd.io/NjvaZY1qQiS17s_uEgZmTw?both
9 changes: 8 additions & 1 deletion docs/tutorials/asymmetric-adds.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ sifnoded query clp estimate-pool-share --externalAmount=0 --nativeAmount=1000000
{
"percentage": "0.183227703974514619",
"native_asset_amount": "549683111923543857",
"external_asset_amount": "366455407949029238"
"external_asset_amount": "366455407949029238",
"swap_info": {
"status": "SELL_NATIVE",
"fee": "1102674246586848",
"fee_rate": "0.003000000000000000",
"amount": "450316888076456143",
"result": "366455407949029237"
}
}
```

Expand Down
31 changes: 31 additions & 0 deletions proto/sifnode/clp/v1/querier.proto
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,35 @@ message PoolShareEstimateRes {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
SwapInfo swap_info = 4 [
(gogoproto.nullable) = false
];

}

enum SwapStatus {
UNSPECIFIED = 0;
NO_SWAP = 1;
SELL_NATIVE = 2;
BUY_NATIVE = 3;
}

message SwapInfo {
SwapStatus status = 1;
string fee = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
string fee_rate = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string amount = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
string result= 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0-beta.rc3
1.1.0-beta.rc4
2 changes: 1 addition & 1 deletion x/clp/keeper/calculations.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func CalculatePoolUnits(P, R, A, r, a sdk.Uint, sellNativeSwapFeeRate, buyNative
case Symmetric:
// R/A == r/a
poolUnits, lpUnits := CalculatePoolUnitsSymmetric(R, r, P)
return poolUnits, lpUnits, NoSwap, sdk.Uint{}, nil
return poolUnits, lpUnits, NoSwap, sdk.ZeroUint(), nil
case NeedMoreX:
// Need more external token to make R/A == r/a
swapAmount := CalculateNativeSwapAmountAsymmetric(R, A, r, a, &sellNativeSwapFeeRateR, &pmtpCurrentRunningRateR)
Expand Down
2 changes: 2 additions & 0 deletions x/clp/keeper/calculations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
expectedPoolUnits: sdk.NewUint(7733018877666646),
expectedLPunits: sdk.NewUint(76564543343234),
expectedSwapStatus: clpkeeper.NoSwap,
expectedSwapAmount: sdk.ZeroUint(),
},
{
name: "negative symmetry - zero external",
Expand Down Expand Up @@ -1292,6 +1293,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
expectedPoolUnits: sdk.NewUintFromString("1606938044258990275541962092341162602522202993783892346929152"),
expectedLPunits: sdk.NewUint(1099511627776),
expectedSwapStatus: clpkeeper.NoSwap,
expectedSwapAmount: sdk.ZeroUint(),
},
{
name: "very big - negative symmetry",
Expand Down
26 changes: 25 additions & 1 deletion x/clp/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (k Querier) GetPoolShareEstimate(c context.Context, req *types.PoolShareEst

nativeAssetDepth, externalAssetDepth := pool.ExtractDebt(pool.NativeAssetBalance, pool.ExternalAssetBalance, false)

newPoolUnits, lpUnits, _, _, err := CalculatePoolUnits(
newPoolUnits, lpUnits, swapStatus, swapAmount, err := CalculatePoolUnits(
pool.PoolUnits,
nativeAssetDepth,
externalAssetDepth,
Expand All @@ -302,6 +302,8 @@ func (k Querier) GetPoolShareEstimate(c context.Context, req *types.PoolShareEst
return nil, err
}

feeRate, swapResult, feeAmount, resSwapStatus := calculateSwapInfo(swapStatus, swapAmount, nativeAssetDepth, externalAssetDepth, sellNativeSwapFeeRate, buyNativeSwapFeeRate, pmtpCurrentRunningRate)

newPoolUnitsD := sdk.NewDecFromBigInt(newPoolUnits.BigInt())
lpUnitsD := sdk.NewDecFromBigInt(lpUnits.BigInt())

Expand All @@ -317,6 +319,28 @@ func (k Querier) GetPoolShareEstimate(c context.Context, req *types.PoolShareEst
Percentage: percentage,
NativeAssetAmount: sdk.NewUintFromBigInt(nativeAssetAmountD.TruncateInt().BigInt()),
ExternalAssetAmount: sdk.NewUintFromBigInt(externalAssetAmountD.TruncateInt().BigInt()),
SwapInfo: types.SwapInfo{
Status: resSwapStatus,
Fee: feeAmount,
FeeRate: feeRate,
Amount: swapAmount,
Result: swapResult,
},
}, nil

}

func calculateSwapInfo(swapStatus int, swapAmount, nativeAssetDepth, externalAssetDepth sdk.Uint, sellNativeSwapFeeRate, buyNativeSwapFeeRate, pmtpCurrentRunningRate sdk.Dec) (sdk.Dec, sdk.Uint, sdk.Uint, types.SwapStatus) {
switch swapStatus {
case NoSwap:
return sdk.ZeroDec(), sdk.ZeroUint(), sdk.ZeroUint(), types.SwapStatus_NO_SWAP
case SellNative:
swapResult, liquidityFee := CalcSwapResult(false, nativeAssetDepth, swapAmount, externalAssetDepth, pmtpCurrentRunningRate, sellNativeSwapFeeRate)
return sellNativeSwapFeeRate, swapResult, liquidityFee, types.SwapStatus_SELL_NATIVE
case BuyNative:
swapResult, liquidityFee := CalcSwapResult(true, externalAssetDepth, swapAmount, nativeAssetDepth, pmtpCurrentRunningRate, buyNativeSwapFeeRate)
return buyNativeSwapFeeRate, swapResult, liquidityFee, types.SwapStatus_BUY_NATIVE
default:
panic("expect not to reach here!")
}
}
20 changes: 20 additions & 0 deletions x/clp/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
expectedExternalAssetAmount sdk.Uint
expectedNativeAssetAmount sdk.Uint
expectedPercentage sdk.Dec
expectedSwapStatus types.SwapStatus
expectedSwapFee sdk.Uint
expectedSwapFeeRate sdk.Dec
expectedSwapAmount sdk.Uint
expectedSwapResult sdk.Uint
err error
errString error
}{
Expand All @@ -164,6 +169,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
expectedExternalAssetAmount: sdk.NewUint(200),
expectedNativeAssetAmount: sdk.NewUint(200),
expectedPercentage: sdk.MustNewDecFromStr("0.166666666666666667"),
expectedSwapStatus: types.SwapStatus_NO_SWAP,
expectedSwapFee: sdk.ZeroUint(),
expectedSwapFeeRate: sdk.ZeroDec(),
expectedSwapAmount: sdk.ZeroUint(),
expectedSwapResult: sdk.ZeroUint(),
},
{
name: "asymmetric",
Expand All @@ -180,6 +190,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
expectedExternalAssetAmount: sdk.NewUint(115),
expectedNativeAssetAmount: sdk.NewUint(138),
expectedPercentage: sdk.MustNewDecFromStr("0.115826702033598585"),
expectedSwapStatus: types.SwapStatus_SELL_NATIVE,
expectedSwapFee: sdk.ZeroUint(),
expectedSwapFeeRate: sdk.MustNewDecFromStr("0.003"),
expectedSwapAmount: sdk.NewUint(61),
expectedSwapResult: sdk.NewUint(114),
},
}

Expand Down Expand Up @@ -236,6 +251,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
require.Equal(t, tc.expectedExternalAssetAmount.String(), res.ExternalAssetAmount.String())
require.Equal(t, tc.expectedNativeAssetAmount.String(), res.NativeAssetAmount.String())
require.Equal(t, tc.expectedPercentage.String(), res.Percentage.String())
require.Equal(t, tc.expectedSwapStatus, res.SwapInfo.Status)
require.Equal(t, tc.expectedSwapFee.String(), res.SwapInfo.Fee.String())
require.Equal(t, tc.expectedSwapFeeRate.String(), res.SwapInfo.FeeRate.String())
require.Equal(t, tc.expectedSwapAmount.String(), res.SwapInfo.Amount.String())
require.Equal(t, tc.expectedSwapResult.String(), res.SwapInfo.Result.String())
})
}
}
Loading

0 comments on commit cd1431a

Please sign in to comment.