Skip to content

Commit

Permalink
Merge pull request #350 from multiversx/amount-in-migration-tool
Browse files Browse the repository at this point in the history
Added amount on the migration tool (for tests)
  • Loading branch information
iulianpascalau authored Oct 4, 2024
2 parents 145fab3 + 63662ed commit 7ddfdf8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 39 deletions.
5 changes: 5 additions & 0 deletions cmd/migration/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (
Usage: "The new safe address on Ethereum",
Value: "",
}
denominatedAmount = cli.Uint64Flag{
Name: "denominated-amount",
Usage: "The dominated amount that will be used on all deposits. Very useful in an initial test",
}
)

func getFlags() []cli.Flag {
Expand All @@ -53,6 +57,7 @@ func getFlags() []cli.Flag {
migrationJsonFile,
signatureJsonFile,
newSafeAddress,
denominatedAmount,
}
}
func getFlagsConfig(ctx *cli.Context) config.ContextFlagsConfig {
Expand Down
6 changes: 5 additions & 1 deletion cmd/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ func generateAndSign(ctx *cli.Context, cfg config.MigrationToolConfig) (*interna
}
newSafeAddressValue := common.HexToAddress(ctx.GlobalString(newSafeAddress.Name))

batchInfo, err := creator.CreateBatchInfo(context.Background(), newSafeAddressValue)
trimValue := chainCore.OptionalUint64{
Value: ctx.GlobalUint64(denominatedAmount.Name),
HasValue: ctx.IsSet(denominatedAmount.Name),
}
batchInfo, err := creator.CreateBatchInfo(context.Background(), newSafeAddressValue, trimValue)
if err != nil {
return nil, err
}
Expand Down
17 changes: 14 additions & 3 deletions executors/ethereum/migrationBatchCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/multiversx/mx-bridge-eth-go/clients/ethereum"
"github.com/multiversx/mx-bridge-eth-go/core/batchProcessor"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewMigrationBatchCreator(args ArgsMigrationBatchCreator) (*migrationBatchCr
}

// CreateBatchInfo creates an instance of type BatchInfo
func (creator *migrationBatchCreator) CreateBatchInfo(ctx context.Context, newSafeAddress common.Address) (*BatchInfo, error) {
func (creator *migrationBatchCreator) CreateBatchInfo(ctx context.Context, newSafeAddress common.Address, trimAmount core.OptionalUint64) (*BatchInfo, error) {
creator.logger.Info("started the batch creation process...")

batchesCount, err := creator.safeContractWrapper.BatchesCount(&bind.CallOpts{Context: ctx})
Expand Down Expand Up @@ -87,7 +88,7 @@ func (creator *migrationBatchCreator) CreateBatchInfo(ctx context.Context, newSa

creator.logger.Info("fetched ERC20 contract addresses")

err = creator.fetchBalances(ctx, deposits)
err = creator.fetchBalances(ctx, deposits, trimAmount)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -140,13 +141,23 @@ func (creator *migrationBatchCreator) fetchERC20ContractsAddresses(ctx context.C
return deposits, nil
}

func (creator *migrationBatchCreator) fetchBalances(ctx context.Context, deposits []*DepositInfo) error {
func (creator *migrationBatchCreator) fetchBalances(ctx context.Context, deposits []*DepositInfo, trimAmount core.OptionalUint64) error {
for _, deposit := range deposits {
balance, err := creator.erc20ContractsHolder.BalanceOf(ctx, deposit.ContractAddress, creator.safeContractAddress)
if err != nil {
return fmt.Errorf("%w for address %s in ERC20 contract %s", err, creator.safeContractAddress.String(), deposit.ContractAddress.String())
}

if trimAmount.HasValue {
newBalance := big.NewInt(0).SetUint64(trimAmount.Value)
if balance.Cmp(newBalance) > 0 {
creator.logger.Warn("applied denominated value", "balance", balance.String(), "new value to consider", newBalance.String())
balance = newBalance
} else {
creator.logger.Warn("can not apply denominated value as the balance is under the provided value, will use the whole balance", "balance", balance.String())
}
}

deposit.Amount = balance
deposit.AmountString = balance.String()
}
Expand Down
109 changes: 74 additions & 35 deletions executors/ethereum/migrationBatchCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import (
"fmt"
"math/big"
"strings"
"sync/atomic"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/multiversx/mx-bridge-eth-go/testsCommon/bridge"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/stretchr/testify/assert"
)

var safeContractAddress = common.HexToAddress(strings.Repeat("9", 40))
var tkn1Erc20Address = bytes.Repeat([]byte("2"), 20)
var tkn2Erc20Address = bytes.Repeat([]byte("3"), 20)
var balanceOfTkn1 = big.NewInt(37)
var balanceOfTkn1 = big.NewInt(19)
var balanceOfTkn2 = big.NewInt(38)

func createMockArgsForMigrationBatchCreator() ArgsMigrationBatchCreator {
Expand Down Expand Up @@ -112,7 +114,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.Equal(t, expectedErr, err)
assert.Nil(t, batch)
})
Expand All @@ -127,7 +129,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.Equal(t, expectedErr, err)
assert.Nil(t, batch)
})
Expand All @@ -142,7 +144,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.Equal(t, expectedErr, err)
assert.Nil(t, batch)
})
Expand All @@ -157,7 +159,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.ErrorIs(t, err, errEmptyTokensList)
assert.Nil(t, batch)
})
Expand All @@ -170,7 +172,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.Equal(t, expectedErr, err)
assert.Nil(t, batch)
})
Expand All @@ -183,7 +185,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.ErrorIs(t, err, errWrongERC20AddressResponse)
assert.Nil(t, batch)
})
Expand All @@ -198,7 +200,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}

