Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Security
- Imports System.Collections.Specialized
- Imports AutoMtGox.Sitesource
- Imports AutoMtGox.Sitesource.Log
- Imports AutoMtGox.Sitesource.Network
- Imports AutoMtGox.Sitesource.Currency
- Namespace MtGox
- ''' <summary>
- ''' MtGox API.
- ''' </summary>
- ''' <remarks></remarks>
- Public Class API
- Private _name As String
- Private _pass As SecureString = Nothing
- Private _request As Request
- Private _logger As SimpleLogger
- ''' <summary>
- ''' Create a new instance.
- ''' </summary>
- ''' <param name="logger">Logger.</param>
- ''' <param name="name">MtGox username.</param>
- ''' <param name="pass">MtGox password.</param>
- ''' <remarks></remarks>
- Public Sub New(ByRef logger As SimpleLogger, ByVal [name] As String, ByVal pass As SecureString)
- _request = New Request(logger)
- _name = [name]
- _pass = pass
- _logger = logger
- End Sub
- ''' <summary>
- ''' Set login
- ''' </summary>
- ''' <param name="name"></param>
- ''' <param name="pass"></param>
- ''' <remarks></remarks>
- Public Sub SetLogin(ByVal name As String, ByVal pass As SecureString)
- _name = name
- _pass = pass
- End Sub
- ''' <summary>
- ''' Get current balance.
- ''' </summary>
- ''' <remarks></remarks>
- Public Function GetBalance() As String
- If String.IsNullOrEmpty(_name) Or Encryption.Util.IsNullOrEmpty(_pass) Then Return Nothing
- Dim data As New NameValueCollection()
- data.Add("name", _name)
- data.Add("pass", Encryption.Util.ToInsecureString(_pass))
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlCancelOrder, data)
- Catch ex As Exception
- _logger.WriteEntry(Log.LogEntryType.Error, "SellBTC() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Buy BTC.
- ''' </summary>
- ''' <param name="amount">Amount of BTC you want to buy.</param>
- ''' <param name="price">The price for one BTC you want to buy for.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function BuyBTC(ByVal amount As BTC, ByVal price As USD) As String
- If String.IsNullOrEmpty(_name) Or Encryption.Util.IsNullOrEmpty(_pass) Then Return Nothing
- Dim data As New NameValueCollection()
- data.Add("name", _name)
- data.Add("pass", Encryption.Util.ToInsecureString(_pass))
- data.Add("amount", amount.InvariantString)
- data.Add("price", price.InvariantString)
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlBuyBTC, data)
- Catch ex As Exception
- _logger.WriteEntry(Log.LogEntryType.Error, "SellBTC() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Sell BTC
- ''' </summary>
- ''' <param name="amount">Amount of BTC you want to sell.</param>
- ''' <param name="price">The price for one BTC you want to sell for.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function SellBTC(ByVal amount As BTC, ByVal price As USD) As String
- If String.IsNullOrEmpty(_name) Or Encryption.Util.IsNullOrEmpty(_pass) Then Return Nothing
- Dim data As New NameValueCollection()
- data.Add("name", _name)
- data.Add("pass", Encryption.Util.ToInsecureString(_pass))
- data.Add("amount", amount.InvariantString)
- data.Add("price", price.InvariantString)
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlSellBTC, data)
- Catch ex As Exception
- _logger.WriteEntry(Log.LogEntryType.Error, "SellBTC() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Cancel order.
- ''' </summary>
- ''' <param name="oid">Order id you want to cancel.</param>
- ''' <param name="type">The order type. Buy or sell.</param>
- ''' <remarks></remarks>
- Public Function CancelOrder(ByVal oid As Guid, ByVal type As OrderType) As String
- If String.IsNullOrEmpty(_name) Or Encryption.Util.IsNullOrEmpty(_pass) Then Return Nothing
- Dim data As New NameValueCollection()
- data.Add("name", _name)
- data.Add("pass", Encryption.Util.ToInsecureString(_pass))
- data.Add("oid", oid.ToString())
- data.Add("type", CInt(type))
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlCancelOrder, data)
- Catch ex As Exception
- _logger.WriteEntry(Log.LogEntryType.Error, "CancelOrder() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Get all orders.
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function GetOrders() As String
- If String.IsNullOrEmpty(_name) Or Encryption.Util.IsNullOrEmpty(_pass) Then Return Nothing
- Dim data As New NameValueCollection()
- data.Add("name", _name)
- data.Add("pass", Encryption.Util.ToInsecureString(_pass))
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlGetOrders, data)
- Catch ex As Exception
- _logger.WriteEntry(EventLogEntryType.Error, "GetOrders() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Get Ticker Data.
- ''' </summary>
- ''' <remarks></remarks>
- Public Function GetTicker() As String
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlTicker)
- Catch ex As Exception
- _logger.WriteEntry(EventLogEntryType.Error, "GetTicker() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- ''' <summary>
- ''' Get Trades
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function GetTrades() As String
- Try
- Return _request.GetResponse(My.Settings.Host & My.Settings.UrlGetTrades)
- Catch ex As Exception
- _logger.WriteEntry(EventLogEntryType.Error, "GetTrades() response error. {0}", ex.ToString())
- End Try
- Return Nothing
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment