eos 區塊鏈採坑,scatter 錢包

作個有理想的碼農 ,坑定的有多少的瞭解下當下最火的技術,提到區塊鏈 不懂也的 瞭解下。
有人說如今的區塊鏈哀聲遍野,在這個互聯網寒冬下,有多少的區塊鏈公司,在這個寒冬倒下,如今的各類的 數字貨幣,炒幣的,挖礦的的,,烏煙瘴氣。。。。。json

進入正題,最近了解下 eos 寫了 而後調用 scatter錢包時個各類各樣的的錯,屬實讓人頭疼,這裏我 我總結下了,給本身留個筆記瀏覽器

import iView from 'iview';
import i18n from '@/locale'
import config from "@/config"
import ScatterJS from 'scatterjs-core'
import ScatterEOS from "scatterjs-plugin-eosjs"
import Eos from 'eosjs';
import {GetQueryString} from "../libs/geturl";
window.ScatterJS = null;

// const eosconfig =process.env.NODE_ENV === 'development' ? config.eosConfig.dev : config.eosConfig.pro;

const eosconfig = GetQueryString("dev") != "true" ? config.eosConfig.pro : config.eosConfig.dev;


class getscatter {
    static account;
    static eos;
    static  scatter;
    static instance;


    constructor() {

    }

    ScatterSecureCall(func) {
        const network = ScatterJS.Network.fromJson(eosconfig);
        var app_name = 'dada_poker';

        if (ScatterJS.scatter.eos == null) {
            ScatterJS.plugins(new ScatterEOS());
        }
        ScatterJS.scatter.connect(app_name).then(connected => {
            if (connected) {
                const scatter = ScatterJS.scatter;
                scatter.getIdentity({
                    accounts: [network]
                }).then(() => {
                    const scatter = ScatterJS.scatter;
                    const account = scatter.identity.accounts.find(x => x.blockchain === 'eos');
                    const eosOptions = {
                        expireInSeconds: 60
                    };
                    const eos = scatter.eos(network, Eos, eosOptions);
                    func(scatter, account, eos);

                }).catch(error => {
                    // document.getElementById("box").innerHTML = format_json(error);
                    console.log(error);
                    iView.Message.error({
                        top: 70,
                        content: JSON.stringify(error),
                        duration: 10
                    });
                });
            } else {
                console.log("The Scatter interface call failed! Please install the Scatter plugin or launch the Scatter app");
                // document.getElementById("box").innerHTML = "Scatter接口調用失敗!請安裝Scatter插件或者啓動Scatter應用程序。";
                iView.Message.error({
                    top: 70,
                    content: i18n.t('login_error'),
                    duration: 10
                });
            }
        })
    }

    eosGetIdentity(callback) {
        console.log('enter eosGetIdentity');
        this.ScatterSecureCall((scatter, account, eos) => {
            console.log('[eosGetIdentity] account = ', account);
                this.account = account;
                this.eos = eos;
                this.scatter =scatter
                callback && callback(scatter, this.account, this.eos);
        });
    }

    eosTransfer(to, quantity, memo, callback) {
        if (this.account == null || this.eos == null) {
            console.log("you must eosGetIdentity");
            this.eosGetIdentity(() => {
                this.eosTransfer(to, quantity, memo, callback);
            });
        } else {
            this.eos.contract("eosio.token").then(contract => {
                const transactionOptions = {authorization: [`${this.account.name}@${this.account.authority}`]};
                contract.transfer(this.account.name, to, quantity, memo, transactionOptions).then(transaction => {
                    console.log("eosTransfer transaction processed object: %d", transaction.processed);
                    if (null != callback) {
                        callback(transaction);
                    }
                }).catch(error => {
                    if (typeof error == 'string') {
                        error = JSON.parse(error);
                        let errorMsg = error.error.details[0].message;
                        iView.Message.error({
                            top: 70,
                            content: errorMsg,
                            duration: 10
                        });
                        // alert(errorMsg); //todo
                    }
                    iView.Message.error({
                        top: 70,
                        content: error,
                        duration: 10
                    });
                });
            }).catch(error => {
                console.log(error);
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            });
        }
    }

    eosRedeem(quantity, to, callback) {
        if (this.account == null || this.eos == null) {
            console.log("you must eosGetIdentity");
            this.eosGetIdentity(() => {
                this.eosRedeem(quantity, memo, callback);
            });
        } else {
            this.eos.contract(to).then(contract => {
                const transactionOptions = {
                    authorization: [`${this.account.name}@${this.account.authority}`]
                };
                contract.redeem(this.account.name, quantity, transactionOptions).then(transaction => {
                    console.log("eosTransfer transaction processed object: ", transaction.processed);
                    if (null != callback) {
                        callback(transaction);
                    }
                }).catch(error => {
                    if (typeof error == "string") {
                        error = JSON.parse(error);
                        let errorMsg = error.error.details[0].message;
                        iView.Message.error({
                            top: 70,
                            content: errorMsg,
                            duration: 10
                        });

                    }
                    console.log(error);
                    iView.Message.error({
                        top: 70,
                        content: error,
                        duration: 10
                    });
                })
            }).catch(error => {
                console.log(error);
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            });
        }
    }

    eosLogin(callback) {
        if (this.account == null || this.eos == null) {
            console.log(11);
            this.eosGetIdentity(() => {
                console.log(this.account);
                this.eosLogin(callback);
            });
        } else {
            ScatterJS.login().then(res => {
                console.log(res);
                callback(res, this.account)
            }).catch((error) => {
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            })
        }
    }

    eosLogout(callback) {
        if (this.account == null || this.eos == null) {
            this.eosGetIdentity(() => {
                this.eosLogout(callback)
            })
        } else {
            ScatterJS.logout().then(res => {
                try{//App
                    this.scatter.isConnected()
                    this.account = null;
                    this.eos = null;
                }catch (e) {//瀏覽器
                    // throw  new Error("瀏覽器插件")
                    console.log(e);

                }
                callback && callback(res)
                window.ScatterJS = null;
            }).catch(error => {
                iView.Message.error({
                    top: 70,
                    content: error,
                    duration: 10
                });
            })
        }
    }


}

const ScatterSecureCall = new getscatter();

export {
    // ScatterJS,
    ScatterSecureCall
}
相關文章
相關標籤/搜索