2023-03-07 22:37:45 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
rpchttpclient "github.com/cometbft/cometbft/rpc/client/http"
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrChainAlreadyExists = errors.New("chain already exists")
|
|
|
|
)
|
|
|
|
|
2023-06-20 16:29:25 +00:00
|
|
|
// ChainDetails wraps information about the properties & endpoints of a chain.
|
2023-03-07 22:37:45 +00:00
|
|
|
type ChainDetails struct {
|
2023-06-20 16:29:25 +00:00
|
|
|
RpcUrl string
|
|
|
|
GrpcUrl string
|
|
|
|
EvmRpcUrl string
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2023-03-17 18:30:23 +00:00
|
|
|
ChainId string
|
2023-03-07 22:37:45 +00:00
|
|
|
StakingDenom string
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// EvmClient dials the underlying EVM RPC url and returns an ethclient.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c ChainDetails) EvmClient() (*ethclient.Client, error) {
|
2023-06-20 16:29:25 +00:00
|
|
|
return ethclient.Dial(c.EvmRpcUrl)
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
// RpcConn creates a new connection to the underlying Rpc url.
|
|
|
|
func (c ChainDetails) RpcConn() (*rpchttpclient.HTTP, error) {
|
|
|
|
return rpchttpclient.New(c.RpcUrl, "/websocket")
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// Chains wraps a map of name -> details about how to connect to a chain.
|
|
|
|
// It prevents registering multiple chains with the same name & encapsulates
|
|
|
|
// panicking if attempting to access a chain that does not exist.
|
2023-03-07 22:37:45 +00:00
|
|
|
type Chains struct {
|
|
|
|
byName map[string]*ChainDetails
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// NewChains creates an empty Chains map.
|
2023-03-07 22:37:45 +00:00
|
|
|
func NewChains() Chains {
|
|
|
|
return Chains{byName: make(map[string]*ChainDetails, 0)}
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// MustGetChain returns the chain of a given name,
|
|
|
|
// or panics if a chain with that name has not been registered.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c Chains) MustGetChain(name string) *ChainDetails {
|
|
|
|
chain, found := c.byName[name]
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("no chain with name %s found", name))
|
|
|
|
}
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// Register adds a chain to the map.
|
|
|
|
// It returns an error if a ChainDetails with that name has already been registered.
|
2023-03-07 22:37:45 +00:00
|
|
|
func (c *Chains) Register(name string, chain *ChainDetails) error {
|
|
|
|
if _, found := c.byName[name]; found {
|
|
|
|
return ErrChainAlreadyExists
|
|
|
|
}
|
|
|
|
c.byName[name] = chain
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the Chain details are all hardcoded because they are currently fixed by kvtool.
|
2023-06-20 16:29:25 +00:00
|
|
|
// someday they may be accepted as configurable parameters.
|
2023-03-07 22:37:45 +00:00
|
|
|
var (
|
2024-08-03 13:44:37 +00:00
|
|
|
kvtoolZgChainChain = ChainDetails{
|
2023-06-20 16:29:25 +00:00
|
|
|
RpcUrl: "http://localhost:26657",
|
|
|
|
GrpcUrl: "http://localhost:9090",
|
|
|
|
EvmRpcUrl: "http://localhost:8545",
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
ChainId: "0gchainlocalnet_8888-1",
|
2024-08-03 14:17:29 +00:00
|
|
|
StakingDenom: "ua0gi",
|
2023-03-07 22:37:45 +00:00
|
|
|
}
|
2023-06-20 16:29:25 +00:00
|
|
|
kvtoolIbcChain = ChainDetails{
|
|
|
|
RpcUrl: "http://localhost:26658",
|
|
|
|
GrpcUrl: "http://localhost:9092",
|
|
|
|
EvmRpcUrl: "http://localhost:8547",
|
2023-03-07 22:37:45 +00:00
|
|
|
|
2024-08-03 13:44:37 +00:00
|
|
|
ChainId: "0gchainlocalnet_8889-2",
|
2023-03-07 22:37:45 +00:00
|
|
|
StakingDenom: "uatom",
|
|
|
|
}
|
|
|
|
)
|