Skip to content

Commit

Permalink
duplicated code is the root of all evil
Browse files Browse the repository at this point in the history
- removed duplicated code
  • Loading branch information
iulianpascalau committed Oct 22, 2024
1 parent 16754f7 commit 4541076
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 57 deletions.
77 changes: 24 additions & 53 deletions integrationTests/relayers/slowTests/framework/multiversxHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,59 +941,6 @@ func (handler *MultiversxHandler) submitAggregatorBatchForKey(ctx context.Contex
return hash
}

// CreateDepositsOnMultiversxForToken will send the deposit transactions on MultiversX returning how many tokens should be minted on Ethereum
func (handler *MultiversxHandler) CreateDepositsOnMultiversxForToken(
ctx context.Context,
params TestTokenParams,
) *big.Int {
token := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(handler, token)

valueToMintOnEthereum := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToSendFromMvX == nil {
continue
}

valueToMintOnEthereum.Add(valueToMintOnEthereum, operation.ValueToSendFromMvX)

// transfer to sender tx
hash, txResult := handler.ChainSimulator.ScCall(
ctx,
handler.OwnerKeys.MvxSk,
handler.TestKeys.MvxAddress,
zeroStringValue,
createDepositGasLimit,
esdtTransferFunction,
[]string{
hex.EncodeToString([]byte(token.MvxUniversalToken)),
hex.EncodeToString(operation.ValueToSendFromMvX.Bytes())})
log.Info("transfer to sender tx executed", "hash", hash, "status", txResult.Status)

// send tx to safe contract
scCallParams := []string{
hex.EncodeToString([]byte(token.MvxUniversalToken)),
hex.EncodeToString(operation.ValueToSendFromMvX.Bytes()),
hex.EncodeToString([]byte(unwrapTokenCreateTransactionFunction)),
hex.EncodeToString([]byte(token.MvxChainSpecificToken)),
hex.EncodeToString(handler.TestKeys.EthAddress.Bytes()),
}
dataField := strings.Join(scCallParams, "@")

hash, txResult = handler.ChainSimulator.ScCall(
ctx,
handler.TestKeys.MvxSk,
handler.WrapperAddress,
zeroStringValue,
createDepositGasLimit+gasLimitPerDataByte*uint64(len(dataField)),
esdtTransferFunction,
scCallParams)
log.Info("MultiversX->Ethereum transaction sent", "hash", hash, "status", txResult.Status)
}

return valueToMintOnEthereum
}

// SendDepositTransactionFromMultiversx will send the deposit transaction from MultiversX
func (handler *MultiversxHandler) SendDepositTransactionFromMultiversx(ctx context.Context, token *TokenData, value *big.Int) {
// create transaction params
Expand Down Expand Up @@ -1072,6 +1019,30 @@ func (handler *MultiversxHandler) withdrawFees(ctx context.Context,
withdrawFunction, initialBalanceStr, finalBalanceStr, expectedDelta.String()))
}

// TransferToken is able to create an ESDT transfer
func (handler *MultiversxHandler) TransferToken(ctx context.Context, source KeysHolder, receiver KeysHolder, amount *big.Int, params TestTokenParams) {
tkData := handler.TokensRegistry.GetTokenData(params.AbstractTokenIdentifier)

// transfer to the test key, so it will have funds to carry on with the deposits
hash, txResult := handler.ChainSimulator.ScCall(
ctx,
source.MvxSk,
receiver.MvxAddress,
zeroStringValue,
createDepositGasLimit,
esdtTransferFunction,
[]string{
hex.EncodeToString([]byte(tkData.MvxUniversalToken)),
hex.EncodeToString(amount.Bytes())})

log.Info("transfer to tx executed",
"source address", source.MvxAddress.Bech32(),
"receiver", receiver.MvxAddress.Bech32(),
"token", tkData.MvxUniversalToken,
"amount", amount.String(),
"hash", hash, "status", txResult.Status)
}

func getHexBool(input bool) string {
if input {
return hexTrue
Expand Down
31 changes: 27 additions & 4 deletions integrationTests/relayers/slowTests/framework/testSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,29 +304,52 @@ func (setup *TestSetup) createBatchOnMultiversXForToken(params TestTokenParams)
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

valueToMintOnEthereum := setup.MultiversxHandler.CreateDepositsOnMultiversxForToken(setup.Ctx, params)

setup.transferTokensToTestKey(params)
valueToMintOnEthereum := setup.sendFromMultiversxToEthereumForToken(params)
setup.EthereumHandler.Mint(setup.Ctx, params, valueToMintOnEthereum)
}

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

depositValue.Add(depositValue, operation.ValueToSendFromMvX)
}

setup.MultiversxHandler.TransferToken(
setup.Ctx,
setup.OwnerKeys,
setup.TestKeys,
depositValue,
params,
)
}

// SendFromMultiversxToEthereum will create the deposits that will be gathered in a batch on MultiversX (without mint on Ethereum)
func (setup *TestSetup) SendFromMultiversxToEthereum(tokensParams ...TestTokenParams) {
for _, params := range tokensParams {
setup.sendFromMultiversxToEthereumForToken(params)
_ = setup.sendFromMultiversxToEthereumForToken(params)
}
}

func (setup *TestSetup) sendFromMultiversxToEthereumForToken(params TestTokenParams) {
func (setup *TestSetup) sendFromMultiversxToEthereumForToken(params TestTokenParams) *big.Int {
token := setup.GetTokenData(params.AbstractTokenIdentifier)
require.NotNil(setup, token)

depositValue := big.NewInt(0)
for _, operation := range params.TestOperations {
if operation.ValueToSendFromMvX == nil {
continue
}

depositValue.Add(depositValue, operation.ValueToSendFromMvX)
setup.MultiversxHandler.SendDepositTransactionFromMultiversx(setup.Ctx, token, operation.ValueToSendFromMvX)
}

return depositValue
}

// TestWithdrawTotalFeesOnEthereumForTokens will test the withdrawal functionality for the provided test tokens
Expand Down

0 comments on commit 4541076

Please sign in to comment.