|
| 1 | +import * as tests from "../../tests"; |
| 2 | +import Receive from "./getBalance.sol"; |
| 3 | +import assert from "assert"; |
| 4 | +import { ethers } from "ethers"; |
| 5 | + |
| 6 | +const blockchain = tests.blockchain; |
| 7 | +const wallet = tests.wallet; |
| 8 | + |
| 9 | +if (!blockchain || !wallet) { |
| 10 | + throw "not ready"; |
| 11 | +} |
| 12 | + |
| 13 | +describe("getTransactionCount", () => { |
| 14 | + let contract: ethers.Contract; |
| 15 | + before(async () => { |
| 16 | + const deployer = (await blockchain.listAccounts())[0]; |
| 17 | + const factory = ethers.ContractFactory.fromSolidity( |
| 18 | + Receive.Receive, |
| 19 | + deployer |
| 20 | + ); |
| 21 | + |
| 22 | + contract = await factory.deploy(); |
| 23 | + await blockchain.send("evm_mine", [{ blocks: 1 }]); |
| 24 | + await contract.deploymentTransaction()?.wait(1); |
| 25 | + }); |
| 26 | + |
| 27 | + it("sending transaction from eoa increases nonce", async () => { |
| 28 | + const src = (await blockchain.listAccounts())[0]; |
| 29 | + const dest = (await wallet.listAccounts())[0]; |
| 30 | + |
| 31 | + const balance = "0x100000000000000000000"; |
| 32 | + await blockchain.send("evm_setAccountBalance", [src.address, balance]); |
| 33 | + |
| 34 | + const walletInitalNonce = await wallet.getTransactionCount(src.address); |
| 35 | + const ganacheInitalNonce = await blockchain.getTransactionCount( |
| 36 | + src.address |
| 37 | + ); |
| 38 | + |
| 39 | + assert.equal( |
| 40 | + walletInitalNonce.toString(), |
| 41 | + ganacheInitalNonce.toString(), |
| 42 | + "wallet's nonce matches ganache's before sending transaction" |
| 43 | + ); |
| 44 | + |
| 45 | + const response = await src.sendTransaction({ |
| 46 | + to: dest, |
| 47 | + value: 0n, |
| 48 | + }); |
| 49 | + |
| 50 | + await blockchain.send("evm_mine", [{ blocks: 1 }]); |
| 51 | + |
| 52 | + const transaction = await wallet.getTransaction(response.hash); |
| 53 | + if (!transaction) { |
| 54 | + throw "no transaction"; |
| 55 | + } |
| 56 | + console.log(transaction); |
| 57 | + await transaction.wait(1); |
| 58 | + |
| 59 | + const walletFinalNonce = await wallet.send("eth_getTransactionCount", [ |
| 60 | + src.address, |
| 61 | + ]); |
| 62 | + const ganacheFinalNonce = await blockchain.send( |
| 63 | + "eth_getTransactionCount", |
| 64 | + [src.address] |
| 65 | + ); |
| 66 | + |
| 67 | + assert.equal( |
| 68 | + walletFinalNonce.toString(), |
| 69 | + ganacheFinalNonce.toString(), |
| 70 | + "wallet's nonce matches ganache's after sending transaction" |
| 71 | + ); |
| 72 | + |
| 73 | + const expected = 1 + walletInitalNonce; |
| 74 | + |
| 75 | + assert.equal(walletFinalNonce, expected); |
| 76 | + }); |
| 77 | +}); |
0 commit comments