Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add evm chain if not in wallet. #224

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion engine/paima-mw-core/src/wallets/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ async function switchChain(): Promise<boolean> {
decimals: getChainCurrencyDecimals(),
},
rpcUrls: [getChainUri()],
blockExplorerUrls: [getChainExplorerUri()],
// blockExplorerUrls: Chain not added with empty string.
blockExplorerUrls: getChainExplorerUri() ? [getChainExplorerUri()] : undefined,
});
return await verifyWalletChain();
} catch (addError) {
Expand Down
7 changes: 6 additions & 1 deletion engine/paima-providers/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ export class ProviderNotInitialized extends Error {
}
}
export class ProviderApiError extends Error {
constructor(message?: string) {
public code: number | undefined;

constructor(message?: string, code?: number) {
super(message);
this.name = 'ProviderApiError';
if (code) {
this.code = code;
}
}
}
14 changes: 9 additions & 5 deletions engine/paima-providers/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,19 @@ export class EvmProvider implements IProvider<EvmApi> {
const walletChain = await this.conn.api.request({ method: 'eth_chainId' });
return parseInt(walletChain as string, 16) === parseInt(this.gameInfo.gameChainId, 16);
} catch (e: any) {
throw new ProviderApiError(`[verifyWalletChain] error: ${e?.message}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ProviderApiError(`[verifyWalletChain] error: ${e?.message}`, e?.code);
}
};
addChain = async (newChain: AddEthereumChainParameter): Promise<void> => {
try {
await this.conn.api.request({
method: 'eth_chainId',
method: 'wallet_addEthereumChain',
acedward marked this conversation as resolved.
Show resolved Hide resolved
params: [newChain],
});
} catch (e: any) {
throw new ProviderApiError(`[addChain] error: ${e?.message}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ProviderApiError(`[addChain] error: ${e?.message}`, e?.code);
}
};
switchChain = async (hexChainId: string): Promise<void> => {
Expand All @@ -181,7 +183,8 @@ export class EvmProvider implements IProvider<EvmApi> {
params: [{ chainId: hexChainId }],
});
} catch (e: any) {
throw new ProviderApiError(`[switchChain] error: ${e?.message}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ProviderApiError(`[switchChain] error: ${e?.message}`, e?.code);
}
};
sendTransaction = async (tx: Record<string, any>): Promise<string> => {
Expand All @@ -199,7 +202,8 @@ export class EvmProvider implements IProvider<EvmApi> {
}
return hash;
} catch (e: any) {
throw new ProviderApiError(`[switchChain] error: ${e?.message}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ProviderApiError(`[switchChain] error: ${e?.message}`, e?.code);
acedward marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
6 changes: 3 additions & 3 deletions engine/paima-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ENV {
return process.env.CHAIN_URI || '';
}
static get CHAIN_NAME(): string {
return process.env.CHAIN_NAME || '';
return process.env.CHAIN_NAME || 'UNKNOWN_CHAIN_NAME';
}
static get CHAIN_ID(): number {
return parseInt(process.env.CHAIN_ID || '0', 10);
Expand All @@ -26,10 +26,10 @@ export class ENV {
return process.env.CHAIN_EXPLORER_URI || '';
}
static get CHAIN_CURRENCY_NAME(): string {
return process.env.CHAIN_CURRENCY_NAME || '';
return process.env.CHAIN_CURRENCY_NAME || 'UNKNOWN_CURRENCY_NAME';
}
static get CHAIN_CURRENCY_SYMBOL(): string {
return process.env.CHAIN_CURRENCY_SYMBOL || '';
return process.env.CHAIN_CURRENCY_SYMBOL || 'NONAME';
}
static get CHAIN_CURRENCY_DECIMALS(): number {
return parseInt(process.env.CHAIN_CURRENCY_DECIMALS || '0', 10);
Expand Down
Loading