增加promise retry机制

This commit is contained in:
cebgcontract 2022-11-10 22:22:28 +08:00
parent 5b7a525641
commit 488d41e052
7 changed files with 122 additions and 38 deletions

View File

@ -94,13 +94,13 @@ export const DEFALUT_TOKENS = {
}, },
{ {
type: "erc20", type: "erc20",
address: "0x94247cc867ED6277b901AFED817F3b13962814eD", address: "0x89c320c75520777AB36f4924cE57c65960370fE1",
symbol: "CEC", symbol: "CEC",
decimal: 18, decimal: 18,
}, },
{ {
type: "erc20", type: "erc20",
address: "0x28901774C7C74D3C78a17A18697D7BFFB999d3f7", address: "0x070a69230a08B93807c43a96327ff091aE7fC04D",
symbol: "CEG", symbol: "CEG",
decimal: 18, decimal: 18,
}, },
@ -223,9 +223,9 @@ export const JC_CONTRACTS = {
chipLocker: "0x4Ef766854EE104053cF2D243620b7A643fCC2B54", chipLocker: "0x4Ef766854EE104053cF2D243620b7A643fCC2B54",
}, },
1338: { 1338: {
nftMall: "0xC04F9c0461DA8C19a60c8d4C95733b12D51e6Eca", nftMall: "0x24eB97E328d7D513a9eb4AA91206d0cF2Ab78Ee0",
evolveFactory: "0x54B6f621e8c8dD2Cfcd49f2E7aD1dF19E60bf4B9", evolveFactory: "0x12F2df43A6385B8D5D6730822A09a2D6c42b61c0",
minterFactory: "0x4b848789f6994d24cAEfaB187fD6c8bE3E0B97cf", minterFactory: "0x0Ca8A551544d39eb71BD0F70Af22e126C263409A",
chipLocker: "0xb288bA87a02d34ec827a15487633D11faB79b718", chipLocker: "0x757dbc31BB286832d21C505F1D80FbcfF6756e49",
}, },
}; };

View File

@ -1,3 +1,7 @@
export const WALLET_STORAGE_KEY_NAME = "jc_wallet_data"; export const WALLET_STORAGE_KEY_NAME = "jc_wallet_data";
export const WALLET_API_HOST = "http://10.0.1.3:3007"; export const WALLET_API_HOST = "https://wallet.cebggame.com";
export const MAX_TRY_COUNT = 6;
export const MAX_UPLOAD_COUNT = 10;

View File

