Skip to content

Commit e76e685

Browse files
[CLD-978]: feat(jd): new converter function for chain family (#647)
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. JIRA: https://smartcontract-it.atlassian.net/browse/CLD-978
1 parent ae326c0 commit e76e685

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

.changeset/sour-knives-join.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"chainlink-deployments-framework": minor
3+
---
4+
5+
feat(jd): new mapper function for chain family
6+
7+
Maps JD proto ChainType to the chain selector family string

offchain/jd/chain_type.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package jd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
chain_selectors "github.com/smartcontractkit/chain-selectors"
8+
nodev1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/node"
9+
)
10+
11+
// ChainTypeToFamily converts a JD proto ChainType to a chain selector family.
12+
func ChainTypeToFamily(chainType nodev1.ChainType) (string, error) {
13+
var family string
14+
switch chainType {
15+
case nodev1.ChainType_CHAIN_TYPE_EVM:
16+
family = chain_selectors.FamilyEVM
17+
case nodev1.ChainType_CHAIN_TYPE_APTOS:
18+
family = chain_selectors.FamilyAptos
19+
case nodev1.ChainType_CHAIN_TYPE_SOLANA:
20+
family = chain_selectors.FamilySolana
21+
case nodev1.ChainType_CHAIN_TYPE_STARKNET:
22+
family = chain_selectors.FamilyStarknet
23+
case nodev1.ChainType_CHAIN_TYPE_TRON:
24+
family = chain_selectors.FamilyTron
25+
case nodev1.ChainType_CHAIN_TYPE_TON:
26+
family = chain_selectors.FamilyTon
27+
case nodev1.ChainType_CHAIN_TYPE_SUI:
28+
family = chain_selectors.FamilySui
29+
case nodev1.ChainType_CHAIN_TYPE_UNSPECIFIED:
30+
return "", errors.New("chain type must be specified")
31+
default:
32+
return "", fmt.Errorf("unsupported chain type %s", chainType)
33+
}
34+
35+
return family, nil
36+
}

offchain/jd/chain_type_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package jd
2+
3+
import (
4+
"testing"
5+
6+
chain_selectors "github.com/smartcontractkit/chain-selectors"
7+
nodev1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/node"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestChainTypeToFamily(t *testing.T) {
13+
t.Parallel()
14+
15+
tests := []struct {
16+
name string
17+
chainType nodev1.ChainType
18+
want string
19+
wantErr string
20+
}{
21+
{
22+
name: "EVM chain type",
23+
chainType: nodev1.ChainType_CHAIN_TYPE_EVM,
24+
want: chain_selectors.FamilyEVM,
25+
},
26+
{
27+
name: "Aptos chain type",
28+
chainType: nodev1.ChainType_CHAIN_TYPE_APTOS,
29+
want: chain_selectors.FamilyAptos,
30+
},
31+
{
32+
name: "Solana chain type",
33+
chainType: nodev1.ChainType_CHAIN_TYPE_SOLANA,
34+
want: chain_selectors.FamilySolana,
35+
},
36+
{
37+
name: "Starknet chain type",
38+
chainType: nodev1.ChainType_CHAIN_TYPE_STARKNET,
39+
want: chain_selectors.FamilyStarknet,
40+
},
41+
{
42+
name: "Tron chain type",
43+
chainType: nodev1.ChainType_CHAIN_TYPE_TRON,
44+
want: chain_selectors.FamilyTron,
45+
},
46+
{
47+
name: "TON chain type",
48+
chainType: nodev1.ChainType_CHAIN_TYPE_TON,
49+
want: chain_selectors.FamilyTon,
50+
},
51+
{
52+
name: "Sui chain type",
53+
chainType: nodev1.ChainType_CHAIN_TYPE_SUI,
54+
want: chain_selectors.FamilySui,
55+
},
56+
{
57+
name: "unspecified chain type returns error",
58+
chainType: nodev1.ChainType_CHAIN_TYPE_UNSPECIFIED,
59+
wantErr: "chain type must be specified",
60+
},
61+
{
62+
name: "invalid chain type returns error",
63+
chainType: nodev1.ChainType(9999),
64+
wantErr: "unsupported chain type",
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.name, func(t *testing.T) {
70+
t.Parallel()
71+
72+
got, err := ChainTypeToFamily(tt.chainType)
73+
74+
if tt.wantErr != "" {
75+
require.Error(t, err)
76+
require.ErrorContains(t, err, tt.wantErr)
77+
assert.Empty(t, got)
78+
} else {
79+
require.NoError(t, err)
80+
assert.Equal(t, tt.want, got)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)