/
sendStartSaga.js
88 lines (71 loc) · 2.19 KB
/
sendStartSaga.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { takeLatest, call, select } from 'redux-saga/effects';
import {
Memo, MemoText, Operation, TransactionBuilder,
} from 'stellar-sdk';
import { assets, safeLoadAccount, submitTransaction } from 'core';
import i18n, { getMessageByErrorType } from 'utils/i18n';
import { trackEvent, ErrorTyper } from 'utils';
import navigator from 'state/navigator';
import { getMasterAccount } from 'state/account';
import { getKeypairFor } from 'state/auth';
import {
sendActions,
getAmount,
getAsset,
getDestination,
getMemo,
} from 'state/send';
function* buildTransaction({
amount, asset, destination, memo,
}) {
const account = yield select(getMasterAccount);
const accountKeypair = yield select(getKeypairFor);
const paymentOp = Operation.payment({
destination,
amount,
asset: assets[asset],
});
let tx = new TransactionBuilder(account);
if (memo) {
tx = tx.addMemo(new Memo(MemoText, memo));
}
tx = tx.addOperation(paymentOp).build();
tx.sign(accountKeypair);
return tx;
}
function* run() {
try {
yield call(navigator.navigate, 'Send', 'Loading');
const amount = yield select(getAmount);
const asset = yield select(getAsset);
const destination = yield select(getDestination);
const memo = yield select(getMemo);
yield call(safeLoadAccount, destination);
const tx = yield call(buildTransaction, {
amount,
asset,
destination,
memo,
});
yield call(submitTransaction, tx);
yield call(navigator.navigate, 'Send', 'Notice', {
action: ({ navigate }) => navigate('Dashboard'),
type: 'success',
message: i18n.t('send.success.message', {
amount,
asset: asset.toUpperCase(),
destination: `${destination.slice(0, 11)}…${destination.slice(-11)}`,
}),
});
trackEvent('Send::Success');
} catch (error) {
yield call(navigator.navigate, 'Send', 'Notice', {
action: ({ dispatch }) => dispatch(sendActions.sendStart()),
goBackAction: ({ navigate }) => navigate('Dashboard'),
type: 'error',
message: getMessageByErrorType(new ErrorTyper(error)),
});
trackEvent('Send::Error');
}
}
export default takeLatest(sendActions.sendStart, run);