From 74b9ad5536e1f9e2985eb53099534c0149aab35f Mon Sep 17 00:00:00 2001 From: Alex Towle Date: Mon, 12 Aug 2019 17:12:00 -0700 Subject: [PATCH] `@0x:contracts-exchange` Added unit tests for getCurrentContextAddress --- .../contracts/test/TestTransactions.sol | 47 +++++++++++++++++++ contracts/exchange/package.json | 2 +- contracts/exchange/src/artifacts.ts | 2 + contracts/exchange/src/wrappers.ts | 1 + contracts/exchange/test/transactions.ts | 30 ++++++++++++ .../exchange/test/transactions_unit_tests.ts | 43 +++++++++++++++++ contracts/exchange/tsconfig.json | 1 + 7 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 contracts/exchange/contracts/test/TestTransactions.sol create mode 100644 contracts/exchange/test/transactions_unit_tests.ts diff --git a/contracts/exchange/contracts/test/TestTransactions.sol b/contracts/exchange/contracts/test/TestTransactions.sol new file mode 100644 index 0000000000..754e9f1cfe --- /dev/null +++ b/contracts/exchange/contracts/test/TestTransactions.sol @@ -0,0 +1,47 @@ +/* + + Copyright 2019 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity ^0.5.9; +pragma experimental ABIEncoderV2; + +import "../src/Exchange.sol"; +import "../src/MixinTransactions.sol"; + + +contract TestTransactions is + Exchange +{ + constructor () + public + Exchange(1337) + {} // solhint-disable-line no-empty-blocks + + function setCurrentContextAddress(address context) + external + { + currentContextAddress = context; + } + + function getCurrentContextAddress() + external + view + returns (address) + { + return _getCurrentContextAddress(); + } +} diff --git a/contracts/exchange/package.json b/contracts/exchange/package.json index 8a55693bd6..de09b6a28b 100644 --- a/contracts/exchange/package.json +++ b/contracts/exchange/package.json @@ -35,7 +35,7 @@ "compile:truffle": "truffle compile" }, "config": { - "abis": "./generated-artifacts/@(Exchange|ExchangeWrapper|IAssetProxy|IAssetProxyDispatcher|IEIP1271Wallet|IExchange|IExchangeCore|IMatchOrders|ISignatureValidator|ITransactions|ITransferSimulator|IWallet|IWrapperFunctions|IsolatedExchange|LibExchangeRichErrorDecoder|MixinAssetProxyDispatcher|MixinExchangeCore|MixinMatchOrders|MixinSignatureValidator|MixinTransactions|MixinTransferSimulator|MixinWrapperFunctions|ReentrancyTester|TestAssetProxyDispatcher|TestExchangeInternals|TestLibExchangeRichErrorDecoder|TestSignatureValidator|TestValidatorWallet|TestWrapperFunctions|Whitelist).json", + "abis": "./generated-artifacts/@(Exchange|ExchangeWrapper|IAssetProxy|IAssetProxyDispatcher|IEIP1271Wallet|IExchange|IExchangeCore|IMatchOrders|ISignatureValidator|ITransactions|ITransferSimulator|IWallet|IWrapperFunctions|IsolatedExchange|LibExchangeRichErrorDecoder|MixinAssetProxyDispatcher|MixinExchangeCore|MixinMatchOrders|MixinSignatureValidator|MixinTransactions|MixinTransferSimulator|MixinWrapperFunctions|ReentrancyTester|TestAssetProxyDispatcher|TestExchangeInternals|TestLibExchangeRichErrorDecoder|TestSignatureValidator|TestTransactions|TestValidatorWallet|TestWrapperFunctions|Whitelist).json", "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually." }, "repository": { diff --git a/contracts/exchange/src/artifacts.ts b/contracts/exchange/src/artifacts.ts index 8c1aedae3e..a5551815fd 100644 --- a/contracts/exchange/src/artifacts.ts +++ b/contracts/exchange/src/artifacts.ts @@ -32,6 +32,7 @@ import * as TestAssetProxyDispatcher from '../generated-artifacts/TestAssetProxy import * as TestExchangeInternals from '../generated-artifacts/TestExchangeInternals.json'; import * as TestLibExchangeRichErrorDecoder from '../generated-artifacts/TestLibExchangeRichErrorDecoder.json'; import * as TestSignatureValidator from '../generated-artifacts/TestSignatureValidator.json'; +import * as TestTransactions from '../generated-artifacts/TestTransactions.json'; import * as TestValidatorWallet from '../generated-artifacts/TestValidatorWallet.json'; import * as TestWrapperFunctions from '../generated-artifacts/TestWrapperFunctions.json'; import * as Whitelist from '../generated-artifacts/Whitelist.json'; @@ -64,6 +65,7 @@ export const artifacts = { TestExchangeInternals: TestExchangeInternals as ContractArtifact, TestLibExchangeRichErrorDecoder: TestLibExchangeRichErrorDecoder as ContractArtifact, TestSignatureValidator: TestSignatureValidator as ContractArtifact, + TestTransactions: TestTransactions as ContractArtifact, TestValidatorWallet: TestValidatorWallet as ContractArtifact, TestWrapperFunctions: TestWrapperFunctions as ContractArtifact, }; diff --git a/contracts/exchange/src/wrappers.ts b/contracts/exchange/src/wrappers.ts index 171f93ba3e..f64c78b5cf 100644 --- a/contracts/exchange/src/wrappers.ts +++ b/contracts/exchange/src/wrappers.ts @@ -30,6 +30,7 @@ export * from '../generated-wrappers/test_asset_proxy_dispatcher'; export * from '../generated-wrappers/test_exchange_internals'; export * from '../generated-wrappers/test_lib_exchange_rich_error_decoder'; export * from '../generated-wrappers/test_signature_validator'; +export * from '../generated-wrappers/test_transactions'; export * from '../generated-wrappers/test_validator_wallet'; export * from '../generated-wrappers/test_wrapper_functions'; export * from '../generated-wrappers/whitelist'; diff --git a/contracts/exchange/test/transactions.ts b/contracts/exchange/test/transactions.ts index d8321aeecc..9ad594cd65 100644 --- a/contracts/exchange/test/transactions.ts +++ b/contracts/exchange/test/transactions.ts @@ -37,6 +37,7 @@ import { ExchangeTransactionExecutionEventArgs, ExchangeWrapper, ExchangeWrapperContract, + TestTransactionsContract, WhitelistContract, } from '../src/'; @@ -52,6 +53,7 @@ blockchainTests.resets('Exchange transactions', env => { let feeRecipientAddress: string; let validatorAddress: string; let taker2Address: string; + let transactionsContract: TestTransactionsContract; let erc20TokenA: DummyERC20TokenContract; let erc20TokenB: DummyERC20TokenContract; @@ -138,6 +140,13 @@ blockchainTests.resets('Exchange transactions', env => { makerTransactionFactory = new TransactionFactory(makerPrivateKey, exchangeInstance.address, chainId); takerTransactionFactory = new TransactionFactory(takerPrivateKey, exchangeInstance.address, chainId); taker2TransactionFactory = new TransactionFactory(taker2PrivateKey, exchangeInstance.address, chainId); + + transactionsContract = await TestTransactionsContract.deployFrom0xArtifactAsync( + artifacts.TestTransactions, + env.provider, + env.txDefaults, + {}, + ); }); describe('executeTransaction', () => { describe('general functionality', () => { @@ -1189,4 +1198,25 @@ blockchainTests.resets('Exchange transactions', env => { }); }); }); + describe('getCurrentContext', () => { + it('should return the sender address when there is not a saved context address', async () => { + const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({ + from: makerAddress, + }); + expect(currentContextAddress).to.be.eq(makerAddress); + }); + + it('should return the sender address when there is a saved context address', async () => { + // Set the current context address to the taker address + await transactionsContract.setCurrentContextAddress.sendTransactionAsync(takerAddress, { + from: makerAddress, + }); + + // Ensure that the queried current context address is the same as the address that was set. + const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({ + from: makerAddress, + }); + expect(currentContextAddress).to.be.eq(takerAddress); + }); + }); }); diff --git a/contracts/exchange/test/transactions_unit_tests.ts b/contracts/exchange/test/transactions_unit_tests.ts new file mode 100644 index 0000000000..8079f67df0 --- /dev/null +++ b/contracts/exchange/test/transactions_unit_tests.ts @@ -0,0 +1,43 @@ +import { blockchainTests, chaiSetup, describe } from '@0x/contracts-test-utils'; +import * as chai from 'chai'; +import * as _ from 'lodash'; + +import { artifacts, TestTransactionsContract } from '../src'; + +chaiSetup.configure(); +const expect = chai.expect; +blockchainTests.resets('FillOrder Tests', ({ provider, web3Wrapper, txDefaults }) => { + let transactionsContract: TestTransactionsContract; + let accounts: string[]; + + before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + + transactionsContract = await TestTransactionsContract.deployFrom0xArtifactAsync( + artifacts.TestTransactions, + provider, + txDefaults, + {}, + ); + }); + + describe('getCurrentContext', () => { + it('should return the sender address when there is not a saved context address', async () => { + const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({ + from: accounts[0], + }); + expect(currentContextAddress).to.be.eq(accounts[0]); + }); + + it('should return the sender address when there is a saved context address', async () => { + // Set the current context address to the taker address + await transactionsContract.setCurrentContextAddress.sendTransactionAsync(accounts[1]); + + // Ensure that the queried current context address is the same as the address that was set. + const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({ + from: accounts[0], + }); + expect(currentContextAddress).to.be.eq(accounts[1]); + }); + }); +}); diff --git a/contracts/exchange/tsconfig.json b/contracts/exchange/tsconfig.json index beff07f648..63279898fc 100644 --- a/contracts/exchange/tsconfig.json +++ b/contracts/exchange/tsconfig.json @@ -30,6 +30,7 @@ "generated-artifacts/TestExchangeInternals.json", "generated-artifacts/TestLibExchangeRichErrorDecoder.json", "generated-artifacts/TestSignatureValidator.json", + "generated-artifacts/TestTransactions.json", "generated-artifacts/TestValidatorWallet.json", "generated-artifacts/TestWrapperFunctions.json", "generated-artifacts/Whitelist.json"