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

Commit

Permalink
Cleanup test code & add contract interaction gas estimate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Mar 14, 2023
1 parent b252372 commit e73949a
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 274 deletions.
84 changes: 83 additions & 1 deletion contracts/clients/test/BlsProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("BlsProvider", () => {
rpcUrl = "http://localhost:8545";
network = {
name: "localhost",
chainId: 0x7a69,
chainId: 0x539, // 1337
};

privateKey = await BlsWalletWrapper.getRandomBlsPrivateKey();
Expand Down Expand Up @@ -90,6 +90,7 @@ describe("BlsProvider", () => {
// Arrange
const transaction = {
value: parseEther("1"),
// Explicitly omit 'to'
};

// Act
Expand All @@ -107,6 +108,7 @@ describe("BlsProvider", () => {
const transaction = {
value: parseEther("1"),
to: ethers.Wallet.createRandom().address,
// Explicitly omit 'from'
};

// Act
Expand All @@ -129,4 +131,84 @@ describe("BlsProvider", () => {
// Assert
expect(connection).to.deep.equal(expectedConnection);
});

it("should throw an error when sending an invalid signed transaction", async () => {
// Arrange
const invalidTransaction = "Invalid signed transaction";

// Act
const result = async () =>
await blsProvider.sendTransaction(invalidTransaction);

// Assert
await expect(result()).to.be.rejectedWith(
Error,
"Unexpected token I in JSON at position 0",
);
});

it("should get the polling interval", async () => {
// Arrange
const expectedpollingInterval = 4000; // default
const updatedInterval = 1000;

// Act
const pollingInterval = blsProvider.pollingInterval;
blsProvider.pollingInterval = updatedInterval;
const updatedPollingInterval = blsProvider.pollingInterval;

// Assert
expect(pollingInterval).to.equal(expectedpollingInterval);
expect(updatedPollingInterval).to.equal(updatedInterval);
});

it("should get the event listener count and remove all listeners", async () => {
blsProvider.on("block", () => {});
blsProvider.on("error", () => {});
expect(blsProvider.listenerCount("block")).to.equal(1);
expect(blsProvider.listenerCount("error")).to.equal(1);
expect(blsProvider.listenerCount()).to.equal(2);

blsProvider.removeAllListeners();
expect(blsProvider.listenerCount("block")).to.equal(0);
expect(blsProvider.listenerCount("error")).to.equal(0);
expect(blsProvider.listenerCount()).to.equal(0);
});

it("should return true and an array of listeners if polling", async () => {
// Arrange
const expectedListener = () => {};

// Act
blsProvider.on("block", expectedListener);
const listeners = blsProvider.listeners("block");
const isPolling = blsProvider.polling;
blsProvider.removeAllListeners();

// Assert
expect(listeners).to.deep.equal([expectedListener]);
expect(isPolling).to.be.true;
});

it("should be a provider", async () => {
// Arrange & Act
const isProvider = Experimental.BlsProvider.isProvider(blsProvider);
const isProviderWithInvalidProvider =
Experimental.BlsProvider.isProvider(blsSigner);

// Assert
expect(isProvider).to.equal(true);
expect(isProviderWithInvalidProvider).to.equal(false);
});

it("should a return a promise which will stall until the network has heen established", async () => {
// Arrange
const expectedReady = { name: "localhost", chainId: 1337 };

// Act
const ready = await blsProvider.ready;

// Assert
expect(ready).to.deep.equal(expectedReady);
});
});
163 changes: 25 additions & 138 deletions contracts/test-integration/BlsProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ describe("BlsProvider", () => {

it("should estimate gas without throwing an error", async () => {
// Arrange
const recipient = ethers.Wallet.createRandom().address;
const transactionAmount = parseEther("1");
const getAddressPromise = blsSigner.getAddress();
const transactionRequest = {
to: recipient,
value: transactionAmount,
from: await blsSigner.getAddress(),
to: ethers.Wallet.createRandom().address,
value: parseEther("1"),
from: getAddressPromise,
};

// Act
Expand All @@ -106,16 +105,11 @@ describe("BlsProvider", () => {
const expectedBalance = parseEther("1");
const balanceBefore = await blsProvider.getBalance(recipient);

const unsignedTransaction = {
const signedTransaction = await blsSigner.signTransaction({
value: expectedBalance.toString(),
to: recipient,
data: "0x",
from: await blsSigner.getAddress(),
};

const signedTransaction = await blsSigner.signTransaction(
unsignedTransaction,
);
});

// Act
const transaction = await blsProvider.sendTransaction(signedTransaction);
Expand All @@ -130,18 +124,11 @@ describe("BlsProvider", () => {
it("should get the account nonce when the signer constructs the transaction response", async () => {
// Arrange
const spy = chai.spy.on(BlsWalletWrapper, "Nonce");
const recipient = ethers.Wallet.createRandom().address;
const expectedBalance = parseEther("1");

const unsignedTransaction = {
value: expectedBalance.toString(),
to: recipient,
const signedTransaction = await blsSigner.signTransaction({
value: parseEther("1"),
to: ethers.Wallet.createRandom().address,
data: "0x",
from: await blsSigner.getAddress(),
};
const signedTransaction = await blsSigner.signTransaction(
unsignedTransaction,
);
});

// Act
await blsProvider.sendTransaction(signedTransaction);
Expand All @@ -160,7 +147,6 @@ describe("BlsProvider", () => {
value: parseEther("1"),
to: ethers.Wallet.createRandom().address,
data: "0x",
from: await blsSigner.getAddress(),
});

const userBundle = JSON.parse(signedTransaction);
Expand All @@ -177,21 +163,6 @@ describe("BlsProvider", () => {
);
});

it("should throw an error when sending an invalid signed transaction", async () => {
// Arrange
const invalidTransaction = "Invalid signed transaction";

// Act
const result = async () =>
await blsProvider.sendTransaction(invalidTransaction);

// Assert
await expect(result()).to.be.rejectedWith(
Error,
"Unexpected token I in JSON at position 0",
);
});

it("should throw an error when the transaction receipt cannot be found", async () => {
// Arrange
const randomBytes = ethers.utils.randomBytes(20);
Expand All @@ -215,11 +186,9 @@ describe("BlsProvider", () => {

it("should wait for a transaction and resolve once transaction hash is included in the block", async () => {
// Arrange
const recipient = ethers.Wallet.createRandom().address;
const transactionResponse = await blsSigner.sendTransaction({
to: recipient,
to: ethers.Wallet.createRandom().address,
value: parseEther("1"),
from: await blsSigner.getAddress(),
});

const expectedToAddress = "0x689A095B4507Bfa302eef8551F90fB322B3451c6"; // Verification Gateway address
Expand Down Expand Up @@ -278,11 +247,9 @@ describe("BlsProvider", () => {

it("should retrieve a transaction receipt given a valid hash", async () => {
// Arrange
const recipient = ethers.Wallet.createRandom().address;
const transactionResponse = await blsSigner.sendTransaction({
to: recipient,
to: ethers.Wallet.createRandom().address,
value: parseEther("1"),
from: await blsSigner.getAddress(),
});

const expectedToAddress = "0x689A095B4507Bfa302eef8551F90fB322B3451c6"; // Verification Gateway address
Expand Down Expand Up @@ -339,12 +306,9 @@ describe("BlsProvider", () => {

it("gets a transaction given a valid transaction hash", async () => {
// Arrange
const recipient = ethers.Wallet.createRandom().address;
const transactionAmount = parseEther("1");
const transactionRequest = {
to: recipient,
value: transactionAmount,
from: await blsSigner.getAddress(),
to: ethers.Wallet.createRandom().address,
value: parseEther("1"),
};

const expectedTransactionResponse = await blsSigner.sendTransaction(
Expand Down Expand Up @@ -436,69 +400,9 @@ describe("BlsProvider", () => {
expect(accounts).to.deep.equal(expectedAccounts);
});

it("should get the polling interval", async () => {
// Arrange
const expectedpollingInterval = 4000; // default
const updatedInterval = 1000;

// Act
const pollingInterval = blsProvider.pollingInterval;
blsProvider.pollingInterval = updatedInterval;
const updatedPollingInterval = blsProvider.pollingInterval;

// Assert
expect(pollingInterval).to.equal(expectedpollingInterval);
expect(updatedPollingInterval).to.equal(updatedInterval);
});

it("should get the event listener count and remove all listeners", async () => {
blsProvider.on("block", () => {});
blsProvider.on("error", () => {});
expect(blsProvider.listenerCount("block")).to.equal(1);
expect(blsProvider.listenerCount("error")).to.equal(1);
expect(blsProvider.listenerCount()).to.equal(2);

blsProvider.removeAllListeners();
expect(blsProvider.listenerCount("block")).to.equal(0);
expect(blsProvider.listenerCount("error")).to.equal(0);
expect(blsProvider.listenerCount()).to.equal(0);
});

it("should return true and an array of listeners if polling", async () => {
// Arrange
const expectedListener = () => {};

// Act
blsProvider.on("block", expectedListener);
const listeners = blsProvider.listeners("block");
const isPolling = blsProvider.polling;
blsProvider.removeAllListeners();

// Assert
expect(listeners).to.deep.equal([expectedListener]);
expect(isPolling).to.be.true;
});

it("should be a provider", async () => {
// Arrange & Act
const isProvider = Experimental.BlsProvider.isProvider(blsProvider);
const isProviderWithInvalidProvider =
Experimental.BlsProvider.isProvider(blsSigner);

// Assert
expect(isProvider).to.equal(true);
expect(isProviderWithInvalidProvider).to.equal(false);
});

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,
from: await blsSigner.getAddress(),
};
const address = await blsSigner.getAddress();

const expectedFirstTransactionCount = 0;
const expectedSecondTransactionCount = 1;

Expand All @@ -507,7 +411,10 @@ describe("BlsProvider", () => {
address,
);

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

const secondTransactionCount = await blsProvider.getTransactionCount(
Expand All @@ -526,7 +433,6 @@ describe("BlsProvider", () => {
const sendTransaction = await blsSigner.sendTransaction({
value: BigNumber.from(1),
to: ethers.Wallet.createRandom().address,
from: await blsSigner.getAddress(),
});
await sendTransaction.wait();

Expand Down Expand Up @@ -684,17 +590,6 @@ describe("BlsProvider", () => {
);
expect(feeData.gasPrice).to.deep.equal(expectedFeeData.gasPrice);
});

it("should a return a promise which will stall until the network has heen established", async () => {
// Arrange
const expectedReady = { name: "localhost", chainId: 1337 };

// Act
const ready = await blsProvider.ready;

// Assert
expect(ready).to.deep.equal(expectedReady);
});
});

describe("JsonRpcProvider", () => {
Expand All @@ -718,30 +613,22 @@ describe("JsonRpcProvider", () => {
regularProvider,
);

const transaction = {
// Act
const result = await regularProvider.call({
to: testERC20.address,
data: testERC20.interface.encodeFunctionData("totalSupply"),
};

// Act
const result = await regularProvider.call(transaction);
});

// Assert
expect(formatEther(result)).to.equal(expectedSupply);
});

it("gets a transaction given a valid transaction hash", async () => {
// Arrange
const recipient = ethers.Wallet.createRandom().address;
const transactionAmount = parseEther("1");
const transactionRequest = {
to: recipient,
value: transactionAmount,
};

const expectedTransactionResponse = await wallet.sendTransaction(
transactionRequest,
);
const expectedTransactionResponse = await wallet.sendTransaction({
to: ethers.Wallet.createRandom().address,
value: parseEther("1"),
});

// Act
const transactionResponse = await regularProvider.getTransaction(
Expand Down
Loading

0 comments on commit e73949a

Please sign in to comment.