class IcAgent::Canister

Public Class Methods

new(agent, canister_id, candid=nil) click to toggle source

Constructor for the Canister class.

Parameters:

  • agent: An instance of the agent.

  • canister_id: ID of the canister.

  • candid: (Optional) The Candid description of the canister. If not provided, it will be queried.

# File lib/ic_agent/canister.rb, line 11
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

  # Parse the Candid description to extract information about service methods.
  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