Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Apr 29, 2023
1 parent 6c33a60 commit 9edf047
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions relayer/chains/cosmos/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cosmos/relayer/v2/relayer/ethermint"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,3 +29,60 @@ func TestHandleAccountSequenceMismatchError(t *testing.T) {
p.handleAccountSequenceMismatchError(mockAccountSequenceMismatchError{Actual: 9, Expected: 10})
require.Equal(t, p.nextAccountSeq, uint64(10))
}

type mockTxConfig struct {
legacytx.StdTxConfig
txBuilder *mockTxBuilder
}

func (cfg mockTxConfig) NewTxBuilder() client.TxBuilder {
if cfg.txBuilder == nil {
cfg.txBuilder = &mockTxBuilder{
TxBuilder: cfg.StdTxConfig.NewTxBuilder(),
}
}
return cfg.txBuilder
}

type mockTxBuilder struct {
client.TxBuilder
extOptions []*types.Any
}

func (b *mockTxBuilder) SetExtensionOptions(extOpts ...*codectypes.Any) {
b.extOptions = extOpts
}

func TestSetWithExtensionOptions(t *testing.T) {
cc := &CosmosProvider{PCfg: CosmosProviderConfig{
ExtensionOptions: []provider.ExtensionOption{
{Value: "1000000000"},
{Value: "2000000000"},
},
}}
txf := tx.Factory{}.
WithChainID("chainID").
WithTxConfig(mockTxConfig{})
updatedTxf, err := cc.SetWithExtensionOptions(txf)
require.NoError(t, err)
txb, err := updatedTxf.BuildUnsignedTx()
require.NoError(t, err)
extOptions := txb.(*mockTxBuilder).extOptions
actualNumExtOptions := len(extOptions)
expectedNumExtOptions := len(cc.PCfg.ExtensionOptions)
require.Equal(t, expectedNumExtOptions, actualNumExtOptions)
// Check that each extension option was added with the correct type URL and value
for i, opt := range cc.PCfg.ExtensionOptions {
expectedTypeURL := "/ethermint.types.v1.ExtensionOptionDynamicFeeTx"
max, ok := sdk.NewIntFromString(opt.Value)
require.True(t, ok)
expectedValue, err := (&ethermint.ExtensionOptionDynamicFeeTx{
MaxPriorityPrice: max,
}).Marshal()
require.NoError(t, err)
actualTypeURL := extOptions[i].TypeUrl
actualValue := extOptions[i].Value
require.Equal(t, expectedTypeURL, actualTypeURL)
require.Equal(t, expectedValue, actualValue)
}
}

0 comments on commit 9edf047

Please sign in to comment.