Skip to content

Commit

Permalink
Merge pull request #315 from multiversx/integrate-merged-eth-sc-contr…
Browse files Browse the repository at this point in the history
…acts

Integrated the merged eth contracts
  • Loading branch information
dragos-rebegea authored Jul 30, 2024
2 parents b2d286b + 9514ddd commit 2375733
Show file tree
Hide file tree
Showing 23 changed files with 284 additions and 947 deletions.
4 changes: 2 additions & 2 deletions bridges/ethMultiversX/bridgeExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ func (executor *bridgeExecutor) addBatchSCMetadata(ctx context.Context, transfer
return transfers, nil
}

func (executor *bridgeExecutor) addMetadataToTransfer(transfer *clients.DepositTransfer, events []*contract.SCExecProxyERC20SCDeposit) *clients.DepositTransfer {
func (executor *bridgeExecutor) addMetadataToTransfer(transfer *clients.DepositTransfer, events []*contract.ERC20SafeERC20SCDeposit) *clients.DepositTransfer {
for _, event := range events {
if event.DepositNonce == transfer.Nonce {
if event.DepositNonce.Uint64() == transfer.Nonce {
transfer.Data = []byte(event.CallData)
var err error
transfer.DisplayableData, err = ConvertToDisplayableData(transfer.Data)
Expand Down
20 changes: 10 additions & 10 deletions bridges/ethMultiversX/bridgeExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T
assert.Equal(t, providedNonce, nonce)
return expectedBatch, false, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error) {
return make([]*contract.SCExecProxyERC20SCDeposit, 0), nil
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return make([]*contract.ERC20SafeERC20SCDeposit, 0), nil
},
}
executor, _ := NewBridgeExecutor(args)
Expand All @@ -386,8 +386,8 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error) {
return make([]*contract.SCExecProxyERC20SCDeposit, 0), nil
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return make([]*contract.ERC20SafeERC20SCDeposit, 0), nil
},
}
executor, _ := NewBridgeExecutor(args)
Expand Down Expand Up @@ -415,9 +415,9 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error) {
return []*contract.SCExecProxyERC20SCDeposit{{
DepositNonce: depositNonce,
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return []*contract.ERC20SafeERC20SCDeposit{{
DepositNonce: big.NewInt(0).SetUint64(depositNonce),
CallData: depositData,
}}, nil
},
Expand Down Expand Up @@ -447,9 +447,9 @@ func TestEthToMultiversXBridgeExecutor_GetAndStoreBatchFromEthereum(t *testing.T
assert.Equal(t, providedNonce, nonce)
return expectedBatch, true, nil
},
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error) {
return []*contract.SCExecProxyERC20SCDeposit{{
DepositNonce: depositNonce,
GetBatchSCMetadataCalled: func(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
return []*contract.ERC20SafeERC20SCDeposit{{
DepositNonce: big.NewInt(0).SetUint64(depositNonce),
CallData: depositData,
}}, nil
},
Expand Down
2 changes: 1 addition & 1 deletion bridges/ethMultiversX/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type EthereumClient interface {
GetTransactionsStatuses(ctx context.Context, batchId uint64) ([]byte, error)
GetQuorumSize(ctx context.Context) (*big.Int, error)
IsQuorumReached(ctx context.Context, msgHash common.Hash) (bool, error)
GetBatchSCMetadata(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error)
GetBatchSCMetadata(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error)
CheckClientAvailability(ctx context.Context) error
CheckRequiredBalance(ctx context.Context, erc20Address common.Address, value *big.Int) error
TotalBalances(ctx context.Context, token common.Address) (*big.Int, error)
Expand Down
15 changes: 6 additions & 9 deletions clients/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type ArgsEthereumClient struct {
TokensMapper TokensMapper
SignatureHolder SignaturesHolder
SafeContractAddress common.Address
SCExecProxyAddress common.Address
GasHandler GasHandler
TransferGasLimitBase uint64
TransferGasLimitForEach uint64
Expand All @@ -56,7 +55,6 @@ type client struct {
tokensMapper TokensMapper
signatureHolder SignaturesHolder
safeContractAddress common.Address
scExecProxyAddress common.Address
gasHandler GasHandler
transferGasLimitBase uint64
transferGasLimitForEach uint64
Expand Down Expand Up @@ -91,7 +89,6 @@ func NewEthereumClient(args ArgsEthereumClient) (*client, error) {
tokensMapper: args.TokensMapper,
signatureHolder: args.SignatureHolder,
safeContractAddress: args.SafeContractAddress,
scExecProxyAddress: args.SCExecProxyAddress,
gasHandler: args.GasHandler,
transferGasLimitBase: args.TransferGasLimitBase,
transferGasLimitForEach: args.TransferGasLimitForEach,
Expand Down Expand Up @@ -204,14 +201,14 @@ func (c *client) GetBatch(ctx context.Context, nonce uint64) (*clients.TransferB
}

// GetBatchSCMetadata returns the emitted logs in a batch that hold metadata for SC execution on MVX
func (c *client) GetBatchSCMetadata(ctx context.Context, nonce uint64) ([]*contract.SCExecProxyERC20SCDeposit, error) {
scExecAbi, err := contract.SCExecProxyMetaData.GetAbi()
func (c *client) GetBatchSCMetadata(ctx context.Context, nonce uint64) ([]*contract.ERC20SafeERC20SCDeposit, error) {
scExecAbi, err := contract.ERC20SafeMetaData.GetAbi()
if err != nil {
return nil, err
}

query := ethereum.FilterQuery{
Addresses: []common.Address{c.scExecProxyAddress},
Addresses: []common.Address{c.safeContractAddress},
Topics: [][]common.Hash{
{scExecAbi.Events["ERC20SCDeposit"].ID},
{common.BytesToHash(new(big.Int).SetUint64(nonce).Bytes())},
Expand All @@ -223,16 +220,16 @@ func (c *client) GetBatchSCMetadata(ctx context.Context, nonce uint64) ([]*contr
return nil, err
}

depositEvents := make([]*contract.SCExecProxyERC20SCDeposit, 0)
depositEvents := make([]*contract.ERC20SafeERC20SCDeposit, 0)
for _, vLog := range logs {
event := new(contract.SCExecProxyERC20SCDeposit)
event := new(contract.ERC20SafeERC20SCDeposit)
err = scExecAbi.UnpackIntoInterface(event, "ERC20SCDeposit", vLog.Data)
if err != nil {
return nil, err
}

// Add this manually since UnpackIntoInterface only unpacks non-indexed arguments
event.BatchNonce = nonce
event.BatchId = big.NewInt(0).SetUint64(nonce)
depositEvents = append(depositEvents, event)
}

Expand Down
17 changes: 9 additions & 8 deletions clients/ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var expectedAmounts = []*big.Int{big.NewInt(20), big.NewInt(40)}
Expand Down Expand Up @@ -54,7 +55,6 @@ func createMockEthereumClientArgs() ArgsEthereumClient {
},
SignatureHolder: &testsCommon.SignaturesHolderStub{},
SafeContractAddress: testsCommon.CreateRandomEthereumAddress(),
SCExecProxyAddress: testsCommon.CreateRandomEthereumAddress(),
GasHandler: &testsCommon.GasHandlerStub{},
TransferGasLimitBase: 50,
TransferGasLimitForEach: 20,
Expand Down Expand Up @@ -1216,15 +1216,16 @@ func TestClient_GetBatchSCMetadata(t *testing.T) {
})

t.Run("returns expected logs", func(t *testing.T) {
scExecAbi, _ := contract.SCExecProxyMetaData.GetAbi()
expectedEvent := &contract.SCExecProxyERC20SCDeposit{
BatchNonce: 1,
DepositNonce: 1,
scExecAbi, _ := contract.ERC20SafeMetaData.GetAbi()
expectedEvent := &contract.ERC20SafeERC20SCDeposit{
BatchId: big.NewInt(1),
DepositNonce: big.NewInt(2),
CallData: "call_data_to_unpack",
}

eventInputs := scExecAbi.Events["ERC20SCDeposit"].Inputs.NonIndexed()
packedArgs, _ := eventInputs.Pack(expectedEvent.DepositNonce, expectedEvent.CallData)
packedArgs, err := eventInputs.Pack(expectedEvent.DepositNonce, expectedEvent.CallData)
require.Nil(t, err)

args := createMockEthereumClientArgs()
args.ClientWrapper = &bridgeTests.EthereumClientWrapperStub{
Expand All @@ -1237,11 +1238,11 @@ func TestClient_GetBatchSCMetadata(t *testing.T) {
},
}
c, _ := NewEthereumClient(args)
batch, err := c.GetBatchSCMetadata(context.Background(), expectedEvent.BatchNonce)
batch, err := c.GetBatchSCMetadata(context.Background(), expectedEvent.BatchId.Uint64())

assert.Nil(t, err)
assert.Equal(t, 1, len(batch))
assert.Equal(t, expectedEvent.BatchNonce, batch[0].BatchNonce)
assert.Equal(t, expectedEvent.BatchId, batch[0].BatchId)
assert.Equal(t, expectedEvent.DepositNonce, batch[0].DepositNonce)
assert.Equal(t, expectedEvent.CallData, batch[0].CallData)
})
Expand Down
Loading

0 comments on commit 2375733

Please sign in to comment.