%p#notice= notice

%p
  %b Name:
  = @metapurse.name

-# %p
  %b Data:
  = @metapurse.data

-# - @metapurse.data.each_line do |record|
  %p
    = canister_id = record.split[0]
    = amount = record.split[1].gsub(",","").to_f

-# - icpswap_pool_data_raw = File.read("./pool_data_raw.txt")
- icpswap_pool_data_raw = `dfx --identity anonymous canister --network=ic call ggzvv-5qaaa-aaaag-qck7a-cai getAllPools`
- total_value = 0.0
- @metapurse.data.each_line do |record|
  %p
    - canister_id = record.split[0]
    = number_with_delimiter(amount = record.split[1].gsub(",","").to_f) rescue nil
    - icp_canister_id = "ryjl3-tyaaa-aaaaa-aaaba-cai"
    -# = icpswap_pool_data_raw.split("record {").select{|pool| !pool.scan(canister_id).empty? && !pool.scan(icp_canister_id).empty? }[0]
    - if canister_id == icp_canister_id
      *
      - value = @metapurse.icp_usd
      = "#{number_to_currency value.round(2)}"
      ICP
      - value_amount = (value * amount).round(2) rescue 0
      $#{number_with_delimiter(value_amount)} USD
      - total_value +=  value_amount rescue 0
    - else
      - value = icpswap_pool_data_raw.split("record {").select{|pool| !pool.scan(canister_id).empty? && !pool.scan(icp_canister_id).empty? }[-1].scan(/token0Price = [+-]?([1-9]\d*|0)(\.\d+)?/).join.to_f rescue nil
      *
      = "$#{value.round(2)}"
      = icpswap_pool_data_raw.split("record {").select{|pool| !pool.scan(canister_id).empty? && !pool.scan(icp_canister_id).empty? }[0].scan(/token0Symbol = \"(.*)\"; /)[0][0] rescue nil
      - value_amount = (value * amount).round(2) rescue 0
      $#{number_with_delimiter(value_amount)} USD
      - total_value +=  value_amount rescue 0
%p
  %b Total:
  $#{number_with_delimiter(total_value.round(2))} USD


- if policy(@metapurse).edit?
  = link_to 'Edit', edit_metapurse_path(@metapurse)
  \|
= link_to 'View data', data_metapurse_path(@metapurse)
= link_to 'Back', metapurses_path
