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

Submit item and record public data #173

Merged
merged 1 commit into from
Jul 14, 2023
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
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/client",
"version": "0.28.1",
"version": "0.28.2-2",
"description": "logion SDK for client applications",
"main": "dist/index.js",
"packageManager": "yarn@3.2.0",
Expand Down
145 changes: 131 additions & 14 deletions packages/client/src/LocClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,35 @@ export interface OffchainCollectionItem {
collectionLocId: string;
itemId: string;
addedOn: string;
files: string[];
description?: string;
files: OffchainCollectionItemFile[];
termsAndConditions: OffchainTermsAndConditionsElement[];
token?: OffchainCollectionItemToken;
}

export interface OffchainCollectionItemFile {
hash: string;
name?: string;
contentType?: string;
uploaded?: boolean;
}

export interface OffchainTermsAndConditionsElement {
type?: string;
details?: string;
}

export interface OffchainCollectionItemToken {
type?: string;
id?: string;
}

interface CreateOffchainCollectionItem {
itemId: string;
description: string;
files: OffchainCollectionItemFile[];
termsAndConditions: OffchainTermsAndConditionsElement[];
token?: OffchainCollectionItemToken;
}

export interface ClientTokensRecord {
Expand All @@ -484,7 +512,20 @@ export interface OffchainTokensRecord {
collectionLocId: string;
recordId: string;
addedOn: string;
files: string[];
files: OffchainTokensRecordFile[];
}

interface CreateOffchainTokensRecord {
recordId: string;
description: string;
files: OffchainTokensRecordFile[];
}

export interface OffchainTokensRecordFile {
hash: string;
name?: string;
contentType?: string;
uploaded?: boolean;
}

export abstract class LocClient {
Expand Down Expand Up @@ -538,7 +579,7 @@ export abstract class LocClient {
addedOn: offchainItem.addedOn,
files: onchainItem.files.map(file => ({
...file,
uploaded: offchainItem.files.includes(file.hash),
uploaded: offchainItem.files.find(offchainFile => offchainFile.hash === file.hash)?.uploaded || false,
})),
token: onchainItem.token ? {
type: onchainItem.token.type as TokenType,
Expand Down Expand Up @@ -613,7 +654,7 @@ export abstract class LocClient {
files: onchainItem.files.map(file => ({
...file,
size: BigInt(file.size),
uploaded: offchainItem.files.includes(file.hash),
uploaded: offchainItem.files.find(offchainFile => offchainFile.hash === file.hash)?.uploaded || false,
})),
}
}
Expand Down Expand Up @@ -881,6 +922,24 @@ export class AuthenticatedLocClient extends LocClient {
parameters.specificLicenses.forEach(addTC);
}

await this.submitItemPublicData(locId, {
itemId,
files: itemFiles?.map(file => ({
name: file.name,
contentType: file.contentType.mimeType,
hash: file.hashOrContent.contentHash,
})) || [],
termsAndConditions: termsAndConditions.map(element => ({
type: element.tcType,
details: element.details,
})),
description: itemDescription,
token: itemToken ? {
id: itemToken.id,
type: itemToken.type,
} : undefined,
});

const submittable = this.nodeApi.polkadot.tx.logionLoc.addCollectionItem(
this.nodeApi.adapters.toLocId(locId),
itemId,
Expand All @@ -890,11 +949,16 @@ export class AuthenticatedLocClient extends LocClient {
booleanRestrictedDelivery,
termsAndConditions.map(Adapters.toTermsAndConditionsElement),
);
await signer.signAndSend({
signerId: this.currentAddress.address,
submittable,
callback
});
try {
await signer.signAndSend({
signerId: this.currentAddress.address,
submittable,
callback
});
} catch(e) {
await this.cancelItemPublicDataSubmission(locId, itemId);
throw e;
}

if(itemFiles) {
for(const file of itemFiles) {
Expand All @@ -905,6 +969,25 @@ export class AuthenticatedLocClient extends LocClient {
}
}

private async submitItemPublicData(locId: UUID, item: CreateOffchainCollectionItem) {
try {
await this.backend().post(
`/api/collection/${ locId.toString() }/items`,
item
);
} catch(e) {
throw newBackendError(e);
}
}

private async cancelItemPublicDataSubmission(locId: UUID, itemId: string) {
try {
await this.backend().delete(`/api/collection/${ locId.toString() }/items/${ itemId }`);
} catch(e) {
throw newBackendError(e);
}
}

async uploadItemFile(parameters: { locId: UUID, itemId: string, file: ItemFileWithContent }) {
const { locId, itemId, file } = parameters;

Expand Down Expand Up @@ -1078,17 +1161,32 @@ export class AuthenticatedLocClient extends LocClient {
});
}

await this.submitRecordPublicData(locId, {
recordId,
files: files?.map(file => ({
name: file.name,
contentType: file.contentType.mimeType,
hash: file.hashOrContent.contentHash,
})) || [],
description,
});

const submittable = this.nodeApi.polkadot.tx.logionLoc.addTokensRecord(
this.nodeApi.adapters.toLocId(locId),
recordId,
description,
this.nodeApi.adapters.newTokensRecordFileVec(chainItemFiles),
);
await signer.signAndSend({
signerId: this.currentAddress.address,
submittable,
callback
});
try {
await signer.signAndSend({
signerId: this.currentAddress.address,
submittable,
callback
});
} catch(e) {
await this.cancelRecordPublicDataSubmission(locId, recordId);
throw e;
}

for(const file of files) {
if(file.hashOrContent.hasContent) {
Expand All @@ -1097,6 +1195,25 @@ export class AuthenticatedLocClient extends LocClient {
}
}

private async submitRecordPublicData(locId: UUID, record: CreateOffchainTokensRecord) {
try {
await this.backend().post(
`/api/records/${ locId.toString() }/record`,
record
);
} catch(e) {
throw newBackendError(e);
}
}

private async cancelRecordPublicDataSubmission(locId: UUID, recordId: string) {
try {
await this.backend().delete(`/api/records/${ locId.toString() }/record/${ recordId }`);
} catch(e) {
throw newBackendError(e);
}
}

async uploadTokensRecordFile(parameters: { locId: UUID, recordId: string, file: ItemFileWithContent }) {
const { locId, recordId, file } = parameters;

Expand Down
1 change: 1 addition & 0 deletions packages/client/test/LocUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export function buildOffchainCollectionItem(collectionLocId: string): OffchainCo
itemId: EXISTING_ITEM_ID,
addedOn: DateTime.now().toISO(),
files: [],
termsAndConditions: [],
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/crossmint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/crossmint",
"version": "0.1.19",
"version": "0.1.20-1",
"description": "logion SDK for Crossmint",
"main": "dist/index.js",
"packageManager": "yarn@3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/extension",
"version": "0.5.17",
"version": "0.5.18-1",
"description": "logion SDK for Polkadot JS extension",
"main": "dist/index.js",
"packageManager": "yarn@3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/multiversx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/multiversx",
"version": "0.1.0",
"version": "0.1.1-1",
"description": "logion SDK for MultiversX",
"main": "dist/index.js",
"packageManager": "yarn@3.2.0",
Expand Down
Loading