Class: Cryptsy::WebClient

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptsy/web_client.rb

Overview

Unsafe client that allows you to do things that the Cryptsy API does not permit, including withdrawals to untrusted addresses, as well as pre-approving addresses for withdrawals

Constant Summary collapse

DEFAULT_OPTIONS =
{
  url: 'https://www.cryptsy.com'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, tfa_secret, options = {}) ⇒ WebClient

Returns a new instance of WebClient.

Parameters:

  • username (String)
  • password (String)
  • tfa_secret (String)
  • options (Hash) (defaults to: {})


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cryptsy/web_client.rb', line 21

def initialize(username, password, tfa_secret, options = {})
  @username = username
  @password = password
  @tfa = ROTP::TOTP.new(tfa_secret)

  @cookie_jar = HTTP::CookieJar.new

  @connection = Faraday.new(DEFAULT_OPTIONS.merge(options)) do |builder|
    builder.use :cookie_jar, jar: @cookie_jar
    builder.request :url_encoded
    builder.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

Parameters:

  • (HTTP::CookieJar)


15
16
17
# File 'lib/cryptsy/web_client.rb', line 15

def cookie_jar
  @cookie_jar
end

Instance Method Details

#add_trusted_address(currency_id, address) ⇒ Faraday::Response

Submits a trusted address for pre-approved withdrawals

Parameters:

  • currency_id (Integer)
  • address (String)

Returns:

  • (Faraday::Response)


61
62
63
64
65
66
67
68
69
70
# File 'lib/cryptsy/web_client.rb', line 61

def add_trusted_address(currency_id, address)
  request = {
    'data[Withdrawal][currency_id]' => currency_id,
    'data[Withdrawal][address]' => address,
    'data[Withdrawal][existing_password]' => @password,
    'data[Withdrawal][pincode]' => @tfa.now,
  }

  post_with_token('/users/addtrustedaddress', request)
end

#get(url) ⇒ Faraday::Response

Parameters:

  • url (String)

Returns:

  • (Faraday::Response)


95
96
97
# File 'lib/cryptsy/web_client.rb', line 95

def get(url)
  @connection.get(url)
end

#loginFaraday::Response

Performs login operation using the configured username and password

Returns:

  • (Faraday::Response)


37
38
39
40
41
42
43
44
# File 'lib/cryptsy/web_client.rb', line 37

def 
  request = {
    'data[User][username]' => @username,
    'data[User][password]' => @password,
  }

  post_with_token('/users/login', request)
end

#make_withdrawal(currency_id, address, amount) ⇒ Faraday::Response

Submits a request for withdrawal to an untrusted address

Note that this will only work for addresses that have not been trusted. Use the JSON API for withdrawing to trusted addresses.

Parameters:

  • currency_id (Integer)
  • address (String)
  • amount (Float)

Returns:

  • (Faraday::Response)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cryptsy/web_client.rb', line 81

def make_withdrawal(currency_id, address, amount)
  request = {
    'data[Withdrawal][fee]' => '1.00000000',
    'data[Withdrawal][wdamount]' => amount,
    'data[Withdrawal][address]' => address,
    'data[Withdrawal][existing_password]' => @password,
    'data[Withdrawal][pincode]' => @tfa.now,
  }

  post_with_token("/users/makewithdrawal/#{currency_id}", request)
end

#pincodeFaraday::Response

Finishes login operation using TOTP and TFA secret

Returns:

  • (Faraday::Response)


48
49
50
51
52
53
54
# File 'lib/cryptsy/web_client.rb', line 48

def pincode
  request = {
    'data[User][pincode]' => @tfa.now
  }

  post_with_token('/users/pincode', request)
end

#post(url, body) ⇒ Faraday::Response

Parameters:

  • url (String)
  • body (Hash)

Returns:

  • (Faraday::Response)


102
103
104
# File 'lib/cryptsy/web_client.rb', line 102

def post(url, body)
  @connection.post(url, body)
end

#post_with_token(url, request) ⇒ Faraday::Request

Performs an initial GET request to the given URL to obtain any CSRF tokens, injects them into the given request, then performs a POST request to the given URL

Parameters:

  • url (String)
  • request (Hash)

Returns:

  • (Faraday::Request)


112
113
114
115
# File 'lib/cryptsy/web_client.rb', line 112

def post_with_token(url, request)
  prepare_request(get(url), request)
  post(url, request)
end