Skip to content

Commit 639b7e8

Browse files
committed
Gateways
1 parent 2a33e86 commit 639b7e8

44 files changed

Lines changed: 1505 additions & 844 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Core/Core/Conventions/Gateway.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface ITradeObserver : IGrainObserver
2929
/// Price message
3030
/// </summary>
3131
/// <param name="instrument"></param>
32-
Task StreamTrade(Instrument instrument);
32+
Task StreamInstrument(Instrument instrument);
3333
}
3434

3535
public interface IGateway
@@ -52,7 +52,7 @@ public interface IGateway
5252
/// <summary>
5353
/// Trade message
5454
/// </summary>
55-
Func<Instrument, Task> OnTrade { get; set; }
55+
Func<Instrument, Task> OnInstrument { get; set; }
5656

5757
/// <summary>
5858
/// Connect
@@ -171,7 +171,7 @@ public abstract class Gateway : IGateway, ITradeObserver
171171
/// <summary>
172172
/// Trade message
173173
/// </summary>
174-
public virtual Func<Instrument, Task> OnTrade { get; set; } = o => Task.CompletedTask;
174+
public virtual Func<Instrument, Task> OnInstrument { get; set; } = o => Task.CompletedTask;
175175

176176
/// <summary>
177177
/// Order message
@@ -189,7 +189,7 @@ public abstract class Gateway : IGateway, ITradeObserver
189189
/// Price message
190190
/// </summary>
191191
/// <param name="instrument"></param>
192-
public virtual Task StreamTrade(Instrument instrument) => OnTrade(instrument);
192+
public virtual Task StreamInstrument(Instrument instrument) => OnInstrument(instrument);
193193

194194
/// <summary>
195195
/// Connect

Core/Core/Enums/OrderTypeEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public enum OrderTypeEnum : byte
77
Limit,
88
Market,
99
StopLimit,
10+
LimitMaker,
1011
}
1112
}

Core/Core/Grains/OrderGrain.cs

Lines changed: 0 additions & 114 deletions
This file was deleted.

Core/Core/Grains/OrdersGrain.cs

Lines changed: 33 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Core.Enums;
2-
using Core.Extensions;
32
using Core.Models;
43
using Core.Validators;
54
using Orleans;
6-
using System;
75
using System.Collections.Generic;
8-
using System.Linq;
6+
using System.Threading;
97
using System.Threading.Tasks;
108

119
namespace Core.Grains
@@ -19,139 +17,81 @@ public interface IOrdersGrain : IGrainWithStringKey
1917
Task<OrdersResponse> Orders(Criteria criteria);
2018

2119
/// <summary>
22-
/// Store order
20+
/// Store orders
2321
/// </summary>
24-
/// <param name="order"></param>
25-
Task<OrderResponse> Store(Order order);
22+
/// <param name="orders"></param>
23+
Task<StatusResponse> Store(Dictionary<string, Order> orders);
2624

2725
/// <summary>
28-
/// Send order
26+
/// Store order
2927
/// </summary>
3028
/// <param name="order"></param>
31-
Task<OrderResponse> Send(Order order);
32-
33-
/// <summary>
34-
/// Update order data
35-
/// </summary>
36-
/// <param name="instrument"></param>
37-
Task<StatusResponse> Tap(Instrument instrument);
29+
Task<OrderResponse> Store(Order order);
3830

3931
/// <summary>
4032
/// Remove order from the list
4133
/// </summary>
4234
/// <param name="order"></param>
4335
Task<DescriptorResponse> Clear(Order order);
44-
45-
/// <summary>
46-
/// Clear orders
47-
/// </summary>
48-
Task<StatusResponse> Clear();
4936
}
5037

