forked from deriv-com/flutter-deriv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt5_new_account_response_result.dart
458 lines (393 loc) · 13.5 KB
/
mt5_new_account_response_result.dart
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// ignore_for_file: prefer_single_quotes, unnecessary_import, unused_import
import 'package:equatable/equatable.dart';
import 'package:flutter_deriv_api/api/exceptions/exceptions.dart';
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
import 'package:flutter_deriv_api/api/models/enums.dart';
import 'package:flutter_deriv_api/api/response/mt5_deposit_response_result.dart';
import 'package:flutter_deriv_api/api/response/mt5_get_settings_response_result.dart';
import 'package:flutter_deriv_api/api/response/mt5_password_change_response_result.dart';
import 'package:flutter_deriv_api/api/response/mt5_password_check_response_result.dart';
import 'package:flutter_deriv_api/api/response/mt5_password_reset_response_result.dart';
import 'package:flutter_deriv_api/api/response/mt5_withdrawal_response_result.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_deposit_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_get_settings_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_new_account_receive.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_new_account_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_password_change_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_password_check_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_password_reset_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/mt5_withdrawal_send.dart';
import 'package:flutter_deriv_api/helpers/helpers.dart';
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
import 'package:deriv_dependency_injector/dependency_injector.dart';
/// Mt5 new account response model class.
abstract class Mt5NewAccountResponseModel {
/// Initializes Mt5 new account response model class .
const Mt5NewAccountResponseModel({
this.mt5NewAccount,
});
/// New MT5 account details
final Mt5NewAccount? mt5NewAccount;
}
/// Mt5 new account response class.
class Mt5NewAccountResponse extends Mt5NewAccountResponseModel {
/// Initializes Mt5 new account response class.
const Mt5NewAccountResponse({
super.mt5NewAccount,
});
/// Creates an instance from JSON.
factory Mt5NewAccountResponse.fromJson(
dynamic mt5NewAccountJson,
) =>
Mt5NewAccountResponse(
mt5NewAccount: mt5NewAccountJson == null
? null
: Mt5NewAccount.fromJson(mt5NewAccountJson),
);
/// Converts an instance to JSON.
Map<String, dynamic> toJson() {
final Map<String, dynamic> resultMap = <String, dynamic>{};
if (mt5NewAccount != null) {
resultMap['mt5_new_account'] = mt5NewAccount!.toJson();
}
return resultMap;
}
static final BaseAPI _api = Injector()<BaseAPI>();
/// Creates new MT5 user, either demo or real money user.
///
/// For parameters information refer to [Mt5NewAccountRequest].
/// Throws a [BaseAPIException] if API response contains an error
static Future<Mt5NewAccountResponse> createNewAccount(
Mt5NewAccountRequest request,
) async {
final Mt5NewAccountReceive response = await _api.call(request: request);
checkException(
response: response,
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
BaseAPIException(baseExceptionModel: baseExceptionModel),
);
return Mt5NewAccountResponse.fromJson(response.mt5NewAccount);
}
/// Allows deposit into MT5 account from binary account.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5DepositResponse> deposit({
required double amount,
required String fromBinary,
}) =>
Mt5DepositResponse.deposit(
Mt5DepositRequest(
amount: amount,
fromBinary: fromBinary,
toMt5: mt5NewAccount?.login,
),
);
/// Changes password of the MT5 account.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5PasswordChangeResponse> changePassword({
required String newPassword,
required String oldPassword,
required PasswordType passwordType,
}) =>
Mt5PasswordChangeResponse.changePassword(
Mt5PasswordChangeRequest(
login: mt5NewAccount?.login,
newPassword: newPassword,
oldPassword: oldPassword,
passwordType: getStringFromEnum(passwordType),
),
);
/// Validates the main password for the MT5 user.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5PasswordCheckResponse> checkPassword({
required String password,
required PasswordType passwordType,
}) =>
Mt5PasswordCheckResponse.checkPassword(
Mt5PasswordCheckRequest(
login: mt5NewAccount?.login,
password: password,
passwordType: getStringFromEnum(passwordType),
),
);
/// Resets the password of MT5 account.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5PasswordResetResponse> resetPassword({
required String newPassword,
required PasswordType passwordType,
required String verificationCode,
}) =>
Mt5PasswordResetResponse.resetPassword(
Mt5PasswordResetRequest(
login: mt5NewAccount?.login,
newPassword: newPassword,
passwordType: getStringFromEnum(passwordType),
verificationCode: verificationCode,
),
);
/// Gets the MT5 user account settings.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5GetSettingsResponse> fetchSettings() =>
Mt5GetSettingsResponse.fetchSettings(
Mt5GetSettingsRequest(login: mt5NewAccount?.login));
/// Allows withdrawal from MT5 account to Binary account.
///
/// Throws a [BaseAPIException] if API response contains an error
Future<Mt5WithdrawalResponse> withdraw({
required double amount,
required String toBinary,
}) =>
Mt5WithdrawalResponse.withdraw(
Mt5WithdrawalRequest(
amount: amount,
fromMt5: mt5NewAccount?.login,
toBinary: toBinary,
),
);
/// Creates a copy of instance with given parameters.
Mt5NewAccountResponse copyWith({
Mt5NewAccount? mt5NewAccount,
}) =>
Mt5NewAccountResponse(
mt5NewAccount: mt5NewAccount ?? this.mt5NewAccount,
);
}
/// AccountTypeEnum mapper.
final Map<String, AccountTypeEnum> accountTypeEnumMapper =
<String, AccountTypeEnum>{
"demo": AccountTypeEnum.demo,
"gaming": AccountTypeEnum.gaming,
"financial": AccountTypeEnum.financial,
"all": AccountTypeEnum.all,
};
/// AccountType Enum.
enum AccountTypeEnum {
/// demo.
demo,
/// gaming.
gaming,
/// financial.
financial,
/// all.
all,
}
/// Mt5AccountCategoryEnum mapper.
final Map<String, Mt5AccountCategoryEnum> mt5AccountCategoryEnumMapper =
<String, Mt5AccountCategoryEnum>{
"conventional": Mt5AccountCategoryEnum.conventional,
"swap_free": Mt5AccountCategoryEnum.swapFree,
"gold": Mt5AccountCategoryEnum.gold,
};
/// Mt5AccountCategory Enum.
enum Mt5AccountCategoryEnum {
/// conventional.
conventional,
/// swap_free.
swapFree,
/// gold.
gold,
}
/// Mt5AccountTypeEnum mapper.
final Map<String, Mt5AccountTypeEnum> mt5AccountTypeEnumMapper =
<String, Mt5AccountTypeEnum>{
"financial": Mt5AccountTypeEnum.financial,
"financial_stp": Mt5AccountTypeEnum.financialStp,
"standard": Mt5AccountTypeEnum.standard,
"gold": Mt5AccountTypeEnum.gold,
};
/// Mt5AccountType Enum.
enum Mt5AccountTypeEnum {
/// financial.
financial,
/// financial_stp.
financialStp,
/// standard.
standard,
/// gold.
gold,
}
/// ProductEnum mapper.
final Map<String, ProductEnum> productEnumMapper = <String, ProductEnum>{
"synthetic": ProductEnum.synthetic,
"financial": ProductEnum.financial,
"swap_free": ProductEnum.swapFree,
"zero_spread": ProductEnum.zeroSpread,
"standard": ProductEnum.standard,
"stp": ProductEnum.stp,
"gold": ProductEnum.gold,
};
/// Product Enum.
enum ProductEnum {
/// synthetic.
synthetic,
/// financial.
financial,
/// swap_free.
swapFree,
/// zero_spread.
zeroSpread,
/// standard.
standard,
/// stp.
stp,
/// gold.
gold,
}
/// SubAccountTypeEnum mapper.
final Map<String, SubAccountTypeEnum> subAccountTypeEnumMapper =
<String, SubAccountTypeEnum>{
"standard": SubAccountTypeEnum.standard,
"stp": SubAccountTypeEnum.stp,
"ibt": SubAccountTypeEnum.ibt,
"swap_free": SubAccountTypeEnum.swapFree,
"zero_spread": SubAccountTypeEnum.zeroSpread,
"gold": SubAccountTypeEnum.gold,
};
/// SubAccountType Enum.
enum SubAccountTypeEnum {
/// standard.
standard,
/// stp.
stp,
/// ibt.
ibt,
/// swap_free.
swapFree,
/// zero_spread.
zeroSpread,
/// gold.
gold,
}
/// Mt5 new account model class.
abstract class Mt5NewAccountModel {
/// Initializes Mt5 new account model class .
const Mt5NewAccountModel({
this.accountType,
this.agent,
this.balance,
this.currency,
this.displayBalance,
this.login,
this.mt5AccountCategory,
this.mt5AccountType,
this.product,
this.subAccountType,
});
/// Account type.
final AccountTypeEnum? accountType;
/// Agent Details.
final String? agent;
/// Account balance.
final double? balance;
/// MT5 account currency (`USD` or `EUR`) that depends on the MT5 company (`vanuatu`, `svg`, `malta`).
final String? currency;
/// Account balance, formatted to appropriate decimal places.
final String? displayBalance;
/// Login ID of the user's new MT5 account. Login could have 2 types of prefixes: MTD, MTR. MTD - for demo accounts and MTR for real money accounts.
final String? login;
/// With default value of conventional, unavailable for `financial_stp` sub account type.
final Mt5AccountCategoryEnum? mt5AccountCategory;
/// Sub account type for classic MT5 account.
final Mt5AccountTypeEnum? mt5AccountType;
/// Product name that Deriv offer
final ProductEnum? product;
/// Indicate the different offerings for mt5 account.
final SubAccountTypeEnum? subAccountType;
}
/// Mt5 new account class.
class Mt5NewAccount extends Mt5NewAccountModel {
/// Initializes Mt5 new account class.
const Mt5NewAccount({
super.accountType,
super.agent,
super.balance,
super.currency,
super.displayBalance,
super.login,
super.mt5AccountCategory,
super.mt5AccountType,
super.product,
super.subAccountType,
});
/// Creates an instance from JSON.
factory Mt5NewAccount.fromJson(Map<String, dynamic> json) => Mt5NewAccount(
accountType: json['account_type'] == null
? null
: accountTypeEnumMapper[json['account_type']],
agent: json['agent'],
balance: getDouble(json['balance']),
currency: json['currency'],
displayBalance: json['display_balance'],
login: json['login'],
mt5AccountCategory: json['mt5_account_category'] == null
? null
: mt5AccountCategoryEnumMapper[json['mt5_account_category']],
mt5AccountType: json['mt5_account_type'] == null
? null
: mt5AccountTypeEnumMapper[json['mt5_account_type']],
product:
json['product'] == null ? null : productEnumMapper[json['product']],
subAccountType: json['sub_account_type'] == null
? null
: subAccountTypeEnumMapper[json['sub_account_type']],
);
/// Converts an instance to JSON.
Map<String, dynamic> toJson() {
final Map<String, dynamic> resultMap = <String, dynamic>{};
resultMap['account_type'] = accountTypeEnumMapper.entries
.firstWhere((MapEntry<String, AccountTypeEnum> entry) =>
entry.value == accountType)
.key;
resultMap['agent'] = agent;
resultMap['balance'] = balance;
resultMap['currency'] = currency;
resultMap['display_balance'] = displayBalance;
resultMap['login'] = login;
resultMap['mt5_account_category'] = mt5AccountCategoryEnumMapper.entries
.firstWhere((MapEntry<String, Mt5AccountCategoryEnum> entry) =>
entry.value == mt5AccountCategory)
.key;
resultMap['mt5_account_type'] = mt5AccountTypeEnumMapper.entries
.firstWhere((MapEntry<String, Mt5AccountTypeEnum> entry) =>
entry.value == mt5AccountType)
.key;
resultMap['product'] = productEnumMapper.entries
.firstWhere(
(MapEntry<String, ProductEnum> entry) => entry.value == product)
.key;
resultMap['sub_account_type'] = subAccountTypeEnumMapper.entries
.firstWhere((MapEntry<String, SubAccountTypeEnum> entry) =>
entry.value == subAccountType)
.key;
return resultMap;
}
/// Creates a copy of instance with given parameters.
Mt5NewAccount copyWith({
AccountTypeEnum? accountType,
String? agent,
double? balance,
String? currency,
String? displayBalance,
String? login,
Mt5AccountCategoryEnum? mt5AccountCategory,
Mt5AccountTypeEnum? mt5AccountType,
ProductEnum? product,
SubAccountTypeEnum? subAccountType,
}) =>
Mt5NewAccount(
accountType: accountType ?? this.accountType,
agent: agent ?? this.agent,
balance: balance ?? this.balance,
currency: currency ?? this.currency,
displayBalance: displayBalance ?? this.displayBalance,
login: login ?? this.login,
mt5AccountCategory: mt5AccountCategory ?? this.mt5AccountCategory,
mt5AccountType: mt5AccountType ?? this.mt5AccountType,
product: product ?? this.product,
subAccountType: subAccountType ?? this.subAccountType,
);
}