Add basic token canister parsing

This commit is contained in:
TheNeedful-DO 2023-11-11 14:18:38 +00:00
parent 413bcf3540
commit f00e6b93f7
9 changed files with 100 additions and 46 deletions

View File

@ -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

View File

@ -0,0 +1,5 @@
.token-index
font-family: "monospace"
.token-logo
max-width: 100%

View File

@ -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/

View File

@ -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)

View File

@ -1,3 +1,4 @@
class Token < ActiveRecord::Base
enum standard: [:dip20, :icrc1, :unknown]
validates :canister_id, presence: true, uniqueness: true
end

View File

@ -53,4 +53,7 @@ class TokenPolicy
@user.admin?
end
def refresh?
true
end
end

View File

@ -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 "<img class='token-logo' src=#{token.logo_url} />"
%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?' }

View File

@ -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 "<img class='token-logo' src=#{@token.logo_url} />"
%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

View File

@ -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