Skip to content

Commit

Permalink
fix: httpProvider isReady bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZhangReal committed Apr 15, 2023
1 parent 89330b6 commit 0e3f4a1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion manta-js/package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "manta-extension-sdk",
"version": "1.0.15",
"version": "1.0.17",
"description": "manta.js sdk",
"main": "./dist/browser/index.js",
"exports": {
Expand Down
47 changes: 30 additions & 17 deletions manta-js/package/src/BaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,37 @@ export default class BaseWallet implements IBaseWallet {
rpc: mantaConfig.RPC,
});

if (this.loggingEnabled) {
const queryChainInfo = () => {
Promise.all([
this.api.rpc.system.chain(),
this.api.rpc.system.name(),
this.api.rpc.system.version(),
]).then(([chain, nodeName, nodeVersion]) => {
this.log(
`Wallet is connected to chain ${chain} using ${nodeName} v${nodeVersion}, isHttpProvider: ${this.isHttpProvider}`,
);
});
};
if (this.isHttpProvider) {
this.api.isReady.then(queryChainInfo);
} else {
this.api.on('connected', queryChainInfo);
}
return this.api;
}

// @ts-ignore
async isApiReady() {
if (!this.isHttpProvider) {
return this.api.isReady;
}

// see detail https://github.com/polkadot-js/api/blob/master/packages/api/src/base/Init.ts#L413
// HttpProvider has no retry logic, so it is necessary to resend the meta information request
// to ensure that the api initialization is successful

// At present, this solution is only a temporary solution,
// and this problem will be solved by submitting pr to polkadot/api later

// @ts-ignore
if (this.api._isReady) {
return this.api;
}

// @ts-ignore
const metaData = await this.api._loadMeta();
if (!metaData) {
throw new Error('Metadata initialization failed');
}
// @ts-ignore
this.api._isReady = true;

// @ts-ignore
this.api.emit('ready', this.api);
return this.api;
}

Expand Down
2 changes: 2 additions & 0 deletions manta-js/package/src/PrivateWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export default class PrivateWallet implements IPrivateWallet {
}
const result = await this.wrapWalletIsBusy(async () => {
this.log('Start initial new account');
await this.baseWallet.isApiReady();
await this.wasmWallet.initial_sync(this.getWasmNetWork());
this.log('Initial new account completed');
await this.saveStateToLocal();
Expand Down Expand Up @@ -365,6 +366,7 @@ export default class PrivateWallet implements IPrivateWallet {
try {
const syncType =
this.palletName === 'mantaSBT' ? 'sbt_sync_partial' : 'sync_partial';
await this.baseWallet.isApiReady();
const result = await this.wasmWallet[syncType](this.getWasmNetWork());
await this.saveStateToLocal();
return {
Expand Down
1 change: 1 addition & 0 deletions manta-js/package/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface IBaseWallet {
getStorageStateFromLocal: GetStorageStateFromLocal;
walletIsBusy: boolean;
updateApi(apiEndpoint: string | string[], apiTimeout?: number): ApiPromise;
isApiReady(): Promise<ApiPromise>;
disconnectApi(): Promise<boolean>;
log(message: string, name?: string): void;
}
Expand Down
3 changes: 0 additions & 3 deletions manta-js/package/src/ledger-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default class LedgerApi implements ILedgerApi {
// Pulls data from the ledger from the `checkpoint`
async initial_pull(checkpoint: any) {
try {
await this.api.isReady;
// @ts-ignore
const result = await this.api.rpc[this.palletName].dense_initial_pull(
checkpoint,
Expand Down Expand Up @@ -89,8 +88,6 @@ export default class LedgerApi implements ILedgerApi {
// Pulls data from the ledger from the `checkpoint`
async pull(checkpoint: any) {
try {
await this.api.isReady;

if (this.loggingEnabled) {
this._log('checkpoint ' + JSON.stringify(checkpoint));
}
Expand Down
2 changes: 2 additions & 0 deletions manta-js/package/src/pallets/MantaPayWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class MantaPayWallet
): Promise<SignedTransaction | null> {
const result = await this.wrapWalletIsBusy(
async () => {
await this.baseWallet.isApiReady();
const transaction = await privateTransferBuildUnsigned(
this.wasm,
this.api,
Expand Down Expand Up @@ -113,6 +114,7 @@ export default class MantaPayWallet
): Promise<SignedTransaction | null> {
const result = await this.wrapWalletIsBusy(
async () => {
await this.baseWallet.isApiReady();
const transaction = await toPublicBuildUnsigned(
this.wasm,
this.api,
Expand Down
1 change: 0 additions & 1 deletion manta-js/package/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export async function getAssetMetadata(
assetId: BN,
network: Network,
): Promise<any> {
await api.isReady;
const data: any = await api.query.assetManager.assetIdMetadata(assetId);
const json = JSON.stringify(data.toHuman());
const jsonObj = JSON.parse(json);
Expand Down

0 comments on commit 0e3f4a1

Please sign in to comment.