This repository was archived by the owner on Nov 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMTGONXController.m
More file actions
208 lines (158 loc) · 6.66 KB
/
MTGONXController.m
File metadata and controls
208 lines (158 loc) · 6.66 KB
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
//
// MTGONXController.m
// mtgonxClient
//
// Created by Felix Gläske on 6/11/11.
// Copyright 2011 PsyCoding. All rights reserved.
//
#import "MTGONXController.h"
#import "SBJson.h"
@implementation MTGONXController
@synthesize delegate;
-(void) startGettingBalanceWithUsername:(NSString *)username andPassword:(NSString *)password
{
NSMutableDictionary *userData = [[NSMutableDictionary alloc] init];
[userData setObject:username forKey:@"username"];
[userData setObject:password forKey:@"password"];
[self performSelectorInBackground:@selector(getBalance:) withObject:userData];
}
-(void)startGettingPrices
{
[self performSelectorInBackground:@selector(getPrices) withObject:nil];
}
-(void)startGettingOpenOrders:(NSString *)username andPassword:(NSString *)password
{
NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
[userData setObject:username forKey:@"username"];
[userData setObject:password forKey:@"password"];
[self performSelectorInBackground:@selector(getOpenOrders:) withObject:userData];
}
-(NSDictionary*)sendRequest:(NSString *)url_ withBodyString:(NSString *)bodyString
{
NSURL *url = [ NSURL URLWithString: url_];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
if (bodyString != nil) {
NSString *type = [NSString stringWithString:@"application/x-www-form-urlencoded"];
NSData *body = [NSData dataWithData:[bodyString dataUsingEncoding:2]];
[request addValue:type forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];
}
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *data = [[NSString alloc] initWithData:response
encoding:NSUTF8StringEncoding];
return [data JSONValue];
}
-(void)getOpenOrders:(NSMutableDictionary*)userData
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *url = @"https://mtgox.com/code/getOrders.php";
NSString *bodyString = [NSString stringWithFormat:@"name=%@&pass=%@", [userData objectForKey:@"username"], [userData objectForKey:@"password"]];
NSDictionary* result = [self sendRequest:url withBodyString:bodyString];
if (result == nil) {
NSLog(@"Error while getting OpenOrders");
return;
}
NSArray *orders = [result objectForKey:@"orders"];
if(delegate && [delegate respondsToSelector:@selector(gonxController:ReceivedOpenOrders:)]) {
[delegate gonxController:self ReceivedOpenOrders: orders];
}
[pool drain];
}
-(void)getPrices
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
//getting the actual sell and bid values
NSString *url = @"https://mtgox.com/code/data/ticker.php";
NSDictionary* result = [self sendRequest:url withBodyString:nil];
if (result == nil) {
NSLog(@"Stopped getting Prices");
if(delegate && [delegate respondsToSelector:@selector(gonxControllerStoppedGettingPrices:)])
{
[delegate gonxControllerStoppedGettingPrices:self];
}
return;
}
result = [result objectForKey:@"ticker"];
if (delegate && [delegate respondsToSelector:@selector(gonxController:ReceivedPrices:)]) {
[delegate gonxController:self ReceivedPrices:result];
}
[pool drain];
}
-(void)getBalance:(NSMutableDictionary *)userData
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *url = @"https://mtgox.com/code/getFunds.php";
NSString *bodyString = [NSString stringWithFormat:@"name=%@&pass=%@", [userData objectForKey:@"username"], [userData objectForKey:@"password"]];
NSDictionary* result = [self sendRequest:url withBodyString:bodyString];
if (result == nil) {
NSLog(@"Error while getting Balance");
return;
}
NSLog(@"%@", result);
//check if logn was correct
NSString *error = [result objectForKey:@"error"];
if (error != nil) {
NSLog(@"Login was incorrect!");
if(delegate && [delegate respondsToSelector:@selector(gonxControllerCouldNotLogin:)]) {
[delegate gonxControllerCouldNotLogin:self];
}
}
else {
if(delegate && [delegate respondsToSelector:@selector(gonxController:ReceivedBalance:)]) {
[delegate gonxController:self ReceivedBalance: result];
}
}
[pool drain];
}
-(void)startPlacingOrderWithUsername:(NSString *)username andPassword:(NSString *)password andAmount:(NSString *)amount andType:(NSString *)type andPrice:(NSString*)price
{
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:username forKey:@"username"];
[data setObject:password forKey:@"password"];
[data setObject:amount forKey:@"amount"];
[data setObject:type forKey:@"type"];
[data setObject:price forKey:@"price"];
[self performSelectorInBackground:@selector(placeOrder:) withObject:data];
}
-(void)placeOrder:(NSDictionary *)data
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *url = [NSString stringWithFormat:@"https://mtgox.com/code/%@BTC.php", [data objectForKey:@"type"]];
NSString *bodyString = [NSString stringWithFormat:@"name=%@&pass=%@&amount=%@&price=%@", [data objectForKey:@"username"], [data objectForKey:@"password"], [data objectForKey:@"amount"], [data objectForKey:@"price"]];
NSDictionary* result = [self sendRequest:url withBodyString:bodyString];
if (result == nil) {
NSLog(@"Error while Placing Order");
return;
}
if(delegate && [delegate respondsToSelector:@selector(gonxControllerPlacedAnOrder:)])
{
[delegate gonxControllerPlacedAnOrder:self];
}
[pool drain];
}
-(void)startCancelingOrderWithUsername:(NSString *)username
andPassword:(NSString *)password
andOrderId:(NSNumber *)orderId
andOrderType:(NSNumber *)orderType
{
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:username forKey:@"username"];
[data setObject:password forKey:@"password"];
[data setObject:orderId forKey:@"orderId"];
[data setObject:orderType forKey:@"orderType"];
[self performSelectorInBackground:@selector(cancelOrder:) withObject:data];
}
-(void)cancelOrder:(NSMutableDictionary *)userData
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *url = [NSString stringWithString:@"https://mtgox.com/code/cancelOrder.php"];
NSString *bodyString = [NSString stringWithFormat:@"name=%@&pass=%@&oid=%@&type=%@", [userData objectForKey:@"username"], [userData objectForKey:@"password"], [userData objectForKey:@"orderId"], [userData objectForKey:@"orderType"]];
[self sendRequest:url withBodyString:bodyString];
if(delegate && [delegate respondsToSelector:@selector(gonxControllerCanceledOrder:)])
{
[delegate gonxControllerCanceledOrder:self];
}
[pool drain];
}
@end