Skip to content

Commit

Permalink
Merge pull request #335 from multiversx/init-supply-integration-tests
Browse files Browse the repository at this point in the history
Added integration test with the init supply functionality
  • Loading branch information
iulianpascalau authored Sep 9, 2024
2 parents d64eea4 + 8ebf544 commit e85281f
Show file tree
Hide file tree
Showing 31 changed files with 351 additions and 76 deletions.
Empty file modified clients/ethereum/contract/Bridge.go
100644 → 100755
Empty file.
68 changes: 55 additions & 13 deletions clients/ethereum/contract/ERC20Safe.go
100644 → 100755

Large diffs are not rendered by default.

Empty file modified clients/ethereum/contract/GenericERC20.go
100644 → 100755
Empty file.
Empty file modified clients/ethereum/contract/MintBurnERC20.go
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func TestRelayerShouldExecuteTransfersAndNotCatchErrors(t *testing.T) {
)
}

func TestRelayersShouldExecuteTransfersWithInitSupply(t *testing.T) {
usdcToken := GenerateTestUSDCToken()
usdcToken.InitialSupplyValue = "100000"

memeToken := GenerateTestMEMEToken()
memeToken.InitialSupplyValue = "200000"

_ = testRelayersWithChainSimulatorAndTokens(
t,
make(chan error),
usdcToken,
memeToken,
)
}

func testRelayersWithChainSimulatorAndTokens(tb testing.TB, manualStopChan chan error, tokens ...framework.TestTokenParams) *framework.TestSetup {
startsFromEthFlow, startsFromMvXFlow := createFlowsBasedOnToken(tb, tokens...)

Expand Down
53 changes: 47 additions & 6 deletions integrationTests/relayers/slowTests/framework/ethereumHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,38 @@ func (handler *EthereumHandler) IssueAndWhitelistToken(ctx context.Context, para

// whitelist eth token
auth, _ := bind.NewKeyedTransactorWithChainID(handler.OwnerKeys.EthSK, handler.ChainID)
tx, err := handler.SafeContract.WhitelistToken(auth, erc20Address, big.NewInt(ethMinAmountAllowedToTransfer), big.NewInt(ethMaxAmountAllowedToTransfer), params.IsMintBurnOnEth, params.IsNativeOnEth)
tx, err := handler.SafeContract.WhitelistToken(
auth,
erc20Address,
big.NewInt(ethMinAmountAllowedToTransfer),
big.NewInt(ethMaxAmountAllowedToTransfer),
params.IsMintBurnOnEth,
params.IsNativeOnEth,
zeroValueBigInt,
zeroValueBigInt,
zeroValueBigInt,
)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())

if len(params.InitialSupplyValue) > 0 {
if params.IsMintBurnOnEth {
mintAmount, ok := big.NewInt(0).SetString(params.InitialSupplyValue, 10)
require.True(handler, ok)

tx, err = handler.SafeContract.InitSupplyMintBurn(auth, erc20Address, mintAmount, zeroValueBigInt)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
} else {
// reset the tokens value for the safe contract, so it will "know" about the balance that it has in the ERC20 contract
tx, err = handler.SafeContract.ResetTotalBalance(auth, erc20Address)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())
}
}
}

