forked from ZeusLN/zeus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransactionsStore.ts
135 lines (120 loc) · 4.19 KB
/
TransactionsStore.ts
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
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
import { action, reaction, observable } from 'mobx';
import axios from 'axios';
import Transaction from './../models/Transaction';
import TransactionRequest from './../models/TransactionRequest';
import ErrorUtils from './../utils/ErrorUtils';
import SettingsStore from './SettingsStore';
export default class TransactionsStore {
@observable loading: boolean = false;
@observable error: boolean = false;
@observable error_msg: string | null;
@observable transactions: Array<Transaction> = [];
@observable transaction: Transaction;
@observable payment_route: any; // Route
@observable payment_preimage: string | null;
@observable payment_hash: string | null;
@observable payment_error: string | null;
@observable onchain_address: string;
@observable txid: string | null;
settingsStore: SettingsStore;
constructor(settingsStore: SettingsStore) {
this.settingsStore = settingsStore;
reaction(
() => this.settingsStore.settings,
() => {
this.getTransactions()
}
);
}
@action
public getTransactions = () => {
const { settings } = this.settingsStore;
const { host, port, macaroonHex } = settings;
this.loading = true;
axios.request({
method: 'get',
url: `https://${host}${port ? ':' + port : ''}/v1/transactions`,
headers: {
'Grpc-Metadata-macaroon': macaroonHex
}
}).then((response: any) => {
// handle success
const data = response.data;
this.transactions = data.transactions.reverse();
this.loading = false;
})
.catch(() => {
// handle error
this.transactions = [];
this.loading = false;
});
}
@action
public sendCoins = (transactionRequest: TransactionRequest) => {
const { settings } = this.settingsStore;
const { host, port, macaroonHex } = settings;
this.error = false;
this.error_msg = null;
this.txid = null;
this.loading = true;
axios.request({
method: 'post',
url: `https://${host}${port ? ':' + port : ''}/v1/transactions`,
headers: {
'Grpc-Metadata-macaroon': macaroonHex
},
data: {
...transactionRequest
}
}).then((response: any) => {
// handle success
const data = response.data;
this.txid = data.txid;
this.loading = false;
})
.catch((error: any) => {
// handle error
const errorInfo = error.response.data;
this.error_msg = errorInfo.error;
this.error = true;
this.loading = false;
});
}
sendPayment = (payment_request: string) => {
const { settings } = this.settingsStore;
const { host, port, macaroonHex } = settings;
this.loading = true;
this.error_msg = null;
this.error = false;
this.payment_route = null;
this.payment_preimage = null;
this.payment_hash = null;
this.payment_error = null;
axios.request({
method: 'post',
url: `https://${host}${port ? ':' + port : ''}/v1/channels/transactions`,
headers: {
'Grpc-Metadata-macaroon': macaroonHex
},
data: {
payment_request: payment_request
}
}).then((response: any) => {
// handle success
const data = response.data;
this.loading = false;
this.payment_route = data.payment_route;
this.payment_preimage = data.payment_preimage;
this.payment_hash = data.payment_hash;
this.payment_error = data.payment_error;
})
.catch((error: any)=> {
// handle error
const errorInfo = error.response.data;
const code = errorInfo.code;
this.error = true;
this.loading = false;
this.error_msg = ErrorUtils.errorToUserFriendly(code) || errorInfo.error || 'Error sending payment';
});
}
}