Skip to content

Commit

Permalink
feat: support for specifying evm rpc endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoyang committed Dec 22, 2023
1 parent 623ac3f commit 376c06a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
13 changes: 1 addition & 12 deletions src/commands/add-evm-account.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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,
Expand Down
19 changes: 16 additions & 3 deletions src/commands/create-brick-profile.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<void> {
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,
Expand Down Expand Up @@ -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
})
Expand Down
15 changes: 15 additions & 0 deletions src/lib/PhatBaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

}

0 comments on commit 376c06a

Please sign in to comment.