aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/scripts/controllers/balance.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js
index 4fa4c78f..f83f294c 100644
--- a/app/scripts/controllers/balance.js
+++ b/app/scripts/controllers/balance.js
@@ -5,7 +5,9 @@ const BN = require('ethereumjs-util').BN
class BalanceController {
constructor (opts = {}) {
+ this._validateParams(opts)
const { address, accountTracker, txController, blockTracker } = opts
+
this.address = address
this.accountTracker = accountTracker
this.txController = txController
@@ -65,6 +67,14 @@ class BalanceController {
return pending
}
+ _validateParams (opts) {
+ const { address, accountTracker, txController, blockTracker } = opts
+ if (!address || !accountTracker || !txController || !blockTracker) {
+ const error = 'Cannot construct a balance checker without address, accountTracker, txController, and blockTracker.'
+ throw new Error(error)
+ }
+ }
+
}
module.exports = BalanceController
a id='n77' href='#n77'>77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
import { ECSignature } from '@0xproject/types';
import { BigNumber, fetchAsync, logUtils } from '@0xproject/utils';
import * as _ from 'lodash';
import Paper from 'material-ui/Paper';
import TextField from 'material-ui/TextField';
import * as React from 'react';
import { CopyIcon } from 'ts/components/ui/copy_icon';
import { SideToAssetToken, TokenByAddress, WebsitePaths } from 'ts/types';
import { configs } from 'ts/utils/configs';
import { constants } from 'ts/utils/constants';
import { errorReporter } from 'ts/utils/error_reporter';
import { utils } from 'ts/utils/utils';

interface OrderJSONProps {
    exchangeContractIfExists: string;
    orderExpiryTimestamp: BigNumber;
    orderECSignature: ECSignature;
    orderTakerAddress: string;
    orderMakerAddress: string;
    orderSalt: BigNumber;
    orderMakerFee: BigNumber;
    orderTakerFee: BigNumber;
    orderFeeRecipient: string;
    sideToAssetToken: SideToAssetToken;
    tokenByAddress: TokenByAddress;
}

interface OrderJSONState {
    shareLink: string;
}

export class OrderJSON extends React.Component<OrderJSONProps, OrderJSONState> {
    constructor(props: OrderJSONProps) {
        super(props);
        this.state = {
            shareLink: '',
        };
        // tslint:disable-next-line:no-floating-promises
        this._setShareLinkAsync();
    }
    public render(): React.ReactNode {
        const order = utils.generateOrder(
            this.props.exchangeContractIfExists,
            this.props.sideToAssetToken,
            this.props.orderExpiryTimestamp,
            this.props.orderTakerAddress,
            this.props.orderMakerAddress,
            this.props.orderMakerFee,
            this.props.orderTakerFee,
            this.props.orderFeeRecipient,
            this.props.orderECSignature,
            this.props.tokenByAddress,
            this.props.orderSalt,
        );
        const orderJSON = JSON.stringify(order);
        return (
            <div>
                <div className="pb2">
                    You have successfully generated and cryptographically signed an order! The following JSON contains
                    the order parameters and cryptographic signature that your counterparty will need to execute a trade
                    with you.
                </div>
                <div className="pb2 flex">
                    <div className="inline-block pl1" style={{ top: 1 }}>
                        <CopyIcon data={orderJSON} callToAction="Copy" />
                    </div>
                </div>
                <Paper className="center overflow-hidden">
                    <TextField
                        id="orderJSON"
                        style={{ width: 710 }}
                        value={JSON.stringify(order, null, '\t')}
                        multiLine={true}
                        rows={2}
                        rowsMax={8}
                        underlineStyle={{ display: 'none' }}
                    />
                </Paper>
                <div className="pt3 pb2 center">
                    <div>Share your signed order!</div>
                    <div>
                        <div className="mx-auto overflow-hidden" style={{ width: 152 }}>
                            <TextField id={`${this.state.shareLink}-bitly`} value={this.state.shareLink} />
                        </div>
                    </div>
                    <div className="mx-auto pt1 flex" style={{ width: 91 }}>
                        <div>
                            <i
                                style={{ cursor: 'pointer', fontSize: 29 }}
                                onClick={this._shareViaFacebook.bind(this)}
                                className="zmdi zmdi-facebook-box"
                            />
                        </div>
                        <div className="pl1" style={{ position: 'relative', width: 28 }}>
                            <i
                                style={{
                                    cursor: 'pointer',
                                    fontSize: 32,
                                    position: 'absolute',
                                    top: -2,
                                    left: 8,
                                }}
                                onClick={this._shareViaEmailAsync.bind(this)}
                                className="zmdi zmdi-email"
                            />
                        </div>
                        <div className="pl1">
                            <i
                                style={{ cursor: 'pointer', fontSize: 29 }}
                                onClick={this._shareViaTwitterAsync.bind(this)}
                                className="zmdi zmdi-twitter-box"
                            />
                        </div>
                    </div>
                </div>
            </div>
        );
    }
    private _shareViaTwitterAsync(): void {
        const tweetText = encodeURIComponent(`Fill my order using the 0x protocol: ${this.state.shareLink}`);
        window.open(`https://twitter.com/intent/tweet?text=${tweetText}`, 'Share your order', 'width=500,height=400');
    }
    private _shareViaFacebook(): void {
        (window as any).FB.ui(
            {
                display: 'popup',
                href: this.state.shareLink,
                method: 'share',
            },
            _.noop.bind(_),
        );
    }
    private _shareViaEmailAsync(): void {
        const encodedSubject = encodeURIComponent("Let's trade using the 0x protocol");
        const encodedBody = encodeURIComponent(`I generated an order with the 0x protocol.
You can see and fill it here: ${this.state.shareLink}`);
        const mailToLink = `mailto:mail@example.org?subject=${encodedSubject}&body=${encodedBody}`;
        window.open(mailToLink, '_blank');
    }
    private async _setShareLinkAsync(): Promise<void> {
        const shareLink = await this._generateShareLinkAsync();
        this.setState({
            shareLink,
        });
    }
    private async _generateShareLinkAsync(): Promise<string> {
        const longUrl = encodeURIComponent(this._getOrderUrl());
        const bitlyRequestUrl = `${constants.URL_BITLY_API}/v3/shorten?access_token=${
            configs.BITLY_ACCESS_TOKEN
        }&longUrl=${longUrl}`;
        const response = await fetchAsync(bitlyRequestUrl);
        const responseBody = await response.text();
        const bodyObj = JSON.parse(responseBody);
        if (response.status !== 200 || bodyObj.status_code !== 200) {
            // TODO: Show error message in UI
            logUtils.log(`Unexpected status code: ${response.status} -> ${responseBody}`);
            errorReporter.report(new Error(`Bitly returned non-200: ${JSON.stringify(response)}`));
            return '';
        }
        return bodyObj.data.url;
    }
    private _getOrderUrl(): string {
        const order = utils.generateOrder(
            this.props.exchangeContractIfExists,
            this.props.sideToAssetToken,
            this.props.orderExpiryTimestamp,
            this.props.orderTakerAddress,
            this.props.orderMakerAddress,
            this.props.orderMakerFee,
            this.props.orderTakerFee,
            this.props.orderFeeRecipient,
            this.props.orderECSignature,
            this.props.tokenByAddress,
            this.props.orderSalt,
        );
        const orderJSONString = JSON.stringify(order);
        const orderUrl = `${configs.BASE_URL}${WebsitePaths.Portal}/fill?order=${orderJSONString}`;
        return orderUrl;
    }
}