-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from terra-money/feat/v2.11/alliance-wasm-bin…
…dings feat: added alliance bindings
- Loading branch information
Showing
20 changed files
with
1,649 additions
and
42 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,52 @@ | ||
package custom_queriers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/CosmWasm/wasmd/x/wasm" | ||
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" | ||
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings" | ||
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper" | ||
tokenfactorybindings "github.com/terra-money/core/v2/x/tokenfactory/bindings" | ||
tokenfactorykeeper "github.com/terra-money/core/v2/x/tokenfactory/keeper" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type Querier func(ctx sdk.Context, request json.RawMessage) ([]byte, error) | ||
|
||
func CustomQueriers(queriers ...Querier) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { | ||
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) { | ||
for _, querier := range queriers { | ||
res, err := querier(ctx, request) | ||
if err == nil || !strings.Contains(err.Error(), "unknown query") { | ||
return res, err | ||
} | ||
} | ||
return nil, fmt.Errorf("unknown query") | ||
} | ||
} | ||
|
||
func RegisterCustomPlugins( | ||
bank *bankkeeper.BaseKeeper, | ||
tokenFactory *tokenfactorykeeper.Keeper, | ||
allianceKeeper *alliancekeeper.Keeper, | ||
) []wasmkeeper.Option { | ||
tfQuerier := tokenfactorybindings.CustomQuerier(tokenfactorybindings.NewQueryPlugin(bank, tokenFactory)) | ||
allianceQuerier := alliancebindings.CustomQuerier(alliancebindings.NewAllianceQueryPlugin(allianceKeeper)) | ||
queriers := CustomQueriers(tfQuerier, allianceQuerier) | ||
|
||
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{ | ||
Custom: queriers, | ||
}) | ||
messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator( | ||
tokenfactorybindings.CustomMessageDecorator(bank, tokenFactory), | ||
) | ||
|
||
return []wasm.Option{ | ||
queryPluginOpt, | ||
messengerDecoratorOpt, | ||
} | ||
} |
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,109 @@ | ||
package custom_queriers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
alliancebindings "github.com/terra-money/alliance/x/alliance/bindings" | ||
"github.com/terra-money/alliance/x/alliance/bindings/types" | ||
"github.com/terra-money/core/v2/x/tokenfactory/bindings" | ||
types2 "github.com/terra-money/core/v2/x/tokenfactory/bindings/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func AlwaysErrorQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) { | ||
return nil, fmt.Errorf("always error") | ||
} | ||
|
||
func AlwaysUnknownQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) { | ||
return nil, fmt.Errorf("unknown query") | ||
} | ||
|
||
func AlwaysGoodQuerier(ctx sdk.Context, request json.RawMessage) ([]byte, error) { | ||
return []byte("good"), nil | ||
} | ||
|
||
func TestCustomQueriers(t *testing.T) { | ||
querier := CustomQueriers(AlwaysUnknownQuerier, AlwaysErrorQuerier, AlwaysGoodQuerier) | ||
_, err := querier(sdk.Context{}, nil) | ||
require.ErrorContainsf(t, err, "always error", "") | ||
|
||
querier = CustomQueriers(AlwaysUnknownQuerier, AlwaysGoodQuerier, AlwaysErrorQuerier) | ||
_, err = querier(sdk.Context{}, nil) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestWithTfAndAllianceButCallAlliance(t *testing.T) { | ||
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{}) | ||
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{}) | ||
querier := CustomQueriers(tfQuerier, allianceQuerier) | ||
|
||
query := types.AllianceQuery{ | ||
Alliance: &types.Alliance{Denom: "123"}, | ||
} | ||
bz, err := json.Marshal(query) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
stack := make([]byte, 1024) | ||
runtime.Stack(stack, false) | ||
// We make sure alliance is called here | ||
require.Containsf(t, string(stack), "GetAlliance", "") | ||
} | ||
}() | ||
|
||
// We call querier but it will panic because we don't have a keeper | ||
_, err = querier(sdk.Context{}, bz) | ||
require.Fail(t, "should panic") | ||
} | ||
|
||
func TestWithTfAndAllianceButCallTf(t *testing.T) { | ||
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{}) | ||
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{}) | ||
querier := CustomQueriers(tfQuerier, allianceQuerier) | ||
|
||
query := types2.TokenFactoryQuery{ | ||
Token: &types2.TokenQuery{ | ||
Params: &types2.GetParams{}, | ||
}, | ||
} | ||
bz, err := json.Marshal(query) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
stack := make([]byte, 1024) | ||
runtime.Stack(stack, false) | ||
// We make sure tf is called here | ||
require.Containsf(t, string(stack), "GetParams", "") | ||
} | ||
}() | ||
|
||
// We call querier but it will panic because we don't have a keeper | ||
_, err = querier(sdk.Context{}, bz) | ||
require.Fail(t, "should panic") | ||
} | ||
|
||
func TestWithTfAndAllianceButRandomCall(t *testing.T) { | ||
tfQuerier := bindings.CustomQuerier(&bindings.QueryPlugin{}) | ||
allianceQuerier := alliancebindings.CustomQuerier(&alliancebindings.QueryPlugin{}) | ||
querier := CustomQueriers(tfQuerier, allianceQuerier) | ||
|
||
query := sdk.NewCoin("denom", sdk.NewInt(1)) | ||
bz, err := json.Marshal(query) | ||
require.NoError(t, err) | ||
|
||
// We call querier but it will panic because we don't have a keeper | ||
_, err = querier(sdk.Context{}, bz) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestRegisterCustomPlugins(t *testing.T) { | ||
options := RegisterCustomPlugins(nil, nil, nil) | ||
require.Len(t, options, 2) | ||
} |
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
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
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,5 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown" | ||
wasm-debug = "build --target wasm32-unknown-unknown" | ||
unit-test = "test --lib" | ||
schema = "run --example schema" |
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,2 @@ | ||
scripts | ||
target |
Oops, something went wrong.