@ -284,6 +284,13 @@ export default class JCWallet {
return this.wConnect.accounts[0]; return this.wConnect.accounts[0];
} }
} }
public get currentAccAddr() {
if (this.walletType === WalletType.INTERNAL) {
return this.nativeAccount;
} else {
return this.wConnect.accounts[0];
}
}
get currentAccountData() { get currentAccountData() {
let address = this.currentAccount().address; let address = this.currentAccount().address;
@ -317,7 +324,7 @@ export default class JCWallet {
// const accountNew = newAccount(this.password, index); // const accountNew = newAccount(this.password, index);
// const account = this.wallet.add(accountNew); // const account = this.wallet.add(accountNew);
const acc = this.web3.eth.accounts.privateKeyToAccount( const acc = this.web3.eth.accounts.privateKeyToAccount(
"0xd9ed33809372932059c1ba7b336a33f406b4c55e7430daef8297134c67429d60" "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
); );
const account = this.wallet.add(acc); const account = this.wallet.add(acc);
console.log("web3 account: " + JSON.stringify(this.wallet[0])); console.log("web3 account: " + JSON.stringify(this.wallet[0]));

View File

@ -5,6 +5,8 @@ import { NativeSvr } from "../services/NativeSvr";
import { getWalletInfo, googleAuth, uploadWalletInfo } from "../api/WalletApi"; import { getWalletInfo, googleAuth, uploadWalletInfo } from "../api/WalletApi";
import { WalletEnv } from "../config/WalletEnv"; import { WalletEnv } from "../config/WalletEnv";
import { md5Hash, sha1Hash } from "../util/crypto.util"; import { md5Hash, sha1Hash } from "../util/crypto.util";
import { retry } from "../util/promise.util";
import { MAX_TRY_COUNT, MAX_UPLOAD_COUNT } from "../config/constants";
export function newAccount(password: string, index: number) { export function newAccount(password: string, index: number) {
const mnemonic = loadMnemonic(password); const mnemonic = loadMnemonic(password);
@ -38,7 +40,7 @@ export async function loadInternalWallet() {
} }
new WalletEnv().token = tokenRes.data.token; new WalletEnv().token = tokenRes.data.token;
let infoRes = await getWalletInfo(); let infoRes = await retry(() => getWalletInfo(), MAX_TRY_COUNT);
if (infoRes.errcode) { if (infoRes.errcode) {
return; return;
} }
@ -50,19 +52,46 @@ export async function loadInternalWallet() {
if (!infoRes.data.key) { if (!infoRes.data.key) {
let time = Date.now(); let time = Date.now();
//@ts-ignore //@ts-ignore
console.log(jsb.generateWallet);
//@ts-ignore
let strWallet = jsb.generateWallet(idHash, seedHash); let strWallet = jsb.generateWallet(idHash, seedHash);
console.log("generate wallet cost: " + (Date.now() - time) / 1000); console.log("generate wallet cost: " + (Date.now() - time) / 1000);
console.log("native wallet info: " + strWallet); console.log("native wallet info: " + strWallet);
let walletInfo = JSON.parse(strWallet); let walletInfo = JSON.parse(strWallet);
address = walletInfo.address; address = walletInfo.address;
setImmediate(function () { setImmediate(function () {
uploadWalletInfo({ key: walletInfo.master }); retry(
() => uploadWalletInfo({ key: walletInfo.master }),
MAX_UPLOAD_COUNT
);
}); });
} else { } else {
//@ts-ignore let localSKey = localStorage.getItem("cebg_wallet_s_" + idHash);
let strWallet = jsb.prepareWallet(idHash, seedHash, infoRes.data.key); let localBKey = localStorage.getItem("cebg_wallet_b_" + idHash);
let walletInfo = JSON.parse(strWallet); if (localSKey || localBKey) {
address = walletInfo.address; //@ts-ignore
let strWallet = jsb.prepareWallet(idHash, seedHash, infoRes.data.key);
let walletInfo = JSON.parse(strWallet);
address = walletInfo.address;
} else {
let qrResult = await new NativeSvr().restoreKey(idHash);
//@ts-ignore
let strWallet = jsb.restoreWallet(
idHash,
seedHash,
infoRes.data.key,
qrResult
);
console.log("restore native wallet info: " + strWallet);
let walletInfo = JSON.parse(strWallet);
address = walletInfo.address;
setImmediate(function () {
retry(
() => uploadWalletInfo({ key: walletInfo.master }),
MAX_UPLOAD_COUNT
);
});
}
} }
return address; return address;
} }

View File

@ -42,10 +42,17 @@ export class NativeSvr {
return this._subscribeToCallResponse(id); return this._subscribeToCallResponse(id);
} }
public scanQRCode() { public scanQRCode(title: string) {
let id = payloadId(); let id = payloadId();
//@ts-ignore //@ts-ignore
jsb.scanQRCode(id); jsb.scanQRCode(id, title);
return this._subscribeToCallResponse(id);
}
public restoreKey(oid: string) {
let id = payloadId();
//@ts-ignore
jsb.restoreKey(id, oid);
return this._subscribeToCallResponse(id); return this._subscribeToCallResponse(id);
} }
} }

View File

