Example #1
0
 before(async () => {
     const accounts = await web3Wrapper.getAvailableAddressesAsync();
     [maker, feeRecipient] = accounts;
     const tokenRegistry = await deployer.deployAsync(ContractName.TokenRegistry);
     const tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy);
     const [rep, dgd, zrx] = await Promise.all([
         deployer.deployAsync(ContractName.DummyToken, constants.DUMMY_TOKEN_ARGS),
         deployer.deployAsync(ContractName.DummyToken, constants.DUMMY_TOKEN_ARGS),
         deployer.deployAsync(ContractName.DummyToken, constants.DUMMY_TOKEN_ARGS),
     ]);
     const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [
         zrx.address,
         tokenTransferProxy.address,
     ]);
     const exchange = new ExchangeContract(web3Wrapper, exchangeInstance.abi, exchangeInstance.address);
     await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] });
     const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID });
     exchangeWrapper = new ExchangeWrapper(exchange, zeroEx);
     const defaultOrderParams = {
         exchangeContractAddress: exchange.address,
         maker,
         feeRecipient,
         makerTokenAddress: rep.address,
         takerTokenAddress: dgd.address,
         makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
         takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
         makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
         takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
     };
     orderFactory = new OrderFactory(zeroEx, defaultOrderParams);
     signedOrder = await orderFactory.newSignedOrderAsync();
 });
Example #2
0
 it('should return true with a valid signature', async () => {
     const success = await exchangeWrapper.isValidSignatureAsync(signedOrder);
     const orderHashHex = ZeroEx.getOrderHashHex(signedOrder);
     const isValidSignature = ZeroEx.isValidSignature(orderHashHex, signedOrder.ecSignature, signedOrder.maker);
     expect(isValidSignature).to.be.true();
     expect(success).to.be.true();
 });
            it('should set confirmation time with enough confirmations', async () => {
                const destination = multiSig.address;
                const from = owners[0];
                const dataParams = {
                    name: 'changeTimeLock',
                    abi: MULTI_SIG_ABI,
                    args: [SECONDS_TIME_LOCKED.toNumber()],
                };
                let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams);
                const subRes = await zeroEx.awaitTransactionMinedAsync(txHash);
                const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs<
                    SubmissionContractEventArgs
                >;

                txId = log.args.transactionId;
                txHash = await multiSig.confirmTransaction.sendTransactionAsync(txId, { from: owners[1] });
                const res = await zeroEx.awaitTransactionMinedAsync(txHash);
                expect(res.logs).to.have.length(2);

                const blockNum = await web3Wrapper.getBlockNumberAsync();
                const blockInfo = await web3Wrapper.getBlockAsync(blockNum);
                const timestamp = new BigNumber(blockInfo.timestamp);
                const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.callAsync(txId));

                expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum);
            });
Example #4
0
        it('should create an unfillable order', async () => {
            signedOrder = await orderFactory.newSignedOrderAsync({
                makerTokenAmount: new BigNumber(1001),
                takerTokenAmount: new BigNumber(3),
            });

            const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
                ZeroEx.getOrderHashHex(signedOrder),
            );
            expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);

            const fillTakerTokenAmount1 = new BigNumber(2);
            await exWrapper.fillOrderAsync(signedOrder, taker, {
                fillTakerTokenAmount: fillTakerTokenAmount1,
            });

            const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync(
                ZeroEx.getOrderHashHex(signedOrder),
            );
            expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1);

            const fillTakerTokenAmount2 = new BigNumber(1);
            await exWrapper.fillOrderAsync(signedOrder, taker, {
                fillTakerTokenAmount: fillTakerTokenAmount2,
            });

            const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync(
                ZeroEx.getOrderHashHex(signedOrder),
            );
            expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1);
        });
 return async () => {
     logUtils.log(`Processing ${tokenSymbol} ${recipientAddress}`);
     const amountToDispense = new BigNumber(DISPENSE_AMOUNT_TOKEN);
     const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(tokenSymbol);
     if (_.isUndefined(token)) {
         throw new Error(`Unsupported asset type: ${tokenSymbol}`);
     }
     const baseUnitAmount = ZeroEx.toBaseUnitAmount(amountToDispense, token.decimals);
     const userBalanceBaseUnits = await zeroEx.token.getBalanceAsync(token.address, recipientAddress);
     const maxAmountBaseUnits = ZeroEx.toBaseUnitAmount(
         new BigNumber(DISPENSE_MAX_AMOUNT_TOKEN),
         token.decimals,
     );
     if (userBalanceBaseUnits.greaterThanOrEqualTo(maxAmountBaseUnits)) {
         logUtils.log(
             `User exceeded token balance maximum (${maxAmountBaseUnits}) ${recipientAddress} ${userBalanceBaseUnits} `,
         );
         return;
     }
     const txHash = await zeroEx.token.transferAsync(
         token.address,
         configs.DISPENSER_ADDRESS,
         recipientAddress,
         baseUnitAmount,
     );
     logUtils.log(`Sent ${amountToDispense} ZRX to ${recipientAddress} tx: ${txHash}`);
 };
            before('deploy a walet', async () => {
                const multiSigInstance = await deployer.deployAsync(ContractName.MultiSigWalletWithTimeLock, [
                    owners,
                    SIGNATURES_REQUIRED,
                    SECONDS_TIME_LOCKED,
                ]);
                multiSig = new MultiSigWalletWithTimeLockContract(
                    web3Wrapper,
                    multiSigInstance.abi,
                    multiSigInstance.address,
                );
                multiSigWrapper = new MultiSigWrapper((multiSig as any) as MultiSigWalletContract);

                const secondsTimeLocked = await multiSig.secondsTimeLocked.callAsync();
                initialSecondsTimeLocked = secondsTimeLocked.toNumber();
                const destination = multiSig.address;
                const from = owners[0];
                const dataParams = {
                    name: 'changeTimeLock',
                    abi: MULTI_SIG_ABI,
                    args: [newSecondsTimeLocked],
                };
                let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams);
                const subRes = await zeroEx.awaitTransactionMinedAsync(txHash);
                const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs<
                    SubmissionContractEventArgs
                >;
                txId = log.args.transactionId;
                txHash = await multiSig.confirmTransaction.sendTransactionAsync(txId, {
                    from: owners[1],
                });
                const confRes = await zeroEx.awaitTransactionMinedAsync(txHash);
                expect(confRes.logs).to.have.length(2);
            });
            it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => {
                const destination = multiSig.address;
                const from = owners[0];
                const dataParams = {
                    name: 'changeTimeLock',
                    abi: MULTI_SIG_ABI,
                    args: [SECONDS_TIME_LOCKED.toNumber()],
                };
                let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams);
                const subRes = await zeroEx.awaitTransactionMinedAsync(txHash);
                const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs<
                    SubmissionContractEventArgs
                >;

                txId = log.args.transactionId;
                txHash = await multiSig.confirmTransaction.sendTransactionAsync(txId, { from: owners[1] });

                expect(initialSecondsTimeLocked).to.be.equal(0);

                txHash = await multiSig.executeTransaction.sendTransactionAsync(txId, { from: owners[0] });
                const res = await zeroEx.awaitTransactionMinedAsync(txHash);
                expect(res.logs).to.have.length(2);

                const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.callAsync());
                expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED);
            });
