How to use @0x/contracts-test-utils - 10 common examples

To help you get started, we’ve selected a few @0x/contracts-test-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 0xProject / 0x-monorepo / contracts / integrations / src / deployment_mananger.ts View on Github external
environment.provider,
            environment.txDefaults,
            exchangeArtifacts,
            new BigNumber(chainId),
        );
        const assetProxyOwner = await AssetProxyOwnerContract.deployFrom0xArtifactAsync(
            multisigArtifacts.AssetProxyOwner,
            environment.provider,
            txDefaults,
            multisigArtifacts,
            [],
            [],
            [],
            [owner],
            new BigNumber(1),
            constants.ZERO_AMOUNT,
        );
        const tokens = await DeploymentManager._deployTokenContractsAsync(environment, txDefaults);
        const staking = await DeploymentManager._deployStakingContractsAsync(
            environment,
            owner,
            txDefaults,
            tokens,
            assetProxies,
        );

        // Configure the asset proxies with the exchange and the exchange with the staking contracts.
        await DeploymentManager._configureAssetProxiesWithExchangeAsync(assetProxies, exchange, owner);
        await DeploymentManager._configureExchangeWithStakingAsync(exchange, staking, owner);

        // Authorize the asset-proxy owner in the staking proxy and in the zrx vault.
        await staking.stakingProxy.addAuthorizedAddress.awaitTransactionSuccessAsync(assetProxyOwner.address, {
github 0xProject / 0x-coordinator-server / ts / src / handlers.ts View on Github external
private async _handleFillsAsync(
        coordinatorOrders: Order[],
        txOrigin: string,
        signedTransaction: SignedZeroExTransaction,
        takerAssetFillAmounts: BigNumber[],
        chainId: number,
    ): Promise {
        await Handlers._validateFillsAllowedOrThrowAsync(signedTransaction, coordinatorOrders, takerAssetFillAmounts);

        const transactionHash = transactionHashUtils.getTransactionHashHex(signedTransaction);
        const fillRequestReceivedEvent = {
            type: EventTypes.FillRequestReceived,
            data: {
                transactionHash,
            },
        };
        this._broadcastCallback(fillRequestReceivedEvent, chainId);
        await utils.sleepAsync(this._configs.SELECTIVE_DELAY_MS); // Await selective delay

        // Check that still a valid fill request after selective delay
        if (this._configs.SELECTIVE_DELAY_MS !== 0) {
            await Handlers._validateFillsAllowedOrThrowAsync(
                signedTransaction,
                coordinatorOrders,
                takerAssetFillAmounts,
            );
github 0xProject / 0x-coordinator-server / ts / src / handlers.ts View on Github external
});
        if (_.isEmpty(coordinatorOrders)) {
            throw new ValidationError([
                {
                    field: 'signedTransaction.data',
                    code: ValidationErrorCodes.NoCoordinatorOrdersIncluded,
                    reason:
                        '0x transaction data does not include any orders involving this coordinators feeRecipientAddresses',
                },
            ]);
        }

        // 4. Enforce that a 0x transaction hasn't been used before. This prevents someone from requesting
        // the same transaction with a different `txOrigin` in an attempt to fill the order through an
        // alternative tx.origin entry-point.
        const transactionHash = transactionHashUtils.getTransactionHashHex(signedTransaction);
        const transactionIfExists = await transactionModel.findByHashAsync(transactionHash);
        if (transactionIfExists !== undefined) {
            throw new ValidationError([
                {
                    field: 'signedTransaction',
                    code: ValidationErrorCodes.TransactionAlreadyUsed,
                    reason: `A transaction can only be approved once. To request approval to perform the same actions, generate and sign an identical transaction with a different salt value.`,
                },
            ]);
        }

        // 5. Validate the 0x transaction signature
        const isValidSignature = await this._chainIdToContractWrappers[chainId].exchange
            .isValidHashSignature(transactionHash, signedTransaction.signerAddress, signedTransaction.signature)
            .callAsync();
        if (!isValidSignature) {
github 0xProject / 0x-monorepo / contracts / asset-proxy / src / erc721_wrapper.ts View on Github external
public async deployDummyTokensAsync(): Promise {
        // tslint:disable-next-line:no-unused-variable
        for (const i of _.times(constants.NUM_DUMMY_ERC721_TO_DEPLOY)) {
            this._dummyTokenContracts.push(
                await DummyERC721TokenContract.deployFrom0xArtifactAsync(
                    erc721Artifacts.DummyERC721Token,
                    this._provider,
                    txDefaults,
                    artifacts,
                    constants.DUMMY_TOKEN_NAME,
                    constants.DUMMY_TOKEN_SYMBOL,
                ),
            );
        }
        return this._dummyTokenContracts;
    }
    public async deployProxyAsync(): Promise {
github 0xProject / 0x-monorepo / contracts / asset-proxy / src / erc721_wrapper.ts View on Github external
public async deployDummyTokensAsync(): Promise {
        // tslint:disable-next-line:no-unused-variable
        for (const i of _.times(constants.NUM_DUMMY_ERC721_TO_DEPLOY)) {
            this._dummyTokenContracts.push(
                await DummyERC721TokenContract.deployFrom0xArtifactAsync(
                    erc721Artifacts.DummyERC721Token,
                    this._provider,
                    txDefaults,
                    artifacts,
                    constants.DUMMY_TOKEN_NAME,
                    constants.DUMMY_TOKEN_SYMBOL,
                ),
            );
        }
        return this._dummyTokenContracts;
    }
    public async deployProxyAsync(): Promise {
github 0xProject / 0x-monorepo / contracts / asset-proxy / src / erc1155_proxy_wrapper.ts View on Github external
this._tokenOwnerAddresses,
                    constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
                );
                const tokenIdAsString = tokenId.toString();
                this._fungibleTokenIds.push(tokenIdAsString);
                // Mint tokens for each owner for this token
                for (const tokenOwnerAddress of this._tokenOwnerAddresses) {
                    // tslint:disable-next-line:no-unused-variable
                    if (fungibleHoldingsByOwner[tokenOwnerAddress] === undefined) {
                        fungibleHoldingsByOwner[tokenOwnerAddress] = {};
                    }
                    if (fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress] === undefined) {
                        fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress] = {};
                    }
                    fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress][tokenIdAsString] =
                        constants.INITIAL_ERC1155_FUNGIBLE_BALANCE;
                    await dummyWrapper.setApprovalForAllAsync(
                        tokenOwnerAddress,
                        (this._proxyContract as ERC1155ProxyContract).address,
                        true,
                    );
                }
            }
            // Non-fungible tokens
            // tslint:disable-next-line:no-unused-variable
            for (const j of _.times(constants.NUM_ERC1155_NONFUNGIBLE_TOKENS_MINT)) {
                const [tokenId, nftIds] = await dummyWrapper.mintNonFungibleTokensAsync(this._tokenOwnerAddresses);
                const tokenIdAsString = tokenId.toString();
                this._nonFungibleTokenIds.push(tokenIdAsString);
                _.each(this._tokenOwnerAddresses, async (tokenOwnerAddress: string, i: number) => {
                    if (nonFungibleHoldingsByOwner[tokenOwnerAddress] === undefined) {
                        nonFungibleHoldingsByOwner[tokenOwnerAddress] = {};
github 0xProject / 0x-monorepo / contracts / asset-proxy / src / erc1155_proxy_wrapper.ts View on Github external
this._validateProxyContractExistsOrThrow();
        this._initialTokenIdsByOwner = {
            fungible: {},
            nonFungible: {},
        };
        const fungibleHoldingsByOwner: ERC1155FungibleHoldingsByOwner = {};
        const nonFungibleHoldingsByOwner: ERC1155NonFungibleHoldingsByOwner = {};
        // Set balances accordingly
        for (const dummyWrapper of this._dummyTokenWrappers) {
            const dummyAddress = dummyWrapper.getContract().address;
            // tslint:disable-next-line:no-unused-variable
            for (const i of _.times(constants.NUM_ERC1155_FUNGIBLE_TOKENS_MINT)) {
                // Create a fungible token
                const tokenId = await dummyWrapper.mintFungibleTokensAsync(
                    this._tokenOwnerAddresses,
                    constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
                );
                const tokenIdAsString = tokenId.toString();
                this._fungibleTokenIds.push(tokenIdAsString);
                // Mint tokens for each owner for this token
                for (const tokenOwnerAddress of this._tokenOwnerAddresses) {
                    // tslint:disable-next-line:no-unused-variable
                    if (fungibleHoldingsByOwner[tokenOwnerAddress] === undefined) {
                        fungibleHoldingsByOwner[tokenOwnerAddress] = {};
                    }
                    if (fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress] === undefined) {
                        fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress] = {};
                    }
                    fungibleHoldingsByOwner[tokenOwnerAddress][dummyAddress][tokenIdAsString] =
                        constants.INITIAL_ERC1155_FUNGIBLE_BALANCE;
                    await dummyWrapper.setApprovalForAllAsync(
                        tokenOwnerAddress,
github 0xProject / 0x-monorepo / contracts / exchange / src / exchange_data_encoder.ts View on Github external
encodeOrdersToExchangeData(fnName: ExchangeFunctionName, orders: SignedOrder[] = []): string {
        const exchangeInstance = new IExchangeContract(constants.NULL_ADDRESS, provider);
        let data;
        if (constants.SINGLE_FILL_FN_NAMES.indexOf(fnName) !== -1) {
            data = (exchangeInstance as any)
                [fnName](orders[0], orders[0].takerAssetAmount, orders[0].signature)
                .getABIEncodedTransactionData();
        } else if (constants.BATCH_FILL_FN_NAMES.indexOf(fnName) !== -1) {
            data = (exchangeInstance as any)
                [fnName](orders, orders.map(order => order.takerAssetAmount), orders.map(order => order.signature))
                .getABIEncodedTransactionData();
        } else if (constants.MARKET_FILL_FN_NAMES.indexOf(fnName) !== -1) {
            const fillAsset = /Buy/.test(fnName) ? 'makerAssetAmount' : 'takerAssetAmount';
            data = (exchangeInstance as any)
                [fnName](
                    orders,
                    orders.map(order => order[fillAsset]).reduce((prev, curr) => prev.plus(curr)),
                    orders.map(order => order.signature),
github 0xProject / 0x-monorepo / contracts / exchange / src / exchange_data_encoder.ts View on Github external
} else if (constants.MARKET_FILL_FN_NAMES.indexOf(fnName) !== -1) {
            const fillAsset = /Buy/.test(fnName) ? 'makerAssetAmount' : 'takerAssetAmount';
            data = (exchangeInstance as any)
                [fnName](
                    orders,
                    orders.map(order => order[fillAsset]).reduce((prev, curr) => prev.plus(curr)),
                    orders.map(order => order.signature),
                )
                .getABIEncodedTransactionData();
        } else if (constants.MATCH_ORDER_FN_NAMES.indexOf(fnName) !== -1) {
            data = exchangeInstance
                .matchOrders(orders[0], orders[1], orders[0].signature, orders[1].signature)
                .getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.CancelOrder) {
            data = exchangeInstance.cancelOrder(orders[0]).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.BatchCancelOrders) {
            data = exchangeInstance.batchCancelOrders(orders).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.CancelOrdersUpTo) {
            data = exchangeInstance.cancelOrdersUpTo(constants.ZERO_AMOUNT).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.PreSign) {
            data = exchangeInstance.preSign(orderHashUtils.getOrderHashHex(orders[0])).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.SetSignatureValidatorApproval) {
            data = exchangeInstance
                .setSignatureValidatorApproval(constants.NULL_ADDRESS, true)
                .getABIEncodedTransactionData();
        } else {
            throw new Error(`Error: ${fnName} not a supported function`);
        }
        return data;
    },
};
github 0xProject / 0x-monorepo / contracts / exchange / src / exchange_data_encoder.ts View on Github external
[fnName](orders, orders.map(order => order.takerAssetAmount), orders.map(order => order.signature))
                .getABIEncodedTransactionData();
        } else if (constants.MARKET_FILL_FN_NAMES.indexOf(fnName) !== -1) {
            const fillAsset = /Buy/.test(fnName) ? 'makerAssetAmount' : 'takerAssetAmount';
            data = (exchangeInstance as any)
                [fnName](
                    orders,
                    orders.map(order => order[fillAsset]).reduce((prev, curr) => prev.plus(curr)),
                    orders.map(order => order.signature),
                )
                .getABIEncodedTransactionData();
        } else if (constants.MATCH_ORDER_FN_NAMES.indexOf(fnName) !== -1) {
            data = exchangeInstance
                .matchOrders(orders[0], orders[1], orders[0].signature, orders[1].signature)
                .getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.CancelOrder) {
            data = exchangeInstance.cancelOrder(orders[0]).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.BatchCancelOrders) {
            data = exchangeInstance.batchCancelOrders(orders).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.CancelOrdersUpTo) {
            data = exchangeInstance.cancelOrdersUpTo(constants.ZERO_AMOUNT).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.PreSign) {
            data = exchangeInstance.preSign(orderHashUtils.getOrderHashHex(orders[0])).getABIEncodedTransactionData();
        } else if (fnName === ExchangeFunctionName.SetSignatureValidatorApproval) {
            data = exchangeInstance
                .setSignatureValidatorApproval(constants.NULL_ADDRESS, true)
                .getABIEncodedTransactionData();
        } else {
            throw new Error(`Error: ${fnName} not a supported function`);
        }
        return data;
    },