diff --git a/CHANGELOG.md b/CHANGELOG.md index 63b574cb3..2a45cd410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#2013](https://github.com/NibiruChain/nibiru/pull/2013) - chore(evm): Set appropriate gas value for the required gas of the "IFunToken.sol" precompile. - [#2014](https://github.com/NibiruChain/nibiru/pull/2014) - feat(evm): Emit block bloom event in EndBlock hook. - [#2019](https://github.com/NibiruChain/nibiru/pull/2019) - chore(evm): enabled debug rpc api on localnet. +- [#2020](https://github.com/NibiruChain/nibiru/pull/2020) - test(evm): e2e tests for debug namespace #### Dapp modules: perp, spot, oracle, etc diff --git a/e2e/evm/README.md b/e2e/evm/README.md index 9e8d359d8..508956d15 100644 --- a/e2e/evm/README.md +++ b/e2e/evm/README.md @@ -41,7 +41,7 @@ bun test v1.1.12 (43f0913c) test/erc20.test.ts: ✓ ERC-20 contract tests > should send properly [8410.41ms] -test/basic_queries.test.ts: +test/native_transfer.test.ts: ✓ Basic Queries > Simple transfer, balance check [4251.02ms] test/contract_infinite_loop_gas.test.ts: diff --git a/e2e/evm/test/debug_queries.test.ts b/e2e/evm/test/debug_queries.test.ts new file mode 100644 index 000000000..2d6a4909a --- /dev/null +++ b/e2e/evm/test/debug_queries.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, it, beforeAll } from "@jest/globals" +import { parseEther } from "ethers" +import { provider } from "./setup" +import { alice, deployERC20 } from "./utils" + +describe("debug queries", () => { + let contractAddress + let txHash + let txIndex + let blockNumber + let blockHash + + beforeAll(async () => { + // Deploy ERC-20 contract + const contract = await deployERC20() + contractAddress = await contract.getAddress() + + // Execute some contract TX + const txResponse = await contract.transfer(alice, parseEther("0.01")) + await txResponse.wait(1, 5e3) + + const receipt = await provider.getTransactionReceipt(txResponse.hash) + txHash = txResponse.hash + txIndex = txResponse.index + blockNumber = receipt.blockNumber + blockHash = receipt.blockHash + }, 20e3) + + it("debug_traceBlockByNumber", async () => { + const traceResult = await provider.send("debug_traceBlockByNumber", [ + blockNumber, + ]) + expectTrace(traceResult) + }) + + it("debug_traceBlockByHash", async () => { + const traceResult = await provider.send("debug_traceBlockByHash", [ + blockHash, + ]) + expectTrace(traceResult) + }) + + it("debug_traceTransaction", async () => { + const traceResult = await provider.send("debug_traceTransaction", [txHash]) + expectTrace([{ result: traceResult }]) + }) + + // TODO: implement that in EVM + it.skip("debug_getBadBlocks", async () => { + const traceResult = await provider.send("debug_getBadBlocks", [txHash]) + expect(traceResult).toBeDefined() + }) + + // TODO: implement that in EVM + it.skip("debug_storageRangeAt", async () => { + const traceResult = await provider.send("debug_storageRangeAt", [ + blockNumber, + txIndex, + contractAddress, + "0x0", + 100, + ]) + expect(traceResult).toBeDefined() + }) +}) + +const expectTrace = (traceResult: any[]) => { + expect(traceResult).toBeDefined() + expect(traceResult.length).toBeGreaterThan(0) + + const trace = traceResult[0]["result"] + expect(trace).toHaveProperty("failed", false) + expect(trace).toHaveProperty("gas") + expect(trace).toHaveProperty("returnValue") + expect(trace).toHaveProperty("structLogs") +} diff --git a/e2e/evm/test/basic_queries.test.ts b/e2e/evm/test/eth_queries.test.ts similarity index 87% rename from e2e/evm/test/basic_queries.test.ts rename to e2e/evm/test/eth_queries.test.ts index a974a70a1..a06e894ea 100644 --- a/e2e/evm/test/basic_queries.test.ts +++ b/e2e/evm/test/eth_queries.test.ts @@ -1,56 +1,10 @@ import { describe, expect, it } from "@jest/globals" -import { - toBigInt, - Wallet, - parseEther, - keccak256, - AbiCoder, - ethers, -} from "ethers" +import { parseEther, keccak256, AbiCoder } from "ethers" import { account, provider } from "./setup" -import { - SendNibiCompiled__factory, - TestERC20Compiled__factory, -} from "../types/ethers-contracts" +import { SendNibiCompiled__factory } from "../types/ethers-contracts" import { alice, deployERC20, sendTestNibi } from "./utils" -describe("Basic Queries", () => { - it("Simple transfer, balance check", async () => { - const amountToSend = toBigInt(5e12) * toBigInt(1e6) // unibi - const senderBalanceBefore = await provider.getBalance(account) - const recipientBalanceBefore = await provider.getBalance(alice) - expect(senderBalanceBefore).toBeGreaterThan(0) - expect(recipientBalanceBefore).toEqual(BigInt(0)) - - // Execute EVM transfer - const transaction = { - gasLimit: toBigInt(100e3), - to: alice, - value: amountToSend, - } - const txResponse = await account.sendTransaction(transaction) - await txResponse.wait(1, 10e3) - expect(txResponse).toHaveProperty("blockHash") - - const senderBalanceAfter = await provider.getBalance(account) - const recipientBalanceAfter = await provider.getBalance(alice) - - // Assert balances with logging - const tenPow12 = toBigInt(1e12) - const gasUsed = 50000n // 50k gas for the transaction - const txCostMicronibi = amountToSend / tenPow12 + gasUsed - const txCostWei = txCostMicronibi * tenPow12 - const expectedSenderWei = senderBalanceBefore - txCostWei - console.debug("DEBUG should send via transfer method %o:", { - senderBalanceBefore, - amountToSend, - expectedSenderWei, - senderBalanceAfter, - }) - expect(senderBalanceAfter).toEqual(expectedSenderWei) - expect(recipientBalanceAfter).toEqual(amountToSend) - }, 20e3) - +describe("eth queries", () => { it("eth_accounts", async () => { const accounts = await provider.listAccounts() expect(accounts).not.toHaveLength(0) diff --git a/e2e/evm/test/native_transfer.test.ts b/e2e/evm/test/native_transfer.test.ts new file mode 100644 index 000000000..d25ef4cc9 --- /dev/null +++ b/e2e/evm/test/native_transfer.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from "@jest/globals" +import { toBigInt } from "ethers" +import { account, provider } from "./setup" +import { alice } from "./utils" + +describe("native transfer", () => { + it("simple transfer, balance check", async () => { + const amountToSend = toBigInt(5e12) * toBigInt(1e6) // unibi + const senderBalanceBefore = await provider.getBalance(account) + const recipientBalanceBefore = await provider.getBalance(alice) + expect(senderBalanceBefore).toBeGreaterThan(0) + expect(recipientBalanceBefore).toEqual(BigInt(0)) + + // Execute EVM transfer + const transaction = { + gasLimit: toBigInt(100e3), + to: alice, + value: amountToSend, + } + const txResponse = await account.sendTransaction(transaction) + await txResponse.wait(1, 10e3) + expect(txResponse).toHaveProperty("blockHash") + + const senderBalanceAfter = await provider.getBalance(account) + const recipientBalanceAfter = await provider.getBalance(alice) + + // Assert balances with logging + const tenPow12 = toBigInt(1e12) + const gasUsed = 50000n // 50k gas for the transaction + const txCostMicronibi = amountToSend / tenPow12 + gasUsed + const txCostWei = txCostMicronibi * tenPow12 + const expectedSenderWei = senderBalanceBefore - txCostWei + console.debug("DEBUG should send via transfer method %o:", { + senderBalanceBefore, + amountToSend, + expectedSenderWei, + senderBalanceAfter, + }) + expect(senderBalanceAfter).toEqual(expectedSenderWei) + expect(recipientBalanceAfter).toEqual(amountToSend) + }, 20e3) +})