Class: Coinbase::Network

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

Overview

A blockchain network.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Network

Constructs a new Network object. Do not use this method directly. Instead, use the Network constants defined in the Coinbase module.

Parameters:

  • id (Symbol, String)

    The Network ID



23
24
25
# File 'lib/coinbase/network.rb', line 23

def initialize(id)
  @id = ::Coinbase.to_sym(id)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/coinbase/network.rb', line 36

def id
  @id
end

Class Method Details

.from_id(network_id) ⇒ Network

Returns the Network object for the given ID, if supported.

Parameters:

  • network_id (Symbol, String)

    The ID of the network

Returns:

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/coinbase/network.rb', line 9

def self.from_id(network_id)
  return network_id if network_id.is_a?(Network)

  network = NETWORK_MAP.fetch(Coinbase.to_sym(network_id), nil)

  return network unless network.nil?

  raise NetworkUnsupportedError, network_id
end

Instance Method Details

#==(other) ⇒ Boolean

Returns the equality of the Network object with another Network object by ID.

Parameters:

Returns:

  • (Boolean)

    Whether the Network objects are equal



30
31
32
33
34
# File 'lib/coinbase/network.rb', line 30

def ==(other)
  return false unless other.is_a?(Network)

  id == other.id
end

#address_path_prefixString

The address path prefix of the Network.

Examples:

network.address_path_prefix #=> "m/44'/60'/0'/0"

Returns:

  • (String)

    The address path prefix of the Network



78
79
80
# File 'lib/coinbase/network.rb', line 78

def address_path_prefix
  model.address_path_prefix
end

#chain_idInteger

The Chain ID of the Network.

Examples:

network.chain_id #=> 84_532

Returns:

  • (Integer)

    The Chain ID of the Network



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

def chain_id
  model.chain_id
end

#display_nameString

The display name of the Network.

Examples:

network.display_name #=> "Base Sepolia"

Returns:

  • (String)

    The display name of the Network



62
63
64
# File 'lib/coinbase/network.rb', line 62

def display_name
  model.display_name
end

#get_asset(asset_id) ⇒ Asset

Gets the Asset with the given ID.

Parameters:

  • asset_id (Symbol)

    The ID of the Asset

Returns:

  • (Asset)

    The Asset with the given ID



85
86
87
# File 'lib/coinbase/network.rb', line 85

def get_asset(asset_id)
  Asset.fetch(@id, asset_id)
end

#inspectObject



108
109
110
# File 'lib/coinbase/network.rb', line 108

def inspect
  to_s
end

#native_assetAsset

Gets the native Asset of the Network.

Returns:

  • (Asset)

    The native Asset of the Network



91
92
93
# File 'lib/coinbase/network.rb', line 91

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

#normalized_idObject



38
39
40
# File 'lib/coinbase/network.rb', line 38

def normalized_id
  id.to_s.gsub('_', '-')
end

#protocol_familyString

The protocol family to which the Network belongs. Example: `evm`.

Examples:

network.protocol_family #=> "evm"

Returns:

  • (String)

    The protocol family to which the Network belongs.



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

def protocol_family
  model.protocol_family
end

#testnet?Boolean

Whether the Network is a testnet.

Examples:

network.testnet? #=> true

Returns:

  • (Boolean)

    Whether the Network is a testnet



54
55
56
# File 'lib/coinbase/network.rb', line 54

def testnet?
  model.is_testnet
end

#to_sObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coinbase/network.rb', line 95

def to_s
  details = { id: id }

  # Only include optional details if the model is already fetched.
  unless @model.nil?
    Coinbase::Client::Network.attribute_map.each_key do |attr|
      details[attr] = @model.send(attr)
    end
  end

  Coinbase.pretty_print_object(self.class, **details)
end