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

feat: package/asset: implement status_change call #169

Merged
merged 5 commits into from
Feb 27, 2024
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
86 changes: 71 additions & 15 deletions packages/asset/src/Asset.chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
AssetUri,
IAssetEntry,
Expand All @@ -9,21 +8,19 @@ import {
SignExtrinsicCallback,
AuthorizationId,
ASSET_PREFIX,
DidUri,
} from '@cord.network/types'

import type { Option } from '@cord.network/types'
import type {
PalletAssetAssetEntry,
} from '@cord.network/augment-api'
import type { PalletAssetAssetEntry, PalletAssetAssetStatusOf } from '@cord.network/augment-api'

import * as Did from '@cord.network/did'
import { uriToIdentifier } from '@cord.network/identifier'
import { Chain } from '@cord.network/network'
import { ConfigService } from '@cord.network/config'
import { SDKErrors } from '@cord.network/utils'

export async function isAssetStored(
assetUri: AssetUri
): Promise<boolean> {
export async function isAssetStored(assetUri: AssetUri): Promise<boolean> {
try {
const api = ConfigService.get('api')
const identifier = uriToIdentifier(assetUri)
Expand Down Expand Up @@ -91,9 +88,7 @@ export async function dispatchIssueToChain(

const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)

const exists = await isAssetStored(
assetEntry.entry.assetId as AssetUri
)
const exists = await isAssetStored(assetEntry.entry.assetId as AssetUri)

if (!exists) {
throw new SDKErrors.CordDispatchError(`Asset Entry not found on chain.`)
Expand Down Expand Up @@ -124,7 +119,6 @@ export async function dispatchIssueToChain(
}
}


export async function dispatchTransferToChain(
assetEntry: IAssetTransfer,
authorAccount: CordKeyringPair,
Expand All @@ -133,10 +127,7 @@ export async function dispatchTransferToChain(
try {
const api = ConfigService.get('api')

const tx = api.tx.asset.transfer(
assetEntry.entry,
assetEntry.digest,
)
const tx = api.tx.asset.transfer(assetEntry.entry, assetEntry.digest)

const extrinsic = await Did.authorizeTx(
assetEntry.owner,
Expand All @@ -157,3 +148,68 @@ export async function dispatchTransferToChain(
}
}

export async function dispatchAssetStatusChangeToChain(
assetId: AssetUri,
assetIssuerDidUri: DidUri,
authorAccount: CordKeyringPair,
newStatus: PalletAssetAssetStatusOf,
signCallback: SignExtrinsicCallback,
assetInstanceId?: string
): Promise<void> {
try {
const api = ConfigService.get('api')
let tx

if (assetInstanceId) {
let encodedAssetInstanceDetail = await api.query.asset.issuance(
assetId,
assetInstanceId
)
if (encodedAssetInstanceDetail.isNone) {
throw new SDKErrors.AssetInstanceNotFound(
`Error: Assset Instance Not Found`
)
}
let assetInstanceDetail = JSON.parse(
encodedAssetInstanceDetail.toString()
)
if (assetIssuerDidUri !== assetInstanceDetail.assetInstanceIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
}
if (assetInstanceDetail.assetInstanceStatus === newStatus) {
throw new SDKErrors.AssetStatusError(
`Error: Asset Instance is already in the ${newStatus} state`
)
}
tx = api.tx.asset.statusChange(assetId, assetInstanceId, newStatus)
} else {
let encodedAssetDetail = await api.query.asset.assets(assetId)
if (encodedAssetDetail.isNone) {
throw new SDKErrors.AssetNotFound(`Error: Assset Not Found`)
}
let assetDetail = JSON.parse(encodedAssetDetail.toString())
if (assetIssuerDidUri !== assetDetail.assetIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
}
if (assetDetail.assetInstanceStatus === newStatus) {
throw new SDKErrors.AssetStatusError(
`Error: Asset is already in the ${newStatus} state`
)
}
tx = api.tx.asset.statusChange(assetId, null, newStatus)
}

const extrinsic = await Did.authorizeTx(
assetIssuerDidUri,
tx,
signCallback,
authorAccount.address
)

await Chain.signAndSubmitTx(extrinsic, authorAccount)
} catch (error) {
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
)
}
}
8 changes: 8 additions & 0 deletions packages/utils/src/SDKErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,11 @@ export class DecodingMessageError extends SDKError {}
export class TimeoutError extends SDKError {}

export class CodecMismatchError extends SDKError {}

export class AssetIssuerMismatch extends SDKError {}

export class AssetInstanceNotFound extends SDKError {}

export class AssetNotFound extends SDKError {}

export class AssetStatusError extends SDKError {}
Loading