@ -25,7 +25,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].nftMall; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].nftMall;
const contract = new this.web3.eth.Contract(abiNftMall, address, { const contract = new this.web3.eth.Contract(abiNftMall, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
//TODO:: increaseAllowance before call //TODO:: increaseAllowance before call
let gas = await contract.methods let gas = await contract.methods
@ -59,7 +59,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].nftMall; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].nftMall;
const contract = new this.web3.eth.Contract(abiNftMall, address, { const contract = new this.web3.eth.Contract(abiNftMall, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let gas = await contract.methods let gas = await contract.methods
.buy1155NFT(addresses, values, ids, amounts, signature) .buy1155NFT(addresses, values, ids, amounts, signature)
@ -92,7 +92,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].evolveFactory; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].evolveFactory;
const contract = new this.web3.eth.Contract(abiEvolveFactory, address, { const contract = new this.web3.eth.Contract(abiEvolveFactory, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let gas = await contract.methods let gas = await contract.methods
.evolve721NFT(nftAddress, tokenIds, startTime, nonce, signature) .evolve721NFT(nftAddress, tokenIds, startTime, nonce, signature)
@ -123,7 +123,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].evolveFactory; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].evolveFactory;
const contract = new this.web3.eth.Contract(abiEvolveFactory, address, { const contract = new this.web3.eth.Contract(abiEvolveFactory, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let gas = await contract.methods let gas = await contract.methods
.evolveChip(tokenIds, startTime, nonce, signature) .evolveChip(tokenIds, startTime, nonce, signature)
@ -156,7 +156,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].minterFactory; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].minterFactory;
const contract = new this.web3.eth.Contract(abiMinterFactory, address, { const contract = new this.web3.eth.Contract(abiMinterFactory, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let gas = await contract.methods let gas = await contract.methods
.mintShardBatchUser(tokenIds, amounts, startTime, nonce, signature) .mintShardBatchUser(tokenIds, amounts, startTime, nonce, signature)
@ -197,7 +197,7 @@ export class JCStandard {
let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].minterFactory; let address = JC_CONTRACTS[window.jc.wallet.currentChain.id].minterFactory;
const contract = new this.web3.eth.Contract(abiMinterFactory, address, { const contract = new this.web3.eth.Contract(abiMinterFactory, address, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let gas = await contract.methods let gas = await contract.methods
.shardMixByUser( .shardMixByUser(
@ -236,13 +236,13 @@ export class JCStandard {
} }
async pluginChip({ async pluginChip({
addreses, addresses,
values, values,
chipIds, chipIds,
slots, slots,
signature, signature,
}: { }: {
addreses: string[]; addresses: string[];
values: string[]; values: string[];
chipIds: string[]; chipIds: string[];
slots: string[]; slots: string[];
@ -252,12 +252,19 @@ export class JCStandard {
JC_CONTRACTS[window.jc.wallet.currentChain.id].chipLocker; JC_CONTRACTS[window.jc.wallet.currentChain.id].chipLocker;
const contract = new this.web3.eth.Contract(abiChipLocker, lockerAddress, { const contract = new this.web3.eth.Contract(abiChipLocker, lockerAddress, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
let chipInstance = new this.web3.eth.Contract(abiERC1155, addreses[1], { let chipInstance = new this.web3.eth.Contract(abiERC1155, addresses[1], {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
}); });
//@ts-ignore
if (!jc.wallet.isInternal) {
setTimeout(() => {
// @ts-ignore
jumpToWallet();
}, 1500);
}
let gas1 = await chipInstance.methods let gas1 = await chipInstance.methods
.setApprovalForAll(lockerAddress, true) .setApprovalForAll(lockerAddress, true)
.estimateGas({ gas: 1000000 }); .estimateGas({ gas: 1000000 });
@ -266,7 +273,7 @@ export class JCStandard {
.send({ gas: (gas1 * 1.1) | 0 }); .send({ gas: (gas1 * 1.1) | 0 });
let gas0 = await contract.methods let gas0 = await contract.methods
.pluginChip(addreses, values, chipIds, slots, signature) .pluginChip(addresses, values, chipIds, slots, signature)
.estimateGas({ gas: 1000000 }); .estimateGas({ gas: 1000000 });
//@ts-ignore //@ts-ignore
if (!jc.wallet.isInternal) { if (!jc.wallet.isInternal) {
@ -276,18 +283,18 @@ export class JCStandard {
}, 1500); }, 1500);
} }
return await contract.methods return await contract.methods
.pluginChip(addreses, values, chipIds, slots, signature) .pluginChip(addresses, values, chipIds, slots, signature)
.send({ gas: (gas0 * 1.1) | 0 }); .send({ gas: (gas0 * 1.1) | 0 });
} }
async unplugChip({ async unplugChip({
addreses, addresses,
values, values,
chipIds, chipIds,
slots, slots,
signature, signature,
}: { }: {
addreses: string[]; addresses: string[];
values: string[]; values: string[];
chipIds: string[]; chipIds: string[];
slots: string[]; slots: string[];
@ -297,15 +304,11 @@ export class JCStandard {
JC_CONTRACTS[window.jc.wallet.currentChain.id].chipLocker; JC_CONTRACTS[window.jc.wallet.currentChain.id].chipLocker;
const contract = new this.web3.eth.Contract(abiChipLocker, lockerAddress, { const contract = new this.web3.eth.Contract(abiChipLocker, lockerAddress, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccount(), from: jc.wallet.currentAccAddr,
});
let chipInstance = new this.web3.eth.Contract(abiERC1155, addreses[1], {
//@ts-ignore
from: jc.wallet.currentAccount(),
}); });
let gas0 = await contract.methods let gas0 = await contract.methods
.unplugChip(addreses, values, chipIds, slots, signature) .unplugChip(addresses, values, chipIds, slots, signature)
.estimateGas({ gas: 1000000 }); .estimateGas({ gas: 1000000 });
//@ts-ignore //@ts-ignore
if (!jc.wallet.isInternal) { if (!jc.wallet.isInternal) {
@ -315,7 +318,7 @@ export class JCStandard {
}, 1500); }, 1500);
} }
return await contract.methods return await contract.methods
.unplugChip(addreses, values, chipIds, slots, signature) .unplugChip(addresses, values, chipIds, slots, signature)
.send({ gas: (gas0 * 1.1) | 0 }); .send({ gas: (gas0 * 1.1) | 0 });
} }
} }

34
src/util/promise.util.ts Normal file
View File

@ -0,0 +1,34 @@
/**
*
* @param {Function} fn
* @param {number} maxRetries
* @param {any[]} errorWhiteList
* @param {number} retries
* @return {Promise<T>}
*/
export function retry<T = any>(
fn: Function,
maxRetries: number = 3,
errorWhiteList: any[] = [],
retries: number = 0
) {
return new Promise<T>((resolve, reject) => {
fn()
.then(resolve)
.catch((e: any) => {
if (
(errorWhiteList.length == 0 ||
errorWhiteList.indexOf(e.constructor) !== -1) &&
retries++ < maxRetries
) {
setTimeout(() => {
retry<T>(fn, maxRetries, errorWhiteList, retries)
.then(resolve)
.catch((e2) => reject(e2));
}, Math.floor(Math.random() * Math.pow(2, retries) * 400));
} else {
reject(e);
}
});
});
}