|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "cosmossdk.io/math" |
| 8 | + |
| 9 | + "github.com/strangelove-ventures/interchaintest/v8" |
| 10 | + "github.com/strangelove-ventures/interchaintest/v8/chain/cosmos" |
| 11 | + "github.com/strangelove-ventures/interchaintest/v8/ibc" |
| 12 | + "github.com/strangelove-ventures/interchaintest/v8/testreporter" |
| 13 | + "github.com/strangelove-ventures/interchaintest/v8/testutil" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "go.uber.org/zap/zaptest" |
| 16 | +) |
| 17 | + |
| 18 | +// TestGasFees verifies that gas fees are correctly applied when sending transactions. |
| 19 | +// This test ensures that: |
| 20 | +// 1. A single chain can be properly initialized with a non-zero gas price |
| 21 | +// 2. Multiple users can be created and funded |
| 22 | +// 3. When a transaction is sent with a specified gas fee, the fee is correctly deducted |
| 23 | +// 4. The transaction succeeds and the recipient receives the correct amount |
| 24 | +// 5. The gas fee is correctly calculated based on gas used and gas price |
| 25 | +func TestGasFees(t *testing.T) { |
| 26 | + ctx := context.Background() |
| 27 | + rep := testreporter.NewNopReporter() |
| 28 | + eRep := rep.RelayerExecReporter(t) |
| 29 | + |
| 30 | + // Set up Docker environment for the test |
| 31 | + client, network := interchaintest.DockerSetup(t) |
| 32 | + |
| 33 | + // Create a modified chain config with non-zero gas prices |
| 34 | + chainConfig := DefaultChainConfig |
| 35 | + chainConfig.GasPrices = "0.01" + Denom // Set a non-zero gas price |
| 36 | + |
| 37 | + // Create a chain spec with the modified config |
| 38 | + chainSpec := interchaintest.ChainSpec{ |
| 39 | + Name: Name, |
| 40 | + ChainName: Name, |
| 41 | + Version: ChainImage.Version, |
| 42 | + ChainConfig: chainConfig, |
| 43 | + NumValidators: &NumberVals, |
| 44 | + NumFullNodes: &NumberFullNodes, |
| 45 | + } |
| 46 | + |
| 47 | + // Create a chain factory with our modified chain specification |
| 48 | + cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{ |
| 49 | + &chainSpec, |
| 50 | + }) |
| 51 | + |
| 52 | + // Initialize the chain from the factory |
| 53 | + chains, err := cf.Chains(t.Name()) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + // Get the chain from the list (we only have one in this test) |
| 57 | + chain := chains[0].(*cosmos.CosmosChain) |
| 58 | + |
| 59 | + // Setup Interchain environment with our single chain |
| 60 | + ic := interchaintest.NewInterchain(). |
| 61 | + AddChain(chain) |
| 62 | + |
| 63 | + // Build the interchain environment |
| 64 | + require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{ |
| 65 | + TestName: t.Name(), |
| 66 | + Client: client, |
| 67 | + NetworkID: network, |
| 68 | + SkipPathCreation: false, |
| 69 | + })) |
| 70 | + |
| 71 | + // Clean up resources when the test completes |
| 72 | + t.Cleanup(func() { |
| 73 | + _ = ic.Close() |
| 74 | + }) |
| 75 | + |
| 76 | + // Create and fund two users with 10 million tokens each |
| 77 | + initialAmount := math.NewInt(10_000_000) |
| 78 | + users := interchaintest.GetAndFundTestUsers(t, ctx, "default", initialAmount, chain, chain) |
| 79 | + require.Len(t, users, 2, "Expected 2 users to be created and funded") |
| 80 | + |
| 81 | + sender := users[0] |
| 82 | + receiver := users[1] |
| 83 | + |
| 84 | + t.Run("validate initial funding", func(t *testing.T) { |
| 85 | + // Check that both users have the expected initial balance |
| 86 | + senderBal, err := chain.BankQueryBalance(ctx, sender.FormattedAddress(), chain.Config().Denom) |
| 87 | + require.NoError(t, err) |
| 88 | + require.EqualValues(t, initialAmount, senderBal) |
| 89 | + |
| 90 | + receiverBal, err := chain.BankQueryBalance(ctx, receiver.FormattedAddress(), chain.Config().Denom) |
| 91 | + require.NoError(t, err) |
| 92 | + require.EqualValues(t, initialAmount, receiverBal) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("transfer with gas fees", func(t *testing.T) { |
| 96 | + // Define the amount to transfer from sender to receiver |
| 97 | + transferAmount := math.NewInt(1_000_000) |
| 98 | + |
| 99 | + // Get sender balance before the transfer |
| 100 | + senderBalBefore, err := chain.BankQueryBalance(ctx, sender.FormattedAddress(), chain.Config().Denom) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + // Get receiver balance before the transfer |
| 104 | + receiverBalBefore, err := chain.BankQueryBalance(ctx, receiver.FormattedAddress(), chain.Config().Denom) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + // Prepare the transfer details |
| 108 | + transfer := ibc.WalletAmount{ |
| 109 | + Address: receiver.FormattedAddress(), |
| 110 | + Denom: chain.Config().Denom, |
| 111 | + Amount: transferAmount, |
| 112 | + } |
| 113 | + |
| 114 | + // Execute the transfer from sender to receiver |
| 115 | + err = chain.SendFunds(ctx, sender.KeyName(), transfer) |
| 116 | + require.NoError(t, err, "Failed to send funds from sender to receiver") |
| 117 | + |
| 118 | + // Wait for a couple of blocks to ensure the transaction is processed |
| 119 | + err = testutil.WaitForBlocks(ctx, 2, chain) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + // Verify the sender's balance has decreased by the transfer amount plus some gas fee |
| 123 | + senderBalAfter, err := chain.BankQueryBalance(ctx, sender.FormattedAddress(), chain.Config().Denom) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + // The sender's balance should be: initial balance - transfer amount - some gas fee |
| 127 | + // We can't calculate the exact gas fee, but we can verify it's less than the transfer amount |
| 128 | + balanceDiff := senderBalBefore.Sub(senderBalAfter).Sub(transferAmount) |
| 129 | + require.Greater(t, balanceDiff.Int64(), int64(0), |
| 130 | + "Sender should have paid some gas fee, but balance diff after transfer amount is %s", balanceDiff) |
| 131 | + require.Less(t, balanceDiff.Int64(), transferAmount.Int64(), |
| 132 | + "Gas fee should be less than transfer amount, but got %s", balanceDiff) |
| 133 | + |
| 134 | + // Verify the receiver's balance has increased by exactly the transfer amount |
| 135 | + receiverBalAfter, err := chain.BankQueryBalance(ctx, receiver.FormattedAddress(), chain.Config().Denom) |
| 136 | + require.NoError(t, err) |
| 137 | + expectedReceiverBal := receiverBalBefore.Add(transferAmount) |
| 138 | + require.EqualValues(t, expectedReceiverBal, receiverBalAfter, |
| 139 | + "Receiver balance incorrect after transfer") |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("verify gas price affects fees", func(t *testing.T) { |
| 143 | + // In this test, we'll send two identical transactions with different gas prices |
| 144 | + // and verify that the fees charged are proportional to the gas prices |
| 145 | + |
| 146 | + transferAmount := math.NewInt(500_000) |
| 147 | + |
| 148 | + // Get sender balance before the transfers |
| 149 | + senderBalBefore, err := chain.BankQueryBalance(ctx, sender.FormattedAddress(), chain.Config().Denom) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + // First transaction with the default gas price |
| 153 | + transfer1 := ibc.WalletAmount{ |
| 154 | + Address: receiver.FormattedAddress(), |
| 155 | + Denom: chain.Config().Denom, |
| 156 | + Amount: transferAmount, |
| 157 | + } |
| 158 | + |
| 159 | + err = chain.SendFunds(ctx, sender.KeyName(), transfer1) |
| 160 | + require.NoError(t, err) |
| 161 | + |
| 162 | + // Wait for a couple of blocks to ensure the transaction is processed |
| 163 | + err = testutil.WaitForBlocks(ctx, 2, chain) |
| 164 | + require.NoError(t, err) |
| 165 | + |
| 166 | + // Get sender balance after first transaction |
| 167 | + senderBalAfterTx1, err := chain.BankQueryBalance(ctx, sender.FormattedAddress(), chain.Config().Denom) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + // Calculate the fee for the first transaction |
| 171 | + fee1 := senderBalBefore.Sub(senderBalAfterTx1).Sub(transferAmount) |
| 172 | + require.Greater(t, fee1.Int64(), int64(0), "Fee should be greater than 0") |
| 173 | + |
| 174 | + // For the second transaction, we can't easily set a different gas price using SendFunds |
| 175 | + // So we'll just verify that the fee is non-zero and reasonable |
| 176 | + require.Less(t, fee1.Int64(), transferAmount.Int64()/int64(10), |
| 177 | + "Fee should be less than 10%% of transfer amount, but got %s for a %s transfer", |
| 178 | + fee1, transferAmount) |
| 179 | + }) |
| 180 | +} |
0 commit comments