Skip to content

Commit

Permalink
feat: update websocketchainreader init code
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 1, 2024
1 parent 67fb2bd commit 146fec2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 34 deletions.
26 changes: 26 additions & 0 deletions node/pkg/chain/websocketchainreader/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ const (
Ethereum BlockchainType = 2
)

type ChainReaderConfig struct {
KaiaWebsocketUrl string
EthWebsocketUrl string
RetryInterval time.Duration
}

type ChainReaderOption func(*ChainReaderConfig)

func WithKaiaWebsocketUrl(url string) ChainReaderOption {
return func(c *ChainReaderConfig) {
c.KaiaWebsocketUrl = url
}
}

func WithEthWebsocketUrl(url string) ChainReaderOption {
return func(c *ChainReaderConfig) {
c.EthWebsocketUrl = url
}
}

func WithRetryInterval(interval time.Duration) ChainReaderOption {
return func(c *ChainReaderConfig) {
c.RetryInterval = interval
}
}

type ChainReader struct {
KaiaClient utils.ClientInterface
EthClient utils.ClientInterface
Expand Down
65 changes: 36 additions & 29 deletions node/pkg/chain/websocketchainreader/websocketreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,51 @@ import (
"github.com/rs/zerolog/log"
)

func New(kaiaWssUrl, ethWssUrl string) (*ChainReader, error) {
if kaiaWssUrl == "" {
log.Error().Str("Player", "ChainReader").Err(errorSentinel.ErrChainKaiaWebsocketUrlNotFound).Msg("Kaia websocket url not found")
return nil, errorSentinel.ErrChainKaiaWebsocketUrlNotFound
func New(opts ...ChainReaderOption) (*ChainReader, error) {
config := &ChainReaderConfig{}
for _, opt := range opts {
opt(config)
}

if ethWssUrl == "" {
log.Error().Str("Player", "ChainReader").Err(errorSentinel.ErrChainEthWebsocketUrlNotFound).Msg("Eth websocket url not found")
return nil, errorSentinel.ErrChainEthWebsocketUrlNotFound
if config.EthWebsocketUrl == "" && config.KaiaWebsocketUrl == "" {
return nil, errorSentinel.ErrChainWebsocketUrlNotProvided
}

ethClient, err := eth_client.Dial(ethWssUrl)
if err != nil {
log.Error().Str("Player", "ChainReader").Err(err).Msg("Failed to connect to eth websocket")
return nil, err
}
chainIdToChainType := make(map[string]BlockchainType)

ethChainId, err := ethClient.ChainID(context.Background())
if err != nil {
log.Error().Str("Player", "ChainReader").Err(err).Msg("Failed to get eth chain id")
return nil, err
}
var err error

kaiaClient, err := client.Dial(kaiaWssUrl)
if err != nil {
log.Error().Str("Player", "ChainReader").Err(err).Msg("Failed to connect to kaia websocket")
return nil, err
}
var ethClient *eth_client.EthClient
var ethChainId *big.Int
if config.EthWebsocketUrl != "" {
ethClient, err = eth_client.Dial(config.EthWebsocketUrl)
if err != nil {
return nil, err
}

kaiaChainId, err := kaiaClient.ChainID(context.Background())
if err != nil {
log.Error().Str("Player", "ChainReader").Err(err).Msg("Failed to get kaia chain id")
return nil, err
ethChainId, err = ethClient.ChainID(context.Background())
if err != nil {
return nil, err
}

chainIdToChainType[ethChainId.String()] = Ethereum
}

chainIdToChainType := make(map[string]BlockchainType)
chainIdToChainType[ethChainId.String()] = Ethereum
chainIdToChainType[kaiaChainId.String()] = Kaia
var kaiaClient *client.Client
var kaiaChainId *big.Int
if config.KaiaWebsocketUrl != "" {
kaiaClient, err = client.Dial(config.KaiaWebsocketUrl)
if err != nil {
return nil, err
}

kaiaChainId, err = kaiaClient.ChainID(context.Background())
if err != nil {
return nil, err
}

chainIdToChainType[kaiaChainId.String()] = Kaia
}

return &ChainReader{
EthClient: ethClient,
Expand Down
3 changes: 1 addition & 2 deletions node/pkg/error/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,10 @@ var (
ErrChainEmptyToAddress = &CustomError{Service: Others, Code: InvalidInputError, Message: "to address is empty"}
ErrChainEmptyGasPrice = &CustomError{Service: Others, Code: InvalidInputError, Message: "gas price is empty"}
ErrChainGasMultiplierTooHigh = &CustomError{Service: Others, Code: InvalidInputError, Message: "gas multiplier too high"}
ErrChainKaiaWebsocketUrlNotFound = &CustomError{Service: Others, Code: InvalidInputError, Message: "kaia websocket url not found"}
ErrChainEthWebsocketUrlNotFound = &CustomError{Service: Others, Code: InvalidInputError, Message: "eth websocket url not found"}
ErrChainWebsocketContractAddressNotfound = &CustomError{Service: Others, Code: InvalidInputError, Message: "websocket contract address not found"}
ErrChainWebsocketChannelNotfound = &CustomError{Service: Others, Code: InvalidInputError, Message: "websocket channel not found"}
ErrChainEmptyEventNameStringParam = &CustomError{Service: Others, Code: InvalidInputError, Message: "empty event name string param"}
ErrChainWebsocketUrlNotProvided = &CustomError{Service: Others, Code: InvalidInputError, Message: "websocket url not provided"}

ErrDbDatabaseUrlNotFound = &CustomError{Service: Others, Code: InternalError, Message: "DATABASE_URL not found"}
ErrDbEmptyTableNameParam = &CustomError{Service: Others, Code: InvalidInputError, Message: "empty table name"}
Expand Down
4 changes: 3 additions & 1 deletion node/pkg/websocketfetcher/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func (a *App) initializeDex(ctx context.Context, appConfig AppConfig) error {
return errors.New("KAIA_WEBSOCKET_URL and ETH_WEBSOCKET_URL must be set")
}

chainReader, err := websocketchainreader.New(kaiaWebsocketUrl, ethWebsocketUrl)
chainReader, err := websocketchainreader.New(
websocketchainreader.WithEthWebsocketUrl(ethWebsocketUrl),
websocketchainreader.WithKaiaWebsocketUrl(kaiaWebsocketUrl))
if err != nil {
log.Error().Err(err).Msg("error in creating chain reader")
return err
Expand Down
5 changes: 4 additions & 1 deletion node/script/test_websocketDexFetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ func main() {
kaiaWebsocketUrl := os.Getenv("KAIA_WEBSOCKET_URL")
ethWebsocketUrl := os.Getenv("ETH_WEBSOCKET_URL")

chainReader, err := websocketchainreader.New(kaiaWebsocketUrl, ethWebsocketUrl)
chainReader, err := websocketchainreader.New(
websocketchainreader.WithEthWebsocketUrl(ethWebsocketUrl),
websocketchainreader.WithKaiaWebsocketUrl(kaiaWebsocketUrl),
)
if err != nil {
log.Error().Err(err).Msg("failed to create websocketchainreader")
return
Expand Down
5 changes: 4 additions & 1 deletion node/script/test_websocketchainreader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func main() {
ethWebsocketUrl := os.Getenv("ETH_WEBSOCKET_URL")

ctx := context.Background()
chainReader, err := websocketchainreader.New(kaiaWebsocketUrl, ethWebsocketUrl)
chainReader, err := websocketchainreader.New(
websocketchainreader.WithEthWebsocketUrl(ethWebsocketUrl),
websocketchainreader.WithKaiaWebsocketUrl(kaiaWebsocketUrl),
)
if err != nil {
log.Error().Err(err).Msg("failed to create websocketchainreader")
return
Expand Down

0 comments on commit 146fec2

Please sign in to comment.