增加add account的功能
This commit is contained in:
parent
3ea26dfbd7
commit
c87a6c1706
@ -4,7 +4,7 @@ import sth = require("./lib/ethSigUtil");
|
||||
import('./lib/fetch');
|
||||
import { ZError } from "./common/ZError";
|
||||
import { AllChains } from "./data/allchain";
|
||||
import { createWalletEvents, WALLET_CHAIN_CHANGE, WALLET_TOKEN_TYPE_CHANGE } from "./common/WalletEvent";
|
||||
import { createWalletEvents, WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_TOKEN_TYPE_CHANGE } from "./common/WalletEvent";
|
||||
import { ERC20Standard } from "./standards/ERC20Standard";
|
||||
import { ERC721Standard } from "./standards/ERC721Standard";
|
||||
import { IAccount, initAccount } from "./data/DataModel";
|
||||
@ -35,10 +35,10 @@ export interface IChainData {
|
||||
@singleton
|
||||
export default class JCWallet {
|
||||
web3: Web3 = null
|
||||
wallet: any = null
|
||||
password: string = '111111'
|
||||
chainSet: Set<number> = new Set()
|
||||
chainMap: Map<number, IChainData> = new Map()
|
||||
private wallet: any = null
|
||||
private password: string = '111111'
|
||||
private chainSet: Set<number> = new Set()
|
||||
private chainMap: Map<number, IChainData> = new Map()
|
||||
private _currentChain: IChainData
|
||||
public erc20Standard: ERC20Standard
|
||||
public erc721Standard: ERC721Standard
|
||||
@ -46,6 +46,7 @@ export default class JCWallet {
|
||||
private dataManage = new DataManage()
|
||||
public data: IAccount[] = []
|
||||
public iconType = 'jazz'
|
||||
private accountIndex = 0
|
||||
|
||||
constructor() {
|
||||
// this.web3 = new Web3('https://rpc-mainnet.kcc.network')
|
||||
@ -89,8 +90,10 @@ export default class JCWallet {
|
||||
if (data) {
|
||||
return data
|
||||
}
|
||||
data = initAccount(address, chain)
|
||||
let accountName = `Account${this.wallet.length}`
|
||||
data = initAccount(address, chain, accountName)
|
||||
this.data.push(data)
|
||||
this.dataManage.saveData(this.data)
|
||||
return data
|
||||
}
|
||||
|
||||
@ -126,14 +129,22 @@ export default class JCWallet {
|
||||
|
||||
}
|
||||
|
||||
public currentAccount() {
|
||||
return this.wallet[0];
|
||||
public currentAccount(): IAccount {
|
||||
return this.wallet[this.accountIndex];
|
||||
}
|
||||
|
||||
public accounts() {
|
||||
|
||||
}
|
||||
|
||||
public createAccount() {
|
||||
let account = this.web3.eth.accounts.create()
|
||||
this.wallet.add(account)
|
||||
this.wallet.save(this.password)
|
||||
this.accountIndex = this.wallet.length - 1
|
||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE)
|
||||
}
|
||||
|
||||
public importAccount(privateKey: string) {
|
||||
|
||||
}
|
||||
@ -148,7 +159,7 @@ export default class JCWallet {
|
||||
public async getBalance(address?: string) {
|
||||
console.log('get balance with address: ', address);
|
||||
if (!address) {
|
||||
let accountData = this.wallet[0]
|
||||
let accountData = this.wallet[this.accountIndex]
|
||||
if (!accountData) {
|
||||
throw new ZError(10, 'no account found')
|
||||
}
|
||||
|
@ -37,10 +37,11 @@ export interface IAccount {
|
||||
chips: INFT[]
|
||||
}
|
||||
|
||||
export function initAccount(address: string, chain: number): IAccount {
|
||||
export function initAccount(address: string, chain: number, nickname: string): IAccount {
|
||||
let data: IAccount = {
|
||||
address,
|
||||
chain,
|
||||
nickname,
|
||||
tokens: [],
|
||||
heros: [],
|
||||
weapons: [],
|
||||
|
@ -1,10 +1,11 @@
|
||||
import { ZError } from "../common/ZError";
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
|
||||
@ccclass
|
||||
export default class MenuPanel extends cc.Component {
|
||||
export default class MenuPanel extends WalletBase {
|
||||
|
||||
@property(cc.Prefab)
|
||||
btnPreb: cc.Prefab = null;
|
||||
@ -32,6 +33,7 @@ export default class MenuPanel extends cc.Component {
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
super.start()
|
||||
this.layoutBtns()
|
||||
}
|
||||
|
||||
@ -64,8 +66,8 @@ export default class MenuPanel extends cc.Component {
|
||||
}
|
||||
|
||||
onAddAccount() {
|
||||
console.log(this.inited)
|
||||
console.log('onAddAccount: ')
|
||||
this.wallet.createAccount()
|
||||
this.onCloseClick()
|
||||
}
|
||||
onImportAccount() {
|
||||
console.log('onImportAccount: ')
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { WALLET_CHAIN_CHANGE } from "../common/WalletEvent";
|
||||
import { WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE } from "../common/WalletEvent";
|
||||
import { DEFAULT_NFT_TYPES } from "../config/chain_config";
|
||||
import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup";
|
||||
import TextBtn from "./comp/TextBtn";
|
||||
@ -31,6 +31,7 @@ export default class TokenTab extends WalletBase {
|
||||
this.onTokenTypeChange.bind(this))
|
||||
this.updateAllBtn();
|
||||
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.updateAllBtn.bind(this))
|
||||
this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.updateAllBtn.bind(this))
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
|
@ -1,10 +1,6 @@
|
||||
import { formatPrice } from "../common/chain.util";
|
||||
import { WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent";
|
||||
import { Bind } from "../decorator/AutoUpdateUI";
|
||||
import JCWallet, { IChainData } from "../JCWallet";
|
||||
import { WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent";
|
||||
import { IChainData } from "../JCWallet";
|
||||
import { renderFromTokenMinimalUnit } from "../util/number.util";
|
||||
import HashIcon from "./comp/HashIcon";
|
||||
import JazzIcon from "./comp/JazzIcon";
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
|
||||
@ -19,6 +15,9 @@ export default class WalletInfo extends WalletBase {
|
||||
@property(cc.Label)
|
||||
balanceLabel: cc.Label = null;
|
||||
|
||||
@property(cc.Label)
|
||||
nameLabel: cc.Label = null;
|
||||
|
||||
@property(cc.Node)
|
||||
avatar: cc.Node = null;
|
||||
|
||||
@ -36,11 +35,13 @@ export default class WalletInfo extends WalletBase {
|
||||
this.updateUI()
|
||||
this.updateBalance()
|
||||
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this))
|
||||
this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.chainChange.bind(this))
|
||||
}
|
||||
|
||||
// update (dt) {}
|
||||
updateUI(): void {
|
||||
super.updateUI()
|
||||
this.nameLabel.string = this.wallet.currentAccountData.nickname || 'Account'
|
||||
this.addressLabel.string = this.formatAddress()
|
||||
this.balanceLabel.string = this.formatMoney()
|
||||
if (this.wallet.iconType === 'jazz') {
|
||||
@ -86,6 +87,10 @@ export default class WalletInfo extends WalletBase {
|
||||
chainChange(data: IChainData) {
|
||||
this.updateBalance()
|
||||
}
|
||||
onAccountChange() {
|
||||
this.accountId = this.wallet.currentAccount().address
|
||||
this.updateBalance()
|
||||
}
|
||||
showReceiveNode() {
|
||||
const address = this.wallet.currentAccount().address
|
||||
const chainId = this.wallet.currentChain.id
|
||||
|
@ -7872,10 +7872,12 @@
|
||||
"balanceLabel": {
|
||||
"__id__": 173
|
||||
},
|
||||
"nameLabel": {
|
||||
"__id__": 179
|
||||
},
|
||||
"avatar": {
|
||||
"__id__": 146
|
||||
},
|
||||
"parentNode": null,
|
||||
"_id": "8bO0XBvlFDPJ87FOgdOxUH"
|
||||
},
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user