修改cookie无法正确删除的问题
This commit is contained in:
parent
aae460cbaa
commit
29dcd58ae2
@ -1,18 +1,28 @@
|
|||||||
diff --git a/node_modules/@walletconnect/browser-utils/dist/esm/local.js b/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
diff --git a/node_modules/@walletconnect/browser-utils/dist/esm/local.js b/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
||||||
index c7f4db3..9e2d455 100644
|
index c7f4db3..9dd7e88 100644
|
||||||
--- a/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
--- a/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
||||||
+++ b/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
+++ b/node_modules/@walletconnect/browser-utils/dist/esm/local.js
|
||||||
@@ -1,26 +1,29 @@
|
@@ -1,26 +1,39 @@
|
||||||
import { safeJsonParse, safeJsonStringify } from "./json";
|
import { safeJsonParse, safeJsonStringify } from "./json";
|
||||||
import { getLocalStorage } from "./browser";
|
import { getLocalStorage } from "./browser";
|
||||||
+var Cookies = require('js-cookie')
|
+var Cookies = require('js-cookie')
|
||||||
|
+
|
||||||
|
+export const generateDomain = () => {
|
||||||
|
+ let host = location.host;
|
||||||
|
+ let hostArr = host.split(".");
|
||||||
|
+ if (host.length > 1) {
|
||||||
|
+ host = hostArr.slice(hostArr.length - 2).join(".");
|
||||||
|
+ }
|
||||||
|
+ return host;
|
||||||
|
+ };
|
||||||
|
+
|
||||||
export function setLocal(key, data) {
|
export function setLocal(key, data) {
|
||||||
const raw = safeJsonStringify(data);
|
const raw = safeJsonStringify(data);
|
||||||
- const local = getLocalStorage();
|
- const local = getLocalStorage();
|
||||||
- if (local) {
|
- if (local) {
|
||||||
- local.setItem(key, raw);
|
- local.setItem(key, raw);
|
||||||
- }
|
- }
|
||||||
+ Cookies.set(key, raw, { domain: 'cebg.games'});
|
+ Cookies.set(key, raw, { domain: generateDomain() });
|
||||||
+ // const local = getLocalStorage();
|
+ // const local = getLocalStorage();
|
||||||
+ // if (local) {
|
+ // if (local) {
|
||||||
+ // local.setItem(key, raw);
|
+ // local.setItem(key, raw);
|
||||||
@ -38,7 +48,7 @@ index c7f4db3..9e2d455 100644
|
|||||||
- if (local) {
|
- if (local) {
|
||||||
- local.removeItem(key);
|
- local.removeItem(key);
|
||||||
- }
|
- }
|
||||||
+ Cookies.remove(key);
|
+ Cookies.remove(key, { domain: generateDomain() });
|
||||||
+ // const local = getLocalStorage();
|
+ // const local = getLocalStorage();
|
||||||
+ // if (local) {
|
+ // if (local) {
|
||||||
+ // local.removeItem(key);
|
+ // local.removeItem(key);
|
||||||
|
@ -1,118 +1,129 @@
|
|||||||
import WalletConnectProvider from '@walletconnect/web3-provider'
|
import WalletConnectProvider from "@walletconnect/web3-provider";
|
||||||
import Web3 from 'web3'
|
import Web3 from "web3";
|
||||||
import {
|
import {
|
||||||
AVAILABLE_CHAINS,
|
AVAILABLE_CHAINS,
|
||||||
CONTRACT_ADDRESS,
|
CONTRACT_ADDRESS,
|
||||||
IChainData,
|
IChainData,
|
||||||
} from '../configs/config_chain'
|
} from "../configs/config_chain";
|
||||||
import {
|
import {
|
||||||
EventBus,
|
EventBus,
|
||||||
NEED_CHANGE_CHAIN,
|
NEED_CHANGE_CHAIN,
|
||||||
NEED_LOGIN,
|
NEED_LOGIN,
|
||||||
NEED_NONCE,
|
NEED_NONCE,
|
||||||
SHOW_CHAIN_MODAL,
|
SHOW_CHAIN_MODAL,
|
||||||
} from '../utils/event-bus'
|
} from "../utils/event-bus";
|
||||||
import { hasMetamask, toHexChainId } from '../utils/chain.util'
|
import { hasMetamask, toHexChainId } from "../utils/chain.util";
|
||||||
import { AllChains } from '../configs/allchain'
|
import { AllChains } from "../configs/allchain";
|
||||||
import { ERC20ABI } from '../configs/contracts'
|
import { ERC20ABI } from "../configs/contracts";
|
||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from "web3-core";
|
||||||
import { singleton } from '../decorator/singleton.decorator'
|
import { singleton } from "../decorator/singleton.decorator";
|
||||||
import { isMobile } from '../utils/browser'
|
import { isMobile } from "../utils/browser";
|
||||||
import { UserModule } from '../module/UserModule'
|
import { UserModule } from "../module/UserModule";
|
||||||
import { getItem, removeItem, setItem } from '../utils/cookies'
|
import { getItem, removeItem, setItem } from "../utils/cookies";
|
||||||
|
|
||||||
const EIP721_DOMAIN_DATA = [
|
const EIP721_DOMAIN_DATA = [
|
||||||
{ name: 'name', type: 'string' },
|
{ name: "name", type: "string" },
|
||||||
{ name: 'version', type: 'string' },
|
{ name: "version", type: "string" },
|
||||||
{ name: 'chainId', type: 'uint256' },
|
{ name: "chainId", type: "uint256" },
|
||||||
{ name: 'verifyingContract', type: 'address' },
|
{ name: "verifyingContract", type: "address" },
|
||||||
]
|
];
|
||||||
|
|
||||||
const CACHE_KEY = 'cebg_chain_cache_key'
|
const CACHE_KEY = "cebg_chain_cache_key";
|
||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
export class Blockchain {
|
export class Blockchain {
|
||||||
provider: any
|
provider: any;
|
||||||
web3: Web3
|
web3: Web3;
|
||||||
currentChain = 0
|
currentChain = 0;
|
||||||
// 0: null, 1: metamask, 2: walletconnect
|
// 0: null, 1: metamask, 2: walletconnect
|
||||||
walletType = 0
|
walletType = 0;
|
||||||
dataCached = false
|
dataCached = false;
|
||||||
instanceCacheMap: Map<string, any>
|
instanceCacheMap: Map<string, any>;
|
||||||
public chainMap: Map<number, IChainData> = new Map()
|
public chainMap: Map<number, IChainData> = new Map();
|
||||||
public rpc: any = {}
|
public rpc: any = {};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const allChainMap: Map<number, IChainData> = new Map()
|
const allChainMap: Map<number, IChainData> = new Map();
|
||||||
for (const d of AllChains) {
|
for (const d of AllChains) {
|
||||||
const id = d.id
|
const id = d.id;
|
||||||
this.rpc[id] = d.rpc
|
this.rpc[id] = d.rpc;
|
||||||
allChainMap.set(id, d)
|
allChainMap.set(id, d);
|
||||||
}
|
}
|
||||||
for (const id of AVAILABLE_CHAINS) {
|
for (const id of AVAILABLE_CHAINS) {
|
||||||
this.chainMap.set(id, allChainMap.get(id)!)
|
this.chainMap.set(id, allChainMap.get(id)!);
|
||||||
}
|
}
|
||||||
this.loadCachedProvider()
|
this.loadCachedProvider();
|
||||||
this.instanceCacheMap = new Map()
|
this.instanceCacheMap = new Map();
|
||||||
// UserModule.updateChainID(chainId)
|
// UserModule.updateChainID(chainId)
|
||||||
EventBus.$on(NEED_LOGIN, this.connect.bind(this))
|
EventBus.$on(NEED_LOGIN, this.connect.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
get isWalletConnect() {
|
get isWalletConnect() {
|
||||||
return !!this.walletType && !!this.currentChain
|
return !!this.walletType && !!this.currentChain;
|
||||||
}
|
}
|
||||||
|
|
||||||
get mallAddress() {
|
get mallAddress() {
|
||||||
return CONTRACT_ADDRESS[this.currentChain]?.mall
|
return CONTRACT_ADDRESS[this.currentChain]?.mall;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async updateAccount() {
|
||||||
|
const accounts = await this.web3.eth.getAccounts();
|
||||||
|
if (accounts && accounts.length > 0) {
|
||||||
|
UserModule.updateAccount(accounts[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get hexChainId() {
|
public get hexChainId() {
|
||||||
return toHexChainId(this.currentChain)
|
return toHexChainId(this.currentChain);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async chainSelected(id: number) {
|
public async chainSelected(id: number) {
|
||||||
if (!this.chainMap.has(id)) {
|
if (!this.chainMap.has(id)) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
this.currentChain = id
|
this.currentChain = id;
|
||||||
if (this.provider) {
|
if (this.provider) {
|
||||||
await this.switchEthereumChain()
|
await this.switchEthereumChain();
|
||||||
} else {
|
} else {
|
||||||
await this.connectWallet(true)
|
await this.connectWallet(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectWallet(isManual: boolean) {
|
public async connectWallet(isManual: boolean) {
|
||||||
console.log('begin connect to wallet: ', this.walletType, this.currentChain)
|
console.log(
|
||||||
|
"begin connect to wallet: ",
|
||||||
|
this.walletType,
|
||||||
|
this.currentChain
|
||||||
|
);
|
||||||
if (this.walletType === 1) {
|
if (this.walletType === 1) {
|
||||||
this.provider = await this.connectMetaMask()
|
this.provider = await this.connectMetaMask();
|
||||||
} else if (this.walletType === 2) {
|
} else if (this.walletType === 2) {
|
||||||
this.provider = await this.connectWalletConnect()
|
this.provider = await this.connectWalletConnect();
|
||||||
}
|
}
|
||||||
if (!this.provider) {
|
if (!this.provider) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
this.web3 = new Web3(this.provider)
|
this.web3 = new Web3(this.provider);
|
||||||
const chainId = await this.web3.eth.getChainId()
|
const chainId = await this.web3.eth.getChainId();
|
||||||
// await this.checkChain(chainId)
|
// await this.checkChain(chainId)
|
||||||
|
|
||||||
this.subscribeToEvents()
|
this.subscribeToEvents();
|
||||||
const accounts = await this.web3.eth.getAccounts()
|
const accounts = await this.web3.eth.getAccounts();
|
||||||
if (accounts && accounts.length > 0) {
|
if (accounts && accounts.length > 0) {
|
||||||
UserModule.updateAccount(accounts[0])
|
UserModule.updateAccount(accounts[0]);
|
||||||
}
|
}
|
||||||
if (!this.currentChain) this.currentChain = chainId
|
if (!this.currentChain) this.currentChain = chainId;
|
||||||
this.saveProvider()
|
this.saveProvider();
|
||||||
UserModule.updateChainID(chainId)
|
UserModule.updateChainID(chainId);
|
||||||
UserModule.updateWalletStatus(true)
|
UserModule.updateWalletStatus(true);
|
||||||
console.log('current login chain: ', chainId)
|
console.log("current login chain: ", chainId);
|
||||||
console.log('accountsLogin: ', accounts, UserModule.accountId)
|
console.log("accountsLogin: ", accounts, UserModule.accountId);
|
||||||
if (isManual) {
|
if (isManual) {
|
||||||
EventBus.$emit(NEED_NONCE)
|
EventBus.$emit(NEED_NONCE);
|
||||||
} else {
|
} else {
|
||||||
UserModule.updateStep(1)
|
UserModule.updateStep(1);
|
||||||
}
|
}
|
||||||
return { account: accounts[0], chainId }
|
return { account: accounts[0], chainId };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,9 +138,9 @@ export class Blockchain {
|
|||||||
if (!this.chainMap.has(chainId)) {
|
if (!this.chainMap.has(chainId)) {
|
||||||
// if (this.walletType === 1) {
|
// if (this.walletType === 1) {
|
||||||
try {
|
try {
|
||||||
await this.selectChain()
|
await this.selectChain();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await this.disconnect()
|
await this.disconnect();
|
||||||
}
|
}
|
||||||
// } else if (this.walletType === 2) {
|
// } else if (this.walletType === 2) {
|
||||||
// await this.disconnect()
|
// await this.disconnect()
|
||||||
@ -148,22 +159,22 @@ export class Blockchain {
|
|||||||
public async connect(isManual = false) {
|
public async connect(isManual = false) {
|
||||||
if (isMobile()) {
|
if (isMobile()) {
|
||||||
if (hasMetamask()) {
|
if (hasMetamask()) {
|
||||||
this.walletType = 1
|
this.walletType = 1;
|
||||||
} else {
|
} else {
|
||||||
this.walletType = 2
|
this.walletType = 2;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (hasMetamask()) {
|
if (hasMetamask()) {
|
||||||
if (isManual && !this.walletType) {
|
if (isManual && !this.walletType) {
|
||||||
this.walletType = await this.selectWallet()
|
this.walletType = await this.selectWallet();
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.walletType = 2
|
this.walletType = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isManual || this.isWalletConnect) {
|
if (isManual || this.isWalletConnect) {
|
||||||
await this.connectWallet(isManual)
|
await this.connectWallet(isManual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,15 +187,15 @@ export class Blockchain {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
EventBus.$emit(SHOW_CHAIN_MODAL, {
|
EventBus.$emit(SHOW_CHAIN_MODAL, {
|
||||||
confirm: (id: number) => {
|
confirm: (id: number) => {
|
||||||
console.log('select wallet: ', id)
|
console.log("select wallet: ", id);
|
||||||
resolve && resolve(id)
|
resolve && resolve(id);
|
||||||
},
|
},
|
||||||
cancel: (reason: any) => {
|
cancel: (reason: any) => {
|
||||||
console.log('cancel select wallet: ', reason)
|
console.log("cancel select wallet: ", reason);
|
||||||
reject && reject(reason)
|
reject && reject(reason);
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -196,27 +207,27 @@ export class Blockchain {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
EventBus.$emit(NEED_CHANGE_CHAIN, {
|
EventBus.$emit(NEED_CHANGE_CHAIN, {
|
||||||
confirm: async (id: number) => {
|
confirm: async (id: number) => {
|
||||||
console.log('select chain: ', id)
|
console.log("select chain: ", id);
|
||||||
this.currentChain = id
|
this.currentChain = id;
|
||||||
if (this.provider) {
|
if (this.provider) {
|
||||||
await this.switchEthereumChain()
|
await this.switchEthereumChain();
|
||||||
}
|
}
|
||||||
resolve && resolve(id)
|
resolve && resolve(id);
|
||||||
},
|
},
|
||||||
cancel: (reason: any) => {
|
cancel: (reason: any) => {
|
||||||
console.log('cancel select chain: ', reason)
|
console.log("cancel select chain: ", reason);
|
||||||
reject && reject(reason)
|
reject && reject(reason);
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectMetaMask() {
|
public async connectMetaMask() {
|
||||||
let provider = null
|
let provider = null;
|
||||||
if (typeof window.ethereum !== 'undefined') {
|
if (typeof window.ethereum !== "undefined") {
|
||||||
provider = window.ethereum
|
provider = window.ethereum;
|
||||||
try {
|
try {
|
||||||
await provider.request({ method: 'eth_requestAccounts' })
|
await provider.request({ method: "eth_requestAccounts" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === -32002) {
|
if (error.code === -32002) {
|
||||||
// const hexChainId = toHexChainId(this.currentChain)
|
// const hexChainId = toHexChainId(this.currentChain)
|
||||||
@ -224,63 +235,65 @@ export class Blockchain {
|
|||||||
// method: 'wallet_switchEthereumChain',
|
// method: 'wallet_switchEthereumChain',
|
||||||
// params: [{ chainId: hexChainId }]
|
// params: [{ chainId: hexChainId }]
|
||||||
// })
|
// })
|
||||||
throw new Error('MeatMask not login, Open MeatMask and login first')
|
throw new Error("MeatMask not login, Open MeatMask and login first");
|
||||||
} else {
|
} else {
|
||||||
throw new Error('User Rejected')
|
throw new Error("User Rejected");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (window.web3) {
|
} else if (window.web3) {
|
||||||
provider = window.web3.currentProvider
|
provider = window.web3.currentProvider;
|
||||||
} else if (window.celo) {
|
} else if (window.celo) {
|
||||||
provider = window.celo
|
provider = window.celo;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No Web3 Provider found')
|
throw new Error("No Web3 Provider found");
|
||||||
}
|
}
|
||||||
return provider
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectWalletConnect() {
|
public async connectWalletConnect() {
|
||||||
const provider = new WalletConnectProvider({
|
const provider = new WalletConnectProvider({
|
||||||
infuraId: process.env.VUE_APP_WALLET_INFURAID,
|
infuraId: process.env.VUE_APP_WALLET_INFURAID,
|
||||||
rpc: this.rpc,
|
rpc: this.rpc,
|
||||||
})
|
});
|
||||||
try {
|
try {
|
||||||
await provider.enable()
|
await provider.enable();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('connect to wallet connect error: ', err)
|
console.log("connect to wallet connect error: ", err);
|
||||||
await Promise.reject(err)
|
await Promise.reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getContractInstance(address: string, abi: any = ERC20ABI) {
|
public async getContractInstance(address: string, abi: any = ERC20ABI) {
|
||||||
if (!this.instanceCacheMap.has(address)) {
|
if (!this.instanceCacheMap.has(address)) {
|
||||||
const instance = new this.web3.eth.Contract(abi, address, {
|
const instance = new this.web3.eth.Contract(abi, address, {
|
||||||
from: UserModule.accountId,
|
from: UserModule.accountId,
|
||||||
})
|
});
|
||||||
this.instanceCacheMap.set(address, instance)
|
this.instanceCacheMap.set(address, instance);
|
||||||
}
|
}
|
||||||
return this.instanceCacheMap.get(address)
|
return this.instanceCacheMap.get(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public clearCachedProvider() {
|
public clearCachedProvider() {
|
||||||
console.log('clear cached provider')
|
console.log("clear cached provider");
|
||||||
removeItem(CACHE_KEY)
|
removeItem(CACHE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public loadCachedProvider() {
|
public loadCachedProvider() {
|
||||||
const dataStr = getItem(CACHE_KEY)
|
const dataStr = getItem(CACHE_KEY);
|
||||||
|
|
||||||
if (dataStr) {
|
if (dataStr) {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(dataStr)
|
const data = JSON.parse(dataStr);
|
||||||
this.walletType = data.walletType
|
this.walletType = data.walletType;
|
||||||
if (this.chainMap.has(data.chainId)) {
|
// if (this.chainMap.has(data.chainId)) {
|
||||||
this.currentChain = data.chainId
|
this.currentChain = data.chainId;
|
||||||
}
|
|
||||||
|
// }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('err parse cached json')
|
console.log("err parse cached json");
|
||||||
this.clearCachedProvider()
|
this.clearCachedProvider();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -289,53 +302,56 @@ export class Blockchain {
|
|||||||
const data = {
|
const data = {
|
||||||
walletType: this.walletType,
|
walletType: this.walletType,
|
||||||
chainId: this.currentChain,
|
chainId: this.currentChain,
|
||||||
}
|
};
|
||||||
setItem(CACHE_KEY, JSON.stringify(data))
|
setItem(CACHE_KEY, JSON.stringify(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async disconnect() {
|
public async disconnect() {
|
||||||
try {
|
try {
|
||||||
await UserModule.LogOut()
|
await UserModule.LogOut();
|
||||||
await this.provider?.disconnect()
|
await this.provider?.disconnect();
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
this.currentChain = 0
|
this.currentChain = 0;
|
||||||
this.walletType = 0
|
this.walletType = 0;
|
||||||
this.instanceCacheMap.clear()
|
this.instanceCacheMap.clear();
|
||||||
this.clearCachedProvider()
|
this.clearCachedProvider();
|
||||||
UserModule.updateStep(0)
|
UserModule.updateStep(0);
|
||||||
UserModule.updateChainID(0)
|
UserModule.updateChainID(0);
|
||||||
UserModule.updateNonce('')
|
UserModule.updateNonce("");
|
||||||
UserModule.updateAccount('')
|
UserModule.updateAccount("");
|
||||||
UserModule.updateWalletStatus(false)
|
UserModule.updateWalletStatus(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribeToEvents = () => {
|
public subscribeToEvents = () => {
|
||||||
// Subscribe to accounts change
|
// Subscribe to accounts change
|
||||||
this.provider.on('accountsChanged', async (accounts: string[]) => {
|
this.provider.on("accountsChanged", async (accounts: string[]) => {
|
||||||
console.log('accountsChanged: ', accounts)
|
console.log("accountsChanged: ", accounts);
|
||||||
if (accounts && accounts.length > 0) {
|
if (accounts && accounts.length > 0) {
|
||||||
if (UserModule.accountId !== accounts[0]) {
|
if (UserModule.accountId !== accounts[0]) {
|
||||||
await UserModule.LogOut()
|
this.clearCachedProvider();
|
||||||
location.reload()
|
await UserModule.LogOut();
|
||||||
|
setTimeout(() => {
|
||||||
|
location.reload();
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
// Subscribe to chainId change
|
// Subscribe to chainId change
|
||||||
this.provider.on('chainChanged', async (chainId: string) => {
|
this.provider.on("chainChanged", async (chainId: string) => {
|
||||||
const chainIdNum = parseInt(chainId)
|
const chainIdNum = parseInt(chainId);
|
||||||
console.log('chainChanged', chainId, chainIdNum)
|
console.log("chainChanged", chainId, chainIdNum);
|
||||||
await this.checkChain(chainIdNum)
|
await this.checkChain(chainIdNum);
|
||||||
this.currentChain = chainIdNum
|
this.currentChain = chainIdNum;
|
||||||
this.saveProvider()
|
this.saveProvider();
|
||||||
UserModule.updateChainID(this.currentChain)
|
UserModule.updateChainID(this.currentChain);
|
||||||
})
|
});
|
||||||
|
|
||||||
// Subscribe to session disconnection
|
// Subscribe to session disconnection
|
||||||
this.provider.on('disconnect', (err: any) => {
|
this.provider.on("disconnect", (err: any) => {
|
||||||
console.log('disconnect', err)
|
console.log("disconnect", err);
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* change chain of metamask
|
* change chain of metamask
|
||||||
@ -344,27 +360,27 @@ export class Blockchain {
|
|||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async switchEthereumChain(chainId?: number, cb?: () => void) {
|
async switchEthereumChain(chainId?: number, cb?: () => void) {
|
||||||
chainId = chainId || this.currentChain
|
chainId = chainId || this.currentChain;
|
||||||
const hexChainId = toHexChainId(chainId)
|
const hexChainId = toHexChainId(chainId);
|
||||||
const onChainChange = (chainId: string) => {
|
const onChainChange = (chainId: string) => {
|
||||||
console.log('switchEthereumChain: ', chainId)
|
console.log("switchEthereumChain: ", chainId);
|
||||||
this.provider.removeListener('chainChanged', onChainChange)
|
this.provider.removeListener("chainChanged", onChainChange);
|
||||||
cb && cb()
|
cb && cb();
|
||||||
}
|
};
|
||||||
this.provider.on('chainChanged', onChainChange)
|
this.provider.on("chainChanged", onChainChange);
|
||||||
try {
|
try {
|
||||||
await this.provider.request({
|
await this.provider.request({
|
||||||
method: 'wallet_switchEthereumChain',
|
method: "wallet_switchEthereumChain",
|
||||||
params: [{ chainId: hexChainId }],
|
params: [{ chainId: hexChainId }],
|
||||||
})
|
});
|
||||||
console.log('switch chain success')
|
console.log("switch chain success");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('switch chain error: ', e)
|
console.log("switch chain error: ", e);
|
||||||
if (e.code === 4902 || e.message.indexOf('Unrecognized chain ID') >= 0) {
|
if (e.code === 4902 || e.message.indexOf("Unrecognized chain ID") >= 0) {
|
||||||
try {
|
try {
|
||||||
const data = this.chainMap.get(chainId)!
|
const data = this.chainMap.get(chainId)!;
|
||||||
await this.provider.request({
|
await this.provider.request({
|
||||||
method: 'wallet_addEthereumChain',
|
method: "wallet_addEthereumChain",
|
||||||
params: [
|
params: [
|
||||||
{
|
{
|
||||||
chainId: hexChainId,
|
chainId: hexChainId,
|
||||||
@ -378,11 +394,11 @@ export class Blockchain {
|
|||||||
rpcUrls: [data.rpc],
|
rpcUrls: [data.rpc],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
});
|
||||||
console.log('add chain success')
|
console.log("add chain success");
|
||||||
} catch (addError) {
|
} catch (addError) {
|
||||||
console.error('add chain error: ', addError)
|
console.error("add chain error: ", addError);
|
||||||
this.provider.removeListener('chainChanged', onChainChange)
|
this.provider.removeListener("chainChanged", onChainChange);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// console.error(e)
|
// console.error(e)
|
||||||
@ -390,13 +406,15 @@ export class Blockchain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getTransactionReceipt(txHash: string) {
|
public async getTransactionReceipt(txHash: string) {
|
||||||
return this.web3.eth.getTransactionReceipt(txHash)
|
return this.web3.eth.getTransactionReceipt(txHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getTxConfirms(txhash: string) {
|
public async getTxConfirms(txhash: string) {
|
||||||
const receipt: TransactionReceipt = await this.getTransactionReceipt(txhash)
|
const receipt: TransactionReceipt = await this.getTransactionReceipt(
|
||||||
const latest = await this.web3.eth.getBlockNumber()
|
txhash
|
||||||
return latest - receipt.blockNumber + 1
|
);
|
||||||
|
const latest = await this.web3.eth.getBlockNumber();
|
||||||
|
return latest - receipt.blockNumber + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async signPresale({
|
public async signPresale({
|
||||||
@ -406,50 +424,50 @@ export class Blockchain {
|
|||||||
buyerAddress,
|
buyerAddress,
|
||||||
netId,
|
netId,
|
||||||
}: {
|
}: {
|
||||||
type: number | string
|
type: number | string;
|
||||||
paymentTokenAddress: string
|
paymentTokenAddress: string;
|
||||||
price: any
|
price: any;
|
||||||
buyerAddress: string
|
buyerAddress: string;
|
||||||
netId?: any
|
netId?: any;
|
||||||
}) {
|
}) {
|
||||||
const nonce = (Math.random() * 100000) | 0
|
const nonce = (Math.random() * 100000) | 0;
|
||||||
|
|
||||||
const signMsg = {
|
const signMsg = {
|
||||||
item: type,
|
item: type,
|
||||||
token: paymentTokenAddress,
|
token: paymentTokenAddress,
|
||||||
price: price,
|
price: price,
|
||||||
salt: nonce,
|
salt: nonce,
|
||||||
}
|
};
|
||||||
netId = netId || (await this.web3.eth.getChainId())
|
netId = netId || (await this.web3.eth.getChainId());
|
||||||
const signObj = {
|
const signObj = {
|
||||||
types: {
|
types: {
|
||||||
EIP712Domain: EIP721_DOMAIN_DATA,
|
EIP712Domain: EIP721_DOMAIN_DATA,
|
||||||
set: [
|
set: [
|
||||||
{ name: 'item', type: 'uint256' },
|
{ name: "item", type: "uint256" },
|
||||||
{ name: 'token', type: 'address' },
|
{ name: "token", type: "address" },
|
||||||
{ name: 'price', type: 'uint256' },
|
{ name: "price", type: "uint256" },
|
||||||
{ name: 'salt', type: 'uint256' },
|
{ name: "salt", type: "uint256" },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
primaryType: 'set',
|
primaryType: "set",
|
||||||
domain: {
|
domain: {
|
||||||
name: 'BEBoxMall',
|
name: "BEBoxMall",
|
||||||
version: '1',
|
version: "1",
|
||||||
chainId: netId,
|
chainId: netId,
|
||||||
verifyingContract: this.mallAddress,
|
verifyingContract: this.mallAddress,
|
||||||
},
|
},
|
||||||
message: signMsg,
|
message: signMsg,
|
||||||
}
|
};
|
||||||
const signature = await this.signData(signObj, buyerAddress)
|
const signature = await this.signData(signObj, buyerAddress);
|
||||||
return { nonce, signature }
|
return { nonce, signature };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async increaseAllowance(address: string, price: string) {
|
public async increaseAllowance(address: string, price: string) {
|
||||||
const coinInstance: any = await this.getContractInstance(address)
|
const coinInstance: any = await this.getContractInstance(address);
|
||||||
const res = await coinInstance.methods
|
const res = await coinInstance.methods
|
||||||
.increaseAllowance(this.mallAddress, price)
|
.increaseAllowance(this.mallAddress, price)
|
||||||
.send({ gas: 1000000 })
|
.send({ gas: 1000000 });
|
||||||
console.log('increaseAllowance: ', res)
|
console.log("increaseAllowance: ", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -458,35 +476,35 @@ export class Blockchain {
|
|||||||
* @return {Promise<any>}
|
* @return {Promise<any>}
|
||||||
*/
|
*/
|
||||||
public async getBalance(address: string, account: string | null) {
|
public async getBalance(address: string, account: string | null) {
|
||||||
account = account || UserModule.accountId
|
account = account || UserModule.accountId;
|
||||||
const coinInstance: any = await this.getContractInstance(address)
|
const coinInstance: any = await this.getContractInstance(address);
|
||||||
return await coinInstance.methods.balanceOf(account).call()
|
return await coinInstance.methods.balanceOf(account).call();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async transferToAccount(
|
public async transferToAccount(
|
||||||
account: string,
|
account: string,
|
||||||
amount: number,
|
amount: number,
|
||||||
address: string,
|
address: string
|
||||||
) {
|
) {
|
||||||
const amountBN = this.web3.utils.toBN(this.web3.utils.toWei(amount + ''))
|
const amountBN = this.web3.utils.toBN(this.web3.utils.toWei(amount + ""));
|
||||||
const coinInstance: any = await this.getContractInstance(address)
|
const coinInstance: any = await this.getContractInstance(address);
|
||||||
return coinInstance.methods
|
return coinInstance.methods
|
||||||
.transfer(account, amountBN)
|
.transfer(account, amountBN)
|
||||||
.send({ gas: 1000000 })
|
.send({ gas: 1000000 });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async signData(signObj: any, signer: string) {
|
public async signData(signObj: any, signer: string) {
|
||||||
const msgParams = JSON.stringify(signObj)
|
const msgParams = JSON.stringify(signObj);
|
||||||
const from = signer
|
const from = signer;
|
||||||
console.log('clicked, sending personal sign req', 'from', from, msgParams)
|
console.log("clicked, sending personal sign req", "from", from, msgParams);
|
||||||
const params = [from, msgParams]
|
const params = [from, msgParams];
|
||||||
const result: any = await this.sendCmd({
|
const result: any = await this.sendCmd({
|
||||||
method: 'eth_signTypedData_v4',
|
method: "eth_signTypedData_v4",
|
||||||
params,
|
params,
|
||||||
from,
|
from,
|
||||||
})
|
});
|
||||||
console.log(result)
|
console.log(result);
|
||||||
return result.result
|
return result.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendCmd({ method, params, from }: any) {
|
public async sendCmd({ method, params, from }: any) {
|
||||||
@ -501,12 +519,12 @@ export class Blockchain {
|
|||||||
},
|
},
|
||||||
async function (err: any, result: any) {
|
async function (err: any, result: any) {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject && reject(err)
|
reject && reject(err);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
resolve && resolve(result)
|
resolve && resolve(result);
|
||||||
},
|
}
|
||||||
)
|
);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,9 @@ export class JCChain {
|
|||||||
return !!UserModule.token;
|
return !!UserModule.token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get token() {
|
||||||
|
return UserModule.token;
|
||||||
|
}
|
||||||
get account() {
|
get account() {
|
||||||
return UserModule.accountId;
|
return UserModule.accountId;
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
import Cookies from 'js-cookie'
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
const languageKey = 'language'
|
const languageKey = "language";
|
||||||
export const getLanguage = () => Cookies.get(languageKey)
|
export const getLanguage = () => Cookies.get(languageKey);
|
||||||
export const setLanguage = (language: string) =>
|
export const setLanguage = (language: string) =>
|
||||||
Cookies.set(languageKey, language)
|
Cookies.set(languageKey, language);
|
||||||
|
|
||||||
const sizeKey = 'size'
|
const sizeKey = "size";
|
||||||
export const getSize = () => Cookies.get(sizeKey)
|
export const getSize = () => Cookies.get(sizeKey);
|
||||||
export const setSize = (size: string) => Cookies.set(sizeKey, size)
|
export const setSize = (size: string) => Cookies.set(sizeKey, size);
|
||||||
|
|
||||||
const miniKey = 'mini'
|
const miniKey = "mini";
|
||||||
export const getMini = () => Cookies.get(miniKey)
|
export const getMini = () => Cookies.get(miniKey);
|
||||||
export const setMini = (mini: string) => Cookies.set(miniKey, mini)
|
export const setMini = (mini: string) => Cookies.set(miniKey, mini);
|
||||||
|
|
||||||
// User
|
// User
|
||||||
const tokenKey = 'vue_typescript_access_token'
|
const tokenKey = "vue_typescript_access_token";
|
||||||
// export const getToken = () => localStorage.getItem(tokenKey)
|
// export const getToken = () => localStorage.getItem(tokenKey)
|
||||||
// export const setToken = (token: string) => localStorage.setItem(tokenKey, token)
|
// export const setToken = (token: string) => localStorage.setItem(tokenKey, token)
|
||||||
// export const removeToken = () => localStorage.removeItem(tokenKey)
|
// export const removeToken = () => localStorage.removeItem(tokenKey)
|
||||||
@ -22,11 +22,26 @@ const tokenKey = 'vue_typescript_access_token'
|
|||||||
// export const getItem = (key: string) => localStorage.getItem(key)
|
// export const getItem = (key: string) => localStorage.getItem(key)
|
||||||
// export const setItem = (key: string, val: string) => localStorage.setItem(miniKey, val)
|
// export const setItem = (key: string, val: string) => localStorage.setItem(miniKey, val)
|
||||||
// export const removeItem = (key: string) => localStorage.removeItem(key)
|
// export const removeItem = (key: string) => localStorage.removeItem(key)
|
||||||
|
export const generateDomain = () => {
|
||||||
export const getToken = () => Cookies.get(tokenKey)
|
let host = location.host;
|
||||||
export const setToken = (token: string) => Cookies.set(tokenKey, token, { domain: 'cebg.games'})
|
let hostArr = host.split(".");
|
||||||
export const removeToken = () => Cookies.remove(tokenKey)
|
if (host.length > 1) {
|
||||||
|
host = hostArr.slice(hostArr.length - 2).join(".");
|
||||||
export const getItem = (key: string) => Cookies.get(key)
|
}
|
||||||
export const setItem = (key: string, val: string) => Cookies.set(key, val, { domain: 'cebg.games'})
|
return host;
|
||||||
export const removeItem = (key: string) => Cookies.remove(key)
|
};
|
||||||
|
export const getToken = () => Cookies.get(tokenKey);
|
||||||
|
export const setToken = (token: string) => {
|
||||||
|
const host = generateDomain();
|
||||||
|
Cookies.set(tokenKey, token, { domain: host });
|
||||||
|
};
|
||||||
|
export const removeToken = () =>
|
||||||
|
Cookies.remove(tokenKey, { domain: generateDomain() });
|
||||||
|
//TODO::
|
||||||
|
export const getItem = (key: string) => Cookies.get(key);
|
||||||
|
export const setItem = (key: string, val: string) => {
|
||||||
|
const host = generateDomain();
|
||||||
|
Cookies.set(key, val, { domain: host });
|
||||||
|
};
|
||||||
|
export const removeItem = (key: string) =>
|
||||||
|
Cookies.remove(key, { domain: generateDomain() });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user