Skip to content

Commit

Permalink
Merge pull request #24 from hungdinh82/hung_oracle_module_test_Proces…
Browse files Browse the repository at this point in the history
…sBandOraclePrices

Hung add test ProcessBandOraclePrices
  • Loading branch information
ThanhNhann authored Oct 4, 2024
2 parents d0599dc + 4844705 commit 1b5f180
Showing 1 changed file with 127 additions and 2 deletions.
129 changes: 127 additions & 2 deletions x/oracle/keeper/band_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"cosmossdk.io/math"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/onomyprotocol/reserve/app"
"github.com/onomyprotocol/reserve/x/oracle/types"
"github.com/onomyprotocol/reserve/x/oracle/utils"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -170,7 +172,7 @@ func TestGetPrice(t *testing.T) {

// Create variables for expected prices
expectedPrice10 := math.LegacyNewDec(10)
expectedPrice05 := math.LegacyNewDec(5) // For ATOM/NOM (10/2)
expectedPrice05 := math.LegacyNewDec(5) // For ATOM/NOM (10/2)
expectedPrice01 := math.LegacyNewDec(1).Quo(math.LegacyNewDec(10)) // 0.1

tests := []struct {
Expand Down Expand Up @@ -235,7 +237,7 @@ func TestGetPrice(t *testing.T) {
quoteSymbol: "USD",
basePriceState: bandPriceStateATOM,
quotePriceState: bandPriceStateUSD,
expectedPrice: &expectedPrice10,
expectedPrice: &expectedPrice10,
expectNil: false,
},
{
Expand Down Expand Up @@ -274,3 +276,126 @@ func TestGetPrice(t *testing.T) {
})
}
}

func TestProcessBandOraclePrices(t *testing.T) {
// Set up the application and context
app := app.Setup(t, false)
ctx := app.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "3", Time: time.Unix(1618997040, 0)})

// Define table-driven test cases
tests := []struct {
name string
clientID string
calldata *types.CalldataRecord
oracleOutput interface{}
expectedError bool
expectedRate int64
}{
{
name: "Fail when ClientID is not a valid integer",
clientID: "invalid-id",
calldata: nil,
oracleOutput: nil,
expectedError: true,
},
{
name: "Return nil when no CallDataRecord found",
clientID: "1",
calldata: nil,
oracleOutput: nil,
expectedError: false,
},
{
name: "Fail when decoding OracleInput",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: []byte{0xFF, 0xFF},
},
oracleOutput: nil,
expectedError: true,
},
{
name: "Fail when decoding OracleOutput",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: utils.MustEncode(types.Input{
Symbols: []string{"ATOM", "BTC"},
Multiplier: types.BandPriceMultiplier,
}),
},
oracleOutput: []byte{0xFF, 0xFF},
expectedError: true,
},
{
name: "Success with valid OracleResponsePacketData",
clientID: "1",
calldata: &types.CalldataRecord{
ClientId: 1,
Calldata: utils.MustEncode(types.Input{
Symbols: []string{"ATOM", "BTC"},
Multiplier: types.BandPriceMultiplier,
}),
},
oracleOutput: types.BandOutput{
Responses: []types.Response{
{Symbol: "ATOM", ResponseCode: 0, Rate: 100 * types.BandPriceMultiplier},
{Symbol: "BTC", ResponseCode: 0, Rate: 50000 * types.BandPriceMultiplier},
},
},
expectedError: false,
expectedRate: 100,
},
}

// Iterate over each test case
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.calldata != nil {
err := app.OracleKeeper.SetBandCallDataRecord(ctx, tt.calldata)
require.NoError(t, err)
}

var result []byte
if tt.oracleOutput != nil {
switch v := tt.oracleOutput.(type) {
case types.BandOutput:
result = utils.MustEncode(v)
case []byte:
result = v
}
}

oraclePacketData := types.OracleResponsePacketData{
ClientID: tt.clientID,
RequestID: 1,
Result: result,
ResolveTime: time.Now().Unix(),
}

relayer := sdk.AccAddress("mock-relayer-address")

err := app.OracleKeeper.ProcessBandOraclePrices(ctx, relayer, oraclePacketData)

if tt.expectedError {
require.Error(t, err)
} else {
require.NoError(t, err)

record := app.OracleKeeper.GetBandCallDataRecord(ctx, 1)
require.Nil(t, record, "BandCallDataRecord was not deleted after processing")

if tt.expectedRate != 0 {
priceState := app.OracleKeeper.GetBandPriceState(ctx, "ATOM")
require.NotNil(t, priceState)

actualPrice := priceState.PriceState.Price

expectedPrice := math.LegacyNewDec(tt.expectedRate)
require.True(t, actualPrice.Equal(expectedPrice), "Price for ATOM did not match. Expected: %s, Got: %s", expectedPrice, actualPrice)
}
}
})
}
}

0 comments on commit 1b5f180

Please sign in to comment.