import { SpaceportSDK, Network } from "@spaceport/sdk";
import { privateKeyToAccount } from "viem/accounts";
import { parseEther } from "viem";
import * as fs from "fs";
import * as path from "path";
async function createAssetAndListing() {
// Initialize SDK with wallet
const account = privateKeyToAccount(`0x${process.env.LICENSOR_PRIVATE_KEY}`);
const sdk = SpaceportSDK.createWithAccount(account, {
chain: "devnet",
apiKey: process.env.SDK_API_KEY,
});
// 1. Get or create user
let currentUser;
try {
currentUser = await sdk.users.getCurrentUser();
} catch (error) {
currentUser = await sdk.users.create({
email: "[email protected]",
firstName: "Alice",
lastName: "Creator",
});
}
// 2. Create asset record in Directus
const logoPath = path.join(__dirname, "../media/spaceport_logo.png");
const logoBuffer = fs.readFileSync(logoPath);
const logoFile = new File([logoBuffer], "spaceport_logo.png", {
type: "image/png",
});
const assetResult = await sdk.assets.create({
name: "My Digital Asset",
title: "My Digital Asset",
userId: currentUser.id,
organizationId: currentUser.organizationId,
storageAccess: "public",
description: "A unique digital creation",
tags: ["art", "digital", "nft"],
mediaFile: logoFile,
thumbnailFile: logoFile,
});
// 3. Set up bond token approvals
const requiredAllowance = parseEther("100"); // Adjust based on your needs
await sdk.bondToken.approveAllForAmount(requiredAllowance);
// 4. Get existing smart license
const smartLicenses = await sdk.smartLicenses.getAll({ status: "published" });
const smartLicenseHash = smartLicenses[0]?.smartLicenseHash;
// 5. Create listing
const listingResult = await sdk.listings.create({
licensorAddress: account.address,
price: parseEther("10"), // 10 tokens
smartLicenseHash: smartLicenseHash,
duration: BigInt(365 * 24 * 60 * 60), // 1 year
licenseRoyalties: 200n, // 2%
resellRoyalties: 500n, // 5%
availableAgreements: 100,
listingType: "AutoReview",
assetIds: [assetResult.assetId],
userId: currentUser.id,
organizationId: currentUser.organizationId,
title: "My Digital Asset License",
description: "License for my unique digital creation",
category: "digital-art",
});
// 6. Monitor job status
if (listingResult.jobId) {
const jobStatus = await sdk.utils.pollJobStatus(listingResult.jobId, {
timeout: 300000, // 5 minutes
interval: 3000, // 3 seconds
});
console.log("Listing creation status:", jobStatus.status);
}
}