Skip to content

Commit

Permalink
refactor to send tokens to Alice at batch creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmatudor committed Dec 10, 2024
1 parent 05097de commit 2932dd1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
30 changes: 13 additions & 17 deletions integrationTests/relayers/slowTests/framework/ethereumHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,6 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
require.NoError(handler, err)
require.Equal(handler, mintAmount.String(), balance.String())

if params.IsNativeOnEth {
tx, err = ethMintBurnContract.Mint(auth, handler.AliceKeys.EthAddress, mintAmount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}

return ethMintBurnAddress, ethMintBurnContract
}

Expand All @@ -374,8 +367,8 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
ethGenericTokenContract, err := contract.NewGenericERC20(ethGenericTokenAddress, handler.SimulatedChain.Client())
require.NoError(handler, err)

// mint the address that will create the transfers
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.AliceKeys.EthAddress)
// mint to the depositor
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.DepositorKeys.EthAddress)
if len(params.InitialSupplyValue) > 0 {
handler.mintTokens(ctx, ethGenericTokenContract, params.InitialSupplyValue, handler.SafeAddress)
}
Expand Down Expand Up @@ -465,15 +458,18 @@ func (handler *EthereumHandler) SettleBatchOnEthereum() {
}
}

// Mint will mint the provided token on Ethereum with the provided value on the behalf of the Depositor address
func (handler *EthereumHandler) Mint(ctx context.Context, params TestTokenParams, valueToMint *big.Int) {
token := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(handler, token)
require.NotNil(handler, token.EthErc20Contract)
// TransferToken will transfer the amount of tokens from one address to another
func (handler *EthereumHandler) TransferToken(
ctx context.Context,
params TestTokenParams,
from KeysHolder,
to KeysHolder,
amount *big.Int,
) {
tkData := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)

// mint erc20 token into eth safe
auth, _ := bind.NewKeyedTransactorWithChainID(handler.DepositorKeys.EthSK, handler.ChainID)
tx, err := token.EthErc20Contract.Mint(auth, handler.SafeAddress, valueToMint)
auth, _ := bind.NewKeyedTransactorWithChainID(from.EthSK, handler.ChainID)
tx, err := tkData.EthErc20Contract.Transfer(auth, to.EthAddress, amount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
Expand Down
1 change: 1 addition & 0 deletions integrationTests/relayers/slowTests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ERC20Contract interface {
BalanceOf(opts *bind.CallOpts, account common.Address) (*big.Int, error)
Mint(opts *bind.TransactOpts, recipientAddress common.Address, amount *big.Int) (*types.Transaction, error)
Approve(opts *bind.TransactOpts, spender common.Address, value *big.Int) (*types.Transaction, error)
Transfer(opts *bind.TransactOpts, to common.Address, value *big.Int) (*types.Transaction, error)
}

// TokensRegistry defines the registry used for the tokens in tests
Expand Down
44 changes: 34 additions & 10 deletions integrationTests/relayers/slowTests/framework/testSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,6 @@ func (setup *TestSetup) IssueAndConfigureTokens(tokens ...TestTokenParams) {
setup.AddToken(token.IssueTokenParams)
setup.EthereumHandler.IssueAndWhitelistToken(setup.Ctx, token.IssueTokenParams)
setup.MultiversxHandler.IssueAndWhitelistToken(setup.Ctx, token.IssueTokenParams)
if token.IsNativeOnMvX {
setup.transferTokensToMvxTestKey(token) // TODO: (Next PRs) this will be moved an batch creation time
}
setup.ChainSimulator.GenerateBlocks(setup.Ctx, 10)

esdtBalanceForSafe := setup.MultiversxHandler.GetESDTChainSpecificTokenBalance(setup.Ctx, setup.MultiversxHandler.SafeAddress, token.AbstractTokenIdentifier)

setup.mutBalances.Lock()
setup.initMvxInitialBalancesForUniversalUnsafe(token,
Expand All @@ -188,6 +182,7 @@ func (setup *TestSetup) IssueAndConfigureTokens(tokens ...TestTokenParams) {
)
setup.mutBalances.Unlock()

esdtBalanceForSafe := setup.MultiversxHandler.GetESDTChainSpecificTokenBalance(setup.Ctx, setup.MultiversxHandler.SafeAddress, token.AbstractTokenIdentifier)
log.Info("recorded the ESDT balance for safe contract", "token", token.AbstractTokenIdentifier, "balance", esdtBalanceForSafe.String())
}

Expand Down Expand Up @@ -521,11 +516,17 @@ func (setup *TestSetup) createBatchOnMultiversXForToken(params TestTokenParams)
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

// TODO: transfer only required amount for deposit to the test key
setup.transferTokensToMvxTestKey(params, setup.AliceKeys)
setup.ChainSimulator.GenerateBlocks(setup.Ctx, 10)

setup.mutBalances.Lock()
setup.initMvxInitialBalancesForUniversalUnsafe(params, setup.AliceKeys.MvxAddress)
setup.mutBalances.Unlock()

_ = setup.createDepositOnMultiversxForToken(setup.AliceKeys, setup.BobKeys, params)
}

func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams) {
func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams, holder KeysHolder) {
depositValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToSendFromMvX == nil {
Expand All @@ -538,7 +539,7 @@ func (setup *TestSetup) transferTokensToMvxTestKey(params TestTokenParams) {
setup.MultiversxHandler.TransferToken(
setup.Ctx,
setup.OwnerKeys,
setup.AliceKeys,
holder,
depositValue,
params.IssueTokenParams,
)
Expand Down Expand Up @@ -592,10 +593,33 @@ func (setup *TestSetup) createBatchOnEthereumForToken(mvxCalleeScAddress sdkCore
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

// TODO: transfer only required amount for deposit to the test key
setup.transferTokensToEthTestKey(params, setup.AliceKeys)

setup.mutBalances.Lock()
setup.initEthInitialBalancesUnsafe(params, setup.AliceKeys.EthAddress)
setup.mutBalances.Unlock()

setup.createDepositOnEthereumForToken(setup.AliceKeys, setup.BobKeys, mvxCalleeScAddress, params)
}

func (setup *TestSetup) transferTokensToEthTestKey(params TestTokenParams, holder KeysHolder) {
depositValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToTransferToMvx == nil {
continue
}

depositValue.Add(depositValue, operation.ValueToTransferToMvx)
}

setup.EthereumHandler.TransferToken(
setup.Ctx,
params,
setup.DepositorKeys,
holder,
depositValue)
}

// SendFromEthereumToMultiversX will create the deposits that will be gathered in a batch on Ethereum
func (setup *TestSetup) SendFromEthereumToMultiversX(from KeysHolder, to KeysHolder, mvxTestCallerAddress sdkCore.AddressHandler, tokensParams ...TestTokenParams) {
for _, params := range tokensParams {
Expand Down

0 comments on commit 2932dd1

Please sign in to comment.