Buy Contract / AuthorizationRequired
I'm trying buy contract with ruby using faye websocket as example on bot-library.
To buy a contract it's necessary an ID that it's generated from Proposal, until here it's OK. The problem is how to implement this using the event machine because I can't load the request-response of Proposal in the Event Machine OnOpen because only in the Event OnMessage I get the response of the proposal. To solve that I saved first response data a variable and ran another Event Machine but when I tried this the response of buy contract show this error:
"error"=>{"code"=>"AuthorizationRequired", "message"=>"Please log in."},
class ServiceMethod::Binary require 'faye/websocket' require 'eventmachine' require 'pry' require 'json' require 'oauth2' def initialize @json = nil @buy_id = nil @contract = nil end def negociate prepare # buy end def prepare EM.run { ws = Faye::WebSocket::Client.new('wss://ws.binaryws.com/websockets/v3?app_id=xxx') ws.send(JSON.generate({authorize:'xxx'})) ws.onopen = lambda do |event| p [:open, ws.headers] ws.send(JSON.generate({ "proposal": 1, "amount": 100, "barrier": "+0.10", "basis": "payout", "contract_type": "CALL", "currency": "USD", "duration": 60, "duration_unit": "s", "symbol": "R_100" })) end ws.onclose = lambda do |close| p [:close, close.code, close.reason] ws = nil end ws.onerror = lambda do |error| p [:error, error.message] end ws.onmessage = lambda do |message| @json = JSON.parse(message.data) p [:message, message.data] EM.stop end } EM.run { ws = Faye::WebSocket::Client.new('wss://ws.binaryws.com/websockets/v3?app_id=xxx') ws.send(JSON.generate({authorize:'xxx'})) ws.on :open do |event| ws.send(JSON.generate({ "buy": @json["proposal"]["id"], "price": @json["proposal"]["display_value"].to_f, })) p [:open] end ws.on :message do |event| p JSON.parse(event.data) EM.stop end ws.onclose = lambda do |close| p [:close, close.code, close.reason] ws = nil end } end end
the console log:
irb(main):028:0> ServiceMethod::Binary.new.prepare [:open, {"date"=>"Wed, 08 Apr 2020 06:11:39 GMT", "content-length"=>"0", "connection"=>"upgrade", "set-cookie"=>"__cfduid=d4fddb6a8c4ff5d5a7b3d7be8a1d958181586326298; expires=Fri, 08-May-20 06:11:38 GMT; path=/; domain=.binaryws.com; HttpOnly; SameSite=Lax", "sec-websocket-accept"=>"jTb7L5GDAESHS+MIvoG9cLgKuc0=", "content-language"=>"en", "upgrade"=>"websocket", "cf-cache-status"=>"DYNAMIC", "expect-ct"=>"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "server"=>"cloudflare", "cf-ray"=>"5809d5056dd0f1d2-GRU"}] [:message, "{\"echo_req\":{\"amount\":100,\"barrier\":\"+0.10\",\"basis\":\"payout\",\"contract_type\":\"CALL\",\"currency\":\"USD\",\"duration\":60,\"duration_unit\":\"s\",\"product_type\":\"basic\",\"proposal\":1,\"symbol\":\"R_100\"},\"msg_type\":\"proposal\",\"proposal\":{\"ask_price\":48.6,\"date_start\":1586326299,\"display_value\":\"48.60\",\"id\":\"f2527864-153f-602f-8a47-e261e65772de\",\"longcode\":\"Win payout if Volatility 100 Index is strictly higher than entry spot plus 0.10 at 1 minute after contract start time.\",\"payout\":100,\"spot\":1144.27,\"spot_time\":1586326298}}"] [:close, 1006, ""] [:open] {"echo_req"=>{"buy"=>"f2527864-153f-602f-8a47-e261e65772de", "price"=>48.6}, "error"=>{"code"=>"AuthorizationRequired", "message"=>"Please log in."}, "msg_type"=>"buy"} [:close, 1006, ""] => nil irb(main):029:0>
Tagged:
Comments
I solved it that way