Skip to content
Closed
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/mr-tron/base58 v1.2.0
github.com/pelletier/go-toml v1.9.5
github.com/pelletier/go-toml/v2 v2.2.4
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.22.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/scylladb/go-reflectx v1.0.1
Expand Down Expand Up @@ -131,7 +132,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/run v1.2.0 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.65.0 // indirect
Expand Down
120 changes: 120 additions & 0 deletions pkg/chaintype/chaintype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package chaintype

import (
"fmt"
"slices"
"strings"

"github.com/pkg/errors"
)

// ChainType denotes the chain or network to work with
type ChainType string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a chain family enum for this somewhere in common already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find one exactly, but we use string fields and reference chain selector consts in a few places. Could we do that here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm let me take a look at the chain selector consts to see if that'll work.

Wdym by string fields though? Can I point me to an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah looks like I can just use FamilyEVM in chain-selectors? Is that what you had in mind?


const (
// EVM for Ethereum or other chains supporting the EVM
EVM ChainType = "evm"
// Cosmos for the Cosmos chain
Cosmos ChainType = "cosmos"
// Solana for the Solana chain
Solana ChainType = "solana"
// StarkNet for the StarkNet chain
StarkNet ChainType = "starknet"
// Aptos for the Aptos chain
Aptos ChainType = "aptos"
// Tron for the Tron chain
Tron ChainType = "tron"
// TON for the TON chain
TON ChainType = "ton"
// Sui for the Sui chain
Sui ChainType = "sui"
// Offchain is used by the MultichainKeyringAdapter when we are signing for offchain (eg. for DKG).
Offchain ChainType = "offchain"
)

type ChainTypes []ChainType

func (c ChainTypes) String() (out string) {
var sb strings.Builder
for i, chain := range c {
if i != 0 {
sb.WriteString(", ")
}
sb.WriteString(string(chain))
}
return sb.String()
}

func NewChainType(typ uint8) (ChainType, error) {
switch typ {
case 1:
return EVM, nil
case 2:
return Solana, nil
case 3:
return Cosmos, nil
case 4:
return StarkNet, nil
case 5:
return Aptos, nil
case 6:
return Tron, nil
case 7:
return TON, nil
case 8:
return Sui, nil
case 9:
return Offchain, nil
default:
return "", fmt.Errorf("unexpected chaintype.ChainType: %#v", typ)
}
}

func (c ChainType) Type() (uint8, error) {
switch c {
case EVM:
return 1, nil
case Solana:
return 2, nil
case Cosmos:
return 3, nil
case StarkNet:
return 4, nil
case Aptos:
return 5, nil
case Tron:
return 6, nil
case TON:
return 7, nil
case Sui:
return 8, nil
case Offchain:
return 9, nil
default:
return 0, fmt.Errorf("unexpected chaintype.ChainType: %#v", c)
}
}

// SupportedChainTypes contain all chains that are supported
var SupportedChainTypes = ChainTypes{EVM, Cosmos, Solana, StarkNet, Aptos, Tron, TON, Sui}

// ErrInvalidChainType is an error to indicate an unsupported chain type
var ErrInvalidChainType error

func init() {
supported := make([]string, 0, len(SupportedChainTypes))
for _, chainType := range SupportedChainTypes {
supported = append(supported, fmt.Sprintf(`"%s"`, chainType))
}
ErrInvalidChainType = fmt.Errorf("valid types include: [%s]", strings.Join(supported, ", "))
}

// IsSupportedChainType checks to see if the chain is supported
func IsSupportedChainType(chainType ChainType) bool {
return slices.Contains(SupportedChainTypes, chainType)
}

// NewErrInvalidChainType returns an error wrapping ErrInvalidChainType for an unsupported chain
func NewErrInvalidChainType(chainType ChainType) error {
return errors.Wrapf(ErrInvalidChainType, `unknown chain type "%s"`, chainType)
}
Loading