(async () => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)( (logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { done(new Error('Expected this subscription to have been cancelled')); }, ); zeroEx.etherToken.subscribe( etherTokenAddress, EtherTokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled, ); const callbackToBeCalled = reportNodeCallbackErrors(done)(); const newProvider = web3Factory.getRpcProvider(); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); zeroEx.etherToken.subscribe( etherTokenAddress, EtherTokenEvents.Transfer, indexFilterValues, callbackToBeCalled, ); await zeroEx.token.transferAsync( etherTokenAddress, addressWithETH, addressWithoutFunds, transferAmount, ); })().catch(done);
describe('Assertion library', () => { const web3 = web3Factory.create(); const config = { networkId: constants.TESTRPC_NETWORK_ID, }; const zeroEx = new ZeroEx(web3.currentProvider, config); describe('#isSenderAddressHexAsync', () => { it('throws when address is invalid', async () => { const address = '0xdeadbeef'; const varName = 'address'; return expect( assert.isSenderAddressAsync(varName, address, (zeroEx as any)._web3Wrapper), ).to.be.rejectedWith(`Expected ${varName} to be of type ETHAddressHex, encountered: ${address}`); }); it('throws when address is unavailable', async () => { const validUnrelatedAddress = '0x8b0292b11a196601eddce54b665cafeca0347d42'; const varName = 'address'; return expect( assert.isSenderAddressAsync(varName, validUnrelatedAddress, (zeroEx as any)._web3Wrapper), ).to.be.rejectedWith( `Specified ${varName} ${validUnrelatedAddress} isn't available through the supplied web3 provider`, ); }); it("doesn't throw if address is available", async () => { const availableAddress = (await zeroEx.getAvailableAddressesAsync())[0]; const varName = 'address'; return expect( assert.isSenderAddressAsync(varName, availableAddress, (zeroEx as any)._web3Wrapper), ).to.become(undefined); }); }); });
it('overrides provider in nested web3s and invalidates contractInstances', async () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any)._getExchangeContractAsync(); await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync(); expect((zeroEx.exchange as any)._exchangeContractIfExists).to.not.be.undefined(); expect((zeroEx.tokenRegistry as any)._tokenRegistryContractIfExists).to.not.be.undefined(); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider (newProvider as any).zeroExTestId = 1; zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); // Check that contractInstances with old provider are removed after provider update expect((zeroEx.exchange as any)._exchangeContractIfExists).to.be.undefined(); expect((zeroEx.tokenRegistry as any)._tokenRegistryContractIfExists).to.be.undefined(); // Check that all nested web3 wrapper instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any)._web3Wrapper.getProvider(); expect(nestedWeb3WrapperProvider.zeroExTestId).to.be.a('number'); const exchangeWeb3WrapperProvider = (zeroEx.exchange as any)._web3Wrapper.getProvider(); expect(exchangeWeb3WrapperProvider.zeroExTestId).to.be.a('number'); const tokenRegistryWeb3WrapperProvider = (zeroEx.tokenRegistry as any)._web3Wrapper.getProvider(); expect(tokenRegistryWeb3WrapperProvider.zeroExTestId).to.be.a('number'); });
import { web3Factory } from '@0xproject/dev-utils'; import * as chai from 'chai'; import { ZeroEx } from '../src'; import { chaiSetup } from './utils/chai_setup'; import { constants } from './utils/constants'; chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); describe('TokenTransferProxyWrapper', () => { let zeroEx: ZeroEx; const config = { networkId: constants.TESTRPC_NETWORK_ID, }; before(async () => { zeroEx = new ZeroEx(web3.currentProvider, config); }); describe('#isAuthorizedAsync', () => { it('should return false if the address is not authorized', async () => { const isAuthorized = await zeroEx.proxy.isAuthorizedAsync(ZeroEx.NULL_ADDRESS); expect(isAuthorized).to.be.false(); }); }); describe('#getAuthorizedAddressesAsync', () => { it('should return the list of authorized addresses', async () => { const authorizedAddresses = await zeroEx.proxy.getAuthorizedAddressesAsync(); for (const authorizedAddress of authorizedAddresses) { const isAuthorized = await zeroEx.proxy.isAuthorizedAsync(authorizedAddress);
import { web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; const web3ProviderConfig = { shouldUseInProcessGanache: true }; export const web3 = web3Factory.create(web3ProviderConfig); export const web3Wrapper = new Web3Wrapper(web3.currentProvider);
before(async () => { const hasAddresses = false; const web3WithoutAccounts = web3Factory.create({ hasAddresses }); zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config); });
before(async () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); });