-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(vpool): Lots of testing for 'nibid q vpool all-pools'. #830
Changes from all commits
542f96e
0686e8a
d5faeab
7c15379
8bce8b5
51232e8
040687b
806fdae
f9f7da0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,3 +200,19 @@ func (pairs AssetPairs) MarshalJSON() ([]byte, error) { | |
} | ||
return json.Marshal(assetPairsJSON(pairs)) | ||
} | ||
|
||
func ToSdkPointer(num interface{}) interface{} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see where this function is being used in the current PR. Plus I think the name is a little misleading. |
||
switch sdkType := num.(type) { | ||
case sdk.Int: | ||
pointer := new(sdk.Int) | ||
*pointer = num.(sdk.Int) | ||
return pointer | ||
case sdk.Dec: | ||
pointer := new(sdk.Dec) | ||
*pointer = num.(sdk.Dec) | ||
return pointer | ||
default: | ||
errMsg := fmt.Errorf("type passed must be sdk.Int or sdk.Dec, not %s", sdkType) | ||
panic(errMsg) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mock | ||
|
||
import ( | ||
"testing" | ||
|
||
sdktestsmocks "github.com/cosmos/cosmos-sdk/tests/mocks" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
gomock "github.com/golang/mock/gomock" | ||
) | ||
|
||
/* AppendCtxWithMockLogger sets the logger for an input context as a mock logger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we stick with |
||
with 'EXPECT' statements. This enables testing on functions logged to the context. | ||
For example, | ||
|
||
```go | ||
// This is a passing test example | ||
import ( | ||
gomock "github.com/golang/mock/gomock" | ||
sdktestsmocks "github.com/cosmos/cosmos-sdk/tests/mocks" | ||
) | ||
// assume t is a *testing.T variable. | ||
ctx, logger := AppendCtxWithMockLogger(t, ctx) | ||
logger.EXPECT().Debug("debug") | ||
logger.EXPECT().Info("info") | ||
logger.EXPECT().Error("error") | ||
|
||
ctx.Logger().Debug("debug") | ||
ctx.Logger().Info("info") | ||
ctx.Logger().Error("error") | ||
``` | ||
*/ | ||
func AppendCtxWithMockLogger(t *testing.T, ctx sdk.Context) (sdk.Context, *sdktestsmocks.MockLogger) { | ||
ctrl := gomock.NewController(t) | ||
logger := sdktestsmocks.NewMockLogger(ctrl) | ||
return ctx.WithLogger(logger), logger | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ func (k Keeper) getPool(ctx sdk.Context, pair common.AssetPair) ( | |
) { | ||
bz := ctx.KVStore(k.storeKey).Get(types.GetPoolKey(pair)) | ||
if bz == nil { | ||
return nil, fmt.Errorf("Could not find vpool for pair %s", pair.String()) | ||
return nil, fmt.Errorf("could not find vpool for pair %s", pair.String()) | ||
} | ||
|
||
var pool types.Pool | ||
|
@@ -120,22 +120,44 @@ func (k Keeper) GetAllPools(ctx sdk.Context) []*types.Pool { | |
} | ||
|
||
// GetPoolPrices returns the mark price, twap (mark) price, and index price for a vpool. | ||
func (k Keeper) GetPoolPrices(ctx sdk.Context, pool types.Pool) types.PoolPrices { | ||
// An error is returned if | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forgot to finish documentation? |
||
func (k Keeper) GetPoolPrices( | ||
ctx sdk.Context, pool types.Pool, | ||
) (prices types.PoolPrices, err error) { | ||
// Validation - guarantees no panics in GetUnderlyingPrice or GetCurrentTWAP | ||
if err := pool.Pair.Validate(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a proof that the current design of |
||
return prices, err | ||
} | ||
if !k.ExistsPool(ctx, pool.Pair) { | ||
return prices, types.ErrPairNotSupported.Wrap(pool.Pair.String()) | ||
} | ||
if err := pool.ValidateReserves(); err != nil { | ||
return prices, err | ||
} | ||
|
||
indexPriceStr := "" | ||
indexPrice, err := k.GetUnderlyingPrice(ctx, pool.Pair) | ||
if err != nil { | ||
// fail gracefully so that vpool queries run even if the oracle price feeds stop | ||
k.Logger(ctx).Error(err.Error()) | ||
} else { | ||
indexPriceStr = indexPrice.String() | ||
} | ||
twapMarkStr := "" | ||
twapMark, err := k.GetCurrentTWAP(ctx, pool.Pair) | ||
if err != nil { | ||
// fail gracefully so that vpool queries run even if the TWAP is undefined. | ||
k.Logger(ctx).Error(err.Error()) | ||
} else { | ||
twapMarkStr = twapMark.Price.String() | ||
} | ||
|
||
return types.PoolPrices{ | ||
Pair: pool.Pair.String(), | ||
MarkPrice: pool.QuoteAssetReserve.Quo(pool.BaseAssetReserve), | ||
IndexPrice: indexPrice, | ||
TwapMark: twapMark.Price, | ||
IndexPrice: indexPriceStr, | ||
TwapMark: twapMarkStr, | ||
SwapInvariant: pool.BaseAssetReserve.Mul(pool.QuoteAssetReserve).RoundInt(), | ||
BlockNumber: ctx.BlockHeight(), | ||
} | ||
}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the removal of the custom type?