Skip to content

Commit

Permalink
deploy component implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Feb 13, 2024
1 parent c12398e commit 3019ac6
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 11 deletions.
26 changes: 26 additions & 0 deletions contracts/bridgeContracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package contracts

import (
"github.com/multiversx/mx-chain-sovereign-bridge-go/common"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/cmd/config"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/deploy"
)

func DeployBridgeContracts(cfg *config.ContractsConfig) error {
wallet, err := common.LoadWallet(cfg.WalletConfig)
if err != nil {
return err
}

deployer, err := deploy.CreateDeployer(wallet, cfg.DeployConfig)
if err != nil {
return err
}

err = deployer.DeployEsdtSafeContract(cfg.DeployConfig.Contracts.EsdtSafeContractPath)
if err != nil {
return err
}

return nil
}
7 changes: 5 additions & 2 deletions contracts/cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import "github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/deploy"
import (
"github.com/multiversx/mx-chain-sovereign-bridge-go/common"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/deploy"
)

type ContractsConfig struct {
DeployConfig deploy.DeployConfig
WalletConfig deploy.WalletConfig
WalletConfig common.WalletConfig
}
4 changes: 3 additions & 1 deletion contracts/cmd/contracts/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ WALLET_PASSWORD=""
# MultiversX proxy (e.g.: https://testnet-gateway.multiversx.com)
MULTIVERSX_PROXY="https://testnet-gateway.multiversx.com"
# Max retries to wait in seconds for account nonce update after sending bridge txs
MAX_RETRIES_SECONDS_WAIT_NONCE=60
MAX_RETRIES_SECONDS_WAIT_NONCE=60
# esdt-safe contract path
ESDT_SAFE_CONTRACT_PATH="/home/ubuntu/Projects/chain-go/mx-sovereign-sc/esdt-safe/output/esdt-safe.wasm"
16 changes: 14 additions & 2 deletions contracts/cmd/contracts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/joho/godotenv"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-sovereign-bridge-go/common"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/cmd/config"
"github.com/multiversx/mx-chain-sovereign-bridge-go/contracts/deploy"
"github.com/urfave/cli"
Expand All @@ -18,6 +20,7 @@ const (
envPassword = "WALLET_PASSWORD"
envMultiversXProxy = "MULTIVERSX_PROXY"
envMaxRetriesWaitNonce = "MAX_RETRIES_SECONDS_WAIT_NONCE"
esdtSafeContractPath = "ESDT_SAFE_CONTRACT_PATH"
)

