Documentation
¶
Overview ¶
Package chain provides the core blockchain abstraction layer for the Chainlink Deployments Framework, supporting multiple blockchain families through a unified interface.
Overview ¶
The chain package serves as the foundation for blockchain interactions in the Chainlink Deployments Framework. It defines the core BlockChain interface that all blockchain implementations must satisfy, and provides a powerful collection system for managing and querying multiple chains across different blockchain families.
Architecture ¶
The package consists of several key components:
- BlockChain Interface (blockchain.go) - Core abstraction for all blockchains
- BlockChains Collection (blockchain.go) - Container for managing multiple chains
- Provider Interface (provider.go) - Abstraction for blockchain providers
- Chain Family Support - Type-safe access to specific blockchain implementations
Basic Usage ¶
Core BlockChain Interface ¶
Every blockchain implementation satisfies the BlockChain interface:
import "github.com/smartcontractkit/chainlink-deployments-framework/chain" // BlockChain interface provides basic chain information type BlockChain interface { String() string // Human-readable chain info Name() string // Chain name ChainSelector() uint64 // Unique chain identifier Family() string // Blockchain family (evm, solana, etc.) } // Example usage with any blockchain func printChainInfo(bc chain.BlockChain) { fmt.Printf("Chain: %s\n", bc.String()) // "Ethereum Mainnet (1)" fmt.Printf("Name: %s\n", bc.Name()) // "Ethereum Mainnet" fmt.Printf("Selector: %d\n", bc.ChainSelector()) // 1 fmt.Printf("Family: %s\n", bc.Family()) // "evm" }
BlockChains Collection ¶
The BlockChains collection provides powerful querying and management capabilities:
import ( "github.com/smartcontractkit/chainlink-deployments-framework/chain" "github.com/smartcontractkit/chainlink-deployments-framework/chain/evm" "github.com/smartcontractkit/chainlink-deployments-framework/chain/solana" ) // Create a collection of chains := chain.NewBlockChains(map[uint64]chain.BlockChain{ 1: evmMainnet, // Ethereum mainnet 1151111081099710: solanaMainnet, // Solana mainnet 42161: arbChain, // Arbitrum }) // Check if chains exist if chains.Exists(1) { fmt.Println("Ethereum mainnet is available") } // Check multiple chains at once if chains.ExistsN(1, 42161) { fmt.Println("Both Ethereum and Arbitrum are available") }
Family-Specific Chain Access ¶
Retrieve chains by their blockchain family with type safety:
// Get all EVM chains evmChains := chains.EVMChains() for selector, evmChain := range evmChains { fmt.Printf("EVM Chain %d: %s\n", selector, evmChain.String()) // evmChain is typed as evm.Chain } // Get all Solana chains solanaChains := chains.SolanaChains() for selector, solChain := range solanaChains { fmt.Printf("Solana Chain %d: %s\n", selector, solChain.String()) // solChain is typed as solana.Chain } // Similarly for other families aptosChains := chains.AptosChains() suiChains := chains.SuiChains() tonChains := chains.TonChains() tronChains := chains.TronChains()
Iterating Over All Chains ¶
Use the iterator interface to process all chains:
// Iterate over all chains for selector, blockchain := range chains.All() { fmt.Printf("Processing chain %d (%s) from family %s\n", selector, blockchain.Name(), blockchain.Family()) // Handle each chain based on its family switch blockchain.Family() { case "evm": // Handle EVM-specific logic case "solana": // Handle Solana-specific logic case "aptos": // Handle Aptos-specific logic }
Advanced Querying ¶
Chain Selector Filtering ¶
The package provides flexible filtering options for chain selectors:
// Get all chain selectors allSelectors := chains.ListChainSelectors() fmt.Printf("All chains: %v\n", allSelectors) // Filter by blockchain family evmSelectors := chains.ListChainSelectors( chain.WithFamily("evm"), ) fmt.Printf("EVM chains: %v\n", evmSelectors) // Exclude specific chains filteredSelectors := chains.ListChainSelectors( chain.WithFamily("evm"), chain.WithChainSelectorsExclusion([]uint64{42161}), // Exclude Arbitrum ) fmt.Printf("EVM chains excluding Arbitrum: %v\n", filteredSelectors) // Combine multiple families testnetSelectors := chains.ListChainSelectors( chain.WithFamily("evm"), chain.WithFamily("solana"), chain.WithChainSelectorsExclusion([]uint64{1, 1151111081099710}), // Exclude mainnets )
Creating Collections from Slices ¶
// Create from slice of chains chainList := []chain.BlockChain{evmChain, solanaChain, aptosChain} chainsFromSlice := chain.NewBlockChainsFromSlice(chainList) // Equivalent to map-based creation chainsFromMap := chain.NewBlockChains(map[uint64]chain.BlockChain{ evmChain.ChainSelector(): evmChain, solanaChain.ChainSelector(): solanaChain, aptosChain.ChainSelector(): aptosChain, })
Provider System ¶
The Provider interface enables pluggable blockchain implementations:
import "context" // Provider interface for blockchain providers type Provider interface { Initialize(ctx context.Context) (BlockChain, error) Name() string ChainSelector() uint64 BlockChain() BlockChain } // Example provider usage func setupChain(provider chain.Provider) (chain.BlockChain, error) { ctx := context.Background() // Initialize the provider blockchain, err := provider.Initialize(ctx) if err != nil { return nil, fmt.Errorf("failed to initialize %s: %w", provider.Name(), err) } fmt.Printf("Initialized %s chain with selector %d\n", provider.Name(), provider.ChainSelector()) return blockchain, nil }
Integration with Deployment Framework ¶
The chain package integrates seamlessly with the broader deployment framework:
import "github.com/smartcontractkit/chainlink-deployments-framework/deployment" // Create deployment environment with multiple chains env := &deployment.Environment{ Chains: chains, // BlockChains collection // ... other environment fields } // Access chains in deployment operations evmChains := env.Chains.EVMChains() solanaChains := env.Chains.SolanaChains()
Index ¶
- type BlockChain
- type BlockChains
- func (b BlockChains) All() iter.Seq2[uint64, BlockChain]
- func (b BlockChains) AptosChains() map[uint64]aptos.Chain
- func (b BlockChains) EVMChains() map[uint64]evm.Chain
- func (b BlockChains) Exists(selector uint64) bool
- func (b BlockChains) ExistsN(selectors ...uint64) bool
- func (b BlockChains) ListChainSelectors(options ...ChainSelectorsOption) []uint64
- func (b BlockChains) SolanaChains() map[uint64]solana.Chain
- func (b BlockChains) SuiChains() map[uint64]sui.Chain
- func (b BlockChains) TonChains() map[uint64]ton.Chain
- func (b BlockChains) TronChains() map[uint64]tron.Chain
- type ChainSelectorsOption
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlockChain ¶
type BlockChain interface { // String returns chain name and selector "<name> (<selector>)" String() string // Name returns the name of the chain Name() string ChainSelector() uint64 Family() string }
BlockChain is an interface that represents a chain. A chain can be an EVM chain, Solana chain Aptos chain or others.
type BlockChains ¶
type BlockChains struct {
// contains filtered or unexported fields
}
BlockChains represents a collection of chains. It provides querying capabilities for different types of chains.
func NewBlockChains ¶
func NewBlockChains(chains map[uint64]BlockChain) BlockChains
NewBlockChains initializes a new BlockChains instance
func NewBlockChainsFromSlice ¶ added in v0.8.0
func NewBlockChainsFromSlice(chains []BlockChain) BlockChains
NewBlockChainsFromSlice initializes a new BlockChains instance from a slice of BlockChain.
func (BlockChains) All ¶ added in v0.8.0
func (b BlockChains) All() iter.Seq2[uint64, BlockChain]
All returns an iterator over all chains with their selectors.
func (BlockChains) AptosChains ¶
func (b BlockChains) AptosChains() map[uint64]aptos.Chain
AptosChains returns a map of all Aptos chains with their selectors.
func (BlockChains) EVMChains ¶
func (b BlockChains) EVMChains() map[uint64]evm.Chain
EVMChains returns a map of all EVM chains with their selectors.
func (BlockChains) Exists ¶ added in v0.5.1
func (b BlockChains) Exists(selector uint64) bool
Exists checks if a chain with the given selector exists.
func (BlockChains) ExistsN ¶ added in v0.5.1
func (b BlockChains) ExistsN(selectors ...uint64) bool
ExistsN checks if all chains with the given selectors exist.
func (BlockChains) ListChainSelectors ¶
func (b BlockChains) ListChainSelectors(options ...ChainSelectorsOption) []uint64
ListChainSelectors returns all chain selectors with optional filtering Options: - WithFamily: filter by family eg WithFamily(chain_selectors.FamilySolana) - WithChainSelectorsExclusion: exclude specific chain selectors
func (BlockChains) SolanaChains ¶
func (b BlockChains) SolanaChains() map[uint64]solana.Chain
SolanaChains returns a map of all Solana chains with their selectors.
func (BlockChains) SuiChains ¶
func (b BlockChains) SuiChains() map[uint64]sui.Chain
SuiChains returns a map of all Sui chains with their selectors.
func (BlockChains) TonChains ¶
func (b BlockChains) TonChains() map[uint64]ton.Chain
TonChains returns a map of all Ton chains with their selectors.
func (BlockChains) TronChains ¶ added in v0.19.0
func (b BlockChains) TronChains() map[uint64]tron.Chain
TronChains returns a map of all Tron chains with their selectors.
type ChainSelectorsOption ¶
type ChainSelectorsOption func(*chainSelectorsOptions)
ChainSelectorsOption defines a function type for configuring ChainSelectors
func WithChainSelectorsExclusion ¶
func WithChainSelectorsExclusion(chainSelectors []uint64) ChainSelectorsOption
WithChainSelectorsExclusion returns an option to exclude specific chain selectors
func WithFamily ¶
func WithFamily(family string) ChainSelectorsOption
WithFamily returns an option to filter chains by family (evm, solana, aptos) Use constants from chain_selectors package eg WithFamily(chain_selectors.FamilySolana) This can be used more than once to include multiple families.
type Provider ¶ added in v0.7.0
type Provider interface { Initialize(ctx context.Context) (BlockChain, error) Name() string ChainSelector() uint64 BlockChain() BlockChain }