Example #8
0
 it('should return false with an invalid signature', async () => {
     signedOrder.ecSignature.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR'));
     signedOrder.ecSignature.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS'));
     const success = await exchangeWrapper.isValidSignatureAsync(signedOrder);
     const orderHashHex = ZeroEx.getOrderHashHex(signedOrder);
     const isValidSignature = ZeroEx.isValidSignature(orderHashHex, signedOrder.ecSignature, signedOrder.maker);
     expect(isValidSignature).to.be.false();
     expect(success).to.be.false();
 });
Example #9
0
        it('should throw when taker is specified and order is claimed by other', async () => {
            signedOrder = await orderFactory.newSignedOrderAsync({
                taker: feeRecipient,
                makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
                takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
            });

            return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
        });
Example #10
0
    public async providerTypeUpdatedFireAndForgetAsync(providerType: ProviderType) {
        utils.assert(!_.isUndefined(this.zeroEx), 'ZeroEx must be instantiated.');
        // Should actually be Web3.Provider|ProviderEngine union type but it causes issues
        // later on in the logic.
        let provider;
        switch (providerType) {
            case ProviderType.LEDGER: {
                const isU2FSupported = await utils.isU2FSupportedAsync();
                if (!isU2FSupported) {
                    throw new Error('Cannot update providerType to LEDGER without U2F support');
                }

                // Cache injected provider so that we can switch the user back to it easily
                this.cachedProvider = this.web3Wrapper.getProviderObj();

                this.dispatcher.updateUserAddress(''); // Clear old userAddress

                provider = new ProviderEngine();
                this.ledgerSubProvider = ledgerWalletSubproviderFactory(this.getBlockchainNetworkId.bind(this));
                provider.addProvider(this.ledgerSubProvider);
                provider.addProvider(new FilterSubprovider());
                const networkId = configs.isMainnetEnabled ?
                    constants.MAINNET_NETWORK_ID :
                    constants.TESTNET_NETWORK_ID;
                provider.addProvider(new RedundantRPCSubprovider(
                    constants.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId],
                ));
                provider.start();
                this.web3Wrapper.destroy();
                const shouldPollUserAddress = false;
                this.web3Wrapper = new Web3Wrapper(this.dispatcher, provider, this.networkId, shouldPollUserAddress);
                await this.zeroEx.setProviderAsync(provider);
                await this.postInstantiationOrUpdatingProviderZeroExAsync();
                break;
            }

            case ProviderType.INJECTED: {
                if (_.isUndefined(this.cachedProvider)) {
                    return; // Going from injected to injected, so we noop
                }
                provider = this.cachedProvider;
                const shouldPollUserAddress = true;
                this.web3Wrapper = new Web3Wrapper(this.dispatcher, provider, this.networkId, shouldPollUserAddress);
                await this.zeroEx.setProviderAsync(provider);
                await this.postInstantiationOrUpdatingProviderZeroExAsync();
                delete this.ledgerSubProvider;
                delete this.cachedProvider;
                break;
            }

            default:
                throw utils.spawnSwitchErr('providerType', providerType);
        }

        await this.fetchTokenInformationAsync();
    }