Class: OrionX::Endpoints::Transactions

Inherits:
Object
  • Object
show all
Defined in:
lib/orionx/endpoints/transactions.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Transactions

Returns a new instance of Transactions.



4
5
6
7
# File 'lib/orionx/endpoints/transactions.rb', line 4

def initialize(api_client)
  @api = api_client
  @logger = api_client.logger
end

Instance Method Details

#convert(quote_option_id:, amount:, market_code:, sell:) ⇒ Object

Convert/Trade instantly



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/orionx/endpoints/transactions.rb', line 207

def convert(quote_option_id:, amount:, market_code:, sell:)
  raise OrionX::ValidationError, "Quote option ID cannot be nil or empty" if quote_option_id.nil? || quote_option_id.empty?
  raise OrionX::ValidationError, "Amount must be positive" if amount.nil? || amount <= 0
  raise OrionX::ValidationError, "Market code cannot be nil or empty" if market_code.nil? || market_code.empty?

  query = <<~GRAPHQL
    mutation sdk_convert(
      $quoteOptionId: String
      $amount: BigInt!
      $marketCode: String!
      $sell: Boolean!
    ) {
      instantTransaction(
        quoteOptionId: $quoteOptionId
        amount: $amount
        marketCode: $marketCode
        sell: $sell
      )
    }
  GRAPHQL

  variables = {
    quoteOptionId: quote_option_id,
    amount: amount,
    marketCode: market_code,
    sell: sell
  }

  @logger.debug("Converting/trading instantly: #{variables.inspect}")
  response = @api.call(query, variables)
  
  result = response.dig("instantTransaction")
  @logger.info("Instant conversion completed successfully") if result
  result
end

#get_history(currency_code, limit: 50, page: 1) ⇒ Object

Get transaction history for a specific currency



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/orionx/endpoints/transactions.rb', line 244

def get_history(currency_code, limit: 50, page: 1)
  # First get the wallet ID for the currency
   = <<~GRAPHQL
    query sdk_getAccount($assetId: ID!) {
      wallet(code: $assetId) {
        _id
      }
    }
  GRAPHQL

   = @api.call(, { assetId: currency_code })
  wallet_id = .dig("wallet", "_id")

  return nil unless wallet_id

  # Get transactions for this wallet
  get_transactions({
    walletId: wallet_id,
    limit: limit,
    page: page,
    sortType: "DESC"
  })
end

#get_transaction(transaction_id) ⇒ Object

Get a specific transaction by ID



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
# File 'lib/orionx/endpoints/transactions.rb', line 10

def get_transaction(transaction_id)
  raise OrionX::ValidationError, "Transaction ID cannot be nil or empty" if transaction_id.nil? || transaction_id.empty?

  query = <<~GRAPHQL
    query sdk_getTransaction($_id: ID!) {
      transaction(_id: $_id) {
        _id
        amount
        balance
        commission
        currency {
          code
          units
        }
        date
        type
        adds
        hash
        description
        market {
          code
          mainCurrency {
            code
            units
          }
          secondaryCurrency {
            code
            units
          }
        }
        price
        cost
        explorerURL
        isInside
        meta {
          status
        }
      }
    }
  GRAPHQL

  @logger.debug("Fetching transaction: #{transaction_id}")
  response = @api.call(query, { _id: transaction_id })
  
  result = response.dig("transaction")
  @logger.info("Transaction #{transaction_id} retrieved successfully") if result
  result
end

#get_transactions(filters = {}) ⇒ Object

Get transactions with optional filters



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
# File 'lib/orionx/endpoints/transactions.rb', line 60

def get_transactions(filters = {})
  query = <<~GRAPHQL
    query sdk_getTransactions($filter: String, $walletId: ID, $types: [String], $initPeriod: Date, $finalPeriod: Date, $page: Int, $limit: Int, $sortBy: String, $sortType: SortType) {
      transactions(filter: $filter, walletId: $walletId, types: $types, initPeriod: $initPeriod, finalPeriod: $finalPeriod, page: $page, limit: $limit, sortBy: $sortBy, sortType: $sortType) {
        _id
        items {
          amount
          balance
          commission
          currency {
            code
            units
          }
          date
          type
          adds
          hash
          description
          market {
            code
            mainCurrency {
              code
              units
            }
            secondaryCurrency {
              code
              units
            }
          }
          price
          cost
          explorerURL
          isInside
          meta {
            status
          }
        }
      }
    }
  GRAPHQL

  @logger.debug("Fetching transactions with filters: #{filters.inspect}")
  response = @api.call(query, filters)
  
  result = response.dig("transactions")
  @logger.info("Transactions retrieved: #{result&.dig('items')&.length || 0} transactions") if result
  result
end

#send_crypto(wallet_id:, network:, amount:, contact_id: nil, description: nil, client_id: nil) ⇒ Object

Send cryptocurrency



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
# File 'lib/orionx/endpoints/transactions.rb', line 110

def send_crypto(wallet_id:, network:, amount:, contact_id: nil, description: nil, client_id: nil)
  raise OrionX::ValidationError, "Wallet ID cannot be nil or empty" if wallet_id.nil? || wallet_id.empty?
  raise OrionX::ValidationError, "Network cannot be nil or empty" if network.nil? || network.empty?
  raise OrionX::ValidationError, "Amount must be positive" if amount.nil? || amount <= 0

  query = <<~GRAPHQL
    mutation sdk_send(
      $fromWalletId: ID!
      $contactId: ID
      $network: String!
      $amount: BigInt!
      $description: String
      $clientId: ID
    ) {
      sendCrypto(
        fromWalletId: $fromWalletId
        contactId: $contactId
        network: $network
        amount: $amount
        description: $description
        clientId: $clientId
      ) {
        _id
        type
        amount
        price
        hash
        date
        market {
          code
        }
        meta { 
          status
        }
      }
    }
  GRAPHQL

  variables = {
    fromWalletId: wallet_id,
    contactId: contact_id,
    network: network,
    amount: amount,
    description: description,
    clientId: client_id
  }

  @logger.debug("Sending crypto: #{variables.inspect}")
  response = @api.call(query, variables)
  
  result = response.dig("sendCrypto")
  @logger.info("Crypto send initiated successfully: #{result&.dig('_id')}") if result
  result
end

#withdrawal_request(wallet_id:, account_id:, amount:) ⇒ Object

Request withdrawal



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
# File 'lib/orionx/endpoints/transactions.rb', line 166

def withdrawal_request(wallet_id:, account_id:, amount:)
  raise OrionX::ValidationError, "Wallet ID cannot be nil or empty" if wallet_id.nil? || wallet_id.empty?
  raise OrionX::ValidationError, "Account ID cannot be nil or empty" if .nil? || .empty?
  raise OrionX::ValidationError, "Amount must be positive" if amount.nil? || amount <= 0

  query = <<~GRAPHQL
    mutation sdk_withdrawalRequest(
      $walletId: ID
      $accountId: ID
      $amount: BigInt
    ) {
      requestWithdrawal(
        walletId: $walletId
        accountId: $accountId
        amount: $amount
      ) {
        _id
        amount
        commission
        date
        type
        description
      }
    }
  GRAPHQL

  variables = {
    walletId: wallet_id,
    accountId: ,
    amount: amount
  }

  @logger.debug("Requesting withdrawal: #{variables.inspect}")
  response = @api.call(query, variables)
  
  result = response.dig("requestWithdrawal")
  @logger.info("Withdrawal request created successfully: #{result&.dig('_id')}") if result
  result
end