Skip to content

Commit 17ce3c7

Browse files
committed
Add swap info to pool share estimation query
1 parent 9cc2dee commit 17ce3c7

File tree

8 files changed

+633
-103
lines changed

8 files changed

+633
-103
lines changed

docs/proposals/asymmetric-adds.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,19 @@ sifnoded query clp estimate-pool-share \
154154
{
155155
"percentage": "0.183227703974514619",
156156
"native_asset_amount": "549683111923543857",
157-
"external_asset_amount": "366455407949029238"
157+
"external_asset_amount": "366455407949029238",
158+
"swap_info": {
159+
"status": "SELL_NATIVE",
160+
"fee": "1102674246586848",
161+
"fee_rate": "0.003000000000000000",
162+
"amount": "450316888076456143",
163+
"result": "366455407949029237"
164+
}
158165
}
159166
```
160167

168+
Where `swap_info` `swap_status` is one of `NO_SWAP`, `SELL_NATIVe`, `BUY_NATIVE`.
169+
161170
## References
162171

163172
Detailed derivation of formulas https://hackmd.io/NjvaZY1qQiS17s_uEgZmTw?both

docs/tutorials/asymmetric-adds.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ sifnoded query clp estimate-pool-share --externalAmount=0 --nativeAmount=1000000
8686
{
8787
"percentage": "0.183227703974514619",
8888
"native_asset_amount": "549683111923543857",
89-
"external_asset_amount": "366455407949029238"
89+
"external_asset_amount": "366455407949029238",
90+
"swap_info": {
91+
"status": "SELL_NATIVE",
92+
"fee": "1102674246586848",
93+
"fee_rate": "0.003000000000000000",
94+
"amount": "450316888076456143",
95+
"result": "366455407949029237"
96+
}
9097
}
9198
```
9299

proto/sifnode/clp/v1/querier.proto

+31
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,35 @@ message PoolShareEstimateRes {
231231
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
232232
(gogoproto.nullable) = false
233233
];
234+
SwapInfo swap_info = 4 [
235+
(gogoproto.nullable) = false
236+
];
237+
238+
}
239+
240+
enum SwapStatus {
241+
UNSPECIFIED = 0;
242+
NO_SWAP = 1;
243+
SELL_NATIVE = 2;
244+
BUY_NATIVE = 3;
245+
}
246+
247+
message SwapInfo {
248+
SwapStatus status = 1;
249+
string fee = 2 [
250+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
251+
(gogoproto.nullable) = false
252+
];
253+
string fee_rate = 3 [
254+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
255+
(gogoproto.nullable) = false
256+
];
257+
string amount = 4 [
258+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
259+
(gogoproto.nullable) = false
260+
];
261+
string result= 5 [
262+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
263+
(gogoproto.nullable) = false
264+
];
234265
}

