Class: Runar::SDK::RunarArtifact

Inherits:
Object
  • Object
show all
Defined in:
lib/runar/sdk/types.rb

Overview

Compiled output of a Runar compiler.

Use RunarArtifact.from_hash to load from a JSON-parsed Hash, or RunarArtifact.from_json to parse a raw JSON string.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version: '', compiler_version: '', contract_name: '', abi: ABI.new, script: '', asm: '', state_fields: [], constructor_slots: [], build_timestamp: '', code_separator_index: nil, code_separator_indices: nil, anf: nil) ⇒ RunarArtifact

Returns a new instance of RunarArtifact.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/runar/sdk/types.rb', line 74

def initialize(
  version: '',
  compiler_version: '',
  contract_name: '',
  abi: ABI.new,
  script: '',
  asm: '',
  state_fields: [],
  constructor_slots: [],
  build_timestamp: '',
  code_separator_index: nil,
  code_separator_indices: nil,
  anf: nil
)
  @version                = version
  @compiler_version       = compiler_version
  @contract_name          = contract_name
  @abi                    = abi
  @script                 = script
  @asm                    = asm
  @state_fields           = state_fields
  @constructor_slots      = constructor_slots
  @build_timestamp        = build_timestamp
  @code_separator_index   = code_separator_index
  @code_separator_indices = code_separator_indices
  @anf                    = anf
end

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def abi
  @abi
end

#anfObject (readonly)

Returns the value of attribute anf.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def anf
  @anf
end

#asmObject (readonly)

Returns the value of attribute asm.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def asm
  @asm
end

#build_timestampObject (readonly)

Returns the value of attribute build_timestamp.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def build_timestamp
  @build_timestamp
end

#code_separator_indexObject (readonly)

Returns the value of attribute code_separator_index.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def code_separator_index
  @code_separator_index
end

#code_separator_indicesObject (readonly)

Returns the value of attribute code_separator_indices.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def code_separator_indices
  @code_separator_indices
end

#compiler_versionObject (readonly)

Returns the value of attribute compiler_version.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def compiler_version
  @compiler_version
end

#constructor_slotsObject (readonly)

Returns the value of attribute constructor_slots.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def constructor_slots
  @constructor_slots
end

#contract_nameObject (readonly)

Returns the value of attribute contract_name.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def contract_name
  @contract_name
end

#scriptObject (readonly)

Returns the value of attribute script.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def script
  @script
end

#state_fieldsObject (readonly)

Returns the value of attribute state_fields.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def state_fields
  @state_fields
end

#versionObject (readonly)

Returns the value of attribute version.



69
70
71
# File 'lib/runar/sdk/types.rb', line 69

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object

Load an artifact from a JSON-parsed Hash (keys may be camelCase strings).



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/runar/sdk/types.rb', line 103

def self.from_hash(hash)
  abi_raw = hash.fetch('abi', {})

  ctor_params = Array(abi_raw.dig('constructor', 'params')).map do |p|
    ABIParam.new(name: p['name'], type: p['type'])
  end

  methods = Array(abi_raw['methods']).map do |m|
    params = Array(m['params']).map { |p| ABIParam.new(name: p['name'], type: p['type']) }
    ABIMethod.new(
      name: m['name'],
      params: params,
      is_public: m.fetch('isPublic', true),
      is_terminal: m['isTerminal']
    )
  end

  state_fields = Array(hash['stateFields']).map do |sf|
    StateField.new(
      name: sf['name'],
      type: sf['type'],
      index: sf['index'],
      initial_value: sf['initialValue']
    )
  end

  constructor_slots = Array(hash['constructorSlots']).map do |cs|
    ConstructorSlot.new(param_index: cs['paramIndex'], byte_offset: cs['byteOffset'])
  end

  new(
    version:                hash.fetch('version', ''),
    compiler_version:       hash.fetch('compilerVersion', ''),
    contract_name:          hash.fetch('contractName', ''),
    abi:                    ABI.new(constructor_params: ctor_params, methods: methods),
    script:                 hash.fetch('script', ''),
    asm:                    hash.fetch('asm', ''),
    state_fields:           state_fields,
    constructor_slots:      constructor_slots,
    build_timestamp:        hash.fetch('buildTimestamp', ''),
    code_separator_index:   hash['codeSeparatorIndex'],
    code_separator_indices: hash['codeSeparatorIndices'],
    anf:                    hash['anf']
  )
end

.from_json(json_string) ⇒ Object

Load an artifact from a raw JSON string.



150
151
152
153
# File 'lib/runar/sdk/types.rb', line 150

def self.from_json(json_string)
  require 'json'
  from_hash(JSON.parse(json_string))
end