Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relayer/pkg/chainlink: move cmd/chainlink-starknet from chainlink repo #333

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions relayer/pkg/chainlink/cmd/chainlink-starknet/main.go
aalu1418 marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading