Add data view to metapurses
This commit is contained in:
parent
559a05913c
commit
be135ecc27
@ -1,5 +1,5 @@
|
||||
class MetapursesController < ApplicationController
|
||||
before_action :set_metapurse, only: %i[ show edit update destroy ]
|
||||
before_action :set_metapurse, only: %i[ show edit update destroy data ]
|
||||
|
||||
# GET /metapurses or /metapurses.json
|
||||
def index
|
||||
@ -11,6 +11,11 @@ class MetapursesController < ApplicationController
|
||||
authorize @metapurse
|
||||
end
|
||||
|
||||
def data
|
||||
authorize @metapurse
|
||||
end
|
||||
|
||||
|
||||
# GET /metapurses/new
|
||||
def new
|
||||
authorize @metapurse = current_user.metapurses.new
|
||||
|
||||
@ -8,4 +8,26 @@ class Metapurse < ActiveRecord::Base
|
||||
JSON.parse(`curl -s https://www.binance.com/api/v3/avgPrice?symbol=ICPUSDT`)["price"].to_f
|
||||
end
|
||||
|
||||
def icp_canister_id
|
||||
"ryjl3-tyaaa-aaaaa-aaaba-cai"
|
||||
end
|
||||
|
||||
def total_usd
|
||||
total_usd = 0.0
|
||||
self.data.each_line do |record|
|
||||
canister_id = record.split[0]
|
||||
amount = record.split[1].gsub(",","").to_f # rescue nil
|
||||
if canister_id == icp_canister_id
|
||||
value = self.icp_usd
|
||||
value_amount = (value * amount).round(2) # rescue 0
|
||||
total_usd += 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? }[0].scan(/token0Price = [+-]?([1-9]\d*|0)(\.\d+)?/).join.to_f # rescue nil
|
||||
value_amount = (value * amount).round(2) # rescue 0
|
||||
total_usd += value_amount # rescue 0
|
||||
end
|
||||
end
|
||||
return total_usd.round(2)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@ -33,6 +33,10 @@ class MetapursePolicy
|
||||
true
|
||||
end
|
||||
|
||||
def data?
|
||||
true
|
||||
end
|
||||
|
||||
def create?
|
||||
@user.admin? || @user.signed_in?
|
||||
end
|
||||
|
||||
23
app/views/metapurses/data.html.haml
Normal file
23
app/views/metapurses/data.html.haml
Normal file
@ -0,0 +1,23 @@
|
||||
%p#notice= notice
|
||||
|
||||
%p
|
||||
%b Name:
|
||||
= @metapurse.name
|
||||
|
||||
%p
|
||||
%b Data:
|
||||
%pre= @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")
|
||||
|
||||
- if policy(@metapurse).edit?
|
||||
= link_to 'Edit', edit_metapurse_path(@metapurse)
|
||||
\|
|
||||
= link_to 'Show Value', metapurse_path(@metapurse)
|
||||
\|
|
||||
= link_to 'Back', metapurses_path
|
||||
@ -13,7 +13,9 @@
|
||||
%tbody
|
||||
- @metapurses.each do |metapurse|
|
||||
%tr
|
||||
%td= link_to metapurse.name, metapurse
|
||||
%td
|
||||
= link_to metapurse.name, metapurse
|
||||
= "#{number_to_currency metapurse.total_usd} USD"
|
||||
-# %td= metapurse.data
|
||||
- if policy(metapurse).update?
|
||||
-# %td= link_to 'Show', metapurse
|
||||
|
||||
@ -23,15 +23,18 @@
|
||||
- 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
|
||||
ICP
|
||||
*
|
||||
- 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
|
||||
= 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 = icpswap_pool_data_raw.split("record {").select{|pool| !pool.scan(canister_id).empty? && !pool.scan(icp_canister_id).empty? }[0].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
|
||||
@ -43,4 +46,5 @@
|
||||
- if policy(@metapurse).edit?
|
||||
= link_to 'Edit', edit_metapurse_path(@metapurse)
|
||||
\|
|
||||
= link_to 'View data', data_metapurse_path(@metapurse)
|
||||
= link_to 'Back', metapurses_path
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :metapurses
|
||||
resources :metapurses do
|
||||
member do
|
||||
get 'data'
|
||||
end
|
||||
end
|
||||
get '/games', to: "visitors#games_index"
|
||||
resources :rooms do
|
||||
resources :messages
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user