Skip to content

Commit fd5722d

Browse files
docs: godoc for chain and offchain (#242)
Introduce package level godoc for chain and offchain explaining the framework and interfaces to implement. These docs will be available to be viewed at https://pkg.go.dev/github.com/smartcontractkit/chainlink-deployments-framework
1 parent 440d5ea commit fd5722d

File tree

2 files changed

+307
-0
lines changed

2 files changed

+307
-0
lines changed

chain/doc.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
Package chain provides the core blockchain abstraction layer for the Chainlink Deployments Framework,
3+
supporting multiple blockchain families through a unified interface.
4+
5+
# Overview
6+
7+
The chain package serves as the foundation for blockchain interactions in the Chainlink Deployments Framework.
8+
It defines the core BlockChain interface that all blockchain implementations must satisfy, and provides
9+
a powerful collection system for managing and querying multiple chains across different blockchain families.
10+
11+
# Architecture
12+
13+
The package consists of several key components:
14+
15+
1. BlockChain Interface (blockchain.go) - Core abstraction for all blockchains
16+
2. BlockChains Collection (blockchain.go) - Container for managing multiple chains
17+
3. Provider Interface (provider.go) - Abstraction for blockchain providers
18+
4. Chain Family Support - Type-safe access to specific blockchain implementations
19+
20+
# Basic Usage
21+
22+
# Core BlockChain Interface
23+
24+
Every blockchain implementation satisfies the BlockChain interface:
25+
26+
import "github.com/smartcontractkit/chainlink-deployments-framework/chain"
27+
28+
// BlockChain interface provides basic chain information
29+
type BlockChain interface {
30+
String() string // Human-readable chain info
31+
Name() string // Chain name
32+
ChainSelector() uint64 // Unique chain identifier
33+
Family() string // Blockchain family (evm, solana, etc.)
34+
}
35+
36+
// Example usage with any blockchain
37+
func printChainInfo(bc chain.BlockChain) {
38+
fmt.Printf("Chain: %s\n", bc.String()) // "Ethereum Mainnet (1)"
39+
fmt.Printf("Name: %s\n", bc.Name()) // "Ethereum Mainnet"
40+
fmt.Printf("Selector: %d\n", bc.ChainSelector()) // 1
41+
fmt.Printf("Family: %s\n", bc.Family()) // "evm"
42+
}
43+
44+
# BlockChains Collection
45+
46+
The BlockChains collection provides powerful querying and management capabilities:
47+
48+
import (
49+
"github.com/smartcontractkit/chainlink-deployments-framework/chain"
50+
"github.com/smartcontractkit/chainlink-deployments-framework/chain/evm"
51+
"github.com/smartcontractkit/chainlink-deployments-framework/chain/solana"
52+
)
53+
54+
// Create a collection of chains
55+
:= chain.NewBlockChains(map[uint64]chain.BlockChain{
56+
1: evmMainnet, // Ethereum mainnet
57+
1151111081099710: solanaMainnet, // Solana mainnet
58+
42161: arbChain, // Arbitrum
59+
})
60+
61+
// Check if chains exist
62+
if chains.Exists(1) {
63+
fmt.Println("Ethereum mainnet is available")
64+
}
65+
66+
// Check multiple chains at once
67+
if chains.ExistsN(1, 42161) {
68+
fmt.Println("Both Ethereum and Arbitrum are available")
69+
}
70+
71+
# Family-Specific Chain Access
72+
73+
Retrieve chains by their blockchain family with type safety:
74+
75+
// Get all EVM chains
76+
evmChains := chains.EVMChains()
77+
for selector, evmChain := range evmChains {
78+
fmt.Printf("EVM Chain %d: %s\n", selector, evmChain.String())
79+
// evmChain is typed as evm.Chain
80+
}
81+
82+
// Get all Solana chains
83+
solanaChains := chains.SolanaChains()
84+
for selector, solChain := range solanaChains {
85+
fmt.Printf("Solana Chain %d: %s\n", selector, solChain.String())
86+
// solChain is typed as solana.Chain
87+
}
88+
89+
// Similarly for other families
90+
aptosChains := chains.AptosChains()
91+
suiChains := chains.SuiChains()
92+
tonChains := chains.TonChains()
93+
tronChains := chains.TronChains()
94+
95+
# Iterating Over All Chains
96+
97+
Use the iterator interface to process all chains:
98+
99+
// Iterate over all chains
100+
for selector, blockchain := range chains.All() {
101+
fmt.Printf("Processing chain %d (%s) from family %s\n",
102+
selector, blockchain.Name(), blockchain.Family())
103+
104+
// Handle each chain based on its family
105+
switch blockchain.Family() {
106+
case "evm":
107+
// Handle EVM-specific logic
108+
case "solana":
109+
// Handle Solana-specific logic
110+
case "aptos":
111+
// Handle Aptos-specific logic
112+
}
113+
114+
# Advanced Querying
115+
116+
# Chain Selector Filtering
117+
118+
The package provides flexible filtering options for chain selectors:
119+
120+
// Get all chain selectors
121+
allSelectors := chains.ListChainSelectors()
122+
fmt.Printf("All chains: %v\n", allSelectors)
123+
124+
// Filter by blockchain family
125+
evmSelectors := chains.ListChainSelectors(
126+
chain.WithFamily("evm"),
127+
)
128+
fmt.Printf("EVM chains: %v\n", evmSelectors)
129+
130+
// Exclude specific chains
131+
filteredSelectors := chains.ListChainSelectors(
132+
chain.WithFamily("evm"),
133+
chain.WithChainSelectorsExclusion([]uint64{42161}), // Exclude Arbitrum
134+
)
135+
fmt.Printf("EVM chains excluding Arbitrum: %v\n", filteredSelectors)
136+
137+
// Combine multiple families
138+
testnetSelectors := chains.ListChainSelectors(
139+
chain.WithFamily("evm"),
140+
chain.WithFamily("solana"),
141+
chain.WithChainSelectorsExclusion([]uint64{1, 1151111081099710}), // Exclude mainnets
142+
)
143+
144+
# Creating Collections from Slices
145+
146+
// Create from slice of chains
147+
chainList := []chain.BlockChain{evmChain, solanaChain, aptosChain}
148+
chainsFromSlice := chain.NewBlockChainsFromSlice(chainList)
149+
150+
// Equivalent to map-based creation
151+
chainsFromMap := chain.NewBlockChains(map[uint64]chain.BlockChain{
152+
evmChain.ChainSelector(): evmChain,
153+
solanaChain.ChainSelector(): solanaChain,
154+
aptosChain.ChainSelector(): aptosChain,
155+
})
156+
157+
# Provider System
158+
159+
The Provider interface enables pluggable blockchain implementations:
160+
161+
import "context"
162+
163+
// Provider interface for blockchain providers
164+
type Provider interface {
165+
Initialize(ctx context.Context) (BlockChain, error)
166+
Name() string
167+
ChainSelector() uint64
168+
BlockChain() BlockChain
169+
}
170+
171+
// Example provider usage
172+
func setupChain(provider chain.Provider) (chain.BlockChain, error) {
173+
ctx := context.Background()
174+
175+
// Initialize the provider
176+
blockchain, err := provider.Initialize(ctx)
177+
if err != nil {
178+
return nil, fmt.Errorf("failed to initialize %s: %w",
179+
provider.Name(), err)
180+
}
181+
182+
fmt.Printf("Initialized %s chain with selector %d\n",
183+
provider.Name(), provider.ChainSelector())
184+
185+
return blockchain, nil
186+
}
187+
188+
# Integration with Deployment Framework
189+
190+
The chain package integrates seamlessly with the broader deployment framework:
191+
192+
import "github.com/smartcontractkit/chainlink-deployments-framework/deployment"
193+
194+
// Create deployment environment with multiple chains
195+
env := &deployment.Environment{
196+
Chains: chains, // BlockChains collection
197+
// ... other environment fields
198+
}
199+
200+
// Access chains in deployment operations
201+
evmChains := env.Chains.EVMChains()
202+
solanaChains := env.Chains.SolanaChains()
203+
*/
204+
package chain

offchain/doc.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Package offchain provides client abstractions and provider interfaces for interacting with
3+
off-chain services in the Chainlink Deployments Framework ecosystem.
4+
5+
# Overview
6+
7+
The offchain package serves as the main entry point for managing off-chain client connections
8+
and providers. It provides unified interfaces for connecting to various off-chain services
9+
such as Job Distributors (JD) and other Chainlink infrastructure components.
10+
11+
# Architecture
12+
13+
The package consists of two main components:
14+
15+
1. Client (offchain.go) - Client interface for off-chain service interactions
16+
2. Provider Interface (provider.go) - Abstraction for different off-chain service providers
17+
18+
# Basic Usage
19+
20+
# Provider Interface
21+
22+
The Provider interface allows for pluggable off-chain service implementations:
23+
24+
import (
25+
"context"
26+
"github.com/smartcontractkit/chainlink-deployments-framework/offchain"
27+
)
28+
29+
// Example provider usage
30+
func connectToOffchainService(provider offchain.Provider) error {
31+
ctx := context.Background()
32+
33+
// Initialize the provider
34+
client, err := provider.Initialize(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
39+
// Get provider name for logging
40+
providerName := provider.Name()
41+
log.Printf("Connected to provider: %s", providerName)
42+
43+
// Use the client for operations
44+
offchainClient := provider.OffchainClient()
45+
// ... perform operations with offchainClient
46+
47+
return nil
48+
}
49+
50+
# Available Providers
51+
52+
The offchain package includes several specialized providers:
53+
54+
# Job Distributor (JD) Provider
55+
56+
Located in the jd subpackage, providing:
57+
58+
- Client Provider - Connects to existing JD services
59+
60+
- CTF Provider - Creates JD Docker containers for testing
61+
62+
# Provider Implementation
63+
64+
To implement a custom off-chain provider, satisfy the Provider interface:
65+
66+
type CustomProvider struct {
67+
client offchain.Client
68+
name string
69+
}
70+
71+
func (p *CustomProvider) Initialize(ctx context.Context) (offchain.Client, error) {
72+
// Initialize your custom client
73+
// ... setup logic
74+
return p.client, nil
75+
}
76+
77+
func (p *CustomProvider) Name() string {
78+
return p.name
79+
}
80+
81+
func (p *CustomProvider) OffchainClient() offchain.Client {
82+
return p.client
83+
}
84+
85+
# Integration with Deployment Framework
86+
87+
The offchain package integrates seamlessly with the deployment framework:
88+
89+
import (
90+
"github.com/smartcontractkit/chainlink-deployments-framework/deployment"
91+
"github.com/smartcontractkit/chainlink-deployments-framework/offchain"
92+
)
93+
94+
// Create environment with offchain client
95+
env := &deployment.Environment{
96+
Offchain: offchainClient, // offchain.Client is compatible
97+
// ... other environment fields
98+
}
99+
100+
This compatibility ensures that existing deployment workflows can leverage
101+
the new offchain provider abstractions without breaking changes.
102+
*/
103+
package offchain

0 commit comments

Comments
 (0)