-
Notifications
You must be signed in to change notification settings - Fork 10
/
rfb_file.js
299 lines (229 loc) · 11.2 KB
/
rfb_file.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import moment from 'moment-timezone';
const DEFAULT_TIMEZONE = 'America/Sao_Paulo';
function getIdentityRFB(identity_type){
return (identity_type === 'CPF') ? 1
: (identity_type === 'CNPJ') ? 2
: (identity_type === 'NIF_PF') ? 3
: (identity_type === 'NIF_PJ') ? 4
: (identity_type === 'PASSPORT') ? 5
: (identity_type === 'COUNTRY_NO_ID') ? 6
: (identity_type === 'USER_NO_ID') ? 7
: '';
}
function formatDate(date) {
return moment.tz(date, DEFAULT_TIMEZONE).format('DDMMYYYY');
}
export function createHeader(obj) {
const line_type = '0000';
const { exchange_name, exchange_cnpj, exchange_url } = obj;
const rfb_exchange_cnpj = exchange_cnpj.match(/\d+/g).join('');
return `${line_type}|${rfb_exchange_cnpj}|${exchange_name}|${exchange_url}\r\n`;
}
export function createFooter(obj) {
const line_type = '9999';
const { buySellQuantity, permutationQuantity, depositQuantity, withdrawQuantity, paymentQuantity, otherQuantity, buySellTotal, balanceReportQuantity } = obj;
const rfb_buySellTotal = Number(buySellTotal).toFixed(2).replace(/\./g, '');
return `${line_type}|${buySellQuantity}|${rfb_buySellTotal}|${permutationQuantity}|${depositQuantity}|${withdrawQuantity}|${paymentQuantity}|${otherQuantity}|${balanceReportQuantity}\r\n`;
}
export function createBuySellOp(obj) {
const line_type = '0110';
const operation_code = 'I';
const {
date,
id,
brl_value,
brl_fees,
coin_symbol,
coin_quantity,
buyer_identity_type,
buyer_country,
buyer_document,
buyer_fullname,
buyer_address,
seller_identity_type,
seller_country,
seller_document,
seller_fullname,
seller_address,
} = obj;
const rfb_brl_value = brl_value.toFixed(2).replace(/\./g, '');
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_coin_quantity = coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_buyer_identity_type = getIdentityRFB(buyer_identity_type);
const rfb_buyer_cpf = (buyer_document && [1,2].includes(rfb_buyer_identity_type)) ? buyer_document.match(/\d+/g).join('') : '';
const rfb_buyer_nif = (buyer_document && [3,4,5].includes(rfb_buyer_identity_type)) ? buyer_document : '';
const rfb_seller_identity_type = getIdentityRFB(seller_identity_type);
const rfb_seller_cpf = (seller_document && [1,2].includes(rfb_seller_identity_type)) ? seller_document.match(/\d+/g).join('') : '';
const rfb_seller_nif = (seller_document && [3,4,5].includes(rfb_seller_identity_type)) ? seller_document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_value}|${rfb_brl_fees}|${coin_symbol}|${rfb_coin_quantity}|${rfb_buyer_identity_type}|${buyer_country}|${rfb_buyer_cpf}|${rfb_buyer_nif}|${buyer_fullname}|${buyer_address}|${rfb_seller_identity_type}|${seller_country}|${rfb_seller_cpf}|${rfb_seller_nif}|${seller_fullname}|${seller_address}\r\n`;
}
export function createPermutationOp(obj) {
const line_type = '0210';
const operation_code = 'II';
const {
date,
id,
brl_fees,
user1_coin_symbol,
user1_coin_quantity,
user1_identity_type,
user1_country,
user1_document,
user1_fullname,
user1_address,
user2_coin_symbol,
user2_coin_quantity,
user2_identity_type,
user2_country,
user2_document,
user2_fullname,
user2_address,
} = obj;
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_user1_coin_quantity = user1_coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_user2_coin_quantity = user2_coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_user1_identity_type = getIdentityRFB(user1_identity_type);
const rfb_user2_identity_type = getIdentityRFB(user2_identity_type);
const rfb_user1_cpf = (user1_document && [1,2].includes(rfb_user1_identity_type)) ? user1_document.match(/\d+/g).join('') : '';
const rfb_user1_nif = (user1_document && [3,4,5].includes(rfb_user1_identity_type)) ? user1_document : '';
const rfb_user2_cpf = (user2_document && [1,2].includes(rfb_user2_identity_type)) ? user2_document.match(/\d+/g).join('') : '';
const rfb_user2_nif = (user2_document && [3,4,5].includes(rfb_user2_identity_type)) ? user2_document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_fees}|${user1_coin_symbol}|${rfb_user1_coin_quantity}|${rfb_user1_identity_type}|${user1_country}|${rfb_user1_cpf}|${rfb_user1_nif}|${user1_fullname}|${user1_address}|${user2_coin_symbol}|${rfb_user2_coin_quantity}|${rfb_user2_identity_type}|${user2_country}|${rfb_user2_cpf}|${rfb_user2_nif}|${user2_fullname}|${user2_address}\r\n`;
}
export function createDepositOp(obj) {
const line_type = '0410';
const operation_code = 'IV';
const {
date,
id,
brl_fees,
coin_symbol,
coin_quantity,
identity_type,
country,
document,
fullname,
address,
} = obj;
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_coin_quantity = coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_identity_type = getIdentityRFB(identity_type);
const rfb_cpf = (document && [1,2].includes(rfb_identity_type)) ? document.match(/\d+/g).join('') : '';
const rfb_nif = (document && [3,4,5].includes(rfb_identity_type)) ? document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_fees}|${coin_symbol}|${rfb_coin_quantity}|${rfb_identity_type}|${country}|${rfb_cpf}|${rfb_nif}|${fullname}|${address}\r\n`;
}
export function createWithdrawOp(obj) {
const line_type = '0510';
const operation_code = 'V';
const {
date,
id,
brl_fees,
coin_symbol,
coin_quantity,
identity_type,
country,
document,
fullname,
address,
} = obj;
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_coin_quantity = coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_identity_type = getIdentityRFB(identity_type);
const rfb_cpf = (document && [1,2].includes(rfb_identity_type)) ? document.match(/\d+/g).join('') : '';
const rfb_nif = (document && [3,4,5].includes(rfb_identity_type)) ? document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_fees}|${coin_symbol}|${rfb_coin_quantity}|${rfb_identity_type}|${country}|${rfb_cpf}|${rfb_nif}|${fullname}|${address}\r\n`;
}
export function createPaymentOp(obj) {
const line_type = '0710';
const operation_code = 'VII';
const {
date,
id,
brl_fees,
coin_symbol,
coin_quantity,
payer_identity_type,
payer_country,
payer_document,
payer_fullname,
payer_address,
receiver_identity_type,
receiver_country,
receiver_document,
receiver_fullname,
receiver_address,
} = obj;
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_coin_quantity = coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_payer_identity_type = getIdentityRFB(payer_identity_type);
const rfb_receiver_identity_type = getIdentityRFB(receiver_identity_type);
const rfb_payer_cpf = (payer_document && [1,2].includes(rfb_payer_identity_type)) ? payer_document.match(/\d+/g).join('') : '';
const rfb_payer_nif = (payer_document && [3,4,5].includes(rfb_payer_identity_type)) ? payer_document : '';
const rfb_receiver_cpf = (receiver_document && [1,2].includes(rfb_receiver_identity_type)) ? receiver_document.match(/\d+/g).join('') : '';
const rfb_receiver_nif = (receiver_document && [3,4,5].includes(rfb_receiver_identity_type)) ? receiver_document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_fees}|${coin_symbol}|${rfb_coin_quantity}|${rfb_payer_identity_type}|${payer_country}|${rfb_payer_cpf}|${rfb_payer_nif}|${payer_fullname}|${payer_address}|${rfb_receiver_identity_type}|${receiver_country}|${rfb_receiver_cpf}|${rfb_receiver_nif}|${receiver_fullname}|${receiver_address}\r\n`;
}
export function createOtherOp(obj) {
const line_type = '0910';
const operation_code = 'IX';
const {
date,
id,
brl_fees,
coin_symbol,
coin_quantity,
origin_identity_type,
origin_country,
origin_document,
origin_fullname,
origin_address,
recipient_identity_type,
recipient_country,
recipient_document,
recipient_fullname,
recipient_address,
} = obj;
const rfb_brl_fees = brl_fees.toFixed(2).replace(/\./g, '');
const rfb_coin_quantity = coin_quantity.toFixed(10).replace(/\./g, '');
const rfb_origin_identity_type = getIdentityRFB(origin_identity_type);
const rfb_recipient_identity_type = getIdentityRFB(recipient_identity_type);
const rfb_origin_cpf = (origin_document && [1,2].includes(rfb_origin_identity_type)) ? origin_document.match(/\d+/g).join('') : '';
const rfb_origin_nif = (origin_document && [3,4,5].includes(rfb_origin_identity_type)) ? origin_document : '';
const rfb_recipient_cpf = (recipient_document && [1,2].includes(rfb_recipient_identity_type)) ? recipient_document.match(/\d+/g).join('') : '';
const rfb_recipient_nif = (recipient_document && [3,4,5].includes(rfb_recipient_identity_type)) ? recipient_document : '';
return `${line_type}|${formatDate(date)}|${id}|${operation_code}|${rfb_brl_fees}|${coin_symbol}|${rfb_coin_quantity}|${rfb_origin_identity_type}|${origin_country}|${rfb_origin_cpf}|${rfb_origin_nif}|${origin_fullname}|${origin_address}|${rfb_recipient_identity_type}|${recipient_country}|${rfb_recipient_cpf}|${rfb_recipient_nif}|${recipient_fullname}|${recipient_address}\r\n`;
}
export function createBalanceReportData(obj) {
const line_type_1 = '1000';
const line_type_2 = '1010';
const {
date,
identity_type,
country,
document,
fullname,
address,
fiat_balance,
coin_symbol: old_coin_symbol,
coin_balance: old_coin_balance,
} = obj;
let { coin_balances } = obj;
if (!coin_balances) coin_balances = [];
if (old_coin_symbol && old_coin_balance) {
coin_balances.push({
coin_symbol: old_coin_symbol,
coin_balance: old_coin_balance,
});
}
const rfb_fiat_balance = fiat_balance.toFixed(2).replace(/\./g, '');
const rfb_identity_type = getIdentityRFB(identity_type);
const rfb_cpf = (document && [1,2].includes(rfb_identity_type)) ? document.match(/\d+/g).join('') : '';
const rfb_nif = (document && [3,4,5].includes(rfb_identity_type)) ? document : '';
let returnString = `${line_type_1}|${formatDate(date)}|${rfb_identity_type}|${country}|${rfb_cpf}|${rfb_nif}|${fullname}|${address}|${rfb_fiat_balance}\r\n`;
for (const { coin_symbol, coin_balance } of coin_balances) {
const rfb_coin_balance = coin_balance.toFixed(10).replace(/\./g, '');
returnString += `${line_type_2}|${coin_symbol}|${rfb_coin_balance}|\r\n`;
}
return returnString;
}