Class: Stellar::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/asset.rb

Constant Summary collapse

TYPES =
%i[native alphanum4 alphanum12]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alphanum12(code, issuer) ⇒ Stellar::Asset::AlphaNum4

Returns asset4 representation.

Parameters:

  • code (String)

    asset code

  • issuer (#to_keypair)

    asset issuer

Returns:

  • (Stellar::Asset::AlphaNum4)

    asset4 representation

Raises:

  • (ArgumentError)

25
26
27
28
29
30
31
# File 'lib/stellar/asset.rb', line 25

def self.alphanum12(code, issuer)
  issuer = issuer.to_keypair if issuer.respond_to?(:to_keypair)
  raise ArgumentError, "Bad :issuer" unless issuer.is_a?(KeyPair)
  code = normalize_code(code, 12)
  an = AlphaNum12.new({asset_code: code, issuer: issuer.})
  new(:asset_type_credit_alphanum12, an)
end

.alphanum4(code, issuer) ⇒ Stellar::Asset::AlphaNum4

Returns asset4 representation.

Parameters:

  • code (String)

    asset code

  • issuer (#to_keypair)

    asset issuer

Returns:

  • (Stellar::Asset::AlphaNum4)

    asset4 representation

Raises:

  • (ArgumentError)

13
14
15
16
17
18
19
# File 'lib/stellar/asset.rb', line 13

def self.alphanum4(code, issuer)
  issuer = issuer.to_keypair if issuer.respond_to?(:to_keypair)
  raise ArgumentError, "Bad :issuer" unless issuer.is_a?(KeyPair)
  code = normalize_code(code, 4)
  an = AlphaNum4.new({asset_code: code, issuer: issuer.})
  new(:asset_type_credit_alphanum4, an)
end

.nativeObject


5
6
7
# File 'lib/stellar/asset.rb', line 5

def self.native
  new(:asset_type_native)
end

.normalize_code(code, length) ⇒ Object

Raises:

  • (ArgumentError)

75
76
77
78
79
# File 'lib/stellar/asset.rb', line 75

def self.normalize_code(code, length)
  raise ArgumentError, "Invalid asset code: #{code}, must be <= #{length} bytes" if code.length > length

  code.ljust(length, "\x00")
end

Instance Method Details

#codeObject


53
54
55
56
57
58
59
60
61
62
# File 'lib/stellar/asset.rb', line 53

def code
  case switch
  when AssetType.asset_type_credit_alphanum4
    alpha_num4!.asset_code
  when AssetType.asset_type_credit_alphanum12
    alpha_num12!.asset_code
  else
    raise "#{switch} assets do not have a code"
  end
end

#inspectObject


48
49
50
51
# File 'lib/stellar/asset.rb', line 48

def inspect
  # label = switch.to_s
  "#<Stellar::Asset #{self}>"
end

#issuerObject


64
65
66
67
68
69
70
71
72
73
# File 'lib/stellar/asset.rb', line 64

def issuer
  case switch
  when AssetType.asset_type_credit_alphanum4
    alpha_num4!.issuer
  when AssetType.asset_type_credit_alphanum12
    alpha_num12!.issuer
  else
    raise "#{switch} assets do not have a isuuer"
  end
end

#to_sObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stellar/asset.rb', line 33

def to_s
  case switch
  when AssetType.asset_type_native
    "native"
  when AssetType.asset_type_credit_alphanum4
    anum = alpha_num4!
    issuer_address = Stellar::Convert.pk_to_address(anum.issuer)
    "#{anum.asset_code}/#{issuer_address}"
  when AssetType.asset_type_credit_alphanum12
    anum = alpha_num12!
    issuer_address = Stellar::Convert.pk_to_address(anum.issuer)
    "#{anum.asset_code}/#{issuer_address}"
  end
end