From bf9b4b993f9ca9636377cfca23d8cd8d723452a0 Mon Sep 17 00:00:00 2001 From: Lawrence Forman Date: Thu, 6 Feb 2020 01:55:55 -0500 Subject: [PATCH] `@0x/contracts-test-utils`: Fix `blockchainTests` fork config to work with other tests. --- .../contracts/src/interfaces/IDydx.sol | 18 +++++------ .../contracts/test/TestDydxBridge.sol | 6 ++-- .../dev-utils/dydx_order_validation_test.ts | 4 +-- contracts/test-utils/src/mocha_blockchain.ts | 32 +++++++++++++++++-- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/contracts/asset-proxy/contracts/src/interfaces/IDydx.sol b/contracts/asset-proxy/contracts/src/interfaces/IDydx.sol index 2f2296bd88..8b353ad6ac 100644 --- a/contracts/asset-proxy/contracts/src/interfaces/IDydx.sol +++ b/contracts/asset-proxy/contracts/src/interfaces/IDydx.sol @@ -117,6 +117,15 @@ interface IDydx { ) external; + // @dev Approves/disapproves any number of operators. An operator is an external address that has the + // same permissions to manipulate an account as the owner of the account. Operators are simply + // addresses and therefore may either be externally-owned Ethereum accounts OR smart contracts. + // Operators are also able to act as AutoTrader contracts on behalf of the account owner if the + // operator is a smart contract and implements the IAutoTrader interface. + // @param args A list of OperatorArgs which have an address and a boolean. The boolean value + // denotes whether to approve (true) or revoke approval (false) for that address. + function setOperators(OperatorArg[] calldata args) external; + /// @dev Return true if a particular address is approved as an operator for an owner's accounts. /// Approved operators can act on the accounts of the owner as if it were the operator's own. /// @param owner The owner of the accounts @@ -171,13 +180,4 @@ interface IDydx { external view returns (Value memory supplyValue, Value memory borrowValue); - - // @dev Approves/disapproves any number of operators. An operator is an external address that has the - // same permissions to manipulate an account as the owner of the account. Operators are simply - // addresses and therefore may either be externally-owned Ethereum accounts OR smart contracts. - // Operators are also able to act as AutoTrader contracts on behalf of the account owner if the - // operator is a smart contract and implements the IAutoTrader interface. - // @param args A list of OperatorArgs which have an address and a boolean. The boolean value - // denotes whether to approve (true) or revoke approval (false) for that address. - function setOperators(OperatorArg[] calldata args) external; } diff --git a/contracts/asset-proxy/contracts/test/TestDydxBridge.sol b/contracts/asset-proxy/contracts/test/TestDydxBridge.sol index 9345e21cdc..ce3f4f8b78 100644 --- a/contracts/asset-proxy/contracts/test/TestDydxBridge.sol +++ b/contracts/asset-proxy/contracts/test/TestDydxBridge.sol @@ -172,6 +172,9 @@ contract TestDydxBridge is return _testTokenAddress; } + /// @dev Unused. + function setOperators(OperatorArg[] calldata args) external {} + /// @dev Unused. function getIsLocalOperator( address owner, @@ -216,9 +219,6 @@ contract TestDydxBridge is returns (Value memory supplyValue, Value memory borrowValue) {} - /// @dev Unused. - function setOperators(OperatorArg[] calldata args) external {} - /// @dev overrides `_getDydxAddress()` from `DeploymentConstants` to return this address. function _getDydxAddress() internal diff --git a/contracts/integrations/test/dev-utils/dydx_order_validation_test.ts b/contracts/integrations/test/dev-utils/dydx_order_validation_test.ts index 79d4ce2226..9986bd759b 100644 --- a/contracts/integrations/test/dev-utils/dydx_order_validation_test.ts +++ b/contracts/integrations/test/dev-utils/dydx_order_validation_test.ts @@ -33,11 +33,11 @@ enum DydxAssetReference { const MAKER_ADDRESS = '0x3a9F7C8cA36C42d7035E87C3304eE5cBd353a532'; -blockchainTests.config = { +blockchainTests.configure({ fork: { unlockedAccounts: [MAKER_ADDRESS], }, -}; +}); blockchainTests.fork('DevUtils dydx order validation tests', env => { const { ZERO_AMOUNT: ZERO } = constants; diff --git a/contracts/test-utils/src/mocha_blockchain.ts b/contracts/test-utils/src/mocha_blockchain.ts index 061fecd3e7..b9cf11e6d4 100644 --- a/contracts/test-utils/src/mocha_blockchain.ts +++ b/contracts/test-utils/src/mocha_blockchain.ts @@ -30,12 +30,14 @@ export interface BlockchainContextConfig { }>; } +let TEST_ENV_CONFIG: Partial = {}; + /** * Interface for `blockchainTests()`. */ export interface BlockchainContextDefinition { (description: string, callback: BlockchainSuiteCallback): ISuite; - config: Partial; + configure: (config?: Partial) => void; only: BlockchainContextDefinitionCallback; skip: BlockchainContextDefinitionCallback; optional: BlockchainContextDefinitionCallback; @@ -102,6 +104,11 @@ export class StandardBlockchainTestsEnvironmentSingleton extends BlockchainTests return StandardBlockchainTestsEnvironmentSingleton._instance; } + // Reset the singleton. + public static reset(): void { + StandardBlockchainTestsEnvironmentSingleton._instance = undefined; + } + // Get the singleton instance of this class. public static getInstance(): StandardBlockchainTestsEnvironmentSingleton | undefined { return StandardBlockchainTestsEnvironmentSingleton._instance; @@ -130,8 +137,13 @@ export class ForkedBlockchainTestsEnvironmentSingleton extends BlockchainTestsEn return ForkedBlockchainTestsEnvironmentSingleton._instance; } + // Reset the singleton. + public static reset(): void { + ForkedBlockchainTestsEnvironmentSingleton._instance = undefined; + } + protected static _createWeb3Provider(forkHost: string): Web3ProviderEngine { - const forkConfig = blockchainTests.config.fork || {}; + const forkConfig = TEST_ENV_CONFIG.fork || {}; const unlockedAccounts = forkConfig.unlockedAccounts; return web3Factory.getRpcProvider({ ...providerConfigs, @@ -172,6 +184,11 @@ export class LiveBlockchainTestsEnvironmentSingleton extends BlockchainTestsEnvi return LiveBlockchainTestsEnvironmentSingleton._instance; } + // Reset the singleton. + public static reset(): void { + LiveBlockchainTestsEnvironmentSingleton._instance = undefined; + } + protected static _createWeb3Provider(rpcHost: string): Web3ProviderEngine { const providerEngine = new Web3ProviderEngine(); providerEngine.addProvider(new RPCSubprovider(rpcHost)); @@ -223,7 +240,16 @@ export const blockchainTests: BlockchainContextDefinition = _.assign( return defineBlockchainSuite(StandardBlockchainTestsEnvironmentSingleton, description, callback, describe); }, { - config: {}, + configure(config?: Partial): void { + // Update the global config and reset all environment singletons. + TEST_ENV_CONFIG = { + ...TEST_ENV_CONFIG, + ...config, + }; + ForkedBlockchainTestsEnvironmentSingleton.reset(); + StandardBlockchainTestsEnvironmentSingleton.reset(); + LiveBlockchainTestsEnvironmentSingleton.reset(); + }, only(description: string, callback: BlockchainSuiteCallback): ISuite { return defineBlockchainSuite( StandardBlockchainTestsEnvironmentSingleton,