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 all commits
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
6 changes: 5 additions & 1 deletion engine/paima-mw-core/src/wallets/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ async function switchChain(): Promise<boolean> {
decimals: getChainCurrencyDecimals(),
},
rpcUrls: [getChainUri()],
blockExplorerUrls: [getChainExplorerUri()],
// blockExplorerUrls: Chain not added with empty string.
blockExplorerUrls: getChainExplorerUri() ? [getChainExplorerUri()] : undefined,
});
await EvmConnector.instance()
.getOrThrowProvider()
.switchChain(hexChainId);
return await verifyWalletChain();
} catch (addError) {
errorFxn(PaimaMiddlewareErrorCode.ERROR_ADDING_CHAIN, 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;
}
}
}
18 changes: 11 additions & 7 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 @@ -192,14 +195,15 @@ export class EvmProvider implements IProvider<EvmApi> {
params: [tx],
});
if (typeof hash !== 'string') {
console.log('[sendWalletTransaction] invalid signature:', hash);
console.log('[sendTransaction] invalid signature:', hash);
throw new ProviderApiError(
`[sendWalletTransaction] Received "hash" of type ${typeof hash}`
`[sendTransaction] Received "hash" of type ${typeof hash}`
);
}
return hash;
} catch (e: any) {
throw new ProviderApiError(`[switchChain] error: ${e?.message}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ProviderApiError(`[sendTransaction] error: ${e?.message}`, e?.code);
}
};
}
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