Class: IcAgent::Canister

Inherits:
Object
  • Object
show all
Defined in:
lib/ic_agent/canister.rb

Instance Method Summary collapse

Constructor Details

#initialize(agent, canister_id, candid = nil) ⇒ Canister

Returns a new instance of Canister.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ic_agent/canister.rb', line 5

def initialize(agent, canister_id, candid=nil)
  @agent = agent
  @canister_id = canister_id
  if candid
    @candid = candid
  else
    candid = @agent.query_raw(canister_id, '__get_candid_interface_tmp_hack', encode([]))
    @candid = candid[0]['value']
  end
  if candid.nil?
    puts candid
    puts 'Please provide candid description'
    raise BaseException, "canister #{@canister_id} has no __get_candid_interface_tmp_hack method."
  end

  parser = IcAgent::Ast::Parser.new
  parser.parse(@candid)

  ic_service_methods = parser.ic_service_methods.elements
  ic_service_methods.each do |item|
    service_method = item.to_obj
    method_name = service_method['ic_service_method_name']
    anno = service_method['ic_service_method_query']
    args = service_method['ic_service_method_params']
    rets = service_method['ic_service_method_return']

    args_arr = args.nil? ? [] : args.split(',').map(&:strip)
    args_type_arrs = []
    args_arr.each do |arg|
      args_type_arrs << get_param_to_ic_type(parser, arg)
    end

    rets_arr = rets.nil? ? [] : rets.split(',').map(&:strip)
    rets_type_arr = []
    rets_arr.each do |ret|
      rets_type_arr << get_param_to_ic_type(parser, ret)
    end

    add_caniter_method(method_name, args, args_type_arrs, rets_type_arr, anno)
  end
end