Skip to content

Commit d0c4cf8

Browse files
committed
Added support for 'ticker' and 'trollbox' push API calls
1 parent 8a670b8 commit d0c4cf8

23 files changed

+523
-39
lines changed

Diff for: PoloniexApi.Net/General/ApiWebClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public T PostData<T>(string command, Dictionary<string, object> postData)
5050
postData.Add("command", command);
5151
postData.Add("nonce", Helper.GetCurrentHttpPostNonce());
5252

53-
var jsonString = PostString(Helper.ApiUrlRelativeTrading, postData.ToHttpPostString());
53+
var jsonString = PostString(Helper.ApiUrlHttpsRelativeTrading, postData.ToHttpPostString());
5454
var output = JsonSerializer.DeserializeObject<T>(jsonString);
5555

5656
return output;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Jojatekok.PoloniexAPI.MarketTools;
2+
using System;
3+
4+
namespace Jojatekok.PoloniexAPI
5+
{
6+
public class TickerChangedEventArgs : EventArgs
7+
{
8+
public CurrencyPair CurrencyPair { get; private set; }
9+
public MarketData MarketData { get; private set; }
10+
11+
internal TickerChangedEventArgs(CurrencyPair currencyPair, MarketData marketData)
12+
{
13+
CurrencyPair = currencyPair;
14+
MarketData = marketData;
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace Jojatekok.PoloniexAPI
4+
{
5+
public class TrollboxMessageEventArgs : EventArgs
6+
{
7+
public string SenderName { get; private set; }
8+
public uint SenderReputation { get; private set; }
9+
public string MessageType { get; private set; }
10+
public ulong MessageNumber { get; private set; }
11+
public string MessageText { get; private set; }
12+
13+
internal TrollboxMessageEventArgs(string senderName, uint senderReputation, string messageType, ulong messageNumber, string messageText)
14+
{
15+
SenderName = senderName;
16+
SenderReputation = senderReputation;
17+
MessageType = messageType;
18+
MessageNumber = messageNumber;
19+
MessageText = messageText;
20+
}
21+
}
22+
}

Diff for: PoloniexApi.Net/General/ObservableDictionary.cs

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.ComponentModel;
5+
using System.Collections.Generic;
6+
using System.Collections.Specialized;
7+
8+
namespace Jojatekok.PoloniexAPI
9+
{
10+
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged
11+
{
12+
private const string CountString = "Count";
13+
private const string IndexerName = "Item[]";
14+
private const string KeysName = "Keys";
15+
private const string ValuesName = "Values";
16+
17+
private IDictionary<TKey, TValue> Dictionary { get; set; }
18+
19+
#region Constructors
20+
21+
public ObservableDictionary()
22+
{
23+
Dictionary = new Dictionary<TKey, TValue>();
24+
}
25+
public ObservableDictionary(IDictionary<TKey, TValue> dictionary)
26+
{
27+
Dictionary = new Dictionary<TKey, TValue>(dictionary);
28+
}
29+
public ObservableDictionary(IEqualityComparer<TKey> comparer)
30+
{
31+
Dictionary = new Dictionary<TKey, TValue>(comparer);
32+
}
33+
public ObservableDictionary(int capacity)
34+
{
35+
Dictionary = new Dictionary<TKey, TValue>(capacity);
36+
}
37+
public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
38+
{
39+
Dictionary = new Dictionary<TKey, TValue>(dictionary, comparer);
40+
}
41+
public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer)
42+
{
43+
Dictionary = new Dictionary<TKey, TValue>(capacity, comparer);
44+
}
45+
46+
#endregion
47+
48+
#region IDictionary<TKey, TValue> Members
49+
50+
public void Add(TKey key, TValue value)
51+
{
52+
Insert(key, value, true);
53+
}
54+
55+
public bool ContainsKey(TKey key)
56+
{
57+
return Dictionary.ContainsKey(key);
58+
}
59+
60+
public ICollection<TKey> Keys
61+
{
62+
get { return Dictionary.Keys; }
63+
}
64+
65+
public bool Remove(TKey key)
66+
{
67+
if (key == null) throw new ArgumentNullException("key");
68+
69+
TValue value;
70+
Dictionary.TryGetValue(key, out value);
71+
var isRemoved = Dictionary.Remove(key);
72+
if (isRemoved) OnCollectionChanged();
73+
74+
return isRemoved;
75+
}
76+
77+
public bool TryGetValue(TKey key, out TValue value)
78+
{
79+
return Dictionary.TryGetValue(key, out value);
80+
}
81+
82+
public ICollection<TValue> Values
83+
{
84+
get { return Dictionary.Values; }
85+
}
86+
87+
public TValue this[TKey key]
88+
{
89+
get { return Dictionary[key]; }
90+
set { Insert(key, value, false); }
91+
}
92+
93+
#endregion
94+
95+
#region ICollection<KeyValuePair<TKey, TValue>> Members
96+
97+
public void Add(KeyValuePair<TKey, TValue> item)
98+
{
99+
Insert(item.Key, item.Value, true);
100+
}
101+
102+
public void Clear()
103+
{
104+
if (Dictionary.Count != 0) {
105+
Dictionary.Clear();
106+
OnCollectionChanged();
107+
}
108+
}
109+
110+
public bool Contains(KeyValuePair<TKey, TValue> item)
111+
{
112+
return Dictionary.Contains(item);
113+
}
114+
115+
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
116+
{
117+
Dictionary.CopyTo(array, arrayIndex);
118+
}
119+
120+
public int Count
121+
{
122+
get { return Dictionary.Count; }
123+
}
124+
125+
public bool IsReadOnly
126+
{
127+
get { return Dictionary.IsReadOnly; }
128+
}
129+
130+
public bool Remove(KeyValuePair<TKey, TValue> item)
131+
{
132+
return Remove(item.Key);
133+
}
134+
135+
#endregion
136+
137+
#region IEnumerable<KeyValuePair<TKey, TValue>> Members
138+
139+
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
140+
{
141+
return Dictionary.GetEnumerator();
142+
}
143+
144+
#endregion
145+
146+
#region IEnumerable Members
147+
148+
IEnumerator IEnumerable.GetEnumerator()
149+
{
150+
return ((IEnumerable)Dictionary).GetEnumerator();
151+
}
152+
153+
#endregion
154+
155+
#region INotifyCollectionChanged Members
156+
157+
public event NotifyCollectionChangedEventHandler CollectionChanged;
158+
159+
#endregion
160+
161+
#region INotifyPropertyChanged Members
162+
163+
public event PropertyChangedEventHandler PropertyChanged;
164+
165+
#endregion
166+
167+
public void AddRange(IDictionary<TKey, TValue> items)
168+
{
169+
if (items == null) throw new ArgumentNullException("items");
170+
171+
if (items.Count != 0) {
172+
if (Dictionary.Count != 0) {
173+
if (items.Keys.Any(x => Dictionary.ContainsKey(x))) {
174+
throw new ArgumentException("An item with the same key has already been added.");
175+
}
176+
177+
foreach (var item in items) {
178+
Dictionary.Add(item);
179+
}
180+
181+
} else {
182+
Dictionary = new Dictionary<TKey, TValue>(items);
183+
}
184+
185+
OnCollectionChanged(NotifyCollectionChangedAction.Add, items.ToArray());
186+
}
187+
}
188+
189+
private void Insert(TKey key, TValue value, bool add)
190+
{
191+
if (key == null) throw new ArgumentNullException("key");
192+
193+
TValue item;
194+
if (Dictionary.TryGetValue(key, out item)) {
195+
if (add) throw new ArgumentException("An item with the same key has already been added.");
196+
if (Equals(item, value)) return;
197+
Dictionary[key] = value;
198+
199+
OnCollectionChanged(NotifyCollectionChangedAction.Replace, new KeyValuePair<TKey, TValue>(key, value), new KeyValuePair<TKey, TValue>(key, item));
200+
201+
} else {
202+
Dictionary[key] = value;
203+
204+
OnCollectionChanged(NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TValue>(key, value));
205+
}
206+
}
207+
208+
private void OnPropertyChanged()
209+
{
210+
OnPropertyChanged(CountString);
211+
OnPropertyChanged(IndexerName);
212+
OnPropertyChanged(KeysName);
213+
OnPropertyChanged(ValuesName);
214+
}
215+
216+
protected virtual void OnPropertyChanged(string propertyName)
217+
{
218+
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
219+
}
220+
221+
private void OnCollectionChanged()
222+
{
223+
OnPropertyChanged();
224+
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
225+
}
226+
227+
private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> changedItem)
228+
{
229+
OnPropertyChanged();
230+
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, changedItem));
231+
}
232+
233+
private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair<TKey, TValue> newItem, KeyValuePair<TKey, TValue> oldItem)
234+
{
235+
OnPropertyChanged();
236+
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem));
237+
}
238+
239+
private void OnCollectionChanged(NotifyCollectionChangedAction action, IList newItems)
240+
{
241+
OnPropertyChanged();
242+
if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItems));
243+
}
244+
}
245+
}

