Skip to content

Commit

Permalink
ChainSpace: Separate preparation of create chainspace extrinsic from …
Browse files Browse the repository at this point in the history
…chain dispatch (#203)
  • Loading branch information
hardiksharma11 authored Apr 29, 2024
1 parent 340f366 commit ee63717
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions packages/chain-space/src/ChainSpace.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {
SpaceDigest,
AuthorizationUri,
SpaceUri,
SubmittableExtrinsic
} from '@cord.network/types'
import { SDKErrors, DecoderUtils } from '@cord.network/utils'
import {
Expand Down Expand Up @@ -228,6 +229,33 @@ export async function getUriForSpace(
* @returns A promise resolving to an object containing the ChainSpace URI and authorization ID.
* @throws {SDKErrors.CordDispatchError} - Thrown when there's an error during the dispatch process.
*/

export async function prepareCreateSpaceExtrinsic(
chainSpace: IChainSpace,
creatorUri: DidUri,
signCallback: SignExtrinsicCallback,
authorAccount: CordKeyringPair
): Promise<SubmittableExtrinsic> {
try {
const api = ConfigService.get('api')

const tx = api.tx.chainSpace.create(chainSpace.digest)
const extrinsic = await Did.authorizeTx(
creatorUri,
tx,
signCallback,
authorAccount.address
)
return extrinsic;


} catch (error) {
throw new SDKErrors.CordDispatchError(
`Error preparing extrinsic for creation of chainspace: "${error}".`
);
}
}

export async function dispatchToChain(
chainSpace: IChainSpace,
creatorUri: DidUri,
Expand All @@ -240,20 +268,9 @@ export async function dispatchToChain(
}

try {
const api = ConfigService.get('api')

const exists = await isChainSpaceStored(chainSpace.uri)
if (!exists) {
const tx = api.tx.chainSpace.create(chainSpace.digest)
const extrinsic = await Did.authorizeTx(
creatorUri,
tx,
signCallback,
authorAccount.address
)

await Chain.signAndSubmitTx(extrinsic, authorAccount)
}
const extrinsic = await prepareCreateSpaceExtrinsic(chainSpace, creatorUri, signCallback, authorAccount)
await Chain.signAndSubmitTx(extrinsic, authorAccount)

return returnObject
} catch (error) {
Expand Down

0 comments on commit ee63717

Please sign in to comment.