-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat (webapp): show open position #1871
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c37289d
chore: ingore data dir from webapp
bonomat 101cfbf
feat: allow creating new order through webapp
bonomat 13fb60b
chore: disable post new order button while submitting
bonomat a6f4cb6
chore: don't rethrow errors with added text
bonomat dd57e3e
chore: introduce custom http client to overwrite url
bonomat 8dc0895
chore: extract new order form into own widget
bonomat 80ca109
feat: show open positions
bonomat b449bb2
chore: make trade screen responsive and redesign a bit
bonomat 342468a
fix: use 00 format instead of 24
bonomat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'dart:convert'; | ||
import 'package:http/http.dart'; | ||
|
||
class HttpClientManager { | ||
static final CustomHttpClient _httpClient = CustomHttpClient(Client(), true); | ||
|
||
static CustomHttpClient get instance => _httpClient; | ||
} | ||
|
||
class CustomHttpClient extends BaseClient { | ||
// TODO: this should come from the settings | ||
|
||
// if this is true, we assume the website is running in dev mode and need to add _host:_port to be able to do http calls | ||
final bool _dev; | ||
|
||
final String _port = "3001"; | ||
final String _host = "localhost"; | ||
|
||
final Client _inner; | ||
|
||
CustomHttpClient(this._inner, this._dev); | ||
|
||
Future<StreamedResponse> send(BaseRequest request) { | ||
return _inner.send(request); | ||
} | ||
|
||
@override | ||
Future<Response> delete(Uri url, | ||
{Map<String, String>? headers, Object? body, Encoding? encoding}) { | ||
if (_dev && url.host == '') { | ||
url = Uri.parse('http://$_host:$_port${url.toString()}'); | ||
} | ||
return _inner.delete(url, headers: headers, body: body, encoding: encoding); | ||
} | ||
|
||
@override | ||
Future<Response> put(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) { | ||
if (_dev && url.host == '') { | ||
url = Uri.parse('http://$_host:$_port${url.toString()}'); | ||
} | ||
return _inner.put(url, headers: headers, body: body, encoding: encoding); | ||
} | ||
|
||
@override | ||
Future<Response> post(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) { | ||
if (_dev && url.host == '') { | ||
url = Uri.parse('http://$_host:$_port${url.toString()}'); | ||
} | ||
return _inner.post(url, headers: headers, body: body, encoding: encoding); | ||
} | ||
|
||
@override | ||
Future<Response> get(Uri url, {Map<String, String>? headers}) { | ||
if (_dev && url.host == '') { | ||
url = Uri.parse('http://$_host:$_port${url.toString()}'); | ||
} | ||
return _inner.get(url, headers: headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:get_10101/common/http_client.dart'; | ||
import 'package:get_10101/common/model.dart'; | ||
|
||
class OrderId { | ||
final String orderId; | ||
|
||
const OrderId({required this.orderId}); | ||
|
||
factory OrderId.fromJson(Map<String, dynamic> json) { | ||
return switch (json) { | ||
{ | ||
'id': String orderId, | ||
} => | ||
OrderId(orderId: orderId), | ||
_ => throw const FormatException('Failed to parse order id.'), | ||
}; | ||
} | ||
} | ||
|
||
class NewOrderService { | ||
const NewOrderService(); | ||
|
||
static Future<String> postNewOrder(Leverage leverage, Usd quantity, bool isLong) async { | ||
final response = await HttpClientManager.instance.post(Uri(path: '/api/orders'), | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
}, | ||
body: jsonEncode(<String, dynamic>{ | ||
'leverage': leverage.asDouble, | ||
'quantity': quantity.asDouble, | ||
'direction': isLong ? "Long" : "Short", | ||
})); | ||
|
||
if (response.statusCode == 200) { | ||
return OrderId.fromJson(jsonDecode(response.body) as Map<String, dynamic>).orderId; | ||
} else { | ||
throw FlutterError("Failed to post new order. Response ${response.body}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'dart:convert'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:get_10101/common/http_client.dart'; | ||
import 'package:get_10101/common/model.dart'; | ||
|
||
class OpenPositionsService { | ||
const OpenPositionsService(); | ||
|
||
static Future<List<Position>> fetchOpenPositions() async { | ||
final response = await HttpClientManager.instance.get(Uri(path: '/api/positions')); | ||
|
||
if (response.statusCode == 200) { | ||
final List<dynamic> jsonData = jsonDecode(response.body); | ||
return jsonData.map((positionData) => Position.fromJson(positionData)).toList(); | ||
} else { | ||
throw FlutterError("Could not fetch positions"); | ||
} | ||
} | ||
} | ||
|
||
class Position { | ||
final Leverage leverage; | ||
final Usd quantity; | ||
final String contractSymbol; | ||
final String direction; | ||
final Usd averageEntryPrice; | ||
final Usd liquidationPrice; | ||
final String positionState; | ||
final Amount collateral; | ||
final DateTime expiry; | ||
final DateTime updated; | ||
final DateTime created; | ||
|
||
Position({ | ||
required this.leverage, | ||
required this.quantity, | ||
required this.contractSymbol, | ||
required this.direction, | ||
required this.averageEntryPrice, | ||
required this.liquidationPrice, | ||
required this.positionState, | ||
required this.collateral, | ||
required this.expiry, | ||
required this.updated, | ||
required this.created, | ||
}); | ||
|
||
factory Position.fromJson(Map<String, dynamic> json) { | ||
return Position( | ||
leverage: Leverage(json['leverage'] as double), | ||
quantity: Usd(json['quantity'] as double), | ||
contractSymbol: json['contract_symbol'] as String, | ||
direction: json['direction'] as String, | ||
averageEntryPrice: Usd(json['average_entry_price'] as double), | ||
liquidationPrice: Usd(json['liquidation_price'] as double), | ||
positionState: json['position_state'] as String, | ||
collateral: Amount(json['collateral']), | ||
expiry: DateTime.parse(json['expiry'] as String), | ||
updated: DateTime.parse(json['updated'] as String), | ||
created: DateTime.parse(json['created'] as String), | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were meaning to share the
data
directory with the root of the project. This is not wrong though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, but if you run
cargo run
from the sub folder it will generate this which is not wrong but annoying to have :)