Skip to content

Commit

Permalink
close connections in other test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Urbas committed Oct 21, 2024
1 parent c700d82 commit fe60f0c
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import Web3 from 'web3';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Web3Account } from 'web3-eth-accounts';
import { GreeterBytecode, GreeterAbi } from '../../shared_fixtures/build/Greeter';
import { getSystemTestProvider, createLocalAccount } from '../../fixtures/system_test_utils';
import {
getSystemTestProvider,
createLocalAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { Contract } from '../../../src';

describe('contract', () => {
Expand All @@ -45,6 +49,10 @@ describe('contract', () => {
};
});

afterEach(async () => {
await closeOpenConnection(contract);
});

it.each(['0x1', '0x2'])('should emit the "sending" event', async txType => {
const handler = jest.fn();
const acc = await createLocalAccount(web3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
import Web3 from 'web3';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Web3Account } from 'web3-eth-accounts';
import { GreeterBytecode, GreeterAbi } from '../../shared_fixtures/build/Greeter';
import {
getSystemTestProvider,
createLocalAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { Contract } from '../../../src';

describe('contract', () => {
describe('deploy', () => {
let contract: Contract<typeof GreeterAbi>;
let sendOptions: Record<string, unknown>;
let deployOptions: Record<string, unknown>;
let localAccount: Web3Account;
let web3: Web3;

beforeEach(async () => {
web3 = new Web3(getSystemTestProvider());
contract = new web3.eth.Contract(GreeterAbi) as unknown as Contract<typeof GreeterAbi>;
deployOptions = {
data: GreeterBytecode,
arguments: ['My Greeting'],
};
localAccount = await createLocalAccount(web3);
sendOptions = {
from: localAccount.address,
gas: '1000000',
};
});

afterAll(async () => {
await closeOpenConnection(contract);
});

it.each(['0x1', '0x2'])('should emit the "sending" event', async txType => {
const handler = jest.fn();
const acc = await createLocalAccount(web3);
const promiEvent = contract
.deploy(deployOptions)
.send({
...sendOptions,
from: acc.address,
type: txType,
})
.on('sending', handler);

// Deploy the contract
await promiEvent;

expect(handler).toHaveBeenCalled();
});

it.each(['0x1', '0x2'])('should deploy contract %p', async txType => {
const acc = await createLocalAccount(web3);
const deployedContract = await contract.deploy(deployOptions).send({
...sendOptions,
from: acc.address,
type: txType,
});
expect(deployedContract.options.address).toBeDefined();
});

it('deploy should fail with low baseFeeGas EIP1559', async () => {
await expect(
contract.deploy(deployOptions).send({
type: '0x2',
gas: '1000',
maxFeePerGas: '0x1',
maxPriorityFeePerGas: '0x1',
from: localAccount.address,
}),
).rejects.toThrow('Signer Error Signer Error gasLimit is too low');
});

it.each(['0x1', '0x2'])(
'should return estimated gas of contract method %p',
async txType => {
const contractDeployed = await contract.deploy(deployOptions).send(sendOptions);

const estimatedGas = await contractDeployed.methods
.setGreeting('Hello')
.estimateGas({
...sendOptions,
type: txType,
});
expect(Number(estimatedGas)).toBeGreaterThan(0);
},
);

it('should deploy the contract if data is provided at initiation', async () => {
const contractWithParams = new web3.eth.Contract(GreeterAbi, undefined, {
provider: web3.provider,
data: GreeterBytecode,
from: localAccount.address,
gas: '1000000',
}) as unknown as Contract<typeof GreeterAbi>;

const deployedContract = await contractWithParams
.deploy({ arguments: ['Hello World'] })
.send();

expect(deployedContract.options.address).toBeDefined();
});

it('should emit the "confirmation" event', async () => {
const confirmationHandler = jest.fn();
const promievent = contract.deploy(deployOptions).send(sendOptions);
const receiptPromise = new Promise<void>(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promievent.on('receipt', () => {
resolve();
});
});

const confirmationPRomise = new Promise<void>(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promievent.on('confirmation', () => {
confirmationHandler();
resolve();
});
});
await promievent;
await receiptPromise;

// Deploy once again to trigger block mining to trigger confirmation
// We can send any other transaction as well
await contract.deploy(deployOptions).send(sendOptions);

await confirmationPRomise;
// eslint-disable-next-line jest/no-standalone-expect
expect(confirmationHandler).toHaveBeenCalled();
});

it('should emit the "transactionHash" event', async () => {
const handler = jest.fn();

const promiEvent = contract
.deploy(deployOptions)
.send(sendOptions)
.on('transactionHash', handler);

// Deploy the contract
await promiEvent;

expect(handler).toHaveBeenCalled();
});

it('should fail with errors deploying a zero length bytecode', () => {
return expect(() =>
contract
.deploy({
...deployOptions,
data: '0x',
})
.send(sendOptions),
).toThrow('contract creation without any data provided.');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import Web3 from 'web3';
import { Web3Account } from 'web3-eth-accounts';
import { Contract } from '../../../src';
import { ERC20TokenAbi, ERC20TokenBytecode } from '../../shared_fixtures/build/ERC20Token';
import { getSystemTestProvider, createLocalAccount } from '../../fixtures/system_test_utils';
import {
getSystemTestProvider,
createLocalAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';

const initialSupply = BigInt('5000000000');

Expand Down Expand Up @@ -53,6 +57,11 @@ describe('contract', () => {
contractDeployed = await contract.deploy(deployOptions).send(sendOptions);
});

afterAll(async () => {
await closeOpenConnection(contract);
await closeOpenConnection(contractDeployed);
});

it('should deploy the contract', () => {
expect(contractDeployed.options.address).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import Web3 from 'web3';
import { Web3Account } from 'web3-eth-accounts';
import { Contract } from '../../../src';
import { ERC721TokenAbi, ERC721TokenBytecode } from '../../shared_fixtures/build/ERC721Token';
import { getSystemTestProvider, createLocalAccount } from '../../fixtures/system_test_utils';
import {
getSystemTestProvider,
createLocalAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { toUpperCaseHex } from '../../shared_fixtures/utils';

describe('contract', () => {
Expand All @@ -32,6 +36,7 @@ describe('contract', () => {
let localAccount: Web3Account;
let web3: Web3;
let contractDeployed: Contract<typeof ERC721TokenAbi>;

beforeAll(async () => {
web3 = new Web3(getSystemTestProvider());
localAccount = await createLocalAccount(web3);
Expand All @@ -53,6 +58,11 @@ describe('contract', () => {
.send({ ...sendOptions, gas: '3000000' });
});

afterAll(async () => {
await closeOpenConnection(contract);
await closeOpenConnection(contractDeployed);
});

it('should deploy the contract', () => {
expect(contractDeployed.options.address).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { utf8ToHex } from 'web3-utils';
import { EventLog } from 'web3-types';
import { Contract } from '../../../src';
import { ERC721TokenAbi, ERC721TokenBytecode } from '../../shared_fixtures/build/ERC721Token';
import { getSystemTestProvider, createLocalAccount } from '../../fixtures/system_test_utils';
import {
getSystemTestProvider,
createLocalAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { toUpperCaseHex } from '../../shared_fixtures/utils';

describe('contract ERC721 overloaded functions', () => {
Expand Down Expand Up @@ -55,6 +59,11 @@ describe('contract ERC721 overloaded functions', () => {
.send({ ...sendOptions, gas: '3000000' });
});

afterAll(async () => {
await closeOpenConnection(contract);
await closeOpenConnection(contractDeployed);
});

it('transferFrom with 4 arguments', async () => {
const tempAccount = await createLocalAccount(web3);
const toAccount = await createLocalAccount(web3);
Expand Down

0 comments on commit fe60f0c

Please sign in to comment.