-
Notifications
You must be signed in to change notification settings - Fork 242
/
setSharedMetadata.ts
70 lines (64 loc) · 2.06 KB
/
setSharedMetadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { BaseTransactionOptions } from "../../../transaction/types.js";
import type { NFTInput } from "../../../utils/nft/parseNft.js";
import { setSharedMetadata as generatedSharedMetadata } from "../__generated__/ISharedMetadata/write/setSharedMetadata.js";
export type SetSharedMetadataParams = {
nft: NFTInput;
};
export function setSharedMetadata(
options: BaseTransactionOptions<SetSharedMetadataParams>,
) {
return generatedSharedMetadata({
contract: options.contract,
asyncParams: async () => {
if (!options.nft.name) {
throw new Error("NFT name is required");
}
const batch: Promise<string>[] = [
// image URI resolution
(async () => {
if (!options.nft.image) {
return "";
}
if (typeof options.nft.image === "string") {
return options.nft.image;
}
const { upload } = await import("../../../storage/upload.js");
return await upload({
client: options.contract.client,
files: [options.nft.image],
});
})(),
// animation URI resolution
(async () => {
if (!options.nft.animation_url) {
return "";
}
if (typeof options.nft.animation_url === "string") {
return options.nft.animation_url;
}
const { upload } = await import("../../../storage/upload.js");
return await upload({
client: options.contract.client,
files: [options.nft.animation_url],
});
})(),
];
const [imageURI, animationURI] = await Promise.all(batch);
return {
metadata: {
name: options.nft.name,
description: sanitizeJSONString(options.nft.description) ?? "",
imageURI: imageURI ?? "",
animationURI: animationURI ?? "",
},
};
},
});
}
function sanitizeJSONString(val: string | undefined | null) {
if (!val) {
return val;
}
const sanitized = JSON.stringify(val);
return sanitized.slice(1, sanitized.length - 1);
}