remove unused code
This commit is contained in:
parent
bd152be292
commit
fa6af8b1e7
1
components.d.ts
vendored
1
components.d.ts
vendored
@ -35,7 +35,6 @@ declare module 'vue' {
|
||||
Gold: typeof import('./src/components/common/searchView/gold.vue')['default']
|
||||
Hanging: typeof import('./src/components/assets/hanging.vue')['default']
|
||||
HangingCard: typeof import('./src/components/common/hangingCard.vue')['default']
|
||||
'HangingCard copy': typeof import('./src/components/common/hangingCard copy.vue')['default']
|
||||
Hero: typeof import('./src/components/common/searchView/hero.vue')['default']
|
||||
HeroCard: typeof import('./src/components/home/HeroCard.vue')['default']
|
||||
HeroWeaponChipSelector: typeof import('./src/components/home/HeroWeaponChipSelector.vue')['default']
|
||||
|
@ -1,48 +0,0 @@
|
||||
import Web3 from 'web3'
|
||||
import { useAppStore } from '@/store/app'
|
||||
import { ERC20ABI } from '@/configs/contracts'
|
||||
import pinia from '@/store';
|
||||
|
||||
const AppModule = useAppStore(pinia);
|
||||
|
||||
export class Chain {
|
||||
private web3: Web3
|
||||
|
||||
constructor(provider: any) {
|
||||
this.web3 = new Web3(provider)
|
||||
}
|
||||
|
||||
public async initInstance({ abi, address, account }: {abi: any, address: string, account: string}) {
|
||||
return new this.web3.eth.Contract(
|
||||
abi,
|
||||
address,
|
||||
{ from: account }
|
||||
)
|
||||
}
|
||||
|
||||
public async initContractInstance(address: string, abi: any = ERC20ABI) {
|
||||
return this.initInstance({
|
||||
abi,
|
||||
address,
|
||||
account: AppModule.accountId
|
||||
})
|
||||
}
|
||||
|
||||
public async sendCmd({ method, params, from }: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
this.web3.currentProvider.send({
|
||||
method,
|
||||
params,
|
||||
from
|
||||
}, async function(err: any, result: any) {
|
||||
if (err) {
|
||||
reject && reject(err)
|
||||
return
|
||||
}
|
||||
resolve && resolve(result)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@ -1,278 +0,0 @@
|
||||
import { Blockchain } from "@/chain/blockchain";
|
||||
import { getNonce } from "@/api/User";
|
||||
import pinia from "@/store";
|
||||
import { useAppStore } from "@/store/app";
|
||||
import { useUserStore } from "@/store/user";
|
||||
import { message } from "ant-design-vue";
|
||||
import { Chain } from "@/chain/Chain";
|
||||
import { AVAILABLE_CHAINS } from "@/configs/configchain";
|
||||
import { AllChains } from "@/configs/allchain";
|
||||
import {
|
||||
ACTIVATE_PROXY_ABI,
|
||||
MYSTERY_BOX_ABI,
|
||||
MYSTERY_PROXY_ABI
|
||||
} from "@/configs/contracts";
|
||||
|
||||
const AppModule = useAppStore(pinia);
|
||||
const UserModule = useUserStore();
|
||||
|
||||
export default class ChainManager {
|
||||
bc: Blockchain;
|
||||
instanceMap: Map<string, any>;
|
||||
public chainMap: Map<number, any> = new Map();
|
||||
private _availableChains: Map<number, any> = new Map();
|
||||
|
||||
constructor() {
|
||||
this.bc = new Blockchain();
|
||||
this.instanceMap = new Map();
|
||||
for (const data of AllChains) {
|
||||
this.chainMap.set(data.id, data);
|
||||
}
|
||||
}
|
||||
|
||||
get availableChains() {
|
||||
if (this._availableChains.size === 0) {
|
||||
for (const id of AVAILABLE_CHAINS) {
|
||||
const d = this.chainMap.get(id);
|
||||
if (d) {
|
||||
this._availableChains.set(id, d);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._availableChains;
|
||||
}
|
||||
|
||||
public async init() {
|
||||
if (this.bc.isWalletConnect) {
|
||||
try {
|
||||
await this.bc.connect();
|
||||
} catch (err) {
|
||||
console.log("connect chain error: ", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get isLogined() {
|
||||
return !!UserModule.token && !!AppModule.step;
|
||||
}
|
||||
|
||||
public async logout() {
|
||||
await this.bc.disconnect();
|
||||
}
|
||||
|
||||
public get currentChain() {
|
||||
return this.bc.currentChain;
|
||||
}
|
||||
|
||||
public async login(type:any) {
|
||||
if (!AppModule.step) {
|
||||
try {
|
||||
await this.bc.connect(true,type);
|
||||
await this.checkNance();
|
||||
} catch (err) {
|
||||
message.error(err.message, 5);
|
||||
this.bc.walletType = 0
|
||||
await this.bc.disconnect();
|
||||
await Promise.reject(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async checkNance() {
|
||||
try {
|
||||
const preRequest: any = await getNonce();
|
||||
let nonce = preRequest.nonce + "";
|
||||
const tips = preRequest.data?.tips;
|
||||
AppModule.updateNonce(nonce);
|
||||
|
||||
await UserModule.Login({
|
||||
bcInstance: this.bc,
|
||||
account: AppModule.accountId,
|
||||
chainId: AppModule.chainId,
|
||||
nonce,
|
||||
tips,
|
||||
});
|
||||
AppModule.updateStep(1);
|
||||
} catch (err) {
|
||||
this.bc.walletType = 0
|
||||
console.log(err);
|
||||
await Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async getInstance(address: string, chainId: number, abi?: any) {
|
||||
const key = `${chainId}_${address}`;
|
||||
if (!this.instanceMap.has(key)) {
|
||||
const chain = new Chain(this.chainMap.get(chainId)!.rpc);
|
||||
const coinInstance = await chain.initContractInstance(address, abi);
|
||||
this.instanceMap.set(key, coinInstance);
|
||||
}
|
||||
return this.instanceMap.get(key);
|
||||
}
|
||||
|
||||
public async getBalance(address: string, chainId: number) {
|
||||
const coinInstance = await this.getInstance(address, chainId);
|
||||
const balance = await coinInstance.methods
|
||||
.balanceOf(AppModule.accountId)
|
||||
.call();
|
||||
console.log("balance: ", balance);
|
||||
return balance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get amount of mystery boxes
|
||||
* this method can get amount of general erc721 also
|
||||
* @param {string} address
|
||||
* @param {number} chainId
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
public async getNftBalance(address: string, chainId: number) {
|
||||
const coinInstance = await this.getInstance(
|
||||
address,
|
||||
chainId,
|
||||
MYSTERY_BOX_ABI
|
||||
);
|
||||
const balance = await coinInstance.methods
|
||||
.balanceOf(AppModule.accountId)
|
||||
.call();
|
||||
console.log("nft balance: ", balance);
|
||||
return balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get NFT list of current user
|
||||
* @param {string} address NFT address
|
||||
* @param {number} chainId chain id
|
||||
* @param {number} start
|
||||
* @param {number} page
|
||||
*/
|
||||
public async getNftList(
|
||||
address: string,
|
||||
chainId: number,
|
||||
start = 0,
|
||||
page = 8
|
||||
) {
|
||||
const nftInstance = await this.getInstance(
|
||||
address,
|
||||
chainId,
|
||||
MYSTERY_BOX_ABI
|
||||
);
|
||||
return nftInstance.methods
|
||||
.userTokens(AppModule.accountId, start, page)
|
||||
.call();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get NFT of current user with index
|
||||
* @param {string} address
|
||||
* @param {number} chainId
|
||||
* @param {number} index
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
public async getNftIdOfIndex(
|
||||
address: string,
|
||||
chainId: number,
|
||||
index: number
|
||||
) {
|
||||
const nftInstance = await this.getInstance(
|
||||
address,
|
||||
chainId,
|
||||
MYSTERY_BOX_ABI
|
||||
);
|
||||
const nftId = await nftInstance.methods
|
||||
.tokenOfOwnerByIndex(AppModule.accountId, index)
|
||||
.call();
|
||||
console.log(
|
||||
`address: ${address}, chainId: ${chainId}, index: ${index}, token: ${nftId}`
|
||||
);
|
||||
return nftId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open one mystery box
|
||||
* @param {string} address address of mystery box contract on current chain
|
||||
* @param {string} boxId nftid of mystery box
|
||||
* @param {number[]} tokenIds
|
||||
* @param {string} nonce
|
||||
* @param {string} signature
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
public async openMysteryBox(
|
||||
address: string,
|
||||
boxId: string,
|
||||
tokenIds: number[],
|
||||
nonce: string,
|
||||
signature: string
|
||||
) {
|
||||
const proxyInstance = await this.bc.getContractInstance(
|
||||
address,
|
||||
MYSTERY_PROXY_ABI
|
||||
);
|
||||
// get transactionHash and upload to server for verify
|
||||
return proxyInstance.methods
|
||||
.openBox(boxId, tokenIds, nonce, signature)
|
||||
.send({ gas: 1000000 });
|
||||
}
|
||||
|
||||
/**
|
||||
* activate one nft with 18 digital id
|
||||
* @param {string} address
|
||||
* @param {string} nftOld
|
||||
* @param {string} nftNew
|
||||
* @param {number} nftType
|
||||
* @param {string} nonce
|
||||
* @param {string} signature
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
public async activateOneNft(
|
||||
address: string,
|
||||
nftOld: string,
|
||||
nftNew: string,
|
||||
nftType: number,
|
||||
nonce: string,
|
||||
signature: string
|
||||
) {
|
||||
const nftProxyInstance = await this.bc.getContractInstance(
|
||||
address,
|
||||
ACTIVATE_PROXY_ABI
|
||||
);
|
||||
const gas = await nftProxyInstance.methods
|
||||
.activateOne(nftOld, nftNew, nftType, nonce, signature)
|
||||
.estimateGas({ gas: 1000000 });
|
||||
console.log("nftProxyInstance activateOne need gas: ", gas);
|
||||
return nftProxyInstance.methods
|
||||
.activateOne(nftOld, nftNew, nftType, nonce, signature)
|
||||
.send({ gas: (gas * 1.1) | 0 });
|
||||
}
|
||||
|
||||
public async transferToAccount({
|
||||
to,
|
||||
amount,
|
||||
chainId,
|
||||
address,
|
||||
}: {
|
||||
to: string;
|
||||
amount: number;
|
||||
chainId: number;
|
||||
address: string;
|
||||
}) {
|
||||
const self = this;
|
||||
if (chainId !== this.bc.currentChain) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bc.switchEthereumChain(chainId, function () {
|
||||
self.bc
|
||||
.transferToAccount(to, amount, address)
|
||||
.then((res) => {
|
||||
resolve && resolve(res);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject && reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return this.bc.transferToAccount(to, amount, address);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,729 +0,0 @@
|
||||
import WalletConnectProvider from "@walletconnect/web3-provider";
|
||||
import { useAppStore } from "@/store/app";
|
||||
import Web3 from "web3";
|
||||
import { AVAILABLE_CHAINS, CONTRACT_ADDRESS } from "@/configs/configchain";
|
||||
import { useUserStore } from "@/store/user";
|
||||
import { isMobile } from "@/utils/resize";
|
||||
import { hasMetamask, toHexChainId } from "@/utils/chain.util";
|
||||
import { AllChains } from "@/configs/allchain";
|
||||
import { ERC20ABI, CLAIM_ABI, BENFTMALL_ABI } from "@/configs/contracts";
|
||||
import { TransactionReceipt } from "web3-core";
|
||||
import { useEventBus } from "@vueuse/core";
|
||||
import { Global } from "@/configs/global";
|
||||
import * as bus_event from "@/bus/event";
|
||||
import pinia from "@/store";
|
||||
|
||||
const EIP721_DOMAIN_DATA = [
|
||||
{ name: "name", type: "string" },
|
||||
{ name: "version", type: "string" },
|
||||
{ name: "chainId", type: "uint256" },
|
||||
{ name: "verifyingContract", type: "address" },
|
||||
];
|
||||
|
||||
const CACHE_KEY = "cebg_chain_cache_key";
|
||||
|
||||
const busShowChainModal = useEventBus(bus_event.SHOW_CHAIN_MODAL);
|
||||
const busNeedChangeChain = useEventBus(bus_event.NEED_CHANGE_CHAIN);
|
||||
const busNeedLogin = useEventBus(bus_event.NEED_LOGIN);
|
||||
const busNeedNonce = useEventBus(bus_event.NEED_NONCE);
|
||||
|
||||
const AppModule = useAppStore(pinia);
|
||||
const UserModule = useUserStore(pinia);
|
||||
|
||||
export class Blockchain {
|
||||
provider: any;
|
||||
web3: Web3;
|
||||
currentChain = 0;
|
||||
// 0: null, 1: metamask, 2: walletconnect
|
||||
walletType = 0;
|
||||
dataCached = false;
|
||||
instanceCacheMap: Map<string, any>;
|
||||
public chainMap: Map<number, any> = new Map();
|
||||
public rpc: any = {};
|
||||
|
||||
constructor() {
|
||||
const allChainMap: Map<number, any> = new Map();
|
||||
for (const d of AllChains) {
|
||||
const id = d.id;
|
||||
this.rpc[id] = d.rpc;
|
||||
allChainMap.set(id, d);
|
||||
}
|
||||
for (const id of AVAILABLE_CHAINS) {
|
||||
this.chainMap.set(id, allChainMap.get(id)!);
|
||||
}
|
||||
this.loadCachedProvider();
|
||||
this.instanceCacheMap = new Map();
|
||||
// AppModule.updateChainID(chainId)
|
||||
busNeedLogin.on(this.connect.bind(this));
|
||||
}
|
||||
|
||||
get isWalletConnect() {
|
||||
return !!this.walletType && !!this.currentChain;
|
||||
}
|
||||
|
||||
get mallAddress() {
|
||||
return CONTRACT_ADDRESS[this.currentChain]?.mall;
|
||||
}
|
||||
|
||||
public get hexChainId() {
|
||||
return toHexChainId(this.currentChain);
|
||||
}
|
||||
|
||||
public async chainSelected(id: number) {
|
||||
if (!this.chainMap.has(id)) {
|
||||
return;
|
||||
}
|
||||
this.currentChain = id;
|
||||
if (this.provider) {
|
||||
await this.switchEthereumChain();
|
||||
} else {
|
||||
await this.connectWallet(true);
|
||||
}
|
||||
}
|
||||
|
||||
public async connectWallet(isManual: boolean) {
|
||||
console.log(
|
||||
"begin connect to wallet: ",
|
||||
this.walletType,
|
||||
this.currentChain
|
||||
);
|
||||
if (this.walletType === 1) {
|
||||
this.provider = await this.connectMetaMask();
|
||||
} else if (this.walletType === 2) {
|
||||
this.provider = await this.connectokxwallet();
|
||||
}
|
||||
if (!this.provider) {
|
||||
return;
|
||||
}
|
||||
this.web3 = new Web3(this.provider);
|
||||
|
||||
const chainId = await this.web3.eth.getChainId();
|
||||
await this.checkChain(chainId);
|
||||
|
||||
this.subscribeToEvents();
|
||||
const accounts = await this.web3.eth.getAccounts();
|
||||
if (accounts && accounts.length > 0) {
|
||||
AppModule.updateAccount(accounts[0]);
|
||||
}
|
||||
if (!this.currentChain) this.currentChain = chainId;
|
||||
this.saveProvider();
|
||||
AppModule.updateChainID(chainId);
|
||||
AppModule.updateWalletStatus(true);
|
||||
|
||||
console.log("current login chain: ", chainId);
|
||||
console.log("accountsLogin: ", accounts, AppModule.accountId);
|
||||
if (isManual) {
|
||||
busNeedNonce.emit(busNeedNonce);
|
||||
} else {
|
||||
AppModule.updateStep(1);
|
||||
}
|
||||
return { account: accounts[0], chainId };
|
||||
}
|
||||
|
||||
/**
|
||||
* check whether special chainId is supported by config
|
||||
* show chain picker when chainId is not supported and current wallet is meatmask
|
||||
* show message box when chainId is not supported and current wallet is wallet connect
|
||||
* @param {number} chainId
|
||||
* @return {Promise<void>}
|
||||
* @private
|
||||
*/
|
||||
private async checkChain(chainId: number) {
|
||||
if (chainId !== AVAILABLE_CHAINS[0]) {
|
||||
// if (this.walletType === 1) {
|
||||
try {
|
||||
await this.selectChain();
|
||||
} catch (err) {
|
||||
await this.disconnect();
|
||||
}
|
||||
// } else if (this.walletType === 2) {
|
||||
// await this.disconnect()
|
||||
// this.walletType = 2
|
||||
// await MessageBox.alert(
|
||||
// 'You need to connect to supported network',
|
||||
// 'Wrong Network',
|
||||
// {
|
||||
// confirmButtonText: 'Confirm'
|
||||
// })
|
||||
// await this.connectWallet(true)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public async connect(isManual = false, type) {
|
||||
if (isMobile()) {
|
||||
if (type == 1) {
|
||||
if (!hasMetamask()) {
|
||||
var next = window.location.href;
|
||||
var url = `https://metamask.app.link/dapp/${next}`;
|
||||
location.href = url;
|
||||
} else {
|
||||
this.walletType = 1;
|
||||
}
|
||||
} else if (type == 2) {
|
||||
const ua = navigator.userAgent;
|
||||
const isIOS = /iphone|ipad|ipod|ios/i.test(ua);
|
||||
const isAndroid = /android|XiaoMi|MiuiBrowser/i.test(ua);
|
||||
const isMobile = isIOS || isAndroid;
|
||||
const isOKApp = /OKApp/i.test(ua);
|
||||
|
||||
if (isMobile && isOKApp) {
|
||||
this.walletType = 2;
|
||||
} else {
|
||||
var next = window.location.href;
|
||||
window.open(`okx://wallet/dapp/details?dappUrl=${next}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// debugger
|
||||
if (type == 1) {
|
||||
if (hasMetamask()) {
|
||||
if (isManual && !this.walletType) {
|
||||
this.walletType = 1;
|
||||
}
|
||||
} else {
|
||||
this.walletType = 1;
|
||||
}
|
||||
} else if (type == 2) {
|
||||
if (typeof window.okxwallet !== "undefined") {
|
||||
if (isManual && !this.walletType) {
|
||||
this.walletType = 2;
|
||||
}
|
||||
} else {
|
||||
this.walletType = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this.walletType = 1;
|
||||
if (isManual) {
|
||||
await this.connectWallet(isManual);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* show wallet picker
|
||||
* @return {Promise<number>}
|
||||
* @private
|
||||
*/
|
||||
private selectWallet(): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
busShowChainModal.emit({
|
||||
confirm: (id: number) => {
|
||||
console.log("select wallet: ", id);
|
||||
resolve && resolve(id);
|
||||
},
|
||||
cancel: (reason: any) => {
|
||||
console.log("cancel select wallet: ", reason);
|
||||
reject && reject(reason);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* show chain picker
|
||||
* @return {Promise<number>}
|
||||
* @private
|
||||
*/
|
||||
private selectChain(): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.switchEthereumChain(AVAILABLE_CHAINS[0], (res: any) => {
|
||||
if (res.err) {
|
||||
reject && reject(res.err);
|
||||
return;
|
||||
}
|
||||
resolve && resolve(res.chain);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public async connectMetaMask() {
|
||||
let provider = null;
|
||||
/// debugger
|
||||
if (typeof window.ethereum !== "undefined") {
|
||||
provider = window.ethereum;
|
||||
try {
|
||||
await provider.request({ method: "eth_requestAccounts" });
|
||||
} catch (error: any) {
|
||||
if (error.code === -32002) {
|
||||
// const hexChainId = toHexChainId(this.currentChain)
|
||||
// await provider.request({
|
||||
// method: 'wallet_switchEthereumChain',
|
||||
// params: [{ chainId: hexChainId }]
|
||||
// })
|
||||
throw new Error("MeatMask not login, Open MeatMask and login first");
|
||||
} else {
|
||||
throw new Error("User Rejected");
|
||||
}
|
||||
}
|
||||
} else if (window.web3) {
|
||||
provider = window.web3.currentProvider;
|
||||
} else if (window.celo) {
|
||||
provider = window.celo;
|
||||
} else {
|
||||
this.walletType = 0;
|
||||
|
||||
setTimeout(function () {
|
||||
window.open("https://metamask.io/download/", "_blank");
|
||||
}, 2200);
|
||||
throw new Error("No Web3 Provider found");
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public async connectokxwallet() {
|
||||
let provider = null;
|
||||
if (typeof window.okxwallet !== "undefined") {
|
||||
provider = window.okxwallet;
|
||||
try {
|
||||
await provider.request({ method: "eth_requestAccounts" });
|
||||
} catch (error: any) {
|
||||
if (error.code === -32002) {
|
||||
// const hexChainId = toHexChainId(this.currentChain)
|
||||
// await provider.request({
|
||||
// method: 'wallet_switchEthereumChain',
|
||||
// params: [{ chainId: hexChainId }]
|
||||
// })
|
||||
throw new Error(
|
||||
"OKX Wallet not login, Open OKX Wallet and login first"
|
||||
);
|
||||
} else {
|
||||
this.walletType = 0;
|
||||
throw new Error("User Rejected");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.walletType = 0;
|
||||
|
||||
setTimeout(function () {
|
||||
window.open("https://www.okx.com/download", "_blank");
|
||||
}, 2200);
|
||||
|
||||
throw new Error("No Web3 Provider found");
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
public async connectWalletConnect() {
|
||||
const provider = new WalletConnectProvider({
|
||||
infuraId: import.meta.env.VUE_APP_WALLET_INFURAID,
|
||||
rpc: this.rpc,
|
||||
});
|
||||
try {
|
||||
await provider.enable();
|
||||
} catch (err) {
|
||||
console.log("connect to wallet connect error: ", err);
|
||||
await Promise.reject(err);
|
||||
}
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
public async getContractInstance(address: string, abi: any = ERC20ABI) {
|
||||
if (!this.web3) {
|
||||
|
||||
await this.connectWallet(true);
|
||||
// throw new Error(
|
||||
// "Web3 instance is not initialized. Please call `initWeb3` method after user login."
|
||||
// );
|
||||
}
|
||||
if (!this.instanceCacheMap.has(address)) {
|
||||
const instance = new this.web3.eth.Contract(abi, address, {
|
||||
from: AppModule.accountId,
|
||||
});
|
||||
this.instanceCacheMap.set(address, instance);
|
||||
}
|
||||
return this.instanceCacheMap.get(address);
|
||||
}
|
||||
|
||||
public clearCachedProvider() {
|
||||
console.log("clear cached provider");
|
||||
localStorage.removeItem(CACHE_KEY);
|
||||
}
|
||||
|
||||
public loadCachedProvider() {
|
||||
const dataStr = localStorage.getItem(CACHE_KEY);
|
||||
if (dataStr) {
|
||||
try {
|
||||
const data = JSON.parse(dataStr);
|
||||
this.walletType = data.walletType;
|
||||
if (this.chainMap.has(data.chainId)) {
|
||||
this.currentChain = data.chainId;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("err parse cached json");
|
||||
this.clearCachedProvider();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public saveProvider() {
|
||||
const data = {
|
||||
walletType: this.walletType,
|
||||
chainId: this.currentChain,
|
||||
};
|
||||
localStorage.setItem(CACHE_KEY, JSON.stringify(data));
|
||||
}
|
||||
|
||||
public async disconnect() {
|
||||
try {
|
||||
await UserModule.Logout();
|
||||
await this.provider?.disconnect();
|
||||
} catch (err) {}
|
||||
this.currentChain = 0;
|
||||
this.walletType = 0;
|
||||
this.instanceCacheMap.clear();
|
||||
this.clearCachedProvider();
|
||||
AppModule.updateStep(0);
|
||||
AppModule.updateChainID(0);
|
||||
AppModule.updateNonce("");
|
||||
AppModule.updateAccount("");
|
||||
AppModule.updateWalletStatus(false);
|
||||
}
|
||||
|
||||
public subscribeToEvents = () => {
|
||||
// Subscribe to accounts change
|
||||
this.provider.on("accountsChanged", async (accounts: string[]) => {
|
||||
console.log("accountsChanged: ", accounts);
|
||||
if (accounts && accounts.length > 0) {
|
||||
if (AppModule.accountId !== accounts[0]) {
|
||||
await UserModule.Logout();
|
||||
|
||||
this.currentChain = 0;
|
||||
this.walletType = 0;
|
||||
this.instanceCacheMap.clear();
|
||||
this.clearCachedProvider();
|
||||
AppModule.updateStep(0);
|
||||
AppModule.updateChainID(0);
|
||||
AppModule.updateNonce("");
|
||||
AppModule.updateAccount("");
|
||||
AppModule.updateWalletStatus(false);
|
||||
localStorage.removeItem("totalSupply");
|
||||
localStorage.removeItem("supplyLimit");
|
||||
localStorage.removeItem("mintableCount");
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Subscribe to chainId change
|
||||
this.provider.on("chainChanged", async (chainId: string) => {
|
||||
const chainIdNum = parseInt(chainId);
|
||||
console.log("chainChanged", chainId, chainIdNum);
|
||||
await this.checkChain(chainIdNum);
|
||||
this.currentChain = chainIdNum;
|
||||
this.saveProvider();
|
||||
AppModule.updateChainID(this.currentChain);
|
||||
});
|
||||
|
||||
// Subscribe to session disconnection
|
||||
this.provider.on("disconnect", (err: any) => {
|
||||
console.log("disconnect", err);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* change chain of metamask
|
||||
* @param {number} chainId
|
||||
* @param {() => void} cb
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async switchEthereumChain(chainId?: number, cb?: (res: any) => void) {
|
||||
chainId = chainId || AVAILABLE_CHAINS[0];
|
||||
const hexChainId = toHexChainId(chainId);
|
||||
const onChainChange = (chainId: string) => {
|
||||
console.log("switchEthereumChain: ", chainId);
|
||||
this.provider.removeListener("chainChanged", onChainChange);
|
||||
cb && cb({ chain: chainId });
|
||||
};
|
||||
this.provider.on("chainChanged", onChainChange);
|
||||
try {
|
||||
const data = this.chainMap.get(chainId)!;
|
||||
await this.provider.request({
|
||||
method: "wallet_addEthereumChain",
|
||||
params: [
|
||||
{
|
||||
chainId: hexChainId,
|
||||
chainName: data.name,
|
||||
nativeCurrency: {
|
||||
name: data.symbol,
|
||||
symbol: data.symbol,
|
||||
decimals: data.decimals || 18,
|
||||
},
|
||||
blockExplorerUrls: [data.explorerurl],
|
||||
rpcUrls: [data.rpc],
|
||||
},
|
||||
],
|
||||
});
|
||||
console.log("add chain success");
|
||||
} catch (addError) {
|
||||
console.error("add chain error: ", addError);
|
||||
this.provider.removeListener("chainChanged", onChainChange);
|
||||
cb && cb({ err: addError });
|
||||
}
|
||||
}
|
||||
|
||||
public async getTransactionReceipt(txHash: string) {
|
||||
return this.web3.eth.getTransactionReceipt(txHash);
|
||||
}
|
||||
|
||||
public async getTxConfirms(txhash: string) {
|
||||
const receipt: TransactionReceipt = await this.getTransactionReceipt(
|
||||
txhash
|
||||
);
|
||||
const latest = await this.web3.eth.getBlockNumber();
|
||||
return latest - receipt.blockNumber + 1;
|
||||
}
|
||||
|
||||
public async signPresale({
|
||||
type,
|
||||
paymentTokenAddress,
|
||||
price,
|
||||
buyerAddress,
|
||||
netId,
|
||||
}: {
|
||||
type: number | string;
|
||||
paymentTokenAddress: string;
|
||||
price: any;
|
||||
buyerAddress: string;
|
||||
netId?: any;
|
||||
}) {
|
||||
const nonce = (Math.random() * 100000) | 0;
|
||||
|
||||
const signMsg = {
|
||||
item: type,
|
||||
token: paymentTokenAddress,
|
||||
price: price,
|
||||
salt: nonce,
|
||||
};
|
||||
netId = netId || (await this.web3.eth.getChainId());
|
||||
const signObj = {
|
||||
types: {
|
||||
EIP712Domain: EIP721_DOMAIN_DATA,
|
||||
set: [
|
||||
{ name: "item", type: "uint256" },
|
||||
{ name: "token", type: "address" },
|
||||
{ name: "price", type: "uint256" },
|
||||
{ name: "salt", type: "uint256" },
|
||||
],
|
||||
},
|
||||
primaryType: "set",
|
||||
domain: {
|
||||
name: "BEBoxMall",
|
||||
version: "1",
|
||||
chainId: netId,
|
||||
verifyingContract: this.mallAddress,
|
||||
},
|
||||
message: signMsg,
|
||||
};
|
||||
const signature = await this.signData(signObj, buyerAddress);
|
||||
return { nonce, signature };
|
||||
}
|
||||
|
||||
public async increaseAllowance(address: string, price: string) {
|
||||
const coinInstance: any = await this.getContractInstance(address);
|
||||
const res = await coinInstance.methods
|
||||
.increaseAllowance(this.mallAddress, price)
|
||||
.send({ gas: 1000000 });
|
||||
console.log("increaseAllowance: ", res);
|
||||
}
|
||||
public async claimReward(
|
||||
address: string,
|
||||
startTime: string,
|
||||
saltNonce: string,
|
||||
signature: string
|
||||
) {
|
||||
const coinInstance: any = await this.getContractInstance(
|
||||
Global.LIMIT_ABI_Address,
|
||||
CLAIM_ABI
|
||||
);
|
||||
let gas = await coinInstance.methods
|
||||
.claim(address, startTime, saltNonce, signature)
|
||||
.estimateGas();
|
||||
gas = (gas * 1.1) | 0;
|
||||
console.log("gas:: ", gas);
|
||||
let gasPrice = await this.web3.eth.getGasPrice();
|
||||
const res = await coinInstance.methods
|
||||
.claim(address, startTime, saltNonce, signature)
|
||||
.send({ gas, gasPrice });
|
||||
console.log("claimReward: ", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// public async buyPlanetNFT(address: string,nftAddresses:string, ids:number,amounts:string,signature:string) {
|
||||
// const coinInstance: any = await this.getContractInstance(
|
||||
// Global.BENftMall_Address,
|
||||
// BENFTMALL_ABI
|
||||
// );
|
||||
// let gas = await coinInstance.methods.buyNFT(address,nftAddresses,ids,amounts,signature).estimateGas()
|
||||
// gas = gas * 1.1 | 0
|
||||
// console.log('gas:: ' , gas)
|
||||
// let gasPrice = await this.web3.eth.getGasPrice()
|
||||
// const res = await coinInstance.methods.buyNFT(address,nftAddresses,ids,amounts,signature).send({gas, gasPrice});
|
||||
// console.log("claimReward: ", res);
|
||||
// return res
|
||||
// }
|
||||
|
||||
public async buyPlanetNFT(
|
||||
address: string,
|
||||
nftAddresses: string[],
|
||||
ids: string[],
|
||||
amounts: string[],
|
||||
values: string[],
|
||||
signature: string
|
||||
) {
|
||||
//debugger;
|
||||
const mallInstance: any = await this.getContractInstance(
|
||||
Global.BENftMall_Address,
|
||||
BENFTMALL_ABI
|
||||
);
|
||||
|
||||
const currencyInstance = await this.getContractInstance(address);
|
||||
// const allowance = await currencyInstance.methods.allowance(
|
||||
// AppModule.accountId,
|
||||
// Global.BENftMall_Address
|
||||
// );
|
||||
// if (allowance < values[1]) {
|
||||
// await currencyInstance.methods
|
||||
// .approve(Global.BENftMall_Address, values[1])
|
||||
// .send();
|
||||
// }
|
||||
await currencyInstance.methods
|
||||
.approve(Global.BENftMall_Address, values[1])
|
||||
.send();
|
||||
|
||||
const nftInstance: any = await this.getContractInstance(nftAddresses[0]);
|
||||
let balance = await nftInstance.methods
|
||||
.balanceOf(Global.BENftMall_Address)
|
||||
.call();
|
||||
if (balance < amounts.length) {
|
||||
return Promise.reject("money not enought");
|
||||
}
|
||||
// debugger
|
||||
let gas = await mallInstance.methods
|
||||
.buyNFT(address, nftAddresses, ids, amounts, values, signature)
|
||||
.estimateGas();
|
||||
gas = (gas * 1.1) | 0;
|
||||
console.log("gas:: ", gas);
|
||||
let gasPrice = await this.web3.eth.getGasPrice();
|
||||
const res = await mallInstance.methods
|
||||
.buyNFT(address, nftAddresses, ids, amounts, values, signature)
|
||||
.send({ gas, gasPrice });
|
||||
console.log("buyPlanetNFT: ", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public async getBalances(account: string) {
|
||||
account = account || AppModule.accountId;
|
||||
const coinInstance: any = await this.getContractInstance(
|
||||
Global.LIMIT_ABI_Address,
|
||||
CLAIM_ABI
|
||||
);
|
||||
return await coinInstance.methods.claimHistory(account).call();
|
||||
}
|
||||
|
||||
public async getNftBalances(nftAddresses: string[]) {
|
||||
// debugger
|
||||
const nftInstance: any = await this.getContractInstance(nftAddresses[0]);
|
||||
let balance = await nftInstance.methods
|
||||
.balanceOf(Global.BENftMall_Address)
|
||||
.call();
|
||||
return balance;
|
||||
}
|
||||
// public async totalSupply(address: string) {
|
||||
// const coinInstance: any = await this.getContractInstance(
|
||||
// Global.LIMIT_ABI_Address,
|
||||
// LIMIT_ABI
|
||||
// );
|
||||
// const res = await coinInstance.methods.totalSupply().call();
|
||||
// console.log("totalSupply: ", res);
|
||||
// return res
|
||||
// }
|
||||
// // 获取当前用户可mint的数量
|
||||
// public async getMintableCount(address?: string) {
|
||||
// // address = address || //TODO: current address
|
||||
// const coinInstance: any = await this.getContractInstance(
|
||||
// Global.MEDAL_REWARDS_Address,
|
||||
// MEDAL_REWARDS_ABI
|
||||
// );
|
||||
// const res = await coinInstance.methods
|
||||
// .getMintableCount(AppModule.accountId)
|
||||
// .call();
|
||||
// console.log("getMintableCount: ", res);
|
||||
// return res
|
||||
// }
|
||||
// // 获取当前用户的mint总数
|
||||
// public async supplyLimit(address: string) {
|
||||
// const coinInstance: any = await this.getContractInstance(
|
||||
// Global.LIMIT_ABI_Address,
|
||||
// LIMIT_ABI
|
||||
// );
|
||||
// const res = await coinInstance.methods.supplyLimit().call();
|
||||
// console.log("supplyLimit: ", res);
|
||||
// return res
|
||||
// }
|
||||
/**
|
||||
* @param {string} address
|
||||
* @param {string | null} account
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
// public async getBalance(address: string, account: string | null) {
|
||||
// account = account || AppModule.accountId;
|
||||
// const coinInstance: any = await this.getContractInstance(address);
|
||||
// return await coinInstance.methods.balanceOf(account).call();
|
||||
// }
|
||||
public async getBalance(address: string, account: string | null) {
|
||||
account = account || AppModule.accountId;
|
||||
const coinInstance: any = await this.getContractInstance(address);
|
||||
return await coinInstance.methods.claimHistory(account).call();
|
||||
}
|
||||
public async transferToAccount(
|
||||
account: string,
|
||||
amount: number,
|
||||
address: string
|
||||
) {
|
||||
const amountBN = this.web3.utils.toBN(this.web3.utils.toWei(amount + ""));
|
||||
const coinInstance: any = await this.getContractInstance(address);
|
||||
return coinInstance.methods
|
||||
.transfer(account, amountBN)
|
||||
.send({ gas: 1000000 });
|
||||
}
|
||||
|
||||
public async signData(signObj: any, signer: string) {
|
||||
const msgParams = JSON.stringify(signObj);
|
||||
const from = signer;
|
||||
console.log("clicked, sending personal sign req", "from", from, msgParams);
|
||||
const params = [from, msgParams];
|
||||
const result: any = await this.sendCmd({
|
||||
method: "eth_signTypedData_v4",
|
||||
params,
|
||||
from,
|
||||
});
|
||||
console.log(result);
|
||||
return result.result;
|
||||
}
|
||||
|
||||
public async sendCmd({ method, params, from }: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
this.web3.currentProvider.sendAsync(
|
||||
{
|
||||
method,
|
||||
params,
|
||||
from,
|
||||
},
|
||||
async function (err: any, result: any) {
|
||||
if (err) {
|
||||
reject && reject(err);
|
||||
return;
|
||||
}
|
||||
resolve && resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
@ -37,7 +37,6 @@
|
||||
import { ref, toRaw, onMounted, getCurrentInstance, defineEmits } from "vue"
|
||||
import BuyDialog from "@/components/Dialogs/buyDialog.vue"
|
||||
import {priceCalculated} from "@/configs/priceCalculate.js"
|
||||
import { useImmutableStore } from "@/store/immutable"
|
||||
import {walletStore} from "@/store/wallet";
|
||||
import { BlockChain } from "@/components/chain/BlockChain"
|
||||
import LazyLoadImg from "@/components/lazyloadimg"
|
||||
|
@ -1,4 +1,3 @@
|
||||
const env = "development";
|
||||
|
||||
export const ALL_PROVIDERS = [
|
||||
{
|
||||
@ -21,45 +20,7 @@ export const ALL_PROVIDERS = [
|
||||
},
|
||||
];
|
||||
|
||||
export const AVAILABLE_CHAINS = env === "production" ? [42161] : [42161];
|
||||
//42161
|
||||
export const OFFICE_ACCOUNT =
|
||||
env === "production"
|
||||
? "0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e"
|
||||
: "0x50A8e60041A206AcaA5F844a1104896224be6F39";
|
||||
|
||||
export const CONTRACT_ADDRESS = {
|
||||
322: {
|
||||
cec: "0xdb6D4bB22E2C12686Efff25a79EC78f9f078fe7D",
|
||||
ceg: "0xC5Cd606b3e9B80b758e8274B198c76D929aA094A",
|
||||
eth: "0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc",
|
||||
mall: "0x2bD8185e1B175c2b64859235981B700DD643dc4a",
|
||||
mystery: "0x7546b0829BDC04515F734fDD290Cf6bC2bB7058f",
|
||||
mysteryProxy: "0xA171C03Fa71bD66A013e53F64dDb9455C58E6567",
|
||||
nftProxy: "0x244B1F2c13eCbF9Ac918a01F5261972459dCd7c6",
|
||||
},
|
||||
97: {
|
||||
cec: "0xFAA03824c38Ed5102F9F901987FA7cd9d193449B",
|
||||
ceg: "0xA70beE785B92d4F662F870Cf4f3EBE774234d795",
|
||||
eth: "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
|
||||
mall: "0xF278ff771F9E24968083B0bA54Cb42eb4B23C2d7",
|
||||
},
|
||||
321: {
|
||||
cec: "0x4446Fc4eb47f2f6586f9fAAb68B3498F86C07521",
|
||||
ceg: "0x4446Fc4eb47f2f6586f9fAAb68B3498F86C07521",
|
||||
eth: "0x4446Fc4eb47f2f6586f9fAAb68B3498F86C07521",
|
||||
mall: "0x4446Fc4eb47f2f6586f9fAAb68B3498F86C07521",
|
||||
mystery: "0x8444404bD78089A5a6d5Cc57f7Df8924f2DdACB4",
|
||||
mysteryProxy: "0xAA9C6e00bBeE2b41128EF46aB669D564d02F75E0",
|
||||
nftProxy: "0x3caD77a9479Aa157e94EFEa7168E202397Ce2ab8",
|
||||
},
|
||||
32: {
|
||||
cec: "0xfeFc3aab779863c1624eE008aba485c53805dCeb",
|
||||
ceg: "0xE388e872e63aadF5a4F1521E4d96C10A28091457",
|
||||
eth: "0xb296bab2ed122a85977423b602ddf3527582a3da",
|
||||
mall: "0xF278ff771F9E24968083B0bA54Cb42eb4B23C2d7",
|
||||
},
|
||||
};
|
||||
export default {
|
||||
ALL_PROVIDERS,
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import ChainManager from '@/chain/ChainManager';
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
|
||||
export const useChainStore = defineStore('chain', ()=> {
|
||||
const logined = ref(false);
|
||||
const chainManager = ref(new ChainManager());
|
||||
chainManager.value.init().then(()=> {
|
||||
// console.log("chain init:", {chainManager})
|
||||
|
||||
logined.value = chainManager.value.isLogined;
|
||||
|
||||
});
|
||||
|
||||
return { chainManager, logined }
|
||||
})
|
@ -1,22 +0,0 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
|
||||
export const useImmutableStore = defineStore('immutable', () => {
|
||||
const accessToken = ref()
|
||||
const access = ref()
|
||||
|
||||
const updateAccessTokenStatus = (_connected) => {
|
||||
accessToken.value = _connected;
|
||||
}
|
||||
const updateAccessStatus = (_connected) => {
|
||||
access.value = _connected;
|
||||
}
|
||||
return {
|
||||
accessToken, updateAccessTokenStatus,
|
||||
access, updateAccessStatus,
|
||||
}
|
||||
},
|
||||
{
|
||||
persist: true,
|
||||
}
|
||||
)
|
@ -93,7 +93,7 @@
|
||||
<div class="btm-left">
|
||||
<h2>Property</h2>
|
||||
<div class="btm-detail">
|
||||
<li v-for="(item, val, index) in nftAbilities" :key="index">
|
||||
<li v-for="(item, val, index) in detailData.detail" :key="index">
|
||||
<div v-show="val == 'quality'">
|
||||
<h5>Tier</h5>
|
||||
<p>{{ item }}</p>
|
||||
@ -157,8 +157,6 @@ const props = defineProps({
|
||||
})
|
||||
import placeholderImg from '@/assets/img/marketplace/GenesisHeroes_NFT.png'
|
||||
const detailData = ref(null)
|
||||
// const detailData = window.history.state.nftData
|
||||
// console.log(detailData)
|
||||
const myAddress = localWalletStore.address
|
||||
const nftAbilities = ref()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user