Merge branch 'v2-prototype' of https://github.com/0xProject/0x-monorepo into feature/improve-linting
This commit is contained in:
		@@ -1,4 +1,13 @@
 | 
			
		||||
[
 | 
			
		||||
    {
 | 
			
		||||
        "version": "0.4.3",
 | 
			
		||||
        "changes": [
 | 
			
		||||
            {
 | 
			
		||||
                "note": "Add optional parameter shouldUseFakeGasEstimate to Web3Config",
 | 
			
		||||
                "pr": 622
 | 
			
		||||
            }
 | 
			
		||||
        ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        "version": "0.4.2",
 | 
			
		||||
        "changes": [
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,11 @@
 | 
			
		||||
import { Web3Wrapper } from '@0xproject/web3-wrapper';
 | 
			
		||||
import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper';
 | 
			
		||||
import { includes } from 'lodash';
 | 
			
		||||
import * as Web3 from 'web3';
 | 
			
		||||
 | 
			
		||||
enum NodeType {
 | 
			
		||||
    Geth = 'GETH',
 | 
			
		||||
    Ganache = 'GANACHE',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class BlockchainLifecycle {
 | 
			
		||||
    private _web3Wrapper: Web3Wrapper;
 | 
			
		||||
@@ -7,17 +14,47 @@ export class BlockchainLifecycle {
 | 
			
		||||
        this._web3Wrapper = web3Wrapper;
 | 
			
		||||
        this._snapshotIdsStack = [];
 | 
			
		||||
    }
 | 
			
		||||
    // TODO: In order to run these tests on an actual node, we should check if we are running against
 | 
			
		||||
    // TestRPC, if so, use snapshots, otherwise re-deploy contracts before every test
 | 
			
		||||
    public async startAsync(): Promise<void> {
 | 
			
		||||
        const snapshotId = await this._web3Wrapper.takeSnapshotAsync();
 | 
			
		||||
        this._snapshotIdsStack.push(snapshotId);
 | 
			
		||||
        const nodeType = await this._getNodeTypeAsync();
 | 
			
		||||
        switch (nodeType) {
 | 
			
		||||
            case NodeType.Ganache:
 | 
			
		||||
                const snapshotId = await this._web3Wrapper.takeSnapshotAsync();
 | 
			
		||||
                this._snapshotIdsStack.push(snapshotId);
 | 
			
		||||
                break;
 | 
			
		||||
            case NodeType.Geth:
 | 
			
		||||
                const blockNumber = await this._web3Wrapper.getBlockNumberAsync();
 | 
			
		||||
                this._snapshotIdsStack.push(blockNumber);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new Error(`Unknown node type: ${nodeType}`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    public async revertAsync(): Promise<void> {
 | 
			
		||||
        const snapshotId = this._snapshotIdsStack.pop() as number;
 | 
			
		||||
        const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId);
 | 
			
		||||
        if (!didRevert) {
 | 
			
		||||
            throw new Error(`Snapshot with id #${snapshotId} failed to revert`);
 | 
			
		||||
        const nodeType = await this._getNodeTypeAsync();
 | 
			
		||||
        switch (nodeType) {
 | 
			
		||||
            case NodeType.Ganache:
 | 
			
		||||
                const snapshotId = this._snapshotIdsStack.pop() as number;
 | 
			
		||||
                const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId);
 | 
			
		||||
                if (!didRevert) {
 | 
			
		||||
                    throw new Error(`Snapshot with id #${snapshotId} failed to revert`);
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case NodeType.Geth:
 | 
			
		||||
                const blockNumber = this._snapshotIdsStack.pop() as number;
 | 
			
		||||
                await this._web3Wrapper.setHeadAsync(blockNumber);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new Error(`Unknown node type: ${nodeType}`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    private async _getNodeTypeAsync(): Promise<NodeType> {
 | 
			
		||||
        const version = await this._web3Wrapper.getNodeVersionAsync();
 | 
			
		||||
        if (includes(version, uniqueVersionIds.geth)) {
 | 
			
		||||
            return NodeType.Geth;
 | 
			
		||||
        } else if (includes(version, uniqueVersionIds.ganache)) {
 | 
			
		||||
            return NodeType.Ganache;
 | 
			
		||||
        } else {
 | 
			
		||||
            throw new Error(`Unknown client version: ${version}`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,16 +17,22 @@ export interface Web3Config {
 | 
			
		||||
    hasAddresses?: boolean; // default: true
 | 
			
		||||
    shouldUseInProcessGanache?: boolean; // default: false
 | 
			
		||||
    rpcUrl?: string; // default: localhost:8545
 | 
			
		||||
    shouldUseFakeGasEstimate?: boolean; // default: true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const web3Factory = {
 | 
			
		||||
    getRpcProvider(config: Web3Config = {}): ProviderEngine {
 | 
			
		||||
        const provider = new ProviderEngine();
 | 
			
		||||
        const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses;
 | 
			
		||||
        config.shouldUseFakeGasEstimate =
 | 
			
		||||
            _.isUndefined(config.shouldUseFakeGasEstimate) || config.shouldUseFakeGasEstimate;
 | 
			
		||||
        if (!hasAddresses) {
 | 
			
		||||
            provider.addProvider(new EmptyWalletSubprovider());
 | 
			
		||||
        }
 | 
			
		||||
        provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT));
 | 
			
		||||
 | 
			
		||||
        if (config.shouldUseFakeGasEstimate) {
 | 
			
		||||
            provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT));
 | 
			
		||||
        }
 | 
			
		||||
        const logger = {
 | 
			
		||||
            log: (arg: any) => {
 | 
			
		||||
                fs.appendFileSync('ganache.log', `${arg}\n`);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user