1import { Just, Nothing } from 'folktale/maybe';
2import React from 'react';
3import * as azimuth from 'azimuth-js';
4import * as need from '../lib/need';
6import { hasReceived, sendMail } from '../lib/inviteMail';
7import { Row, Col, Button, Input, H3, Warning } from '../components/old/Base';
9// for wallet generation
10import * as wg from '../_walletgen/lib/lib';
12// for transaction generation and signing
15 sendSignedTransaction,
16 waitForTransactionConfirm,
21import * as tank from '../lib/tank';
22import { withNetwork } from '../store/network';
23import { compose } from '../lib/lib';
24import { withWallet } from '../store/wallet';
25import { withPointCursor } from '../store/pointCursor';
27const GAS_PRICE_GWEI = 20; // we pay the premium for faster ux
28const GAS_LIMIT = 350000;
29const INVITE_COST = toWei((GAS_PRICE_GWEI * GAS_LIMIT).toString(), 'gwei');
32 INPUT: 'Generate invites',
33 GENERATING: 'Generating invites',
34 CAN_SEND: 'Send invites',
35 FUNDING: 'Funding invites',
36 SENDING: 'Sending invites',
47class InvitesSend extends React.Component {
52 invitesAvailable: Nothing(),
54 targets: [{ email: '', hasReceived: Nothing(), status: Nothing() }],
58 fundingMessage: Nothing(),
62 this.findInvited = this.findInvited.bind(this);
63 this.generateInvites = this.generateInvites.bind(this);
64 this.sendInvites = this.sendInvites.bind(this);
65 this.canSend = this.canSend.bind(this);
66 this.addError = this.addError.bind(this);
67 this.handleEmailInput = this.handleEmailInput.bind(this);
68 this.addTarget = this.addTarget.bind(this);
69 this.removeTarget = this.removeTarget.bind(this);
70 this.setEmailStatus = this.setEmailStatus.bind(this);
71 this.askForFunding = this.askForFunding.bind(this);
75 this.point = parseInt(need.pointCursor(this.props), 10);
76 this.address = need.address(this.props);
77 this.contracts = need.contracts(this.props);
78 this.web3 = need.web3(this.props);
80 console.log(azimuth.delegatedSending);
81 azimuth.delegatedSending
82 .getTotalUsableInvites(this.contracts, this.point)
84 this.setState({ invitesAvailable: Just(count) });
89 componentDidUpdate(prevProps) {
94 let invited = await azimuth.delegatedSending.getInvited(
98 invited = invited.map(async point => {
99 const active = await azimuth.azimuth.isActive(this.contracts, point);
100 const res = { point: Number(point), active: active };
103 invited = await Promise.all(invited);
104 const total = invited.length;
105 const accepted = invited.filter(i => i.active).length;
106 this.setState({ invited: Just({ total, accepted }) });
109 async getTemporaryWallet() {
110 const ticket = await wg.makeTicket(0x10000); // planet-sized ticket
111 const owner = await wg.generateOwnershipWallet(0, ticket);
112 return { ticket, owner: owner.keys.address };
115 async generateInvites() {
116 this.setState({ status: STATUS.GENERATING });
118 const web3 = this.web3;
119 const targets = this.state.targets;
120 const planets = await azimuth.delegatedSending.getPlanetsToSend(
126 // account for the race condition where invites got used up while we were
127 // composing our target list
128 if (planets.length < targets.length) {
129 this.addError('Can currently only send ' + planets.length + ' invites!');
133 const nonce = await web3.eth.getTransactionCount(this.address);
134 const chainId = await web3.eth.net.getId();
136 // create invite wallets and transactions
137 let invites = []; // { email, ticket, signedTx, rawTx }
138 for (let i = 0; i < targets.length; i++) {
139 console.log('generating email', i, targets[i].email);
140 this.setEmailStatus(i, EMAIL_STATUS.BUSY);
142 const email = targets[i].email;
143 const wallet = await this.getTemporaryWallet();
145 let inviteTx = azimuth.delegatedSending.sendPoint(
151 const signedTx = await signTransaction({
154 gasPrice: GAS_PRICE_GWEI.toString(),
155 gasLimit: GAS_LIMIT.toString(),
160 const rawTx = hexify(signedTx.serialize());
162 invites.push({ email, ticket: wallet.ticket, signedTx, rawTx });
163 this.setEmailStatus(i, EMAIL_STATUS.DONE);
166 this.setState({ wipInvites: invites, status: STATUS.CAN_SEND });
169 async sendInvites() {
170 const invites = this.state.wipInvites;
171 const web3 = this.web3;
173 //TODO invite status isn't changing/re-rendering... why?
175 status: STATUS.FUNDING,
176 invites: invites.map(i => {
177 return { ...i, status: Nothing() };
181 const tankWasUsed = await tank.ensureFundsFor(
185 INVITE_COST * invites.length,
186 invites.map(i => i.rawTx),
189 this.setState({ fundingMessage: Nothing() });
193 this.setState({ status: STATUS.SENDING });
195 //TODO nasty loop, probably move into lib... but so many callbacks!
197 for (let i = 0; i < invites.length; i++) {
198 const invite = invites[i];
200 // send transaction. if it fails, inform the user and skip to next
203 txHash = await sendSignedTransaction(
205 Just(invite.signedTx),
210 console.error('invite tx sending failed', err);
211 this.addError('Invite transaction not sent for ' + invite.email);
212 this.setEmailStatus(i, EMAIL_STATUS.FAIL);
216 // ask the email sender to send this. we can do this prior to tx confirm,
217 // because the sender service waits for confirm & success for us.
218 // if this fails, inform the user and skip to next
220 const mailSuccess = await sendMail(
225 if (!mailSuccess) throw new Error();
226 this.setEmailStatus(i, EMAIL_STATUS.DONE);
228 console.log('email send failed');
229 //NOTE this assumes that the transaction succeeds, but we don't know
230 // that for a fact yet...
232 'Invite email failed to send for ' +
234 '. Please give them this ticket: ' +
237 this.setEmailStatus(i, EMAIL_STATUS.FAIL);
241 // update status on transaction confirm, but don't block on it
243 waitForTransactionConfirm(web3, txHash).then(success => {
245 this.setEmailStatus(i, EMAIL_STATUS.DONE);
247 console.log('invite tx rejected');
248 this.addError('Invite transaction failed for ' + invite.email);
249 this.setEmailStatus(i, EMAIL_STATUS.FAIL);
256 const txStatus = await Promise.all(promises);
257 if (txStatus.every(e => e)) {
258 this.setState({ status: STATUS.DONE });
260 this.setState({ status: STATUS.FAIL });
264 setEmailStatus(i, status) {
265 const targets = this.state.targets;
266 targets[i].status = Just(status);
267 this.setState({ targets });
271 const newError = this.state.errors.matchWith({
272 Nothing: () => [error],
274 errs.value.push(error);
278 this.setState({ errors: Just(newError) });
281 askForFunding(address, minBalance, balance) {
283 fundingMessage: Just(
284 `Please make sure ${address} has at least ${fromWei(minBalance)} ETH,` +
285 `we'll continue once that's true. Current balance: ${fromWei(
293 handleEmailInput(i, email) {
294 let targets = this.state.targets;
295 targets[i].email = email;
297 // if it's a valid address, check if it has received an invite before
298 if (email.match(/.*@.*\...+/)) {
299 hasReceived(email).then(res => {
300 targets[i].hasReceived = Just(res);
301 this.setState({ targets });
304 targets[i].hasReceived = Nothing();
307 this.setState({ targets });
311 const targets = this.state.targets;
313 // may not have duplicate targets
314 if (new Set(targets.map(t => t.email)).size !== targets.length)
317 // none may have received invites already
318 for (let i = 0; i < targets.length; i++) {
319 const target = targets[i];
320 const res = target.hasReceived.matchWith({
322 Just: has => has.value,
324 if (res) return false;
327 return this.state.invitesAvailable.matchWith({
328 Nothing: () => false,
329 Just: invites => invites.value > 0,
334 let targets = this.state.targets;
335 targets.push({ email: '', hasReceived: Nothing(), status: Nothing() });
336 this.setState({ targets });
340 let targets = this.state.targets;
341 targets.length = targets.length - 1;
342 this.setState({ targets });
347 this.state.status === STATUS.DONE ? this.state.targets.length : null;
349 const invitesAvailable = this.state.invitesAvailable.matchWith({
350 Nothing: () => 'Loading...',
352 const dia = dif ? `(-${dif})` : '';
353 return `You currently have ${invites.value}${dia} invitations left.`;
357 const invitesSent = this.state.invited.matchWith({
358 Nothing: () => <>{'Loading...'}</>,
360 const dit = dif ? `(+${dif})` : '';
364 {invited.value.total + dit}
365 {' invites you sent, '}
366 {invited.value.accepted}
367 {' have been accepted.'}
373 const error = this.state.errors.matchWith({
376 <Warning className={'mt-8'}>
377 <H3 style={{ marginTop: 0, paddingTop: 0 }}>
378 {'Something went wrong!'}
380 {errors.value.map(err => (
387 const fundingMessage = this.state.fundingMessage.matchWith({
389 Just: msg => <Warning className={'mt-8'}>{msg.value}</Warning>,
393 if (this.state.status === STATUS.DONE) {
394 //TODO proper clickable router thing
395 inputs = '<- Back to Home';
397 const inputDisabled = this.state.status !== STATUS.INPUT;
401 prop-size={'s narrow'}
403 disabled={inputDisabled || this.state.targets.length === 1}
404 onClick={this.removeTarget}>
411 prop-size={'s narrow'}
415 this.state.invitesAvailable.matchWith({
417 Just: inv => inv.value <= this.state.targets.length,
420 onClick={this.addTarget}>
425 let targetInputs = [];
426 for (let i = 0; i < this.state.targets.length; i++) {
427 const target = this.state.targets[i];
428 let progress = target.status.matchWith({
430 Just: status => status.value,
437 this.handleEmailInput(i, value);
439 placeholder={'Email address'}
440 disabled={inputDisabled}
442 {target.hasReceived.matchWith({
445 has.value ? <span>{'×'}</span> : <span>{'°'}</span>,
452 const submitButton = (
454 prop-size={'xl wide'}
457 (inputDisabled && this.state.status !== STATUS.CAN_SEND) ||
461 if (this.state.status === STATUS.INPUT) this.generateInvites();
462 if (this.state.status === STATUS.CAN_SEND) this.sendInvites();
481 <p>{'send invites here, for planets'}</p>
483 <p>{invitesAvailable}</p>
485 <ul>{invitesSent}</ul>
498export default compose(
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list