Skip to content

Commit

Permalink
revert me - propagate context to compute
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Sep 8, 2022
1 parent a15c65e commit 90a707f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions x/twap/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) GetArithmeticTwap(
if err != nil {
return sdk.Dec{}, err
}
return computeArithmeticTwap(startRecord, endRecord, quoteAssetDenom)
return computeArithmeticTwap(ctx, startRecord, endRecord, quoteAssetDenom)
}

// GetArithmeticTwapToNow returns GetArithmeticTwap on the input, with endTime being fixed to ctx.BlockTime()
Expand All @@ -79,7 +79,7 @@ func (k Keeper) GetArithmeticTwapToNow(
return sdk.Dec{}, err
}
ctx.Logger().Info("GetArithmeticTwapToNowLog", "endRecord", endRecord)
return computeArithmeticTwap(startRecord, endRecord, quoteAssetDenom)
return computeArithmeticTwap(ctx, startRecord, endRecord, quoteAssetDenom)
}

// GetCurrentAccumulatorRecord returns a TwapRecord struct corresponding to the state of pool `poolId`
Expand Down
4 changes: 2 additions & 2 deletions x/twap/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (k Keeper) GetInterpolatedRecord(ctx sdk.Context, poolId uint64, asset0Deno
return k.getInterpolatedRecord(ctx, poolId, t, asset0Denom, asset1Denom)
}

func ComputeArithmeticTwap(startRecord types.TwapRecord, endRecord types.TwapRecord, quoteAsset string) (sdk.Dec, error) {
return computeArithmeticTwap(startRecord, endRecord, quoteAsset)
func ComputeArithmeticTwap(ctx sdk.Context, startRecord types.TwapRecord, endRecord types.TwapRecord, quoteAsset string) (sdk.Dec, error) {
return computeArithmeticTwap(ctx, startRecord, endRecord, quoteAsset)
}

func RecordWithUpdatedAccumulators(record types.TwapRecord, t time.Time) types.TwapRecord {
Expand Down
3 changes: 2 additions & 1 deletion x/twap/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (k Keeper) getMostRecentRecord(ctx sdk.Context, poolId uint64, assetA, asse
// if (endRecord.Time == startRecord.Time) returns endRecord.LastSpotPrice
// else returns
// (endRecord.Accumulator - startRecord.Accumulator) / (endRecord.Time - startRecord.Time)
func computeArithmeticTwap(startRecord types.TwapRecord, endRecord types.TwapRecord, quoteAsset string) (sdk.Dec, error) {
func computeArithmeticTwap(ctx sdk.Context, startRecord types.TwapRecord, endRecord types.TwapRecord, quoteAsset string) (sdk.Dec, error) {
// see if we need to return an error, due to spot price issues
var err error = nil
if endRecord.LastErrorTime.After(startRecord.Time) || endRecord.LastErrorTime.Equal(startRecord.Time) {
Expand All @@ -237,5 +237,6 @@ func computeArithmeticTwap(startRecord types.TwapRecord, endRecord types.TwapRec
} else {
accumDiff = endRecord.P1ArithmeticTwapAccumulator.Sub(startRecord.P1ArithmeticTwapAccumulator)
}
ctx.Logger().Info("computeArithmeticTwap", "accumDiff", accumDiff, "timeDelta", timeDelta)
return types.AccumDiffDivDuration(accumDiff, timeDelta), err
}
4 changes: 2 additions & 2 deletions x/twap/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func TestComputeArithmeticTwap(t *testing.T) {
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actualTwap, err := twap.ComputeArithmeticTwap(test.startRecord, test.endRecord, test.quoteAsset)
actualTwap, err := twap.ComputeArithmeticTwap(sdk.Context{}, test.startRecord, test.endRecord, test.quoteAsset)
require.Equal(t, test.expTwap, actualTwap)
require.NoError(t, err)
})
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestComputeArithmeticTwapWithSpotPriceError(t *testing.T) {
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actualTwap, err := twap.ComputeArithmeticTwap(test.startRecord, test.endRecord, test.quoteAsset)
actualTwap, err := twap.ComputeArithmeticTwap(sdk.Context{}, test.startRecord, test.endRecord, test.quoteAsset)
require.Equal(t, test.expTwap, actualTwap)
osmoassert.ConditionalError(t, test.expErr, err)
})
Expand Down

0 comments on commit 90a707f

Please sign in to comment.