51-
public class OrdersGrain : Grain<Orders>, IOrdersGrain
38+
public class OrdersGrain : Grain<Dictionary<string, Order>>, IOrdersGrain
5239
{
5340
/// <summary>
5441
/// Order validator
5542
/// </summary>
5643
protected OrderValidator orderValidator = new();
5744

5845
/// <summary>
59-
/// Get orders
46+
/// Activation
6047
/// </summary>
61-
/// <param name="criteria"></param>
62-
public virtual async Task<OrdersResponse> Orders(Criteria criteria)
48+
/// <param name="cancellation"></param>
49+
public override async Task OnActivateAsync(CancellationToken cancellation)
6350
{
64-
var items = await Task.WhenAll(State
65-
.Grains
66-
.Values
67-
.Select(o => o.Order()));
68-
69-
return new()
70-
{
71-
Data = items
72-
};
51+
State = [];
52+
await base.OnActivateAsync(cancellation);
7353
}
7454

7555
/// <summary>
76-
/// Store order
56+
/// Get orders
7757
/// </summary>
78-
/// <param name="order"></param>
79-
public virtual async Task<OrderResponse> Store(Order order)
58+
/// <param name="criteria"></param>
59+
public virtual async Task<OrdersResponse> Orders(Criteria criteria) => new()
8060
{
81-
var descriptor = this.GetDescriptor(order.Id);
82-
var grain = GrainFactory.GetGrain<IOrderGrain>(descriptor);
83-
84-
await grain.Store(order with
85-
{
86-
Operation = order.Operation with
87-
{
88-
Status = OrderStatusEnum.Order
89-
}
90-
});
91-
92-
State.Grains[order.Id] = grain;
93-
94-
return new()
95-
{
96-
Data = order
97-
};
98-
}
61+
Data = [.. State.Values]
62+
};
9963

10064
/// <summary>
101-
/// Send order
65+
/// Store positions
10266
/// </summary>
103-
/// <param name="order"></param>
104-
public virtual async Task<OrderResponse> Send(Order order)
67+
/// <param name="orders"></param>
68+
public virtual Task<StatusResponse> Store(Dictionary<string, Order> orders)
10569
{
106-
var response = new OrderResponse
107-
{
108-
Errors = [.. Errors(order).Select(error => error.Message).Distinct()]
109-
};
70+
State = orders;
11071

111-
if (response.Errors.Count is 0)
72+
return Task.FromResult(new StatusResponse()
11273
{
113-
var orders = order
114-
.Orders
115-
.Where(o => o.Instruction is null)
116-
.ToList();
117-
118-
if (order.Amount is not null || order.Orders.Count is 0)
119-
{
120-
orders.Add(order);
121-
}
122-
123-
foreach (var o in orders)
124-
{
125-
await Store(o with { Orders = [.. o.Orders.Where(v => v.Instruction is InstructionEnum.Brace)] });
126-
}
127-
}
128-
129-
return response;
74+
Data = StatusEnum.Active
75+
});
13076
}
13177

13278
/// <summary>
133-
/// Update order data
79+
/// Store order
13480
/// </summary>
135-
/// <param name="instrument"></param>
136-
public virtual async Task<StatusResponse> Tap(Instrument instrument)
81+
/// <param name="order"></param>
82+
public virtual async Task<OrderResponse> Store(Order order)
13783
{
138-
var descriptor = this.GetDescriptor();
139-
var positionsGrain = GrainFactory.GetGrain<IPositionsGrain>(descriptor);
140-
141-
foreach (var grain in State.Grains)
84+
State[order.Id] = order with
14285
{
143-
var response = await grain.Value.Tap(instrument);
144-
145-
if (response.Data is not null)
86+
Operation = order.Operation with
14687
{
147-
State.Grains.Remove(grain.Key);
148-
await positionsGrain.Send(response.Data);
88+
Status = OrderStatusEnum.Order
14989
}
150-
}
90+
};
15191

15292
return new()
15393
{
154-
Data = StatusEnum.Active
94+
Data = order
15595
};
15696
}
15797

@@ -161,47 +101,12 @@ public virtual async Task<StatusResponse> Tap(Instrument instrument)
161101
/// <param name="order"></param>
162102
public virtual Task<DescriptorResponse> Clear(Order order)
163103
{
164-
State.Grains.Remove(order.Id);
104+
State.Remove(order.Id);
165105

166106
return Task.FromResult(new DescriptorResponse
167107
{
168108
Data = order.Id
169109
});
170110
}
171-
172-
/// <summary>
173-
/// Clear orders
174-
/// </summary>
175-
public virtual Task<StatusResponse> Clear()
176-
{
177-
State.Grains.Clear();
178-
179-
return Task.FromResult(new StatusResponse
180-
{
181-
Data = StatusEnum.Inactive
182-
});
183-
}
184-
185-
/// <summary>
186-
/// Preprocess order
187-
/// </summary>
188-
/// <param name="order"></param>
189-
protected virtual List<Error> Errors(Order order)
190-
{
191-
var response = new List<Error>();
192-
var orders = order.Orders.Append(order);
193-
194-
foreach (var subOrder in orders)
195-
{
196-
var errors = orderValidator
197-
.Validate(subOrder)
198-
.Errors
199-
.Select(error => new Error { Message = error.ErrorMessage });
200-
201-
response.AddRange(errors);
202-
}
203-
204-
return response;
205-
}
206111
}
207112
}

0 commit comments

Comments
 (0)