func main() {
Expand All @@ -35,7 +38,12 @@ func main() {
}

func deployBridgeContracts(ctx *cli.Context) error {
_, err := loadConfig()
cfg, err := loadConfig()
if err != nil {
return err
}

err = contracts.DeployBridgeContracts(cfg)
if err != nil {
return err
}
Expand All @@ -53,6 +61,7 @@ func loadConfig() (*config.ContractsConfig, error) {

walletPath := os.Getenv(envWallet)
walletPassword := os.Getenv(envPassword)
esdtSafeContract := os.Getenv(esdtSafeContractPath)
proxy := os.Getenv(envMultiversXProxy)
maxRetriesWaitNonceStr := os.Getenv(envMaxRetriesWaitNonce)

Expand All @@ -65,11 +74,14 @@ func loadConfig() (*config.ContractsConfig, error) {
log.Info("loaded config", "maxRetriesWaitNonce", maxRetriesWaitNonce)

return &config.ContractsConfig{
WalletConfig: deploy.WalletConfig{
WalletConfig: common.WalletConfig{
Path: walletPath,
Password: walletPassword,
},
DeployConfig: deploy.DeployConfig{
Contracts: deploy.ContractsLocation{
EsdtSafeContractPath: esdtSafeContract,
},
Proxy: proxy,
MaxRetriesSecondsWaitNonce: maxRetriesWaitNonce,
},
Expand Down
12 changes: 6 additions & 6 deletions contracts/deploy/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package deploy

// WalletConfig holds wallet config
type WalletConfig struct {
Path string
Password string
}

// DeployConfig holds deploy config
type DeployConfig struct {
Contracts ContractsLocation
Proxy string
MaxRetriesSecondsWaitNonce int
}

// ContractsLocation holds all contracts path
type ContractsLocation struct {
EsdtSafeContractPath string
}
4 changes: 4 additions & 0 deletions contracts/deploy/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package deploy

var VmTypeWasmVm = []byte{0x05, 0x00}
var CodeMetadata = []byte{0x05, 0x00}
28 changes: 28 additions & 0 deletions contracts/deploy/dataFormatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package deploy

type dataFormatter struct {
}

// NewDataFormatter creates a sovereign bridge tx data formatter
func NewDataFormatter() *dataFormatter {
return &dataFormatter{}
}

// CreateTxsData creates txs data for bridge operations
func (df *dataFormatter) CreateTxsData(bytes []byte) [][]byte {
txsData := make([][]byte, 0)
if len(bytes) == 0 {
return txsData
}

txsData = append(txsData, bytes)
txsData = append(txsData, VmTypeWasmVm)
txsData = append(txsData, CodeMetadata)

return txsData
}

// IsInterfaceNil checks if the underlying pointer is nil
func (df *dataFormatter) IsInterfaceNil() bool {
return df == nil
}
39 changes: 39 additions & 0 deletions contracts/deploy/dataFormatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package deploy

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewDataFormatter(t *testing.T) {
t.Parallel()

df := NewDataFormatter()
require.False(t, df.IsInterfaceNil())
}

func TestDataFormatter_CreateTxsData(t *testing.T) {
t.Parallel()

df := NewDataFormatter()

t.Run("nil input, should return empty result", func(t *testing.T) {
require.Empty(t, df.CreateTxsData(nil))
})

t.Run("empty data, should return empty result", func(t *testing.T) {
require.Empty(t, df.CreateTxsData(nil))
})

t.Run("should work", func(t *testing.T) {
expectedTxsData := [][]byte{
{0x01, 0x02},
VmTypeWasmVm,
CodeMetadata,
}

txsData := df.CreateTxsData([]byte{0x01, 0x02})
require.Equal(t, expectedTxsData, txsData)
})
}
100 changes: 100 additions & 0 deletions contracts/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package deploy

import (
"context"
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-sovereign-bridge-go/common"
"github.com/multiversx/mx-sdk-go/core"
"github.com/multiversx/mx-sdk-go/data"
"os"
)

var log = logger.GetOrCreate("contracts deploy")

type DeployerArgs struct {
Wallet core.CryptoComponentsHolder
Proxy common.Proxy
TxInteractor common.TxInteractor
TxNonceHandler common.TxNonceSenderHandler
DataFormatter DataFormatter
}

type deployerArgs struct {
wallet core.CryptoComponentsHolder
netConfigs *data.NetworkConfig
txInteractor common.TxInteractor
txNonceHandler common.TxNonceSenderHandler
dataFormatter DataFormatter
}

func NewDeployer(args DeployerArgs) (*deployerArgs, error) {
err := checkArgs(args)
if err != nil {
return nil, err
}

networkConfig, err := args.Proxy.GetNetworkConfig(context.Background())
if err != nil {
return nil, err
}

return &deployerArgs{
wallet: args.Wallet,
netConfigs: networkConfig,
txInteractor: args.TxInteractor,
dataFormatter: args.DataFormatter,
txNonceHandler: args.TxNonceHandler,
}, nil
}

func checkArgs(args DeployerArgs) error {
if check.IfNil(args.Wallet) {
return common.ErrNilWallet
}
if check.IfNil(args.Proxy) {
return common.ErrNilProxy
}
if check.IfNil(args.TxInteractor) {
return common.ErrNilTxInteractor
}
if check.IfNil(args.DataFormatter) {
return common.ErrNilDataFormatter
}
if check.IfNil(args.TxNonceHandler) {
return common.ErrNilNonceHandler
}

return nil
}

func readContractWasm(filePath string) ([]byte, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

return content, nil
}

func (d *deployerArgs) DeployEsdtSafeContract(contractLocation string) error {
log.Info("deploying esdt-safe contract")

binary, err := readContractWasm(contractLocation)
if err != nil {
return err
}

log.Info("esdt-safe contract", "size", len(binary))

// deploy contract send transaction

log.Info("esdt-safe contract deployed successfully")

return nil
}

// IsInterfaceNil checks if the underlying pointer is nil
func (d *deployerArgs) IsInterfaceNil() bool {
return d == nil
}
53 changes: 53 additions & 0 deletions contracts/deploy/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package deploy

import (
"github.com/multiversx/mx-sdk-go/blockchain"
"github.com/multiversx/mx-sdk-go/blockchain/cryptoProvider"
"github.com/multiversx/mx-sdk-go/builders"
"github.com/multiversx/mx-sdk-go/core"
"github.com/multiversx/mx-sdk-go/interactors"
"github.com/multiversx/mx-sdk-go/interactors/nonceHandlerV2"
"time"
)

func CreateDeployer(wallet core.CryptoComponentsHolder, cfg DeployConfig) (*deployerArgs, error) {
args := blockchain.ArgsProxy{
ProxyURL: cfg.Proxy,
Client: nil,
SameScState: false,
ShouldBeSynced: false,
FinalityCheck: false,
CacheExpirationTime: time.Minute,
EntityType: core.Proxy,
}
proxy, err := blockchain.NewProxy(args)
if err != nil {
return nil, err
}

nonceHandler, err := nonceHandlerV2.NewNonceTransactionHandlerV2(nonceHandlerV2.ArgsNonceTransactionsHandlerV2{
Proxy: proxy,
IntervalToResend: time.Second * time.Duration(cfg.MaxRetriesSecondsWaitNonce),
})
if err != nil {
return nil, err
}

txBuilder, err := builders.NewTxBuilder(cryptoProvider.NewSigner())
if err != nil {
return nil, err
}

ti, err := interactors.NewTransactionInteractor(proxy, txBuilder)
if err != nil {
return nil, err
}

return NewDeployer(DeployerArgs{
Wallet: wallet,
Proxy: proxy,
TxInteractor: ti,
TxNonceHandler: nonceHandler,
DataFormatter: NewDataFormatter(),
})
}
7 changes: 7 additions & 0 deletions contracts/deploy/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package deploy

// DataFormatter should format txs data for bridge operations
type DataFormatter interface {
CreateTxsData(bytes []byte) [][]byte
IsInterfaceNil() bool
}
19 changes: 19 additions & 0 deletions testscommon/deployDataFormatterMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testscommon

// DeployDataFormatterMock mocks DataFormatter interface
type DeployDataFormatterMock struct {
CreateTxsDataCalled func(data []byte) [][]byte
}

// CreateTxsData mocks the CreateTxsData method
func (mock *DeployDataFormatterMock) CreateTxsData(data []byte) [][]byte {
if mock.CreateTxsDataCalled != nil {
return mock.CreateTxsDataCalled(data)
}
return nil
}

// IsInterfaceNil -
func (mock *DeployDataFormatterMock) IsInterfaceNil() bool {
return mock == nil
}

0 comments on commit 3019ac6

Please sign in to comment.