func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, params IssueTokenParams) (common.Address, ERC20Contract) {
Expand Down Expand Up @@ -328,20 +356,33 @@ func (handler *EthereumHandler) deployTestERC20Contract(ctx context.Context, par
require.NoError(handler, err)

// mint the address that will create the transfers
handler.mintTokens(ctx, ethGenericTokenContract, params.ValueToMintOnEth, handler.TestKeys.EthAddress)
if len(params.InitialSupplyValue) > 0 {
handler.mintTokens(ctx, ethGenericTokenContract, params.InitialSupplyValue, handler.SafeAddress)
}

return ethGenericTokenAddress, ethGenericTokenContract
}

func (handler *EthereumHandler) mintTokens(
ctx context.Context,
ethGenericTokenContract *contract.GenericERC20,
value string,
recipientAddress common.Address,
) {
auth, _ := bind.NewKeyedTransactorWithChainID(handler.DepositorKeys.EthSK, handler.ChainID)

mintAmount, ok := big.NewInt(0).SetString(params.ValueToMintOnEth, 10)
mintAmount, ok := big.NewInt(0).SetString(value, 10)
require.True(handler, ok)
tx, err := ethGenericTokenContract.Mint(auth, handler.TestKeys.EthAddress, mintAmount)

tx, err := ethGenericTokenContract.Mint(auth, recipientAddress, mintAmount)
require.NoError(handler, err)
handler.SimulatedChain.Commit()
handler.checkEthTxResult(ctx, tx.Hash())

balance, err := ethGenericTokenContract.BalanceOf(nil, handler.TestKeys.EthAddress)
balance, err := ethGenericTokenContract.BalanceOf(nil, recipientAddress)
require.NoError(handler, err)
require.Equal(handler, mintAmount.String(), balance.String())

return ethGenericTokenAddress, ethGenericTokenContract
}

// CreateBatchOnEthereum will create a batch on Ethereum using the provided tokens parameters list
Expand Down
10 changes: 6 additions & 4 deletions integrationTests/relayers/slowTests/framework/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/multiversx/mx-chain-crypto-go/signing"
"github.com/multiversx/mx-chain-crypto-go/signing/ed25519"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -64,6 +62,9 @@ func NewKeysStore(
keysStore.generateRelayersKeys(numRelayers)
keysStore.SCExecutorKeys = keysStore.generateKey("")
keysStore.OwnerKeys = keysStore.generateKey(ethOwnerSK)
log.Info("generated owner",
"MvX address", keysStore.OwnerKeys.MvxAddress.Bech32(),
"Eth address", keysStore.OwnerKeys.EthAddress.String())
keysStore.DepositorKeys = keysStore.generateKey(ethDepositorSK)
keysStore.TestKeys = keysStore.generateKey(ethTestSk)

Expand All @@ -79,7 +80,9 @@ func (keyStore *KeysStore) generateRelayersKeys(numKeys int) {
require.Nil(keyStore, err)

relayerKeys := keyStore.generateKey(string(relayerETHSKBytes))
log.Info("generated relayer", "index", i, "address", relayerKeys.MvxAddress.Bytes())
log.Info("generated relayer", "index", i,
"MvX address", relayerKeys.MvxAddress.Bech32(),
"Eth address", relayerKeys.EthAddress.String())

keyStore.RelayersKeys = append(keyStore.RelayersKeys, relayerKeys)

Expand Down Expand Up @@ -144,7 +147,6 @@ func (keyStore *KeysStore) WalletsToFundOnMultiversX() []string {

// GenerateMvxPrivatePublicKey will generate a new keys holder instance that will hold only the MultiversX generated keys
func GenerateMvxPrivatePublicKey(tb testing.TB) KeysHolder {
keyGenerator := signing.NewKeyGenerator(ed25519.NewEd25519())
sk, pk := keyGenerator.GeneratePair()

skBytes, err := sk.ToByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const (
createTransactionFunction = "createTransaction"
unwrapTokenFunction = "unwrapToken"
setupBridgedTokenWrapperFunction = "setBridgedTokensWrapper"
initSupplyMintBurnEsdtSafe = "initSupplyMintBurnEsdtSafe"
initSupplyEsdtSafe = "initSupplyEsdtSafe"
)

var (
Expand Down Expand Up @@ -616,9 +618,55 @@ func (handler *MultiversxHandler) IssueAndWhitelistToken(ctx context.Context, pa
hex.EncodeToString([]byte(mvxChainSpecificToken)),
hex.EncodeToString([]byte(params.MvxChainSpecificTokenTicker)),
getHexBool(params.IsMintBurnOnMvX),
getHexBool(params.IsNativeOnMvX)})
getHexBool(params.IsNativeOnMvX),
hex.EncodeToString(zeroValueBigInt.Bytes()), // total_balance
hex.EncodeToString(zeroValueBigInt.Bytes()), // mint_balance
hex.EncodeToString(zeroValueBigInt.Bytes()), // burn_balance
})
log.Info("whitelist token tx executed", "hash", hash, "status", txResult.Status)

// set initial supply
if len(params.InitialSupplyValue) > 0 {
initialSupply, okConvert := big.NewInt(0).SetString(params.InitialSupplyValue, 10)
require.True(handler, okConvert)

if params.IsMintBurnOnMvX {
hash, txResult = handler.ChainSimulator.ScCall(
ctx,
handler.OwnerKeys.MvxSk,
handler.MultisigAddress,
zeroStringValue,
setCallsGasLimit,
initSupplyMintBurnEsdtSafe,
[]string{
hex.EncodeToString([]byte(mvxChainSpecificToken)),
hex.EncodeToString(initialSupply.Bytes()),
hex.EncodeToString([]byte{0}),
},
)
log.Info("initial supply tx executed", "hash", hash, "status", txResult.Status,
"initial mint", params.InitialSupplyValue, "initial burned", "0")
} else {
hash, txResult = handler.ChainSimulator.ScCall(
ctx,
handler.OwnerKeys.MvxSk,
handler.MultisigAddress,
zeroStringValue,
setCallsGasLimit,
esdtTransferFunction,
[]string{
hex.EncodeToString([]byte(mvxChainSpecificToken)),
hex.EncodeToString(initialSupply.Bytes()),
hex.EncodeToString([]byte(initSupplyEsdtSafe)),
hex.EncodeToString([]byte(mvxChainSpecificToken)),
hex.EncodeToString(initialSupply.Bytes()),
})

log.Info("initial supply tx executed", "hash", hash, "status", txResult.Status,
"initial value", params.InitialSupplyValue)
}
}

// setPairDecimals on aggregator
hash, txResult = handler.ChainSimulator.ScCall(
ctx,
Expand Down
6 changes: 6 additions & 0 deletions integrationTests/relayers/slowTests/framework/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// IssueTokenParams the parameters when issuing a new token
type IssueTokenParams struct {
InitialSupplyParams
AbstractTokenIdentifier string

// MultiversX
Expand All @@ -29,6 +30,11 @@ type IssueTokenParams struct {
IsNativeOnEth bool
}

// InitialSupplyParams represents the initial supply parameters
type InitialSupplyParams struct {
InitialSupplyValue string
}

// TokenOperations defines a token operation in a test. Usually this can define one or to deposits in a batch
type TokenOperations struct {
ValueToTransferToMvx *big.Int
Expand Down
Empty file.
Empty file.
51 changes: 51 additions & 0 deletions integrationTests/relayers/slowTests/testdata/contracts/eth/ERC20Safe.abi.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,29 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "mintAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "burnAmount",
"type": "uint256"
}
],
"name": "initSupplyMintBurn",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "initialize",
Expand Down Expand Up @@ -706,6 +729,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
}
],
"name": "resetTotalBalance",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -926,6 +962,21 @@
"internalType": "bool",
"name": "native",
"type": "bool"
},
{
"internalType": "uint256",
"name": "totalBalance",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "mintBalance",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "burnBalance",
"type": "uint256"
}
],
"name": "whitelistToken",
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/relayers/slowTests/testdata/contracts/eth/ERC20Safe.hex
100644 → 100755

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"buildInfo": {
"rustc": {
"version": "1.80.0",
"commitHash": "051478957371ee0084a7c0913941d2a8c4757bb9",
"commitDate": "2024-07-21",
"version": "1.78.0",
"commitHash": "9b00956e56009bab2aa15d7bff10916599e3d6d6",
"commitDate": "2024-04-29",
"channel": "Stable",
"short": "rustc 1.80.0 (051478957 2024-07-21)"
"short": "rustc 1.78.0 (9b00956e5 2024-04-29)"
},
"contractCrate": {
"name": "bridge-proxy",
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"buildInfo": {
"rustc": {
"version": "1.80.0",
"commitHash": "051478957371ee0084a7c0913941d2a8c4757bb9",
"commitDate": "2024-07-21",
"version": "1.78.0",
"commitHash": "9b00956e56009bab2aa15d7bff10916599e3d6d6",
"commitDate": "2024-04-29",
"channel": "Stable",
"short": "rustc 1.80.0 (051478957 2024-07-21)"
"short": "rustc 1.78.0 (9b00956e5 2024-04-29)"
},
"contractCrate": {
"name": "bridged-tokens-wrapper",
Expand Down
Binary file not shown.
Loading

0 comments on commit e85281f

Please sign in to comment.