diff --git a/lib/coinbase/fund_operation.rb b/lib/coinbase/fund_operation.rb index 49fb7192..f52587a0 100644 --- a/lib/coinbase/fund_operation.rb +++ b/lib/coinbase/fund_operation.rb @@ -25,14 +25,17 @@ module Status class << self # 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. # @param address_id [String] The Address ID of the sending Address # @param wallet_id [String] The Wallet ID of the sending Wallet # @param amount [BigDecimal] The amount of the Asset to send # @param network [Coinbase::Network, Symbol] The Network or Network ID of the Asset # @param asset_id [Symbol] The Asset ID of the Asset to send - # @return [FundOperation] The new pending FundOperation object - # @raise [Coinbase::ApiError] If the FundOperation fails - def create(wallet_id:, address_id:, amount:, asset_id:, network:) + # @param quote [Coinbase::FundQuote, String] The optional FundQuote to use for the Fund Operation + # @return [FundOperation] The new pending Fund Operation object + # @raise [Coinbase::ApiError] If the Fund Operation fails + def create(wallet_id:, address_id:, amount:, asset_id:, network:, quote: nil) network = Coinbase::Network.from_id(network) asset = network.get_asset(asset_id) @@ -43,7 +46,8 @@ def create(wallet_id:, address_id:, amount:, asset_id:, network:) { 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 @@ -76,6 +80,14 @@ def fetch_page(wallet_id, address_id, page) page: page ) end + + def quote_id(quote) + return nil if quote.nil? + return quote.id if quote.is_a?(FundQuote) + return quote if quote.is_a?(String) + + raise ArgumentError, 'quote must be a FundQuote or a String FundQuote ID' + end end # Returns a new Fund Operation object. Do not use this method directly. Instead, use diff --git a/lib/coinbase/fund_quote.rb b/lib/coinbase/fund_quote.rb index c6a8f75a..50199fee 100644 --- a/lib/coinbase/fund_quote.rb +++ b/lib/coinbase/fund_quote.rb @@ -57,6 +57,20 @@ def id @model.fund_quote_id end + # Executes a fund operation using the quote. + # @return [Coinbase::FundOperation] The FundOperation object + # @raise [Coinbase::ApiError] If the FundOperation fails + def execute! + FundOperation.create( + wallet_id: wallet_id, + address_id: address_id, + amount: amount.amount, + asset_id: asset.asset_id, + network: network.id, + quote: self + ) + end + # Returns the Network the fund quote was created on. # @return [Coinbase::Network] The Network def network @@ -125,5 +139,11 @@ def to_s def inspect to_s end + + private + + def fund_api + @fund_api ||= Coinbase::Client::FundApi.new(Coinbase.configuration.api_client) + end end end