-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
323 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package wasm | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
wasmTypes "github.com/CosmWasm/go-cosmwasm/types" | ||
|
||
"github.com/terra-project/core/x/oracle/internal/keeper" | ||
wasm "github.com/terra-project/core/x/wasm/exported" | ||
) | ||
|
||
var _ wasm.WasmQuerierInterface = WasmQuerier{} | ||
|
||
// WasmQuerier - staking query interface for wasm contract | ||
type WasmQuerier struct { | ||
keeper keeper.Keeper | ||
} | ||
|
||
// NewWasmQuerier return bank wasm query interface | ||
func NewWasmQuerier(keeper keeper.Keeper) WasmQuerier { | ||
return WasmQuerier{keeper} | ||
} | ||
|
||
// Query - implement query function | ||
func (WasmQuerier) Query(_ sdk.Context, _ wasmTypes.QueryRequest) ([]byte, error) { return nil, nil } | ||
|
||
// ExchangeRateQueryParams query request params for exchange rates | ||
type ExchangeRateQueryParams struct { | ||
BaseDenom string `json:"base_denom"` | ||
QuoteDenoms []string `json:"quote_denoms"` | ||
} | ||
|
||
// CosmosQuery custom query interface for oracle querier | ||
type CosmosQuery struct { | ||
ExchangeRates ExchangeRateQueryParams `json:"exchange_rates"` | ||
} | ||
|
||
// ExchangeRatesQueryResponseItem - exchange rates query response item | ||
type exchangeRateItem struct { | ||
ExchangeRate string `json:"exchange_rate"` | ||
QuoteDenom string `json:"quote_denom"` | ||
} | ||
|
||
// ExchangeRatesQueryResponse - exchange rates query response for wasm module | ||
type ExchangeRatesQueryResponse struct { | ||
ExchangeRates []exchangeRateItem `json:"exchange_rates"` | ||
BaseDenom string `json:"base_denom"` | ||
} | ||
|
||
// QueryCustom implements custom query interface | ||
func (querier WasmQuerier) QueryCustom(ctx sdk.Context, data json.RawMessage) ([]byte, error) { | ||
var params CosmosQuery | ||
err := json.Unmarshal(data, ¶ms) | ||
|
||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) | ||
} | ||
|
||
// LUNA / BASE_DENOM | ||
baseDenomExchangeRate, err := querier.keeper.GetLunaExchangeRate(ctx, params.ExchangeRates.BaseDenom) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var items []exchangeRateItem | ||
for _, quoteDenom := range params.ExchangeRates.QuoteDenoms { | ||
quoteDenomExchangeRate, err := querier.keeper.GetLunaExchangeRate(ctx, quoteDenom) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// (BASE_DENOM / LUNA) / (DENOM / LUNA) = BASE_DENOM / QUOTE_DENOM | ||
items = append(items, exchangeRateItem{ | ||
ExchangeRate: baseDenomExchangeRate.Quo(quoteDenomExchangeRate).String(), | ||
QuoteDenom: quoteDenom, | ||
}) | ||
} | ||
|
||
bz, err := json.Marshal(ExchangeRatesQueryResponse{ | ||
BaseDenom: params.ExchangeRates.BaseDenom, | ||
ExchangeRates: items, | ||
}) | ||
|
||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) | ||
} | ||
|
||
return bz, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package wasm | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
core "github.com/terra-project/core/types" | ||
"github.com/terra-project/core/x/oracle/internal/keeper" | ||
) | ||
|
||
func TestQueryExchangeRates(t *testing.T) { | ||
input := keeper.CreateTestInput(t) | ||
|
||
KRWExchangeRate := sdk.NewDec(1700) | ||
USDExchangeRate := sdk.NewDecWithPrec(17, 1) | ||
SDRExchangeRate := sdk.NewDecWithPrec(19, 1) | ||
input.OracleKeeper.SetLunaExchangeRate(input.Ctx, core.MicroKRWDenom, KRWExchangeRate) | ||
input.OracleKeeper.SetLunaExchangeRate(input.Ctx, core.MicroUSDDenom, USDExchangeRate) | ||
input.OracleKeeper.SetLunaExchangeRate(input.Ctx, core.MicroSDRDenom, SDRExchangeRate) | ||
|
||
querier := NewWasmQuerier(input.OracleKeeper) | ||
var err error | ||
|
||
// empty data will occur error | ||
_, err = querier.QueryCustom(input.Ctx, []byte{}) | ||
require.Error(t, err) | ||
|
||
// not existing quote denom query | ||
queryParams := ExchangeRateQueryParams{ | ||
BaseDenom: core.MicroLunaDenom, | ||
QuoteDenoms: []string{core.MicroMNTDenom}, | ||
} | ||
bz, err := json.Marshal(CosmosQuery{ | ||
ExchangeRates: queryParams, | ||
}) | ||
require.NoError(t, err) | ||
|
||
res, err := querier.QueryCustom(input.Ctx, bz) | ||
require.Error(t, err) | ||
|
||
// not existing base denom query | ||
queryParams = ExchangeRateQueryParams{ | ||
BaseDenom: core.MicroCNYDenom, | ||
QuoteDenoms: []string{core.MicroKRWDenom, core.MicroUSDDenom, core.MicroSDRDenom}, | ||
} | ||
bz, err = json.Marshal(CosmosQuery{ | ||
ExchangeRates: queryParams, | ||
}) | ||
require.NoError(t, err) | ||
|
||
res, err = querier.QueryCustom(input.Ctx, bz) | ||
require.Error(t, err) | ||
|
||
// valid query luna exchange rates | ||
queryParams = ExchangeRateQueryParams{ | ||
BaseDenom: core.MicroKRWDenom, | ||
QuoteDenoms: []string{core.MicroLunaDenom, core.MicroUSDDenom, core.MicroSDRDenom}, | ||
} | ||
bz, err = json.Marshal(CosmosQuery{ | ||
ExchangeRates: queryParams, | ||
}) | ||
require.NoError(t, err) | ||
|
||
res, err = querier.QueryCustom(input.Ctx, bz) | ||
require.NoError(t, err) | ||
|
||
var exchangeRatesResponse ExchangeRatesQueryResponse | ||
err = json.Unmarshal(res, &exchangeRatesResponse) | ||
require.NoError(t, err) | ||
require.Equal(t, exchangeRatesResponse, ExchangeRatesQueryResponse{ | ||
BaseDenom: core.MicroKRWDenom, | ||
ExchangeRates: []exchangeRateItem{ | ||
{ | ||
ExchangeRate: KRWExchangeRate.String(), | ||
QuoteDenom: core.MicroLunaDenom, | ||
}, | ||
{ | ||
ExchangeRate: KRWExchangeRate.Quo(USDExchangeRate).String(), | ||
QuoteDenom: core.MicroUSDDenom, | ||
}, | ||
{ | ||
ExchangeRate: KRWExchangeRate.Quo(SDRExchangeRate).String(), | ||
QuoteDenom: core.MicroSDRDenom, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.