x/clp/keeper/calculations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func CalculatePoolUnits(P, R, A, r, a sdk.Uint, sellNativeSwapFeeRate, buyNative
158158
case Symmetric:
159159
// R/A == r/a
160160
poolUnits, lpUnits := CalculatePoolUnitsSymmetric(R, r, P)
161-
return poolUnits, lpUnits, NoSwap, sdk.Uint{}, nil
161+
return poolUnits, lpUnits, NoSwap, sdk.ZeroUint(), nil
162162
case NeedMoreX:
163163
// Need more external token to make R/A == r/a
164164
swapAmount := CalculateNativeSwapAmountAsymmetric(R, A, r, a, &sellNativeSwapFeeRateR, &pmtpCurrentRunningRateR)

x/clp/keeper/calculations_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
12371237
expectedPoolUnits: sdk.NewUint(7733018877666646),
12381238
expectedLPunits: sdk.NewUint(76564543343234),
12391239
expectedSwapStatus: clpkeeper.NoSwap,
1240+
expectedSwapAmount: sdk.ZeroUint(),
12401241
},
12411242
{
12421243
name: "negative symmetry - zero external",
@@ -1292,6 +1293,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
12921293
expectedPoolUnits: sdk.NewUintFromString("1606938044258990275541962092341162602522202993783892346929152"),
12931294
expectedLPunits: sdk.NewUint(1099511627776),
12941295
expectedSwapStatus: clpkeeper.NoSwap,
1296+
expectedSwapAmount: sdk.ZeroUint(),
12951297
},
12961298
{
12971299
name: "very big - negative symmetry",

x/clp/keeper/grpc_query.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (k Querier) GetPoolShareEstimate(c context.Context, req *types.PoolShareEst
289289

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

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

305+
feeRate, swapResult, feeAmount, resSwapStatus := calculateSwapInfo(swapStatus, swapAmount, nativeAssetDepth, externalAssetDepth, sellNativeSwapFeeRate, buyNativeSwapFeeRate, pmtpCurrentRunningRate)
306+
305307
newPoolUnitsD := sdk.NewDecFromBigInt(newPoolUnits.BigInt())
306308
lpUnitsD := sdk.NewDecFromBigInt(lpUnits.BigInt())
307309

@@ -317,6 +319,28 @@ func (k Querier) GetPoolShareEstimate(c context.Context, req *types.PoolShareEst
317319
Percentage: percentage,
318320
NativeAssetAmount: sdk.NewUintFromBigInt(nativeAssetAmountD.TruncateInt().BigInt()),
319321
ExternalAssetAmount: sdk.NewUintFromBigInt(externalAssetAmountD.TruncateInt().BigInt()),
322+
SwapInfo: types.SwapInfo{
323+
Status: resSwapStatus,
324+
Fee: feeAmount,
325+
FeeRate: feeRate,
326+
Amount: swapAmount,
327+
Result: swapResult,
328+
},
320329
}, nil
321330

322331
}
332+
333+
func calculateSwapInfo(swapStatus int, swapAmount, nativeAssetDepth, externalAssetDepth sdk.Uint, sellNativeSwapFeeRate, buyNativeSwapFeeRate, pmtpCurrentRunningRate sdk.Dec) (sdk.Dec, sdk.Uint, sdk.Uint, types.SwapStatus) {
334+
switch swapStatus {
335+
case NoSwap:
336+
return sdk.ZeroDec(), sdk.ZeroUint(), sdk.ZeroUint(), types.SwapStatus_NO_SWAP
337+
case SellNative:
338+
swapResult, liquidityFee := CalcSwapResult(false, nativeAssetDepth, swapAmount, externalAssetDepth, pmtpCurrentRunningRate, sellNativeSwapFeeRate)
339+
return sellNativeSwapFeeRate, swapResult, liquidityFee, types.SwapStatus_SELL_NATIVE
340+
case BuyNative:
341+
swapResult, liquidityFee := CalcSwapResult(true, externalAssetDepth, swapAmount, nativeAssetDepth, pmtpCurrentRunningRate, buyNativeSwapFeeRate)
342+
return buyNativeSwapFeeRate, swapResult, liquidityFee, types.SwapStatus_BUY_NATIVE
343+
default:
344+
panic("expect not to reach here!")
345+
}
346+
}

x/clp/keeper/grpc_query_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
146146
expectedExternalAssetAmount sdk.Uint
147147
expectedNativeAssetAmount sdk.Uint
148148
expectedPercentage sdk.Dec
149+
expectedSwapStatus types.SwapStatus
150+
expectedSwapFee sdk.Uint
151+
expectedSwapFeeRate sdk.Dec
152+
expectedSwapAmount sdk.Uint
153+
expectedSwapResult sdk.Uint
149154
err error
150155
errString error
151156
}{
@@ -164,6 +169,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
164169
expectedExternalAssetAmount: sdk.NewUint(200),
165170
expectedNativeAssetAmount: sdk.NewUint(200),
166171
expectedPercentage: sdk.MustNewDecFromStr("0.166666666666666667"),
172+
expectedSwapStatus: types.SwapStatus_NO_SWAP,
173+
expectedSwapFee: sdk.ZeroUint(),
174+
expectedSwapFeeRate: sdk.ZeroDec(),
175+
expectedSwapAmount: sdk.ZeroUint(),
176+
expectedSwapResult: sdk.ZeroUint(),
167177
},
168178
{
169179
name: "asymmetric",
@@ -180,6 +190,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
180190
expectedExternalAssetAmount: sdk.NewUint(115),
181191
expectedNativeAssetAmount: sdk.NewUint(138),
182192
expectedPercentage: sdk.MustNewDecFromStr("0.115826702033598585"),
193+
expectedSwapStatus: types.SwapStatus_SELL_NATIVE,
194+
expectedSwapFee: sdk.ZeroUint(),
195+
expectedSwapFeeRate: sdk.MustNewDecFromStr("0.003"),
196+
expectedSwapAmount: sdk.NewUint(61),
197+
expectedSwapResult: sdk.NewUint(114),
183198
},
184199
}
185200

@@ -236,6 +251,11 @@ func TestQuerier_GetPoolShareEstimate(t *testing.T) {
236251
require.Equal(t, tc.expectedExternalAssetAmount.String(), res.ExternalAssetAmount.String())
237252
require.Equal(t, tc.expectedNativeAssetAmount.String(), res.NativeAssetAmount.String())
238253
require.Equal(t, tc.expectedPercentage.String(), res.Percentage.String())
254+
require.Equal(t, tc.expectedSwapStatus, res.SwapInfo.Status)
255+
require.Equal(t, tc.expectedSwapFee.String(), res.SwapInfo.Fee.String())
256+
require.Equal(t, tc.expectedSwapFeeRate.String(), res.SwapInfo.FeeRate.String())
257+
require.Equal(t, tc.expectedSwapAmount.String(), res.SwapInfo.Amount.String())
258+
require.Equal(t, tc.expectedSwapResult.String(), res.SwapInfo.Result.String())
239259
})
240260
}
241261
}

0 commit comments

Comments
 (0)