Skip to content

Commit

Permalink
Merge pull request #333 from smartcontractkit/BCF-2701-mv-starknet-cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Oct 24, 2023
2 parents 9be0aa9 + 998d45a commit 1ef3a11
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions relayer/pkg/chainlink/cmd/chainlink-starknet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/go-plugin"
"github.com/pelletier/go-toml/v2"

"github.com/smartcontractkit/chainlink-relay/pkg/loop"
pkgstarknet "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink"
starkchain "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/chain"
stkcfg "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/config"
)

const (
loggerName = "PluginStarknet"
)

func main() {
s := loop.MustNewStartedServer(loggerName)
defer s.Stop()

p := &pluginRelayer{Plugin: loop.Plugin{Logger: s.Logger}}
defer s.Logger.ErrorIfFn(p.Close, "Failed to close")

s.MustRegister(p)

stopCh := make(chan struct{})
defer close(stopCh)

plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: loop.PluginRelayerHandshakeConfig(),
Plugins: map[string]plugin.Plugin{
loop.PluginRelayerName: &loop.GRPCPluginRelayer{
PluginServer: p,
BrokerConfig: loop.BrokerConfig{
StopCh: stopCh,
Logger: s.Logger,
GRPCOpts: s.GRPCOpts,
},
},
},
GRPCServer: s.GRPCOpts.NewServer,
})
}

type pluginRelayer struct {
loop.Plugin
}

// NewRelayer implements the Loopp factory method used by the Loopp server to instantiate a starknet relayer
// [github.com/smartcontractkit/chainlink-relay/pkg/loop.PluginRelayer]
// loopKs must be an implementation that can construct a starknet keystore adapter
// [github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/txm.NewKeystoreAdapter]
func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, loopKs loop.Keystore) (loop.Relayer, error) {
d := toml.NewDecoder(strings.NewReader(config))
d.DisallowUnknownFields()
var cfg struct {
Starknet stkcfg.TOMLConfig
}
if err := d.Decode(&cfg); err != nil {
return nil, fmt.Errorf("failed to decode config toml: %w:\n\t%s", err, config)
}

opts := starkchain.ChainOpts{
Logger: c.Logger,
KeyStore: loopKs,
}

chain, err := starkchain.NewChain(&cfg.Starknet, opts)
if err != nil {
return nil, fmt.Errorf("failed to create chain: %w", err)
}
ra := &loop.RelayerAdapter{Relayer: pkgstarknet.NewRelayer(c.Logger, chain), RelayerExt: chain}

c.SubService(ra)

return ra, nil
}

0 comments on commit 1ef3a11

Please sign in to comment.