Diff for: PoloniexApi.Net/Helper.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace Jojatekok.PoloniexAPI
1414
{
1515
public static class Helper
1616
{
17-
internal const string ApiUrlBase = "https://poloniex.com/";
18-
internal const string ApiUrlRelativePublic = "public?command=";
19-
internal const string ApiUrlRelativeTrading = "tradingApi";
17+
internal const string ApiUrlHttpsBase = "https://poloniex.com/";
18+
internal const string ApiUrlHttpsRelativePublic = "public?command=";
19+
internal const string ApiUrlHttpsRelativeTrading = "tradingApi";
20+
21+
internal const string ApiUrlWssBase = "wss://api.poloniex.com";
2022

2123
private const int DoubleRoundingPrecisionDigits = 8;
2224

Diff for: PoloniexApi.Net/LiveTools/Live.Interface.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Jojatekok.PoloniexAPI
5+
{
6+
public interface ILive
7+
{
8+
/// <summary>Occurs when a currency pair's market data changes.</summary>
9+
event EventHandler<TickerChangedEventArgs> OnTickerChanged;
10+
/// <summary>Occurs when someone sends a message on the trollbox.</summary>
11+
event EventHandler<TrollboxMessageEventArgs> OnTrollboxMessage;
12+
13+
/// <summary>Starts the process of receiving price ticker messages.</summary>
14+
Task SubscribeToTickerAsync();
15+
/// <summary>Starts the process of receiving trollbox messages.</summary>
16+
Task SubscribeToTrollboxAsync();
17+
}
18+
}

0 commit comments

Comments
 (0)