From 376c06a7bd0ec66ee4d5a9417875f4ac71efc8ce Mon Sep 17 00:00:00 2001 From: pacoyang Date: Fri, 22 Dec 2023 14:13:49 +0800 Subject: [PATCH] feat: support for specifying evm rpc endpoint --- src/commands/add-evm-account.ts | 13 +------------ src/commands/create-brick-profile.ts | 19 ++++++++++++++++--- src/lib/PhatBaseCommand.ts | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/commands/add-evm-account.ts b/src/commands/add-evm-account.ts index 4d540cc..d12a0b9 100644 --- a/src/commands/add-evm-account.ts +++ b/src/commands/add-evm-account.ts @@ -1,6 +1,5 @@ import { Flags } from '@oclif/core' import { getContract } from '@phala/sdk' -import { createPublicClient, http } from 'viem' import PhatBaseCommand, { type ParsedFlags, type BrickProfileContract } from '../lib/PhatBaseCommand' @@ -25,17 +24,7 @@ export default class AddEvmAccount extends PhatBaseCommand { } // Verify the RPC endpoint - try { - this.action.start(`Verifying the RPC endpoint: ${evmRpcEndpoint}`) - const client = createPublicClient({ - transport: http(evmRpcEndpoint) - }) - await client.getChainId() - this.action.succeed() - } catch (error) { - this.action.fail('Failed to verify the RPC endpoint.') - return this.error(error as Error) - } + await this.verifyRpcEndpoint(evmRpcEndpoint) const pair = await this.getDecodedPair({ suri: this.parsedFlags.suri || process.env.POLKADOT_WALLET_SURI, diff --git a/src/commands/create-brick-profile.ts b/src/commands/create-brick-profile.ts index cdfd17f..6cff1d0 100644 --- a/src/commands/create-brick-profile.ts +++ b/src/commands/create-brick-profile.ts @@ -1,8 +1,9 @@ +import { Flags } from '@oclif/core' import type { Struct, u128 } from '@polkadot/types' import { PinkContractPromise, OnChainRegistry, type CertificateData } from '@phala/sdk' import { type KeyringPair } from '@polkadot/keyring/types' -import PhatBaseCommand, { type BrickProfileFactoryContract, type BrickProfileContract } from '../lib/PhatBaseCommand' +import PhatBaseCommand, { type ParsedFlags, type BrickProfileFactoryContract, type BrickProfileContract } from '../lib/PhatBaseCommand' import { bindWaitPRuntimeFinalized } from '../lib/utils' interface PartialAccountQueryResult extends Struct { @@ -19,10 +20,22 @@ export default class CreateBrickProfile extends PhatBaseCommand { } static flags = { - ...PhatBaseCommand.flags + ...PhatBaseCommand.flags, + evmRpcEndpoint: Flags.string({ + description: 'EVM RPC endpoint', + required: false, + }), } public async run(): Promise { + const { evmRpcEndpoint } = this.parsedFlags as ParsedFlags & { + evmRpcEndpoint: string + } + + if (evmRpcEndpoint) { + await this.verifyRpcEndpoint(evmRpcEndpoint) + } + const pair = await this.getDecodedPair({ suri: this.parsedFlags.suri || process.env.POLKADOT_WALLET_SURI, accountFilePath: this.parsedFlags.accountFilePath || process.env.POLKADOT_WALLET_ACCOUNT_FILE, @@ -127,7 +140,7 @@ export default class CreateBrickProfile extends PhatBaseCommand { registry, contract: brickProfile, externalAccountCount, - evmRpcEndpoint: type.isDevelopment || type.isLocal ? 'https://polygon-mumbai.g.alchemy.com/v2/YWlujLKt0nSn5GrgEpGCUA0C_wKV1sVQ' : 'https://polygon-mainnet.g.alchemy.com/v2/W1kyx17tiFQFT2b19mGOqppx90BLHp0a', + evmRpcEndpoint: evmRpcEndpoint || (type.isDevelopment || type.isLocal) ? 'https://polygon-mumbai.g.alchemy.com/v2/YWlujLKt0nSn5GrgEpGCUA0C_wKV1sVQ' : 'https://polygon-mainnet.g.alchemy.com/v2/W1kyx17tiFQFT2b19mGOqppx90BLHp0a', pair, cert }) diff --git a/src/lib/PhatBaseCommand.ts b/src/lib/PhatBaseCommand.ts index d048d26..6e8cf72 100644 --- a/src/lib/PhatBaseCommand.ts +++ b/src/lib/PhatBaseCommand.ts @@ -22,6 +22,7 @@ import { Keyring } from '@polkadot/keyring' import { type KeyringPair } from '@polkadot/keyring/types' import type { Result, Vec, u64, u8, Text, Struct } from '@polkadot/types' import type { AccountId, ChainType, Hash } from '@polkadot/types/interfaces' +import { createPublicClient, http } from 'viem' import { MAX_BUILD_SIZE, @@ -616,4 +617,18 @@ export default abstract class PhatBaseCommand extends BaseCommand { return abi } + async verifyRpcEndpoint(endpoint: string) { + try { + this.action.start(`Verifying the RPC endpoint: ${endpoint}`) + const client = createPublicClient({ + transport: http(endpoint) + }) + await client.getChainId() + this.action.succeed() + } catch (error) { + this.action.fail('Failed to verify the RPC endpoint.') + return this.error(error as Error) + } + } + }