Class: Coinbase::Transfer

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

Overview

A representation of a Transfer, which moves an amount of an Asset from a user-controlled Wallet to another address. The fee is assumed to be paid in the native Asset of the Network. Transfers should be created through Wallet#transfer or Address#transfer.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Transfer

Returns a new Transfer object. Do not use this method directly. Instead, use Wallet#transfer or Address#transfer.

Parameters:



74
75
76
77
78
# File 'lib/coinbase/transfer.rb', line 74

def initialize(model)
  raise unless model.is_a?(Coinbase::Client::Transfer)

  @model = model
end

Class Method Details

.create(address_id:, amount:, asset_id:, destination:, network:, wallet_id:, gasless: false) ⇒ Transfer

Creates a new Transfer object.

Parameters:

  • address_id (String)

    The Address ID of the sending Address

  • amount (BigDecimal)

    The amount of the Asset to send

  • asset_id (Symbol)

    The Asset ID of the Asset to send

  • destination (Coinbase::Destination, Coinbase::Wallet, Coinbase::Address, String)

    The destination of the transfer. If the destination is a Wallet, it uses the default Address of the Wallet. If the destination is an Address, it uses the Address’s ID. If the destination is a String, it uses it as the Address ID.

  • network (Coinbase::Network, Symbol)

    The Network or Network ID of the Asset

  • wallet_id (String)

    The Wallet ID of the sending Wallet

Returns:

  • (Transfer)

    The new pending Transfer object

Raises:

  • (Coinbase::ApiError)

    If the Transfer fails



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coinbase/transfer.rb', line 27

def create(address_id:, amount:, asset_id:, destination:, network:, wallet_id:, gasless: false)
  network = Coinbase::Network.from_id(network)
  asset = network.get_asset(asset_id)

  model = Coinbase.call_api do
    transfers_api.create_transfer(
      wallet_id,
      address_id,
      {
        amount: asset.to_atomic_amount(amount).to_i.to_s,
        asset_id: asset.primary_denomination.to_s,
        destination: Coinbase::Destination.new(destination, network: network).address_id,
        network_id: network.normalized_id,
        gasless: gasless
      }
    )
  end

  new(model)
end

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

Enumerates the transfers 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:



52
53
54
55
56
57
58
# File 'lib/coinbase/transfer.rb', line 52

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

Instance Method Details

#amountBigDecimal

Returns the amount of the asset for the Transfer.

Returns:

  • (BigDecimal)

    The amount of the asset



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

def amount
  BigDecimal(@model.amount) / BigDecimal(10).power(@model.asset.decimals)
end

#assetObject



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

def asset
  @asset ||= Coinbase::Asset.from_model(@model.asset)
end

#asset_idSymbol

Returns the Asset ID of the Transfer.

Returns:

  • (Symbol)

    The Asset ID



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

def asset_id
  @model.asset_id.to_sym
end

#broadcast!Transfer

Broadcasts the Transfer to the Network. This raises an error if the Transfer is not signed.

Returns:

Raises:

  • (RuntimeError)

    If the Transfer is not signed



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

def broadcast!
  raise TransactionNotSignedError unless send_tx_delegate.signed?

  @model = Coinbase.call_api do
    transfers_api.broadcast_transfer(
      wallet_id,
      from_address_id,
      id,
      { signed_payload: send_tx_delegate.signature }
    )
  end

  update_transaction(@model) unless @model.transaction.nil?
  update_sponsored_send(@model) unless @model.sponsored_send.nil?

  self
end

#destination_address_idString

Returns the Destination Address ID of the Transfer.

Returns:

  • (String)

    The Destination Address ID



106
107
108
# File 'lib/coinbase/transfer.rb', line 106

def destination_address_id
  @model.destination
end

#from_address_idString

Returns the From Address ID of the Transfer.

Returns:

  • (String)

    The From Address ID



100
101
102
# File 'lib/coinbase/transfer.rb', line 100

def from_address_id
  @model.address_id
end

