-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c12398e
commit 3019ac6
Showing
12 changed files
with
304 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |