@ethereum-waffle/mock-contract
Advanced tools
Comparing version 4.0.3-dev.d863d0a to 4.0.3-dev.e1d3ccd
@@ -5,2 +5,6 @@ import { Contract, Signer, utils } from 'ethers'; | ||
export declare type Stub = ReturnType<typeof stub>; | ||
declare type DeployOptions = { | ||
address: string; | ||
override?: boolean; | ||
}; | ||
export interface MockContract extends Contract { | ||
@@ -19,4 +23,4 @@ mock: { | ||
}; | ||
export declare function deployMockContract(signer: Signer, abi: ABI): Promise<MockContract>; | ||
export declare function deployMockContract(signer: Signer, abi: ABI, options?: DeployOptions): Promise<MockContract>; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,3 +9,31 @@ "use strict"; | ||
const Doppelganger_json_1 = __importDefault(require("./Doppelganger.json")); | ||
async function deploy(signer) { | ||
async function deploy(signer, options) { | ||
if (options) { | ||
const { address, override } = options; | ||
const provider = signer.provider; | ||
if (!override && await provider.getCode(address) !== '0x') { | ||
throw new Error(`${address} already contains a contract. ` + | ||
'If you want to override it, set the override parameter.'); | ||
} | ||
if (provider._hardhatNetwork) { | ||
if (await provider.send('hardhat_setCode', [ | ||
address, | ||
'0x' + Doppelganger_json_1.default.evm.deployedBytecode.object | ||
])) { | ||
return new ethers_1.Contract(address, Doppelganger_json_1.default.abi, signer); | ||
} | ||
else | ||
throw new Error(`Couldn't deploy at ${address}`); | ||
} | ||
else { | ||
if (await provider.send('evm_setAccountCode', [ | ||
address, | ||
'0x' + Doppelganger_json_1.default.evm.deployedBytecode.object | ||
])) { | ||
return new ethers_1.Contract(address, Doppelganger_json_1.default.abi, signer); | ||
} | ||
else | ||
throw new Error(`Couldn't deploy at ${address}`); | ||
} | ||
} | ||
const factory = new ethers_1.ContractFactory(Doppelganger_json_1.default.abi, Doppelganger_json_1.default.bytecode, signer); | ||
@@ -41,6 +69,12 @@ return factory.deploy(); | ||
}, {}); | ||
mockedAbi.receive = { | ||
returns: async () => { throw new Error('Receive function return is not implemented.'); }, | ||
withArgs: () => { throw new Error('Receive function return is not implemented.'); }, | ||
reverts: async () => mockContractInstance.__waffle__receiveReverts('Mock Revert'), | ||
revertsWithReason: async (reason) => mockContractInstance.__waffle__receiveReverts(reason) | ||
}; | ||
return mockedAbi; | ||
} | ||
async function deployMockContract(signer, abi) { | ||
const mockContractInstance = await deploy(signer); | ||
async function deployMockContract(signer, abi, options) { | ||
const mockContractInstance = await deploy(signer, options); | ||
const mock = createMock(abi, mockContractInstance); | ||
@@ -47,0 +81,0 @@ const mockedContract = new ethers_1.Contract(mockContractInstance.address, abi, signer); |
@@ -5,2 +5,6 @@ import { Contract, Signer, utils } from 'ethers'; | ||
export declare type Stub = ReturnType<typeof stub>; | ||
declare type DeployOptions = { | ||
address: string; | ||
override?: boolean; | ||
}; | ||
export interface MockContract extends Contract { | ||
@@ -19,4 +23,4 @@ mock: { | ||
}; | ||
export declare function deployMockContract(signer: Signer, abi: ABI): Promise<MockContract>; | ||
export declare function deployMockContract(signer: Signer, abi: ABI, options?: DeployOptions): Promise<MockContract>; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
import { Contract, ContractFactory, utils } from 'ethers'; | ||
import DoppelgangerContract from './Doppelganger.json'; | ||
async function deploy(signer) { | ||
async function deploy(signer, options) { | ||
if (options) { | ||
const { address, override } = options; | ||
const provider = signer.provider; | ||
if (!override && await provider.getCode(address) !== '0x') { | ||
throw new Error(`${address} already contains a contract. ` + | ||
'If you want to override it, set the override parameter.'); | ||
} | ||
if (provider._hardhatNetwork) { | ||
if (await provider.send('hardhat_setCode', [ | ||
address, | ||
'0x' + DoppelgangerContract.evm.deployedBytecode.object | ||
])) { | ||
return new Contract(address, DoppelgangerContract.abi, signer); | ||
} | ||
else | ||
throw new Error(`Couldn't deploy at ${address}`); | ||
} | ||
else { | ||
if (await provider.send('evm_setAccountCode', [ | ||
address, | ||
'0x' + DoppelgangerContract.evm.deployedBytecode.object | ||
])) { | ||
return new Contract(address, DoppelgangerContract.abi, signer); | ||
} | ||
else | ||
throw new Error(`Couldn't deploy at ${address}`); | ||
} | ||
} | ||
const factory = new ContractFactory(DoppelgangerContract.abi, DoppelgangerContract.bytecode, signer); | ||
@@ -34,6 +62,12 @@ return factory.deploy(); | ||
}, {}); | ||
mockedAbi.receive = { | ||
returns: async () => { throw new Error('Receive function return is not implemented.'); }, | ||
withArgs: () => { throw new Error('Receive function return is not implemented.'); }, | ||
reverts: async () => mockContractInstance.__waffle__receiveReverts('Mock Revert'), | ||
revertsWithReason: async (reason) => mockContractInstance.__waffle__receiveReverts(reason) | ||
}; | ||
return mockedAbi; | ||
} | ||
export async function deployMockContract(signer, abi) { | ||
const mockContractInstance = await deploy(signer); | ||
export async function deployMockContract(signer, abi, options) { | ||
const mockContractInstance = await deploy(signer, options); | ||
const mock = createMock(abi, mockContractInstance); | ||
@@ -40,0 +74,0 @@ const mockedContract = new Contract(mockContractInstance.address, abi, signer); |
{ | ||
"name": "@ethereum-waffle/mock-contract", | ||
"description": "Mock smart contracts in a smart way.", | ||
"version": "4.0.3-dev.d863d0a", | ||
"version": "4.0.3-dev.e1d3ccd", | ||
"author": "Marek Kirejczyk <account@ethworks.io> (http://ethworks.io)", | ||
@@ -39,14 +39,15 @@ "repository": "git@github.com:EthWorks/Waffle.git", | ||
"devDependencies": { | ||
"@ethereum-waffle/chai": "4.0.8-dev.e1d3ccd", | ||
"@ethereum-waffle/compiler": "4.0.3-dev.e1d3ccd", | ||
"@ethereum-waffle/provider": "4.0.5-dev.e1d3ccd", | ||
"@ethersproject/abi": "^5.6.1", | ||
"@ethersproject/providers": "5.6.2", | ||
"eslint": "^7.14.0", | ||
"ethers": "5.6.2", | ||
"@ethersproject/abi": "^5.6.1", | ||
"@ethereum-waffle/chai": "4.0.8-dev.d863d0a", | ||
"@ethereum-waffle/compiler": "4.0.3-dev.d863d0a", | ||
"mocha": "^8.2.1", | ||
"rimraf": "^3.0.2", | ||
"solc": "0.8.15", | ||
"@ethereum-waffle/provider": "4.0.5-dev.d863d0a", | ||
"ts-node": "^9.0.0", | ||
"typechain": "^8.0.0", | ||
"mocha": "^8.2.1", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.6.2", | ||
"eslint": "^7.14.0", | ||
"ts-node": "^9.0.0" | ||
"typescript": "^4.6.2" | ||
}, | ||
@@ -57,12 +58,11 @@ "peerDependencies": { | ||
"scripts": { | ||
"test": "export NODE_ENV=test && yarn test:build && mocha", | ||
"test": "export NODE_ENV=test && mocha", | ||
"lint": "eslint '{src,test}/**/*.ts'", | ||
"lint:fix": "eslint --fix '{src,test}/**/*.ts'", | ||
"build": "rimraf ./dist && yarn build:sol && yarn build:esm && yarn build:cjs", | ||
"build": "rimraf ./dist && yarn build:sol && yarn build:esm && yarn build:cjs && ts-node ./test/helpers/buildTestContracts.ts", | ||
"build:sol": "ts-node compile.ts", | ||
"build:esm": "tsc -p tsconfig.build.json --outDir dist/esm --module ES6", | ||
"build:cjs": "tsc -p tsconfig.build.json --outDir dist/cjs", | ||
"test:build": "ts-node ./test/helpers/buildTestContracts.ts", | ||
"clean": "rimraf ./dist ./test/example/build" | ||
} | ||
} |
@@ -5,2 +5,3 @@ import {Contract, ContractFactory, Signer, utils} from 'ethers'; | ||
import DoppelgangerContract from './Doppelganger.json'; | ||
import type {JsonRpcProvider} from '@ethersproject/providers'; | ||
@@ -11,2 +12,7 @@ type ABI = string | Array<utils.Fragment | JsonFragment | string> | ||
type DeployOptions = { | ||
address: string; | ||
override?: boolean; | ||
} | ||
export interface MockContract extends Contract { | ||
@@ -20,3 +26,27 @@ mock: { | ||
async function deploy(signer: Signer) { | ||
async function deploy(signer: Signer, options?: DeployOptions) { | ||
if (options) { | ||
const {address, override} = options; | ||
const provider = signer.provider as JsonRpcProvider; | ||
if (!override && await provider.getCode(address) !== '0x') { | ||
throw new Error( | ||
`${address} already contains a contract. ` + | ||
'If you want to override it, set the override parameter.'); | ||
} | ||
if ((provider as any)._hardhatNetwork) { | ||
if (await provider.send('hardhat_setCode', [ | ||
address, | ||
'0x' + DoppelgangerContract.evm.deployedBytecode.object | ||
])) { | ||
return new Contract(address, DoppelgangerContract.abi, signer); | ||
} else throw new Error(`Couldn't deploy at ${address}`); | ||
} else { | ||
if (await provider.send('evm_setAccountCode', [ | ||
address, | ||
'0x' + DoppelgangerContract.evm.deployedBytecode.object | ||
])) { | ||
return new Contract(address, DoppelgangerContract.abi, signer); | ||
} else throw new Error(`Couldn't deploy at ${address}`); | ||
} | ||
} | ||
const factory = new ContractFactory(DoppelgangerContract.abi, DoppelgangerContract.bytecode, signer); | ||
@@ -56,7 +86,14 @@ return factory.deploy(); | ||
mockedAbi.receive = { | ||
returns: async () => { throw new Error('Receive function return is not implemented.'); }, | ||
withArgs: () => { throw new Error('Receive function return is not implemented.'); }, | ||
reverts: async () => mockContractInstance.__waffle__receiveReverts('Mock Revert'), | ||
revertsWithReason: async (reason: string) => mockContractInstance.__waffle__receiveReverts(reason) | ||
}; | ||
return mockedAbi; | ||
} | ||
export async function deployMockContract(signer: Signer, abi: ABI): Promise<MockContract> { | ||
const mockContractInstance = await deploy(signer); | ||
export async function deployMockContract(signer: Signer, abi: ABI, options?: DeployOptions): Promise<MockContract> { | ||
const mockContractInstance = await deploy(signer, options); | ||
@@ -63,0 +100,0 @@ const mock = createMock(abi, mockContractInstance); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
352229
1529
0
13