Skip to content

Commit

Permalink
CCIP-3605 Adding helpers function to check whether USDC is enabled or…
Browse files Browse the repository at this point in the history
… not (#194)
  • Loading branch information
mateusz-sekara authored Oct 2, 2024
1 parent 0c1e265 commit 34d7f9b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
13 changes: 13 additions & 0 deletions pluginconfig/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ func (e ExecuteOffchainConfig) Validate() error {
return nil
}

func (e ExecuteOffchainConfig) IsUSDCEnabled() bool {
for _, ob := range e.TokenDataObservers {
if ob.WellFormed() != nil {
continue
}
if ob.IsUSDC() {
return true
}
}
return false

}

// EncodeExecuteOffchainConfig encodes a ExecuteOffchainConfig into bytes using JSON.
func EncodeExecuteOffchainConfig(e ExecuteOffchainConfig) ([]byte, error) {
return json.Marshal(e)
Expand Down
23 changes: 10 additions & 13 deletions pluginconfig/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,26 @@ type TokenDataObserverConfig struct {

// WellFormed checks that the observer's config is syntactically correct - proper struct is initialized based on type
func (t TokenDataObserverConfig) WellFormed() error {
switch t.Type {
case USDCCCTPHandlerType:
if t.IsUSDC() {
if t.USDCCCTPObserverConfig == nil {
return errors.New("USDCCCTPObserverConfig is empty")
}
default:
return errors.New("unknown token data observer type")
return nil
}
return nil
return errors.New("unknown token data observer type")
}

// Validate checks that the observer's config is semantically correct - fields are set correctly
// depending on the config's type
func (t TokenDataObserverConfig) Validate() error {
switch t.Type {
case USDCCCTPHandlerType:
if err := t.USDCCCTPObserverConfig.Validate(); err != nil {
return err
}
default:
return errors.New("unknown token data observer type " + t.Type)
if t.IsUSDC() {
return t.USDCCCTPObserverConfig.Validate()
}
return nil
return errors.New("unknown token data observer type " + t.Type)
}

func (t TokenDataObserverConfig) IsUSDC() bool {
return t.Type == USDCCCTPHandlerType
}

const USDCCCTPHandlerType = "usdc-cctp"
Expand Down
32 changes: 20 additions & 12 deletions pluginconfig/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
}

tests := []struct {
name string
config ExecuteOffchainConfig
wantErr bool
errMsg string
name string
config ExecuteOffchainConfig
usdcEnabled bool
wantErr bool
errMsg string
}{
{
name: "valid config without token data observers",
Expand All @@ -197,8 +198,9 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
Type: "my-fancy-token",
Version: "1.0",
}),
wantErr: true,
errMsg: "unknown token data observer type",
usdcEnabled: false,
wantErr: true,
errMsg: "unknown token data observer type",
},
{
name: "usdc type is set but struct is empty",
Expand All @@ -208,8 +210,9 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
Version: "1.0",
USDCCCTPObserverConfig: &USDCCCTPObserverConfig{},
}),
wantErr: true,
errMsg: "AttestationAPI not set",
usdcEnabled: true,
wantErr: true,
errMsg: "AttestationAPI not set",
},
{
name: "usdc type is set but tokens are missing",
Expand All @@ -223,8 +226,9 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
AttestationAPIInterval: commonconfig.MustNewDuration(500 * time.Millisecond),
},
}),
wantErr: true,
errMsg: "Tokens not set",
usdcEnabled: true,
wantErr: true,
errMsg: "Tokens not set",
},
{
name: "the same observer can't bet set twice",
Expand All @@ -239,8 +243,9 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
Version: "1.0",
USDCCCTPObserverConfig: withUSDCConfig(),
}),
wantErr: true,
errMsg: "duplicate token data observer type",
usdcEnabled: true,
wantErr: true,
errMsg: "duplicate token data observer type",
},
{
name: "valid config with multiple the same observers types but different versions",
Expand All @@ -256,6 +261,7 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
USDCCCTPObserverConfig: withUSDCConfig(),
},
),
usdcEnabled: true,
},
{
name: "valid config with single usdc observer",
Expand All @@ -265,6 +271,7 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
Version: "1.0",
USDCCCTPObserverConfig: withUSDCConfig(),
}),
usdcEnabled: true,
},
}

Expand All @@ -277,6 +284,7 @@ func Test_TokenDataObserver_Validation(t *testing.T) {
} else {
require.NoError(t, err)
}
require.Equal(t, tt.usdcEnabled, tt.config.IsUSDCEnabled())
})
}
}

0 comments on commit 34d7f9b

Please sign in to comment.