diff --git a/README.md b/README.md new file mode 100644 index 0000000..54ad19d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ + +## 整合web3.js和walletconnect, 打包成独立的js包, 供页面使用 \ No newline at end of file diff --git a/src/chain/ChainManager.ts b/src/chain/ChainManager.ts index dde62e5..0287885 100644 --- a/src/chain/ChainManager.ts +++ b/src/chain/ChainManager.ts @@ -1,89 +1,93 @@ -import { Chain } from '../chain/Chain' -import { AVAILABLE_CHAINS, IChainData } from '../configs/config_chain' -import { AllChains } from '../configs/allchain' +import { Chain } from "../chain/Chain"; +import { AVAILABLE_CHAINS, IChainData } from "../configs/config_chain"; +import { AllChains } from "../configs/allchain"; import { ACTIVATE_PROXY_ABI, MYSTERY_BOX_ABI, MYSTERY_PROXY_ABI, -} from '../configs/contracts' -import { singleton } from '../decorator/singleton.decorator' -import { Blockchain } from './blockchain' -import { UserModule } from '../module/UserModule' -import { getNonce } from '../api/User' -import { EventBus, SHOW_ERR_MSG } from '../utils/event-bus' +} from "../configs/contracts"; +import { singleton } from "../decorator/singleton.decorator"; +import { Blockchain } from "./blockchain"; +import { UserModule } from "../module/UserModule"; +import { getNonce } from "../api/User"; +import { EventBus, SHOW_ERR_MSG } from "../utils/event-bus"; @singleton export default class ChainManager { - bc: Blockchain - instanceMap: Map - public chainMap: Map = new Map() - private _availableChains: Map = new Map() + bc: Blockchain; + instanceMap: Map; + public chainMap: Map = new Map(); + private _availableChains: Map = new Map(); constructor() { - this.bc = new Blockchain() - this.instanceMap = new Map() + this.bc = new Blockchain(); + this.instanceMap = new Map(); for (const data of AllChains) { - this.chainMap.set(data.id, data) + 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) + const d = this.chainMap.get(id); if (d) { - this._availableChains.set(id, d) + this._availableChains.set(id, d); } } } - return this._availableChains + return this._availableChains; } public async init() { if (this.bc.isWalletConnect) { try { - await this.bc.connect() - await this.getNance() + await this.bc.connect(); + await this.getNance(); } catch (err) { - console.log('connect chain error: ', err) + console.log("connect chain error: ", err); } } } get isLogined() { - return !!UserModule.token && !!UserModule.step + return !!UserModule.token && !!UserModule.step; } public async logout() { - await this.bc.disconnect() + await this.bc.disconnect(); } public get currentChain() { - return this.bc.currentChain + return this.bc.currentChain; + } + + public get currentAccount() { + return UserModule.accountId; } public async login() { if (!UserModule.step) { try { - await this.bc.connect(true) - await this.checkNance() + await this.bc.connect(true); + await this.checkNance(); } catch (err) { - EventBus.$emit(SHOW_ERR_MSG, err.message) - await Promise.reject(err) + EventBus.$emit(SHOW_ERR_MSG, err.message); + await Promise.reject(err); } } } public async checkNance() { try { - let nonce = UserModule.nonce + let nonce = UserModule.nonce; if (!nonce) { const preRequest: any = await getNonce({ account: UserModule.accountId, net_id: UserModule.chainId, - }) - nonce = preRequest.nonce + '' - UserModule.updateNonce(nonce) + }); + nonce = preRequest.nonce + ""; + UserModule.updateNonce(nonce); } await UserModule.Login({ @@ -91,56 +95,56 @@ export default class ChainManager { account: UserModule.accountId, chainId: UserModule.chainId, nonce, - }) - UserModule.updateStep(1) + }); + UserModule.updateStep(1); } catch (err) { - console.log(err) - await Promise.reject(err) + console.log(err); + await Promise.reject(err); } } public async getNance() { - console.log('need get nance') + console.log("need get nance"); try { const preRequest: any = await getNonce({ account: UserModule.accountId, net_id: UserModule.chainId, - }) - console.log('success get nonce: ', preRequest) + }); + console.log("success get nonce: ", preRequest); // if need check sign and has nonce val, store it if (preRequest.state) { - UserModule.updateStep(1) + UserModule.updateStep(1); } else if (!preRequest.state && preRequest.nonce) { if (UserModule.token) { - await UserModule.LogOut() + await UserModule.LogOut(); } - UserModule.updateStep(0) + UserModule.updateStep(0); } if (preRequest.nonce) { - UserModule.updateNonce(preRequest.nonce + '') + UserModule.updateNonce(preRequest.nonce + ""); } } catch (err) { - EventBus.$emit(SHOW_ERR_MSG, 'Error get login nonce') + EventBus.$emit(SHOW_ERR_MSG, "Error get login nonce"); } } public async getInstance(address: string, chainId: number, abi?: any) { - const key = `${chainId}_${address}` + 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) + 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) + return this.instanceMap.get(key); } public async getBalance(address: string, chainId: number) { - const coinInstance = await this.getInstance(address, chainId) + const coinInstance = await this.getInstance(address, chainId); const balance = await coinInstance.methods .balanceOf(UserModule.accountId) - .call() - console.log('balance: ', balance) - return balance + .call(); + console.log("balance: ", balance); + return balance; } /** @@ -154,13 +158,13 @@ export default class ChainManager { const coinInstance = await this.getInstance( address, chainId, - MYSTERY_BOX_ABI, - ) + MYSTERY_BOX_ABI + ); const balance = await coinInstance.methods .balanceOf(UserModule.accountId) - .call() - console.log('nft balance: ', balance) - return balance + .call(); + console.log("nft balance: ", balance); + return balance; } /** @@ -174,16 +178,16 @@ export default class ChainManager { address: string, chainId: number, start = 0, - page = 8, + page = 8 ) { const nftInstance = await this.getInstance( address, chainId, - MYSTERY_BOX_ABI, - ) + MYSTERY_BOX_ABI + ); return nftInstance.methods .userTokens(UserModule.accountId, start, page) - .call() + .call(); } /** @@ -196,20 +200,20 @@ export default class ChainManager { public async getNftIdOfIndex( address: string, chainId: number, - index: number, + index: number ) { const nftInstance = await this.getInstance( address, chainId, - MYSTERY_BOX_ABI, - ) + MYSTERY_BOX_ABI + ); const nftId = await nftInstance.methods .tokenOfOwnerByIndex(UserModule.accountId, index) - .call() + .call(); console.log( - `address: ${address}, chainId: ${chainId}, index: ${index}, token: ${nftId}`, - ) - return nftId + `address: ${address}, chainId: ${chainId}, index: ${index}, token: ${nftId}` + ); + return nftId; } /** @@ -226,16 +230,16 @@ export default class ChainManager { boxId: string, tokenIds: number[], nonce: string, - signature: string, + signature: string ) { const proxyInstance = await this.bc.getContractInstance( address, - MYSTERY_PROXY_ABI, - ) + MYSTERY_PROXY_ABI + ); // get transactionHash and upload to server for verify return proxyInstance.methods .openBox(boxId, tokenIds, nonce, signature) - .send({ gas: 1000000 }) + .send({ gas: 1000000 }); } /** @@ -254,19 +258,19 @@ export default class ChainManager { nftNew: string, nftType: number, nonce: string, - signature: string, + signature: string ) { const nftProxyInstance = await this.bc.getContractInstance( address, - ACTIVATE_PROXY_ABI, - ) + ACTIVATE_PROXY_ABI + ); const gas = await nftProxyInstance.methods .activateOne(nftOld, nftNew, nftType, nonce, signature) - .estimateGas({ gas: 1000000 }) - console.log('nftProxyInstance activateOne need gas: ', gas) + .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 }) + .send({ gas: (gas * 1.1) | 0 }); } public async transferToAccount({ @@ -275,27 +279,27 @@ export default class ChainManager { chainId, address, }: { - to: string - amount: number - chainId: number - address: string + to: string; + amount: number; + chainId: number; + address: string; }) { - const self = this + 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) + resolve && resolve(res); }) .catch((err) => { - reject && reject(err) - }) - }) - }) + reject && reject(err); + }); + }); + }); } else { - return this.bc.transferToAccount(to, amount, address) + return this.bc.transferToAccount(to, amount, address); } } } diff --git a/src/index.ts b/src/index.ts index 006aec4..216b2b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,27 +1,35 @@ -import ChainManager from './chain/ChainManager' -import { singleton } from './decorator/singleton.decorator' -import { UserModule } from './module/UserModule' +import ChainManager from "./chain/ChainManager"; +import { singleton } from "./decorator/singleton.decorator"; +import { UserModule } from "./module/UserModule"; @singleton export class JCChain { - chainManager = new ChainManager() + chainManager = new ChainManager(); public login() { - return this.chainManager.login() + return this.chainManager.login(); } public selectWalletAndLogin(walletType: number) { - this.chainManager.bc.walletType = walletType - return this.chainManager.login() + this.chainManager.bc.walletType = walletType; + return this.chainManager.login(); } logout() { - return this.chainManager.logout() + return this.chainManager.logout(); } get logined() { - return !!UserModule.token + return !!UserModule.token; + } + + get account() { + return UserModule.accountId; + } + + get chainId() { + return UserModule.chainId; } } -export * from './utils/event-bus' +export * from "./utils/event-bus";