Class: Coinbase::FundOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/fund_operation.rb

Overview

A representation of a Fund Operation, which buys funds from the Coinbase platform, and sends then to the developer’s address.

Defined Under Namespace

Modules: Status

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ FundOperation

Returns a new Fund Operation object. Do not use this method directly. Instead, use Wallet#fund or Address#fund.

Parameters:

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/coinbase/fund_operation.rb', line 96

def initialize(model)
  raise ArgumentError, 'must be a FundOperation' unless model.is_a?(Coinbase::Client::FundOperation)

  @model = model
end

Class Method Details

.create(wallet_id:, address_id:, amount:, asset_id:, network:, quote: nil) ⇒ FundOperation

Creates a new Fund Operation object. This takes an optional FundQuote object that can be used to lock in the rate and fees. Without an explicit quote, we will use the current rate and fees.

Parameters:

  • address_id (String)

    The Address ID of the sending Address

  • wallet_id (String)

    The Wallet ID of the sending Wallet

  • amount (BigDecimal)

    The amount of the Asset to send

  • network (Coinbase::Network, Symbol)

    The Network or Network ID of the Asset

  • asset_id (Symbol)

    The Asset ID of the Asset to send

  • quote (Coinbase::FundQuote, String) (defaults to: nil)

    The optional FundQuote to use for the Fund Operation

Returns:

Raises:

  • (Coinbase::ApiError)

    If the Fund Operation fails



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/coinbase/fund_operation.rb', line 38

def create(wallet_id:, address_id:, amount:, asset_id:, network:, quote: nil)
  network = Coinbase::Network.from_id(network)
  asset = network.get_asset(asset_id)

  model = Coinbase.call_api do
    fund_api.create_fund_operation(
      wallet_id,
      address_id,
      {
        amount: asset.to_atomic_amount(amount).to_i.to_s,
        asset_id: asset.primary_denomination.to_s,
        fund_quote_id: quote_id(quote)
      }.compact
    )
  end

  new(model)
end

.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::FundOperation>

Enumerates the fund operation for a given address belonging to a wallet. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…

Returns:



61
62
63
64
65
66
67
# File 'lib/coinbase/fund_operation.rb', line 61

def list(wallet_id:, address_id:)
  Coinbase::Pagination.enumerate(
    ->(page) { fetch_page(wallet_id, address_id, page) }
  ) do |fund_operation|
    new(fund_operation)
  end
end

Instance Method Details

#address_idString

Returns the Address ID that the fund quote was created for.

Returns:

  • (String)

    The Address ID



122
123
124
# File 'lib/coinbase/fund_operation.rb', line 122

def address_id
  @model.address_id
end

#amountCoinbase::CryptoAmount

Returns the amount that the wallet will receive in crypto.

Returns:



134
135
136
# File 'lib/coinbase/fund_operation.rb', line 134

def amount
  @amount ||= CryptoAmount.from_model(@model.crypto_amount)
end

#assetCoinbase::Asset

Returns the Asset of the Fund Operation.

Returns:



128
129
130
# File 'lib/coinbase/fund_operation.rb', line 128

def asset
  amount.asset
end

#buy_feeCoinbase::FiatAmount

Returns the fee that the wallet’s owner will pay in fiat.

Returns:



146
147
148
# File 'lib/coinbase/fund_operation.rb', line 146

def buy_fee
  @buy_fee ||= FiatAmount.from_model(@model.fees.buy_fee)
end

#fiat_amountCoinbase::FiatAmount

Returns the amount that the wallet’s owner will pay in fiat.

Returns:



140
141
142
# File 'lib/coinbase/fund_operation.rb', line 140

def fiat_amount
  @fiat_amount ||= FiatAmount.from_model(@model.fiat_amount)
end

#idString

Returns the Fund Operation ID.

Returns:

  • (String)

    The Fund Operation ID



104
105
106
# File 'lib/coinbase/fund_operation.rb', line 104

def id
  @model.fund_operation_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Fund Operation.



212
213
214
# File 'lib/coinbase/fund_operation.rb', line 212

def inspect
  to_s
end

#networkCoinbase::Network

Returns the Network of the Fund Operation.

Returns:



110
111
112
# File 'lib/coinbase/fund_operation.rb', line 110

def network
  @network ||= Coinbase::Network.from_id(@model.network_id)
end

#reloadTransfer

Reload reloads the Transfer model with the latest version from the server side.

Returns:

  • (Transfer)

    The most recent version of Transfer from the server.



164
165
166
167
168
169
170
# File 'lib/coinbase/fund_operation.rb', line 164

def reload
  @model = Coinbase.call_api do
    fund_api.get_fund_operation(wallet_id, address_id, id)
  end

  self
end

#statusSymbol

Returns the status of the Fund Operation.

Returns:

  • (Symbol)

    The status



158
159
160
# File 'lib/coinbase/fund_operation.rb', line 158

def status
  @model.status
end

#to_sString

Returns a String representation of the Fund Operation.

Returns:

  • (String)

    a String representation of the Fund Operation



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/coinbase/fund_operation.rb', line 195

def to_s
  Coinbase.pretty_print_object(
    self.class,
    id: id,
    network_id: network.id,
    wallet_id: wallet_id,
    address_id: address_id,
    status: status,
    crypto_amount: amount,
    fiat_amount: fiat_amount,
    buy_fee: buy_fee,
    transfer_fee: transfer_fee
  )
end

#transfer_feeCoinbase::CryptoAmount

Returns the fee that the wallet’s owner will pay in crypto.

Returns:



152
153
154
# File 'lib/coinbase/fund_operation.rb', line 152

def transfer_fee
  @transfer_fee ||= CryptoAmount.from_model(@model.fees.transfer_fee)
end

#wait!(interval_seconds = 1, timeout_seconds = 60) ⇒ Coinbase::FundOperation

Waits until the Fund Operation is completed or failed by polling the at the given interval.

Parameters:

  • interval_seconds (Integer) (defaults to: 1)

    The interval at which to poll the Network, in seconds

  • timeout_seconds (Integer) (defaults to: 60)

    The maximum amount of time to wait for the Fund Operation to complete, in seconds

Returns:

Raises:

  • (Timeout::Error)

    If the Fund Operation takes longer than the given timeout



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/coinbase/fund_operation.rb', line 177

def wait!(interval_seconds = 1, timeout_seconds = 60)
  start_time = Time.now

  loop do
    reload

    return self if terminal_state?

    raise Timeout::Error, 'Fund Operation timed out' if Time.now - start_time > timeout_seconds

    self.sleep interval_seconds
  end

  self
end

#wallet_idString

Returns the Wallet ID that the fund quote was created for.

Returns:

  • (String)

    The Wallet ID



116
117
118
# File 'lib/coinbase/fund_operation.rb', line 116

def wallet_id
  @model.wallet_id
end