-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close connections in other test files
- Loading branch information
Kris Urbas
committed
Oct 21, 2024
1 parent
c700d82
commit fe60f0c
Showing
5 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
packages/web3-eth-contract/test/integration/local_account/contract_deploy.x.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters