-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #769 from input-output-hk/feat/plutus-data-seriali…
…zation feat: nft metadata service
- Loading branch information
Showing
333 changed files
with
27,333 additions
and
11,802 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ env: | |
|
||
on: | ||
pull_request: | ||
branches: ['master'] | ||
push: | ||
branches: ['master'] | ||
tags: ['*.*.*'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ env: | |
|
||
on: | ||
pull_request: | ||
branches: ['master'] | ||
push: | ||
branches: ['master'] | ||
tags: ['*.*.*'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
// auto-generated using ../scripts/createVersionSource.js | ||
export const apiVersion = { | ||
assetInfo: '1.0.0', | ||
chainHistory: '1.0.0', | ||
chainHistory: '2.0.0', | ||
handle: '1.0.0', | ||
networkInfo: '1.0.0', | ||
rewards: '1.0.0', | ||
root: '1.0.0', | ||
stakePool: '1.0.0', | ||
txSubmit: '1.0.0', | ||
utxo: '1.0.0' | ||
txSubmit: '2.0.0', | ||
utxo: '2.0.0' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"assetInfo": "1.0.0", | ||
"chainHistory": "1.0.0", | ||
"chainHistory": "2.0.0", | ||
"handle": "1.0.0", | ||
"networkInfo": "1.0.0", | ||
"rewards": "1.0.0", | ||
"root": "1.0.0", | ||
"stakePool": "1.0.0", | ||
"txSubmit": "1.0.0", | ||
"utxo": "1.0.0" | ||
"txSubmit": "2.0.0", | ||
"utxo": "2.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/cardano-services/src/Asset/TypeOrmNftMetadataService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Asset, Cardano } from '@cardano-sdk/core'; | ||
import { AssetPolicyIdAndName, NftMetadataService } from './types'; | ||
import { NftMetadataEntity } from '@cardano-sdk/projection-typeorm'; | ||
import { TypeormProviderDependencies, TypeormService } from '../util'; | ||
|
||
export class TypeOrmNftMetadataService extends TypeormService implements NftMetadataService { | ||
constructor({ connectionConfig$, logger, entities }: TypeormProviderDependencies) { | ||
super('TypeOrmNftMetadataService', { connectionConfig$, entities, logger }); | ||
} | ||
|
||
async getNftMetadata(assetInfo: AssetPolicyIdAndName): Promise<Asset.NftMetadata | null> { | ||
const assetId = Cardano.AssetId.fromParts(assetInfo.policyId, assetInfo.name); | ||
const stringAssetName = Buffer.from(assetInfo.name, 'hex').toString('utf8'); | ||
return this.withDataSource(async (dataSource) => { | ||
const queryRunner = dataSource.createQueryRunner(); | ||
const nftMetadataRepository = queryRunner.manager.getRepository(NftMetadataEntity); | ||
|
||
const asset = await nftMetadataRepository.findOneBy({ | ||
name: stringAssetName, | ||
userTokenAsset: { id: assetId } | ||
}); | ||
|
||
if (!asset) { | ||
return null; | ||
} | ||
|
||
return { | ||
image: asset.image, | ||
name: asset.name, | ||
...(asset.description && { description: asset.description }), | ||
...(asset.files && { files: asset.files }), | ||
...(asset.mediaType && { mediaType: asset.mediaType }), | ||
...(asset.otherProperties && { otherProperties: asset.otherProperties }) | ||
} as unknown as Asset.NftMetadata; | ||
}); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
packages/cardano-services/src/Asset/TypeormAssetProvider/TypeormAssetProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { | ||
Asset, | ||
AssetProvider, | ||
Cardano, | ||
GetAssetArgs, | ||
GetAssetsArgs, | ||
ProviderError, | ||
ProviderFailure | ||
} from '@cardano-sdk/core'; | ||
import { AssetEntity } from '@cardano-sdk/projection-typeorm'; | ||
import { TokenMetadataService } from '../types'; | ||
import { TypeOrmNftMetadataService } from '../TypeOrmNftMetadataService'; | ||
import { TypeormProvider, TypeormProviderDependencies } from '../../util'; | ||
|
||
interface TypeormAssetProviderProps { | ||
paginationPageSizeLimit: number; | ||
} | ||
|
||
interface TypeormAssetProviderDependencies extends TypeormProviderDependencies { | ||
tokenMetadataService: TokenMetadataService; | ||
} | ||
|
||
export class TypeormAssetProvider extends TypeormProvider implements AssetProvider { | ||
#dependencies: TypeormAssetProviderDependencies; | ||
#nftMetadataService: TypeOrmNftMetadataService; | ||
#paginationPageSizeLimit: number; | ||
|
||
constructor({ paginationPageSizeLimit }: TypeormAssetProviderProps, dependencies: TypeormAssetProviderDependencies) { | ||
const { connectionConfig$, entities, logger } = dependencies; | ||
super('TypeormAssetProvider', { connectionConfig$, entities, logger }); | ||
|
||
this.#dependencies = dependencies; | ||
this.#paginationPageSizeLimit = paginationPageSizeLimit; | ||
this.#nftMetadataService = new TypeOrmNftMetadataService({ connectionConfig$, entities, logger }); | ||
} | ||
|
||
async getAsset({ assetId, extraData }: GetAssetArgs): Promise<Asset.AssetInfo> { | ||
const assetInfo = await this.#getAssetInfo(assetId); | ||
|
||
if (extraData?.nftMetadata) assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo); | ||
if (extraData?.tokenMetadata) { | ||
assetInfo.tokenMetadata = (await this.#fetchTokenMetadataList([assetId]))[0]; | ||
} | ||
|
||
return assetInfo; | ||
} | ||
|
||
async getAssets({ assetIds, extraData }: GetAssetsArgs): Promise<Asset.AssetInfo[]> { | ||
if (assetIds.length > this.#paginationPageSizeLimit) { | ||
throw new ProviderError( | ||
ProviderFailure.BadRequest, | ||
undefined, | ||
`AssetIds count of ${assetIds.length} can not be greater than ${this.#paginationPageSizeLimit}` | ||
); | ||
} | ||
|
||
const assetInfoList = await Promise.all(assetIds.map((assetId) => this.#getAssetInfo(assetId))); | ||
|
||
if (extraData?.nftMetadata) { | ||
await Promise.all( | ||
assetInfoList.map(async (assetInfo) => { | ||
assetInfo.nftMetadata = await this.#getNftMetadata(assetInfo); | ||
}) | ||
); | ||
} | ||
|
||
if (extraData?.tokenMetadata) { | ||
const tokenMetadataList = await this.#fetchTokenMetadataList(assetIds); | ||
|
||
for (const [index, assetInfo] of assetInfoList.entries()) { | ||
assetInfo.tokenMetadata = tokenMetadataList[index]; | ||
} | ||
} | ||
|
||
return assetInfoList; | ||
} | ||
|
||
async #fetchTokenMetadataList(assetIds: Cardano.AssetId[]) { | ||
let tokenMetadataList: (Asset.TokenMetadata | null | undefined)[] = []; | ||
|
||
try { | ||
tokenMetadataList = await this.#dependencies.tokenMetadataService.getTokenMetadata(assetIds); | ||
} catch (error) { | ||
if (error instanceof ProviderError && error.reason === ProviderFailure.Unhealthy) { | ||
this.logger.error(`Failed to fetch token metadata for assets ${assetIds} due to: ${error.message}`); | ||
tokenMetadataList = Array.from({ length: assetIds.length }); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
return tokenMetadataList; | ||
} | ||
|
||
async #getNftMetadata(asset: Asset.AssetInfo): Promise<Asset.NftMetadata | null | undefined> { | ||
try { | ||
return this.#nftMetadataService.getNftMetadata({ | ||
name: asset.name, | ||
policyId: asset.policyId | ||
}); | ||
} catch (error) { | ||
this.logger.error('Failed to get nft metadata', asset.assetId, error); | ||
} | ||
} | ||
|
||
async #getAssetInfo(assetId: Cardano.AssetId): Promise<Asset.AssetInfo> { | ||
const assetName = Cardano.AssetId.getAssetName(assetId); | ||
const policyId = Cardano.AssetId.getPolicyId(assetId); | ||
const fingerprint = Cardano.AssetFingerprint.fromParts(policyId, Cardano.AssetName(assetName)); | ||
|
||
return this.withDataSource(async (dataSource) => { | ||
const queryRunner = dataSource.createQueryRunner(); | ||
const assetRepository = queryRunner.manager.getRepository(AssetEntity); | ||
|
||
const asset = await assetRepository.findOneBy({ id: assetId }); | ||
|
||
if (!asset) throw new ProviderError(ProviderFailure.NotFound, undefined, `Asset not found '${assetId}'`); | ||
const supply = asset.supply!; | ||
|
||
return { | ||
assetId, | ||
fingerprint, | ||
name: assetName, | ||
policyId, | ||
quantity: supply, | ||
supply | ||
}; | ||
}); | ||
} | ||
|
||
async initializeImpl() { | ||
await super.initializeImpl(); | ||
await this.#nftMetadataService.initialize(); | ||
} | ||
|
||
async startImpl() { | ||
await super.startImpl(); | ||
await this.#nftMetadataService.start(); | ||
} | ||
|
||
async shutdownImpl() { | ||
await super.shutdownImpl(); | ||
await this.#nftMetadataService.shutdown(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cardano-services/src/Asset/TypeormAssetProvider/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './TypeormAssetProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.