Class: Coinbase::Transfer
- Inherits:
-
Object
- Object
- Coinbase::Transfer
- 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
-
.create(address_id:, amount:, asset_id:, destination:, network_id:, wallet_id:) ⇒ Transfer
Creates a new Transfer object.
-
.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::Transfer>
Enumerates the transfers for a given address belonging to a wallet.
Instance Method Summary collapse
-
#amount ⇒ BigDecimal
Returns the amount of the asset for the Transfer.
- #asset ⇒ Object
-
#asset_id ⇒ Symbol
Returns the Asset ID of the Transfer.
-
#broadcast! ⇒ Transfer
Broadcasts the Transfer to the Network.
-
#destination_address_id ⇒ String
Returns the Destination Address ID of the Transfer.
-
#from_address_id ⇒ String
Returns the From Address ID of the Transfer.
-
#id ⇒ String
Returns the Transfer ID.
-
#initialize(model) ⇒ Transfer
constructor
Returns a new Transfer object.
-
#inspect ⇒ String
Same as to_s.
-
#network_id ⇒ Symbol
Returns the Network ID of the Transfer.
-
#reload ⇒ Transfer
Reload reloads the Transfer model with the latest version from the server side.
-
#signed_payload ⇒ String
Returns the Signed Payload of the Transfer.
-
#status ⇒ Symbol
Returns the status of the Transfer.
-
#to_s ⇒ String
Returns a String representation of the Transfer.
-
#transaction ⇒ Coinbase::Transaction
Returns the Transfer transaction.
-
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transfer.
-
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
-
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transfer.
-
#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.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Transfer.
Constructor Details
#initialize(model) ⇒ Transfer
Returns a new Transfer object. Do not use this method directly. Instead, use Wallet#transfer or Address#transfer.
73 74 75 76 77 |
# File 'lib/coinbase/transfer.rb', line 73 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_id:, wallet_id:) ⇒ Transfer
Creates a new Transfer object.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/coinbase/transfer.rb', line 28 def create(address_id:, amount:, asset_id:, destination:, network_id:, wallet_id:) asset = Asset.fetch(network_id, 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_id: network_id).address_id, network_id: Coinbase.normalize_network(network_id) } ) 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…
51 52 53 54 55 56 57 |
# File 'lib/coinbase/transfer.rb', line 51 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
#amount ⇒ BigDecimal
Returns the amount of the asset for the Transfer.
121 122 123 |
# File 'lib/coinbase/transfer.rb', line 121 def amount BigDecimal(@model.amount) / BigDecimal(10).power(@model.asset.decimals) end |
#asset ⇒ Object
109 110 111 |
# File 'lib/coinbase/transfer.rb', line 109 def asset @asset ||= Coinbase::Asset.from_model(@model.asset) end |
#asset_id ⇒ Symbol
Returns the Asset ID of the Transfer.
115 116 117 |
# File 'lib/coinbase/transfer.rb', line 115 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/coinbase/transfer.rb', line 166 def broadcast! raise TransactionNotSignedError unless transaction.signed? @model = Coinbase.call_api do transfers_api.broadcast_transfer( wallet_id, from_address_id, id, { signed_payload: transaction.raw.hex } ) end update_transaction(@model) self end |
#destination_address_id ⇒ String
Returns the Destination Address ID of the Transfer.
105 106 107 |
# File 'lib/coinbase/transfer.rb', line 105 def destination_address_id @model.destination end |
#from_address_id ⇒ String
Returns the From Address ID of the Transfer.
99 100 101 |
# File 'lib/coinbase/transfer.rb', line 99 def from_address_id @model.address_id end |
#id ⇒ String
Returns the Transfer ID.
81 82 83 |
# File 'lib/coinbase/transfer.rb', line 81 def id @model.transfer_id end |
#inspect ⇒ String
Same as to_s.
227 228 229 |
# File 'lib/coinbase/transfer.rb', line 227 def inspect to_s end |
#network_id ⇒ Symbol
Returns the Network ID of the Transfer.
87 88 89 |
# File 'lib/coinbase/transfer.rb', line 87 def network_id Coinbase.to_sym(@model.network_id) end |
#reload ⇒ Transfer
Reload reloads the Transfer model with the latest version from the server side.
185 186 187 188 189 190 191 192 193 |
# File 'lib/coinbase/transfer.rb', line 185 def reload @model = Coinbase.call_api do transfers_api.get_transfer(wallet_id, from_address_id, id) end update_transaction(@model) self end |
#signed_payload ⇒ String
Returns the Signed Payload of the Transfer.
140 141 142 |
# File 'lib/coinbase/transfer.rb', line 140 def signed_payload @model.signed_payload end |
#status ⇒ Symbol
Returns the status of the Transfer.
158 159 160 |
# File 'lib/coinbase/transfer.rb', line 158 def status transaction.status end |
#to_s ⇒ String
Returns a String representation of the Transfer.
218 219 220 221 222 223 |
# File 'lib/coinbase/transfer.rb', line 218 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 |
#transaction ⇒ Coinbase::Transaction
Returns the Transfer transaction.
146 147 148 |
# File 'lib/coinbase/transfer.rb', line 146 def transaction @transaction ||= Coinbase::Transaction.new(@model.transaction) end |
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transfer.
152 153 154 |
# File 'lib/coinbase/transfer.rb', line 152 def transaction_hash @model.transaction_hash end |
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
127 128 129 130 |
# File 'lib/coinbase/transfer.rb', line 127 def transaction_link # TODO: Parameterize this by Network. "https://sepolia.basescan.org/tx/#{transaction_hash}" end |
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transfer.
134 135 136 |
# File 'lib/coinbase/transfer.rb', line 134 def unsigned_payload @model.unsigned_payload 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.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/coinbase/transfer.rb', line 200 def wait!(interval_seconds = 0.2, timeout_seconds = 20) start_time = Time.now loop do reload return self if transaction.terminal_state? raise Timeout::Error, 'Transfer timed out' if Time.now - start_time > timeout_seconds self.sleep interval_seconds end self end |
#wallet_id ⇒ String
Returns the Wallet ID of the Transfer.
93 94 95 |
# File 'lib/coinbase/transfer.rb', line 93 def wallet_id @model.wallet_id end |