creator, _ := NewMigrationBatchCreator(args)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.ErrorIs(t, err, expectedErr)
assert.Nil(t, batch)
})
Expand Down Expand Up @@ -235,8 +237,7 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}
args.SafeContractWrapper = &bridge.SafeContractWrapperStub{
DepositsCountCalled: func(opts *bind.CallOpts) (uint64, error) {
depositCountValue := depositCount
depositCount++
depositCountValue := atomic.AddUint64(&depositCount, 1)

return depositCountValue, nil

Expand All @@ -247,34 +248,72 @@ func TestMigrationBatchCreator_CreateBatchInfo(t *testing.T) {
}
creator, _ := NewMigrationBatchCreator(args)

expectedBatch := &BatchInfo{
OldSafeContractAddress: safeContractAddress.String(),
NewSafeContractAddress: newSafeContractAddress.String(),
BatchID: 2245,
MessageHash: common.HexToHash("0x93915c0bea665553dfc85ec3cdf4b883100929f22d6cbbfc44db2f0ee71b3b56"),
DepositsInfo: []*DepositInfo{
{
DepositNonce: 40,
Token: "tkn1",
ContractAddressString: common.BytesToAddress(tkn1Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn1Erc20Address),
Amount: big.NewInt(37),
AmountString: "37",
t.Run("without trim", func(t *testing.T) {
atomic.StoreUint64(&depositCount, depositCountStart)
expectedBatch := &BatchInfo{
OldSafeContractAddress: safeContractAddress.String(),
NewSafeContractAddress: newSafeContractAddress.String(),
BatchID: 2245,
MessageHash: common.HexToHash("0xe87c7ee013d37956c0023c6a07dce7941a3932293d1b98ab3f00cbde5eae93be"),
DepositsInfo: []*DepositInfo{
{
DepositNonce: 41,
Token: "tkn1",
ContractAddressString: common.BytesToAddress(tkn1Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn1Erc20Address),
Amount: big.NewInt(19),
AmountString: "19",
},
{
DepositNonce: 42,
Token: "tkn2",
ContractAddressString: common.BytesToAddress(tkn2Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn2Erc20Address),
Amount: big.NewInt(38),
AmountString: "38",
},
},
{
DepositNonce: 41,
Token: "tkn2",
ContractAddressString: common.BytesToAddress(tkn2Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn2Erc20Address),
Amount: big.NewInt(38),
AmountString: "38",
}

batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, core.OptionalUint64{})
assert.Nil(t, err)
assert.Equal(t, expectedBatch, batch)
})
t.Run("with trim", func(t *testing.T) {
atomic.StoreUint64(&depositCount, depositCountStart)
expectedBatch := &BatchInfo{
OldSafeContractAddress: safeContractAddress.String(),
NewSafeContractAddress: newSafeContractAddress.String(),
BatchID: 2245,
MessageHash: common.HexToHash("0xfa4c46fc0d0b75460d376a03723b2543aac07d64c47f5322b1a506663bcd266d"),
DepositsInfo: []*DepositInfo{
{
DepositNonce: 41,
Token: "tkn1",
ContractAddressString: common.BytesToAddress(tkn1Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn1Erc20Address),
Amount: big.NewInt(19),
AmountString: "19",
},
{
DepositNonce: 42,
Token: "tkn2",
ContractAddressString: common.BytesToAddress(tkn2Erc20Address).String(),
ContractAddress: common.BytesToAddress(tkn2Erc20Address),
Amount: big.NewInt(20),
AmountString: "20",
},
},
},
}
}

batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress)
assert.Nil(t, err)
assert.Equal(t, expectedBatch, batch)
trimValue := core.OptionalUint64{
Value: 20,
HasValue: true,
}
batch, err := creator.CreateBatchInfo(context.Background(), newSafeContractAddress, trimValue)
assert.Nil(t, err)
assert.Equal(t, expectedBatch, batch)
})
})

}

0 comments on commit 7ddfdf8

Please sign in to comment.