Skip to content

Commit

Permalink
fix: opbnb cross chain (#247)
Browse files Browse the repository at this point in the history
* fix: import path error

* fix: change env from string to enum

* fix: add opbnb & support set default account
  • Loading branch information
BarryTong65 authored May 21, 2024
1 parent fd27365 commit 68430e8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
33 changes: 33 additions & 0 deletions bsc/api_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bsc

import (
"github.com/bnb-chain/greenfield-go-sdk/bsctypes"
"github.com/bnb-chain/greenfield-go-sdk/types"
)

type IAccountClient interface {
SetDefaultAccount(account *bsctypes.BscAccount)
GetDefaultAccount() (*bsctypes.BscAccount, error)
}

// SetDefaultAccount - Set the default account of the Client.
//
// If you call other APIs without specifying the account, it will be assumed that you are operating on the default
// account. This includes sending transactions and other actions.
//
// - account: The account to be set as the default account, should be created using a private key or a mnemonic phrase.
func (c *Client) SetDefaultAccount(account *bsctypes.BscAccount) {
c.defaultAccount = account
}

// GetDefaultAccount - Get the default account of the Client.
//
// - ret1: The default account of the Client.
//
// - ret2: Return error when default account doesn't exist, otherwise return nil.
func (c *Client) GetDefaultAccount() (*bsctypes.BscAccount, error) {
if c.defaultAccount == nil {
return nil, types.ErrorDefaultAccountNotExist
}
return c.defaultAccount, nil
}
50 changes: 24 additions & 26 deletions bsc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package bsc
import (
"encoding/json"
"errors"
"io"
"fmt"
"log"
"net/http"
"os"

"github.com/ethereum/go-ethereum/ethclient"
"google.golang.org/grpc"

"github.com/bnb-chain/greenfield-go-sdk/bsctypes"
"github.com/bnb-chain/greenfield-go-sdk/common"
)

// IClient - Declare all BSC SDK Client APIs, including APIs for multi messages & greenfield executor
type IClient interface {
IMultiMessageClient
IGreenfieldExecutorClient
IBasicClient
IAccountClient
}

// Client - The implementation for IClient, implement all Client APIs for Greenfield SDK.
Expand Down Expand Up @@ -51,14 +52,14 @@ type Option struct {
Host string
}

func New(rpcURL string, env string, option Option) (IClient, error) {
func New(rpcURL string, env bsctypes.Environment, option Option) (IClient, error) {
if rpcURL == "" {
return nil, errors.New("fail to get grpcAddress and chainID to construct Client")
}
var (
cc *ethclient.Client
deployment *bsctypes.Deployment
path string
jsonStr string
err error
)
cc, err = ethclient.Dial(rpcURL)
Expand All @@ -67,30 +68,27 @@ func New(rpcURL string, env string, option Option) (IClient, error) {
}

switch env {
case "dev-net":
path = "./common/contract/dev-net.json"
case "qa-net":
path = "./common/contract/qa-net.json"
case "test-net":
path = "./common/contract/test-net.json"
case "main-net":
path = "./common/contract/main-net.json"
case bsctypes.BscDevnet:
jsonStr = common.BscDevnet
case bsctypes.BscQanet:
jsonStr = common.BscQanet
case bsctypes.BscTestnet:
jsonStr = common.BscTestnet
case bsctypes.BscMainnet:
jsonStr = common.BscMainnet
case bsctypes.OpBNBDevnet:
jsonStr = common.OpBNBDevnet
case bsctypes.OpBNBQanet:
jsonStr = common.OpBNBQanet
case bsctypes.OpBNBTestnet:
jsonStr = common.OpBNBTestnet
case bsctypes.OpBNBMainnet:
jsonStr = common.OpBNBMainnet
default:
return nil, fmt.Errorf("invalid environment: %s", env)
}

jsonFile, err := os.Open(path)
if err != nil {
log.Fatalf("failed to open JSON file: %v", err)
}
defer jsonFile.Close()

// Read the JSON file into a byte slice
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
log.Fatalf("failed to read JSON file: %v", err)
return nil, err
}

err = json.Unmarshal(byteValue, &deployment)
err = json.Unmarshal([]byte(jsonStr), &deployment)
if err != nil {
log.Fatalf("failed to unmarshal JSON data: %v", err)
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions bsctypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ const (
Inherit
)

type Environment uint8

const (
BscDevnet Environment = iota
BscQanet
BscTestnet
BscMainnet
OpBNBDevnet
OpBNBQanet
OpBNBTestnet
OpBNBMainnet
)

type ExtraData struct {
AppAddress *common.Address `json:"appAddress"`
RefundAddress *common.Address `json:"refundAddress"`
Expand Down
Loading

0 comments on commit 68430e8

Please sign in to comment.