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

refactore: move ika client to ika package #36

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
NATIVE_RPC=http://localhost:26657
NATIVE_GRPC=127.0.0.1:9090
LOG_FILE_PATH="" # If empty, logs to STDOUT
LOG_LEVEL="debug"

NATIVE_RPC=http://localhost:26657
NATIVE_GRPC=127.0.0.1:9090

IKA_RPC=http://127.0.0.1:9000
IKA_SIGNER_MNEMONIC=""
GAS_ADDRESS=""
IKA_GAS_ACC=""
IKA_GAS_BUDGET=""
IKA_NATIVE_LC_PACKAGE=""
IKA_NATIVE_LC_MODULE=""
IKA_NATIVE_LC_FUNCTION=""
GAS_BUDGET=""

BTC_RPC_USER="user"
BTC_RPC_PASS="pass"
BTC_RPC="localhost:18334"
20 changes: 14 additions & 6 deletions cmd/native-ika/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/block-vision/sui-go-sdk/signer"
"github.com/block-vision/sui-go-sdk/sui"
"github.com/gonative-cc/relayer/ika"
"github.com/gonative-cc/relayer/native"
"github.com/gonative-cc/relayer/native/blockchain"
"github.com/rs/zerolog/log"
Expand All @@ -23,8 +24,8 @@ const (
IkaNativeLcPackage = "IKA_NATIVE_LC_PACKAGE"
IkaNativeLcModule = "IKA_NATIVE_LC_MODULE"
IkaNativeLcFunction = "IKA_NATIVE_LC_FUNCTION"
GasAddress = "GAS_ADDRESS"
GasBudget = "GAS_BUDGET"
IkaGasAcc = "IKA_GAS_ACC"
IkaGasBudget = "IKA_GAS_BUDGET"
)

var (
Expand Down Expand Up @@ -72,14 +73,21 @@ func CmdStart() *cobra.Command {
logger := log.With().Str("module", "native").Logger()
ctx := cmd.Context()

pc, err := native.NewIkaClient(c, signer,
os.Getenv(IkaNativeLcPackage), os.Getenv(IkaNativeLcModule),
os.Getenv(IkaNativeLcFunction), os.Getenv(GasAddress), os.Getenv(GasBudget))
lcContract := ika.SuiCtrCall{
Package: os.Getenv(IkaNativeLcPackage),
Module: os.Getenv(IkaNativeLcModule),
Function: os.Getenv(IkaNativeLcFunction),
}
if err := lcContract.Validate(); err != nil {
return err
}
ikaClient, err := ika.NewClient(c, signer, lcContract,
os.Getenv(IkaGasAcc), os.Getenv(IkaGasBudget))
if err != nil {
return err
}

idx, err := native.NewIndexer(ctx, b, logger, minimumBlockHeight, pc)
idx, err := native.NewIndexer(ctx, b, logger, minimumBlockHeight, ikaClient)
if err != nil {
return err
}
Expand Down
80 changes: 80 additions & 0 deletions ika/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ika

import (
"context"

"github.com/block-vision/sui-go-sdk/models"
"github.com/block-vision/sui-go-sdk/signer"
"github.com/block-vision/sui-go-sdk/sui"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/rs/zerolog"
)

// Client is a wrapper around the Sui client that provides functionality
// for interacting with Ika
type Client struct {
c *sui.Client
Signer *signer.Signer
LcPackage string
Module string
Function string
GasAddr string
GasBudget string
}

// NewClient creates a new Client instance
func NewClient(
c *sui.Client,
signer *signer.Signer,
ctr SuiCtrCall,
gasAddr, gasBudget string,
) (*Client, error) {
i := &Client{
c: c,
Signer: signer,
LcPackage: ctr.Package,
Module: ctr.Module,
Function: ctr.Function,
GasAddr: gasAddr,
GasBudget: gasBudget,
}
return i, nil
}

// UpdateLC sends light blocks to the Native Light Client module in the Ika blockchain.
// It returns the transaction response and an error if any occurred.
func (p *Client) UpdateLC(
ctx context.Context,
lb *tmtypes.LightBlock,
logger zerolog.Logger,
) (models.SuiTransactionBlockResponse, error) {
req := models.MoveCallRequest{
Signer: p.Signer.Address,
PackageObjectId: p.LcPackage,
Module: p.Module,
Function: p.Function,
TypeArguments: []interface{}{},
Arguments: []interface{}{
lb,
},
Gas: p.GasAddr,
GasBudget: p.GasBudget,
}
resp, err := p.c.MoveCall(ctx, req)
if err != nil {
logger.Err(err).Msg("Error calling move function:")
return models.SuiTransactionBlockResponse{}, err // Return zero value for the response
}

// TODO: verify if we need to call this
return p.c.SignAndExecuteTransactionBlock(ctx, models.SignAndExecuteTransactionBlockRequest{
TxnMetaData: resp,
PriKey: p.Signer.PriKey,
Options: models.SuiTransactionBlockOptions{
ShowInput: true,
ShowRawInput: true,
ShowEffects: true,
},
RequestType: "WaitForLocalExecution",
})
}
27 changes: 27 additions & 0 deletions ika/sui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ika

import (
"errors"
"strings"
)

// SuiCtrCall defines a contract path to call in Sui
type SuiCtrCall struct {
Package, Module, Function string
}

// Validate the fields of the struct
func (ctr SuiCtrCall) Validate() error {
return errors.Join(
checkNotEmpty("package", ctr.Package),
checkNotEmpty("module", ctr.Module),
checkNotEmpty("function", ctr.Function),
)
}

func checkNotEmpty(name, s string) error {
if strings.TrimSpace(s) == "" {
return errors.New(name + " must be defined")
}
return nil
}
72 changes: 0 additions & 72 deletions native/client.go

This file was deleted.

2 changes: 1 addition & 1 deletion native/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (i *Indexer) HandleBlock(ctx context.Context, blk *tmtypes.Block) error {
}
i.logger.Info().Int64("light block", lb.SignedHeader.Header.Height).Msg("Light Block ")

txrsp, err := i.pc.updateLC(ctx, lb, i.logger)
txrsp, err := i.ika.UpdateLC(ctx, lb, i.logger)
if err != nil {
return err
}
Expand Down
16 changes: 5 additions & 11 deletions native/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

tmtypes "github.com/cometbft/cometbft/types"
"github.com/gonative-cc/relayer/ika"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
)

const (
Expand All @@ -23,17 +23,17 @@ type Indexer struct {
lowestBlock int
logger zerolog.Logger

pc *IkaClient
ika *ika.Client
}

// NewIndexer returns a new indexer struct with open connections.
func NewIndexer(ctx context.Context, b Blockchain, logger zerolog.Logger,
startBlockHeight int, pc *IkaClient) (*Indexer, error) {
startBlockHeight int, ika *ika.Client) (*Indexer, error) {
i := &Indexer{
b: b,
logger: logger.With().Str("package", "indexer").Logger(),
lowestBlock: startBlockHeight,
pc: pc,
ika: ika,
}
return i, i.onStart(ctx)
}
Expand Down Expand Up @@ -164,11 +164,5 @@ func (i *Indexer) loadChainHeader(_ context.Context) error {

// Close closes all the open connections.
func (i *Indexer) Close(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
return i.b.Close(ctx)
})

return g.Wait()
return i.b.Close(ctx)
}
56 changes: 0 additions & 56 deletions native/tx.go

This file was deleted.

Loading