做个有理想的码农 ,坑定的有多少的了解下当下最火的技术,提到区块链 不懂也的 了解下。
有人说现在的区块链哀声遍野,在这个互联网寒冬下,有多少的区块链公司,在这个寒冬倒下,现在的各种的 数字货币,炒币的,挖矿的的,,乌烟瘴气。。。。。
进入正题,最近了解下 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
}