#idString

Returns the Transfer ID.

Returns:

  • (String)

    The Transfer ID



82
83
84
# File 'lib/coinbase/transfer.rb', line 82

def id
  @model.transfer_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transfer



241
242
243
# File 'lib/coinbase/transfer.rb', line 241

def inspect
  to_s
end

#networkCoinbase::Network

Returns the Network of the Transfer.

Returns:



88
89
90
# File 'lib/coinbase/transfer.rb', line 88

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.



198
199
200
201
202
203
204
205
206
207
# File 'lib/coinbase/transfer.rb', line 198

def reload
  @model = Coinbase.call_api do
    transfers_api.get_transfer(wallet_id, from_address_id, id)
  end

  update_transaction(@model) unless @model.transaction.nil?
  update_sponsored_send(@model) unless @model.sponsored_send.nil?

  self
end

#sign(key) ⇒ Transfer

Signs the Transfer with the given key. This is required before broadcasting the Transfer.

Parameters:

  • key (Eth::Key)

    The key to sign the Transfer with

Returns:

Raises:

  • (RuntimeError)

    If the key is not an Eth::Key



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/coinbase/transfer.rb', line 130

def sign(key)
  raise unless key.is_a?(Eth::Key)

  unless sponsored_send.nil?
    sponsored_send.sign(key)

    return
  end

  transaction.sign(key)

  self
end

Returns the SponsoredSend of the Transfer, if the transfer is gasless.

Returns:



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

def sponsored_send
  @sponsored_send ||= @model.sponsored_send.nil? ? nil : Coinbase::SponsoredSend.new(@model.sponsored_send)
end

#statusSymbol

Returns the status of the Transfer.

Returns:

  • (Symbol)

    The status



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

def status
  send_tx_delegate.status
end

#to_sString

Returns a String representation of the Transfer.

Returns:

  • (String)

    a String representation of the Transfer



232
233
234
235
236
237
# File 'lib/coinbase/transfer.rb', line 232

def to_s
  "Coinbase::Transfer{transfer_id: '#{id}', network_id: '#{network.id}', " \
    "from_address_id: '#{from_address_id}', destination_address_id: '#{destination_address_id}', " \
    "asset_id: '#{asset_id}', amount: '#{amount}', transaction_link: '#{transaction_link}', " \
    "status: '#{status}'}"
end

#transactionCoinbase::Transaction

Returns the Transfer transaction.

Returns:



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

def transaction
  @transaction ||= @model.transaction.nil? ? nil : Coinbase::Transaction.new(@model.transaction)
end

#transaction_hashString

Returns the Transaction Hash of the Transfer.

Returns:

  • (String)

    The Transaction Hash



170
171
172
# File 'lib/coinbase/transfer.rb', line 170

def transaction_hash
  send_tx_delegate.transaction_hash
end

Returns the link to the transaction on the blockchain explorer.

Returns:

  • (String)

    The link to the transaction on the blockchain explorer



164
165
166
# File 'lib/coinbase/transfer.rb', line 164

def transaction_link
  send_tx_delegate.transaction_link
end

#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ Transfer

Waits until the Transfer is completed or failed by polling the Network at the given interval. Raises a Timeout::Error if the Transfer takes longer than the given timeout.

Parameters:

  • interval_seconds (Integer) (defaults to: 0.2)

    The interval at which to poll the Network, in seconds

  • timeout_seconds (Integer) (defaults to: 20)

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

Returns:

  • (Transfer)

    The completed Transfer object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/coinbase/transfer.rb', line 214

def wait!(interval_seconds = 0.2, timeout_seconds = 20)
  start_time = Time.now

  loop do
    reload

    return self if terminal_state?

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

    self.sleep interval_seconds
  end

  self
end

#wallet_idString

Returns the Wallet ID of the Transfer.

Returns:

  • (String)

    The Wallet ID



94
95
96
# File 'lib/coinbase/transfer.rb', line 94

def wallet_id
  @model.wallet_id
end