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:, wallet_id:, gasless: false, skip_batching: false) ⇒ 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 ⇒ Coinbase::Network
Returns the Network of the Transfer.
-
#reload ⇒ Transfer
Reload reloads the Transfer model with the latest version from the server side.
-
#sign(key) ⇒ Transfer
Signs the Transfer with the given key.
-
#sponsored_send ⇒ Coinbase::SponsoredSend
Returns the SponsoredSend of the Transfer, if the transfer is gasless.
-
#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.
-
#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
Class Method Details
.create(address_id:, amount:, asset_id:, destination:, network:, wallet_id:, gasless: false, skip_batching: false) ⇒ Transfer
Creates a new Transfer object.
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 58 59 60 61 62 |
# File 'lib/coinbase/transfer.rb', line 31 def create( address_id:, amount:, asset_id:, destination:, network:, wallet_id:, gasless: false, skip_batching: false ) ensure_can_skip_batching!(gasless, skip_batching) 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, skip_batching: skip_batching } ) 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…
68 69 70 71 72 73 74 |
# File 'lib/coinbase/transfer.rb', line 68 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.
144 145 146 |
# File 'lib/coinbase/transfer.rb', line 144 def amount BigDecimal(@model.amount) / BigDecimal(10).power(@model.asset.decimals) end |
#asset ⇒ Object
132 133 134 |
# File 'lib/coinbase/transfer.rb', line 132 def asset @asset ||= Coinbase::Asset.from_model(@model.asset) end |
#asset_id ⇒ Symbol
Returns the Asset ID of the Transfer.
138 139 140 |
# File 'lib/coinbase/transfer.rb', line 138 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.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/coinbase/transfer.rb', line 200 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_id ⇒ String
Returns the Destination Address ID of the Transfer.
128 129 130 |
# File 'lib/coinbase/transfer.rb', line 128 def destination_address_id @model.destination end |
#from_address_id ⇒ String
Returns the From Address ID of the Transfer.
122 123 124 |
# File 'lib/coinbase/transfer.rb', line 122 def from_address_id @model.address_id end |
#id ⇒ String
Returns the Transfer ID.
104 105 106 |
# File 'lib/coinbase/transfer.rb', line 104 def id @model.transfer_id end |
#inspect ⇒ String
Same as to_s.
263 264 265 |
# File 'lib/coinbase/transfer.rb', line 263 def inspect to_s end |
#network ⇒ Coinbase::Network
Returns the Network of the Transfer.
110 111 112 |
# File 'lib/coinbase/transfer.rb', line 110 def network @network ||= Coinbase::Network.from_id(@model.network_id) end |
#reload ⇒ Transfer
Reload reloads the Transfer model with the latest version from the server side.
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/coinbase/transfer.rb', line 220 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/coinbase/transfer.rb', line 152 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 |
#sponsored_send ⇒ Coinbase::SponsoredSend
Returns the SponsoredSend of the Transfer, if the transfer is gasless.
174 175 176 |
# File 'lib/coinbase/transfer.rb', line 174 def sponsored_send @sponsored_send ||= @model.sponsored_send.nil? ? nil : Coinbase::SponsoredSend.new(@model.sponsored_send) end |
#status ⇒ Symbol
Returns the status of the Transfer.
180 181 182 |
# File 'lib/coinbase/transfer.rb', line 180 def status send_tx_delegate.status end |
#to_s ⇒ String
Returns a String representation of the Transfer.
254 255 256 257 258 259 |
# File 'lib/coinbase/transfer.rb', line 254 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.
168 169 170 |
# File 'lib/coinbase/transfer.rb', line 168 def transaction @transaction ||= @model.transaction.nil? ? nil : Coinbase::Transaction.new(@model.transaction) end |
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transfer.
192 193 194 |
# File 'lib/coinbase/transfer.rb', line 192 def transaction_hash send_tx_delegate.transaction_hash end |
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
186 187 188 |
# File 'lib/coinbase/transfer.rb', line 186 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.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/coinbase/transfer.rb', line 236 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_id ⇒ String
Returns the Wallet ID of the Transfer.
116 117 118 |
# File 'lib/coinbase/transfer.rb', line 116 def wallet_id @model.wallet_id end |