diff --git a/.changeset/sour-knives-join.md b/.changeset/sour-knives-join.md new file mode 100644 index 00000000..ee2bd416 --- /dev/null +++ b/.changeset/sour-knives-join.md @@ -0,0 +1,7 @@ +--- +"chainlink-deployments-framework": minor +--- + +feat(jd): new mapper function for chain family + +Maps JD proto ChainType to the chain selector family string diff --git a/offchain/jd/chain_type.go b/offchain/jd/chain_type.go new file mode 100644 index 00000000..19b23df5 --- /dev/null +++ b/offchain/jd/chain_type.go @@ -0,0 +1,36 @@ +package jd + +import ( + "errors" + "fmt" + + chain_selectors "github.com/smartcontractkit/chain-selectors" + nodev1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/node" +) + +// ChainTypeToFamily converts a JD proto ChainType to a chain selector family. +func ChainTypeToFamily(chainType nodev1.ChainType) (string, error) { + var family string + switch chainType { + case nodev1.ChainType_CHAIN_TYPE_EVM: + family = chain_selectors.FamilyEVM + case nodev1.ChainType_CHAIN_TYPE_APTOS: + family = chain_selectors.FamilyAptos + case nodev1.ChainType_CHAIN_TYPE_SOLANA: + family = chain_selectors.FamilySolana + case nodev1.ChainType_CHAIN_TYPE_STARKNET: + family = chain_selectors.FamilyStarknet + case nodev1.ChainType_CHAIN_TYPE_TRON: + family = chain_selectors.FamilyTron + case nodev1.ChainType_CHAIN_TYPE_TON: + family = chain_selectors.FamilyTon + case nodev1.ChainType_CHAIN_TYPE_SUI: + family = chain_selectors.FamilySui + case nodev1.ChainType_CHAIN_TYPE_UNSPECIFIED: + return "", errors.New("chain type must be specified") + default: + return "", fmt.Errorf("unsupported chain type %s", chainType) + } + + return family, nil +} diff --git a/offchain/jd/chain_type_test.go b/offchain/jd/chain_type_test.go new file mode 100644 index 00000000..77e0b4cf --- /dev/null +++ b/offchain/jd/chain_type_test.go @@ -0,0 +1,84 @@ +package jd + +import ( + "testing" + + chain_selectors "github.com/smartcontractkit/chain-selectors" + nodev1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/node" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestChainTypeToFamily(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + chainType nodev1.ChainType + want string + wantErr string + }{ + { + name: "EVM chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_EVM, + want: chain_selectors.FamilyEVM, + }, + { + name: "Aptos chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_APTOS, + want: chain_selectors.FamilyAptos, + }, + { + name: "Solana chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_SOLANA, + want: chain_selectors.FamilySolana, + }, + { + name: "Starknet chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_STARKNET, + want: chain_selectors.FamilyStarknet, + }, + { + name: "Tron chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_TRON, + want: chain_selectors.FamilyTron, + }, + { + name: "TON chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_TON, + want: chain_selectors.FamilyTon, + }, + { + name: "Sui chain type", + chainType: nodev1.ChainType_CHAIN_TYPE_SUI, + want: chain_selectors.FamilySui, + }, + { + name: "unspecified chain type returns error", + chainType: nodev1.ChainType_CHAIN_TYPE_UNSPECIFIED, + wantErr: "chain type must be specified", + }, + { + name: "invalid chain type returns error", + chainType: nodev1.ChainType(9999), + wantErr: "unsupported chain type", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got, err := ChainTypeToFamily(tt.chainType) + + if tt.wantErr != "" { + require.Error(t, err) + require.ErrorContains(t, err, tt.wantErr) + assert.Empty(t, got) + } else { + require.NoError(t, err) + assert.Equal(t, tt.want, got) + } + }) + } +}