-
Notifications
You must be signed in to change notification settings - Fork 25
Move chaintype package to common #1795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| 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) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?