thirteen-tien-len/app/controllers/tokens_controller.rb

136 lines
4.7 KiB
Ruby

class TokensController < ApplicationController
before_action :set_token, only: %i[ show edit update destroy refresh launch]
# GET /tokens or /tokens.json
def index
authorize @tokens = Token.all
@mytokens = Token.where(ownable_id: current_user.soft_token)
end
# GET /tokens/1 or /tokens/1.json
def show
authorize @token
end
# GET /tokens/new
def new
authorize @token = Token.new(ownable_id: current_user.soft_token)
end
def add_existing
authorize @token = Token.new
end
# GET /tokens/1/edit
def edit
authorize @token
end
def launch
authorize @token
redirect_to @token, notice: "Launch function coming soon!"
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?
# TODO: Use getTokenInfo instead of getMetadata
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
# TODO: Use getTokenInfo instead of getMetadata
tokenInfo = `dfx canister --network ic call #{@token.canister_id} getTokenInfo 2>&1`
@token.holder_number = tokenInfo.lines[2].split[2]
@token.transfer_fee = metadata.lines[2].split[2].to_i
@token.decimals = metadata.lines[3].split[2]
# @token.owner = metadata.lines[4].split[3]
# @token.logo_url = metadata.lines[5].split("\"")[1]
@token.logo_url = tokenInfo.lines[8].split[2].split('"')[1]
@token.name = metadata.lines[6].split("\"")[1]
@token.total_supply = metadata.lines[7].split[2].to_i/(10.0**@token.decimals)
@token.symbol = metadata.lines[8].split("\"")[1]
end
else
@token.standard = :icrc1
@token.transfer_fee = metadata.lines[2].split[7].to_i
@token.decimals = metadata.lines[5].split[7].to_i
begin
@token.logo_url = metadata.lines[9].split("\"")[1]
rescue
end
@token.name = metadata.lines[3].split("\"")[3]
total_supply = `dfx canister --network ic call #{@token.canister_id} icrc1_total_supply 2>&1`
@token.total_supply = total_supply.split[0][1..-1].to_i/(10.0**@token.decimals)
@token.symbol = metadata.lines[4].split("\"")[3]
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)
@token.canister_id = nil if @token.canister_id.blank?
respond_to do |format|
if @token.save
format.html { redirect_to (request.referrer.split("/")[-1].match("add_existing") ? refresh_token_url(@token) : @token), notice: "Token was successfully created." }
format.json { render :show, status: :created, location: @token }
else
format.html { render (request.referrer.split("/")[-1].match("add_existing") ? :add_existing : :new), status: :unprocessable_entity }
format.json { render json: @token.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tokens/1 or /tokens/1.json
def update
authorize @token
@token.canister_id = nil if @token.canister_id.blank?
respond_to do |format|
if @token.update(token_params)
format.html { redirect_to token_url(@token), notice: "Token was successfully updated." }
format.json { render :show, status: :ok, location: @token }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @token.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tokens/1 or /tokens/1.json
def destroy
authorize @token
@token.destroy
respond_to do |format|
format.html { redirect_to tokens_url, notice: "Token was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_token
@token = Token.find(params[:id])
end
# Only allow a list of trusted parameters through.
def token_params
params.require(:token).permit(:canister_id, :standard, :symbol, :name, :total_supply, :decimals, :transfer_fee, :logo_url, :ownable_id)
end
end