Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Add getTransactionCount method and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Feb 21, 2023
1 parent cbbf322 commit c9c03a6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
26 changes: 25 additions & 1 deletion contracts/clients/src/BlsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import Aggregator, { BundleReceipt } from "./Aggregator";
import BlsSigner, { UncheckedBlsSigner, _constructorGuard } from "./BlsSigner";
import poll from "./helpers/poll";
import BlsWalletWrapper from "./BlsWalletWrapper";
import { AggregatorUtilities__factory } from "../typechain-types";
import {
AggregatorUtilities__factory,
BLSWallet__factory,
} from "../typechain-types";

export default class BlsProvider extends ethers.providers.JsonRpcProvider {
readonly aggregator: Aggregator;
Expand Down Expand Up @@ -165,6 +168,27 @@ export default class BlsProvider extends ethers.providers.JsonRpcProvider {
);
}

override async getTransactionCount(
address: string | Promise<string>,
blockTag?:
| ethers.providers.BlockTag
| Promise<ethers.providers.BlockTag>
| undefined,
): Promise<number> {
const walletContract = BLSWallet__factory.connect(await address, this);

const code = await walletContract.provider.getCode(address, blockTag);

if (code === "0x") {
// The wallet doesn't exist yet. Wallets are lazily created, so the nonce
// is effectively zero, since that will be accepted as valid for a first
// operation that also creates the wallet.
return 0;
}

return Number(await walletContract.nonce());
}

async _getTransactionReceipt(
transactionHash: string,
confirmations: number,
Expand Down
49 changes: 38 additions & 11 deletions contracts/test-integration/BlsProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable camelcase */
import chai, { expect } from "chai";
import { ethers } from "ethers";
import { BigNumber, ethers } from "ethers";
import { formatEther, parseEther } from "ethers/lib/utils";

import {
Expand Down Expand Up @@ -483,25 +483,52 @@ describe("BlsProvider", () => {
expect(isProviderWithInvalidProvider).to.equal(false);
});

it("should return the number of transactions an address has ever sent", async function () {
it("should return the number of transactions an address has sent", async function () {
// Arrange
const transaction = {
value: BigNumber.from(1),
to: ethers.Wallet.createRandom().address,
};
const address = await blsSigner.getAddress();
const expectedTransactionCount = await regularProvider.getTransactionCount(

const expectedFirstTransactionCount = 0;
const expectedSecondTransactionCount = 1;

// Act
const firstTransactionCount = await blsProvider.getTransactionCount(
address,
);

const sendTransaction = await blsSigner.sendTransaction(transaction);
await sendTransaction.wait();

const secondTransactionCount = await blsProvider.getTransactionCount(
address,
);
const expectedTransactionCountAtEaliestBlockTag =
await blsProvider.getTransactionCount(address, "earliest");

// Assert
expect(firstTransactionCount).to.equal(expectedFirstTransactionCount);
expect(secondTransactionCount).to.equal(expectedSecondTransactionCount);
});

it("should return the number of transactions an address has sent at a specified block tag", async function () {
// Arrange
const expectedTransactionCount = 0;

const sendTransaction = await blsSigner.sendTransaction({
value: BigNumber.from(1),
to: ethers.Wallet.createRandom().address,
});
await sendTransaction.wait();

// Act
const transactionCount = await blsProvider.getTransactionCount(address);
const transactionCountAtEaliestBlockTag =
await blsProvider.getTransactionCount(address, "earliest");
const transactionCount = await blsProvider.getTransactionCount(
await blsSigner.getAddress(),
"earliest",
);

// Assert
expect(transactionCount).to.equal(expectedTransactionCount);
expect(transactionCountAtEaliestBlockTag).to.equal(
expectedTransactionCountAtEaliestBlockTag,
);
});

it("should return the block from the network", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ describe("Provider tests", function () {

// Act
const storage1 = await blsProvider.getStorageAt(mockERC20.address, 1);
console.log(storage1);

const storage2 = await blsProvider.getStorageAt(mockERC20.address, 2);

// Assert
Expand Down
18 changes: 11 additions & 7 deletions contracts/test-integration/BlsSigner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,26 +574,30 @@ describe("BlsSigner", () => {

it("should get the number of transactions the account has sent", async () => {
// Arrange
const expectedTransactionCount = await regularProvider.getTransactionCount(
blsSigner.wallet.address,
);
const expectedTransactionCount = await blsSigner.wallet.Nonce();

// Act
const result = await blsSigner.getTransactionCount();
const transactionCount = await blsSigner.getTransactionCount();

// Assert
expect(result).to.equal(expectedTransactionCount);
expect(transactionCount).to.equal(expectedTransactionCount);
});

it("should get the number of transactions the account has sent at the specified block tag", async () => {
// Arrange
const expectedTransactionCount = 0;

const sendTransaction = await blsSigner.sendTransaction({
value: BigNumber.from(1),
to: ethers.Wallet.createRandom().address,
});
await sendTransaction.wait();

// Act
const result = await blsSigner.getTransactionCount("earliest");
const transactionCount = await blsSigner.getTransactionCount("earliest");

// Assert
expect(result).to.equal(expectedTransactionCount);
expect(transactionCount).to.equal(expectedTransactionCount);
});

it("should return the result of call using the transactionRequest, with the signer account address being used as the from field", async () => {
Expand Down

0 comments on commit c9c03a6

Please sign in to comment.