From f00e6b93f76ea409f9a02accad6438e49dee6ae1 Mon Sep 17 00:00:00 2001 From: TheNeedful-DO Date: Sat, 11 Nov 2023 14:18:38 +0000 Subject: [PATCH] Add basic token canister parsing --- app/assets/stylesheets/application.css.sass | 9 ++- app/assets/stylesheets/tokens.sass | 5 ++ app/assets/stylesheets/tokens.scss | 3 - app/controllers/tokens_controller.rb | 32 +++++++++- app/models/token.rb | 1 + app/policies/token_policy.rb | 3 + app/views/tokens/index.html.haml | 69 +++++++++++---------- app/views/tokens/show.html.haml | 18 +++--- config/routes.rb | 6 +- 9 files changed, 100 insertions(+), 46 deletions(-) create mode 100644 app/assets/stylesheets/tokens.sass delete mode 100644 app/assets/stylesheets/tokens.scss diff --git a/app/assets/stylesheets/application.css.sass b/app/assets/stylesheets/application.css.sass index afb00758..10ecfee1 100644 --- a/app/assets/stylesheets/application.css.sass +++ b/app/assets/stylesheets/application.css.sass @@ -114,8 +114,9 @@ header *, *:before, *:after box-sizing: inherit - font-family: Impact, Anton, fantasy, sans-serif font-weight: lighter +body + font-family: Impact, Anton, fantasy, sans-serif .eyecandy display: block left: 0 @@ -224,5 +225,11 @@ textarea padding: 0 1em margin: 0 auto +.table-container + max-width: 100% + overflow-x: auto + align-self: start + @import doodads @import profiles +@import tokens diff --git a/app/assets/stylesheets/tokens.sass b/app/assets/stylesheets/tokens.sass new file mode 100644 index 00000000..34a2f75d --- /dev/null +++ b/app/assets/stylesheets/tokens.sass @@ -0,0 +1,5 @@ +.token-index + font-family: "monospace" + +.token-logo + max-width: 100% diff --git a/app/assets/stylesheets/tokens.scss b/app/assets/stylesheets/tokens.scss deleted file mode 100644 index 47b8ce4a..00000000 --- a/app/assets/stylesheets/tokens.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the Tokens controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/tokens_controller.rb b/app/controllers/tokens_controller.rb index abd422c8..ddf0b67f 100644 --- a/app/controllers/tokens_controller.rb +++ b/app/controllers/tokens_controller.rb @@ -1,5 +1,5 @@ class TokensController < ApplicationController - before_action :set_token, only: %i[ show edit update destroy ] + before_action :set_token, only: %i[ show edit update destroy refresh ] # GET /tokens or /tokens.json def index @@ -21,6 +21,36 @@ class TokensController < ApplicationController authorize @token end + def refresh + authorize @token + # if call icrc1_metadata #icrc1 + metadata = `dfx canister --network ic call #{@token.canister_id} icrc1_metadata 2>&1` + if metadata.scan(/no update method/).present? + metadata = `dfx canister --network ic call #{@token.canister_id} getMetadata 2>&1` + if metadata.scan(/no update method/).present? + @token.standard = :unknown + else + @token.standard = :dip20 + @token.transfer_fee = metadata.lines[2].split[2] + @token.decimals = metadata.lines[3].split[2] + # @token.owner = metadata.lines[4].split[3] + @token.logo_url = metadata.lines[5].split[2] + end + else + @token.standard = :icrc1 + end + # elsif call getMetadata #dip20 + respond_to do |format| + if @token.save + format.html { redirect_to token_url(@token), notice: "Token was successfully refreshed." } + format.json { render :show, status: :created, location: @token } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @token.errors, status: :unprocessable_entity } + end + end + end + # POST /tokens or /tokens.json def create authorize @token = Token.new(token_params) diff --git a/app/models/token.rb b/app/models/token.rb index db919465..43142ce4 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -1,3 +1,4 @@ class Token < ActiveRecord::Base + enum standard: [:dip20, :icrc1, :unknown] validates :canister_id, presence: true, uniqueness: true end diff --git a/app/policies/token_policy.rb b/app/policies/token_policy.rb index eb3e10c8..62fb4559 100644 --- a/app/policies/token_policy.rb +++ b/app/policies/token_policy.rb @@ -53,4 +53,7 @@ class TokenPolicy @user.admin? end + def refresh? + true + end end diff --git a/app/views/tokens/index.html.haml b/app/views/tokens/index.html.haml index e69cfb22..0686854f 100644 --- a/app/views/tokens/index.html.haml +++ b/app/views/tokens/index.html.haml @@ -1,37 +1,40 @@ -%h1 Listing tokens +.contain + %h1 Listing tokens -- if policy(Token).new? - = link_to 'Add Token', new_token_path + %div{style: "align-self: start;"} + - if policy(Token).new? + = link_to 'Add Token', new_token_path -%table - %thead - %tr - %th Logo - %th Canister - %th Standard - %th Symbol - %th Name - %th Total supply - %th Decimals - %th Transfer fee - - if policy(@tokens).update? - %th - %th - %th - - %tbody - - @tokens.each do |token| +.table-container + %table.token-index + %thead %tr - %td= token.logo_url - %td= token.canister_id - %td= token.standard - %td= token.symbol - %td= token.name - %td= token.total_supply - %td= token.decimals - %td= token.transfer_fee - - if policy(token).update? - %td= link_to 'Show', token - %td= link_to 'Edit', edit_token_path(token) - %td= link_to 'Destroy', token, method: :delete, data: { confirm: 'Are you sure?' } + %th Logo + %th Canister + %th Standard + %th Symbol + %th Name + %th Total supply + %th Decimals + %th Transfer fee + - if policy(@tokens).update? + %th + %th + %th + + %tbody + - @tokens.each do |token| + %tr + %td= raw "" + %td= link_to token.canister_id, token + %td= token.standard + %td= token.symbol + %td= token.name + %td= token.total_supply + %td= token.decimals + %td= token.transfer_fee + - if policy(token).update? + %td= link_to 'Show', token + %td= link_to 'Edit', edit_token_path(token) + %td= link_to 'Destroy', token, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/app/views/tokens/show.html.haml b/app/views/tokens/show.html.haml index c7c5dbd1..bd69295f 100644 --- a/app/views/tokens/show.html.haml +++ b/app/views/tokens/show.html.haml @@ -1,11 +1,18 @@ -%p#notice= notice -%p - %b Canister: - = @token.logo_url +- if policy(@token).edit? + = link_to 'Edit', edit_token_path(@token) + += link_to "Refresh Data", refresh_token_path(@token) + %p %b Canister: = @token.canister_id +%p + %b Logo: + - if @token.logo_url.present? + -# %img{src: @token.logo_url} + -# :escaped + =raw "" %p %b Standard: = @token.standard @@ -25,6 +32,3 @@ %b Transfer fee: = @token.transfer_fee -= link_to 'Edit', edit_token_path(@token) -\| -= link_to 'Back', tokens_path diff --git a/config/routes.rb b/config/routes.rb index 37fa3f85..d0c57335 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,11 @@ Rails.application.routes.draw do resources :social_links resources :profiles - resources :tokens + resources :tokens do + member do + get 'refresh' + end + end resources :projects resources :doodads resources :canisters