Skip to content

Commit

Permalink
refactor: use getContract and waitFinalized
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoyang committed Dec 22, 2023
1 parent 2ad006e commit f1c540d
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/commands/add-evm-account.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Flags } from '@oclif/core'
import { PinkContractPromise } from '@phala/sdk'
import { getContract } from '@phala/sdk'

import PhatBaseCommand, { type ParsedFlags, type BrickProfileContract } from '../lib/PhatBaseCommand'
import { bindWaitPRuntimeFinalized } from '../lib/utils'

export default class AddEvmAccount extends PhatBaseCommand {
static description = 'Add EVM accounts'
Expand Down Expand Up @@ -54,36 +53,30 @@ export default class AddEvmAccount extends PhatBaseCommand {
registry,
brickProfileContractId
)
const brickProfileContractKey = await registry.getContractKeyOrFail(
brickProfileContractId
)
const brickProfile: BrickProfileContract = new PinkContractPromise(
apiPromise,
registry,
brickProfileAbi,
brickProfileContractId,
brickProfileContractKey
)
const brickProfile = await getContract({
client: registry,
contractId: brickProfileContractId,
abi: brickProfileAbi,
}) as BrickProfileContract
const { output } = await brickProfile.query.externalAccountCount(cert.address, {
cert,
})
if (output.isErr) {
throw new Error(output.asErr.toString())
}
const externalAccountCount = output.asOk.toNumber()
const waitForPRuntimeFinalized = bindWaitPRuntimeFinalized(registry)
await waitForPRuntimeFinalized(
brickProfile.send.generateEvmAccount(
{ cert, address: pair.address, pair },
evmRpcEndpoint
),
async function () {
const { output } = await brickProfile.query.externalAccountCount(cert.address, {
cert,
})
return output.isOk && output.asOk.toNumber() === externalAccountCount + 1
}

const result = await brickProfile.send.generateEvmAccount(
{ cert, address: pair.address, pair },
evmRpcEndpoint
)
await result.waitFinalized(async () => {
const { output } = await brickProfile.query.externalAccountCount(cert.address, {
cert,
})
return output.isOk && output.asOk.toNumber() === externalAccountCount + 1
})

const { output: evmAccountAddressOutput } = await brickProfile.query.getEvmAccountAddress(
cert.address,
{ cert },
Expand Down

0 comments on commit f1c540d

Please sign in to comment.