Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/sour-knives-join.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions offchain/jd/chain_type.go
Original file line number Diff line number Diff line change
@@ -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
}
84 changes: 84 additions & 0 deletions offchain/jd/chain_type_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}