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.

Defined Under Namespace

Modules: Status

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:



34
35
36
# File 'lib/coinbase/transfer.rb', line 34

def initialize(model)
  @model = model
end

Instance Method Details

#amountBigDecimal

Returns the amount of the asset for the Transfer.

Returns:

  • (BigDecimal)

    The amount of the asset



76
77
78
79
80
81
82
83
# File 'lib/coinbase/transfer.rb', line 76

def amount
  case asset_id
  when :eth
    BigDecimal(@model.amount) / BigDecimal(Coinbase::WEI_PER_ETHER.to_s)
  else
    BigDecimal(@model.amount)
  end
end

#asset_idSymbol

Returns the Asset ID of the Transfer.

Returns:

  • (Symbol)

    The Asset ID



70
71
72
# File 'lib/coinbase/transfer.rb', line 70

def asset_id
  @model.asset_id.to_sym
end

#destination_address_idString

Returns the Destination Address ID of the Transfer.

Returns:

  • (String)

    The Destination Address ID



64
65
66
# File 'lib/coinbase/transfer.rb', line 64

def destination_address_id
  @model.destination
end

#from_address_idString

Returns the From Address ID of the Transfer.

Returns:

  • (String)

    The From Address ID



58
59
60
# File 'lib/coinbase/transfer.rb', line 58

def from_address_id
  @model.address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transfer



190
191
192
# File 'lib/coinbase/transfer.rb', line 190

def inspect
  to_s
end

#network_idSymbol

Returns the Network ID of the Transfer.

Returns:

  • (Symbol)

    The Network ID



46
47
48
# File 'lib/coinbase/transfer.rb', line 46

def network_id
  Coinbase.to_sym(@model.network_id)
end

#signed_payloadString

Returns the Signed Payload of the Transfer.

Returns:

  • (String)

    The Signed Payload



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

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transfer.

Returns:

  • (Symbol)

    The status



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/coinbase/transfer.rb', line 136

def status
  # Check if the transfer has been signed yet.
  return Status::PENDING if transaction_hash.nil?

  onchain_transaction = Coinbase.configuration.base_sepolia_client.eth_getTransactionByHash(transaction_hash)

  if onchain_transaction.nil?
    # If the transaction has not been broadcast, it is still pending.
    Status::PENDING
  elsif onchain_transaction['blockHash'].nil?
    # If the transaction has been broadcast but hasn't been included in a block, it is
    # broadcast.
    Status::BROADCAST
  else
    transaction_receipt = Coinbase.configuration.base_sepolia_client.eth_getTransactionReceipt(transaction_hash)

    if transaction_receipt['status'].to_i(16) == 1
      Status::COMPLETE
    else
      Status::FAILED
    end
  end
end

#to_sString

Returns a String representation of the Transfer.

Returns:

  • (String)

    a String representation of the Transfer



181
182
183
184
185
186
# File 'lib/coinbase/transfer.rb', line 181

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

#transactionEth::Tx::Eip1559

Returns the underlying Transfer transaction, creating it if it has not been yet.

Returns:

  • (Eth::Tx::Eip1559)

    The Transfer transaction



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/coinbase/transfer.rb', line 112

def transaction
  return @transaction unless @transaction.nil?

  raw_payload = [unsigned_payload].pack('H*')
  parsed_payload = JSON.parse(raw_payload)

  params = {
    chain_id: parsed_payload['chainId'].to_i(16),
    nonce: parsed_payload['nonce'].to_i(16),
    priority_fee: parsed_payload['maxPriorityFeePerGas'].to_i(16),
    max_gas_fee: parsed_payload['maxFeePerGas'].to_i(16),
    gas_limit: parsed_payload['gas'].to_i(16), # TODO: Handle multiple currencies.
    from: Eth::Address.new(from_address_id),
    to: Eth::Address.new(parsed_payload['to']),
    value: parsed_payload['value'].to_i(16),
    data: parsed_payload['input'] || ''
  }

  @transaction = Eth::Tx::Eip1559.new(Eth::Tx.validate_eip1559_params(params))
  @transaction
end

#transaction_hashString

Returns the Transaction Hash of the Transfer.

Returns:

  • (String)

    The Transaction Hash



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

def transaction_hash
  @model.transaction_hash
end

Returns the link to the transaction on the blockchain explorer.

Returns:

  • (String)

    The link to the transaction on the blockchain explorer



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

def transaction_link
  # TODO: Parameterize this by Network.
  "https://sepolia.basescan.org/tx/#{transaction_hash}"
end

#transfer_idString

Returns the Transfer ID.

Returns:

  • (String)

    The Transfer ID



40
41
42
# File 'lib/coinbase/transfer.rb', line 40

def transfer_id
  @model.transfer_id
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transfer.

Returns:

  • (String)

    The Unsigned Payload



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

def unsigned_payload
  @model.unsigned_payload
end

#wait!(interval_seconds = 0.2, timeout_seconds = 10) ⇒ 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: 10)

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

Returns:

  • (Transfer)

    The completed Transfer object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/coinbase/transfer.rb', line 165

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

  loop do
    return self if status == Status::COMPLETE || status == Status::FAILED

    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



52
53
54
# File 'lib/coinbase/transfer.rb', line 52

def wallet_id
  @model.wallet_id
end