From 2762ba0373e6ea94fd82519dde6102d493966c77 Mon Sep 17 00:00:00 2001 From: Graham Goh Date: Tue, 30 Dec 2025 23:46:59 +1100 Subject: [PATCH] feat(jd): new mapper function for chain family Maps JD proto ChainType to the chain selector family string. This is useful when users are using JD client received a response for node chain config where the type is a JD proto ChainType which is an integer , to make it useful, users would have to perform the [conversion themselves. ](https://github.com/smartcontractkit/chainlink/blob/0f6fe00217300ef6f8543abb40e6acc367c64c82/deployment/common/view/nops.go#L376-L394) Seems to make sense to have it in a single place for this mapping logic. --- .changeset/sour-knives-join.md | 7 +++ offchain/jd/chain_type.go | 36 +++++++++++++++ offchain/jd/chain_type_test.go | 84 ++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 .changeset/sour-knives-join.md create mode 100644 offchain/jd/chain_type.go create mode 100644 offchain/jd/chain_type_test.go 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) + } + }) + } +}