Skip to content

Commit

Permalink
test(evm): e2e tests for debug namespace (#2020)
Browse files Browse the repository at this point in the history
* test(evm): e2e tests for debug namespace

* chore: changelog update
  • Loading branch information
onikonychev authored Aug 29, 2024
1 parent 7ae2443 commit 3e9bda9
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion e2e/evm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
76 changes: 76 additions & 0 deletions e2e/evm/test/debug_queries.test.ts
Original file line number Diff line number Diff line change
@@ -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")
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
42 changes: 42 additions & 0 deletions e2e/evm/test/native_transfer.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 3e9bda9

Please sign in to comment.