Class: Xgt::Ruby::Rpc
- Inherits:
-
Object
- Object
- Xgt::Ruby::Rpc
- Defined in:
- lib/xgt/ruby.rb
Instance Method Summary collapse
- #broadcast_transaction(txn, wifs, chain_id) ⇒ Object
- #call(mthd, params) ⇒ Object
-
#initialize(url, client: nil) ⇒ Rpc
constructor
A new instance of Rpc.
- #transaction_ready?(id) ⇒ Boolean
Constructor Details
#initialize(url, client: nil) ⇒ Rpc
Returns a new instance of Rpc.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/xgt/ruby.rb', line 21 def initialize(url, client: nil) @url = url @client = client || Faraday::Connection.new(url: @url) do |faraday| faraday.request(:json) faraday.request :retry, max: 10, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2, # TODO: Make this more specific exceptions: ['Error'] faraday.response(:json) faraday.response(:logger) if ENV['LOGGING_ENABLED'] faraday.adapter(Faraday.default_adapter) end end |
Instance Method Details
#broadcast_transaction(txn, wifs, chain_id) ⇒ Object
66 67 68 69 70 |
# File 'lib/xgt/ruby.rb', line 66 def broadcast_transaction(txn, wifs, chain_id) signed = Xgt::Ruby::Auth.sign_transaction(self, txn, wifs, chain_id) response = self.call('transaction_api.broadcast_transaction', [signed]) response['id'] end |
#call(mthd, params) ⇒ Object
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 |
# File 'lib/xgt/ruby.rb', line 38 def call(mthd, params) id = SecureRandom.hex(6).to_i(16) payload = { 'jsonrpc' => '2.0', 'method' => mthd, 'id' => id } payload['params'] = params unless params.nil? response = @client.post('/', payload) # TODO: Verify status code unless response.body raise RpcError.new(%(No response body!\n#{response.inspect}), response) end if response.body['error'] raise RpcError.new(%(Endpoint returned an error response!\n#{JSON.pretty_generate(response.body['error'])}), response) end unless response.body['result'] raise RpcError.new(%(No result in response body!\n#{response.inspect}), response) end # TODO: XXX: Breaking change! response.body['result'] end |
#transaction_ready?(id) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/xgt/ruby.rb', line 72 def transaction_ready?(id) begin self.call('wallet_history_api.get_transaction', { 'id' => id }) true rescue Xgt::Ruby::RpcError => e = e&.response &.body &.fetch('error', nil) &.fetch('message', nil) wait_regexps = Regexp.union(%r(transaction.*?>.*?trx_in_block)) raise e unless .match(wait_regexps) false end end |