UNPKG

@gotake/gotake-sdk

Version:

SDK for interacting with GoTake blockchain contracts

382 lines (381 loc) 18 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IPNFTWrapper = void 0; var ethers_1 = require("ethers"); var IPNFT__factory_1 = require("gotake-contracts/dist/typechain/factories/contracts/IPNFT__factory"); var base_transaction_wrapper_1 = require("./base-transaction-wrapper"); /** * Wrapper for IPNFT contract, used for minting and managing IP NFTs */ var IPNFTWrapper = /** @class */ (function (_super) { __extends(IPNFTWrapper, _super); /** * Create IPNFT wrapper instance * @param provider Provider instance * @param signer Signer instance */ function IPNFTWrapper(provider, signer) { var _this = _super.call(this, provider, signer) || this; _this._ipnft = null; return _this; } /** * Get IPNFT contract instance * @returns IPNFT contract instance */ IPNFTWrapper.prototype.getIPNFT = function () { return __awaiter(this, void 0, void 0, function () { var address; return __generator(this, function (_a) { switch (_a.label) { case 0: // Ensure networkId is initialized return [4 /*yield*/, this.init()]; case 1: // Ensure networkId is initialized _a.sent(); if (!this._ipnft) { address = this.getContractAddress('ipnft'); this._ipnft = IPNFT__factory_1.IPNFT__factory.connect(address, this._signer); } return [2 /*return*/, this._ipnft]; } }); }); }; /** * Mint a new IPNFT (New Contract Interface) * @param to Recipient address * @param uri Metadata URI * @param options Transaction options * @returns Transaction object and minted NFT ID */ IPNFTWrapper.prototype.mint = function (to, uri, options) { return __awaiter(this, void 0, void 0, function () { var ipnft, overrides, tx, receipt, transferEvent, tokenId; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _b.sent(); overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig); return [4 /*yield*/, ipnft.mint(to, uri, overrides)]; case 2: tx = _b.sent(); return [4 /*yield*/, tx.wait()]; case 3: receipt = _b.sent(); transferEvent = (_a = receipt.events) === null || _a === void 0 ? void 0 : _a.find(function (event) { return event.event === 'MediaNFTMinted' || event.event === 'Transfer'; }); if (!transferEvent || !transferEvent.args) { throw new Error('Unable to get TokenId from transaction'); } tokenId = transferEvent.args.tokenId; return [2 /*return*/, { tx: tx, tokenId: tokenId }]; } }); }); }; /** * Set media information for a token * @param tokenId Token ID * @param mediaData Media information data * @param options Transaction options * @returns Transaction object */ IPNFTWrapper.prototype.setMediaInfo = function (tokenId, mediaData, options) { return __awaiter(this, void 0, void 0, function () { var ipnft, tokenIdBN, overrides, populatedTx, txWithOverrides; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _a.sent(); tokenIdBN = ethers_1.ethers.BigNumber.from(tokenId); overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig); if (!(Object.keys(overrides).length > 0)) return [3 /*break*/, 3]; return [4 /*yield*/, ipnft.populateTransaction.setMediaInfo(tokenIdBN, mediaData.seriesTitle, mediaData.description, mediaData.totalSeasons, mediaData.totalEpisodes, mediaData.genres, mediaData.creators, mediaData.posterUri)]; case 2: populatedTx = _a.sent(); txWithOverrides = __assign(__assign({}, populatedTx), overrides); return [2 /*return*/, this._signer.sendTransaction(txWithOverrides)]; case 3: // Standard call without overrides return [2 /*return*/, ipnft.setMediaInfo(tokenIdBN, mediaData.seriesTitle, mediaData.description, mediaData.totalSeasons, mediaData.totalEpisodes, mediaData.genres, mediaData.creators, mediaData.posterUri)]; } }); }); }; /** * Get media information (New Contract Interface) * @param tokenId NFT ID * @returns Media information */ IPNFTWrapper.prototype.getMediaInfo = function (tokenId) { return __awaiter(this, void 0, void 0, function () { var ipnft, tokenIdBN, mediaInfo; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _a.sent(); tokenIdBN = ethers_1.ethers.BigNumber.from(tokenId); return [4 /*yield*/, ipnft.getMediaInfo(tokenIdBN)]; case 2: mediaInfo = _a.sent(); return [2 /*return*/, { seriesTitle: mediaInfo.seriesTitle, description: mediaInfo.description, totalSeasons: mediaInfo.totalSeasons, totalEpisodes: mediaInfo.totalEpisodes, genres: mediaInfo.genres, creators: mediaInfo.creators, createdAt: mediaInfo.createdAt, posterUri: mediaInfo.posterUri }]; } }); }); }; /** * Mint with metadata (Convenience method that combines mint and setMediaInfo) * @param to Recipient address * @param uri Metadata URI * @param metadata Media metadata * @param options Transaction options * @returns Transaction object and minted NFT ID */ IPNFTWrapper.prototype.mintWithMetadata = function (to, uri, metadata, options) { return __awaiter(this, void 0, void 0, function () { var mintResult, setInfoTx, mediaData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.mint(to, uri, options)]; case 1: mintResult = _a.sent(); if (!metadata.title) return [3 /*break*/, 4]; mediaData = { seriesTitle: metadata.title, description: metadata.description || '', totalSeasons: metadata.totalSeasons || 1, totalEpisodes: metadata.totalEpisodes || 1, genres: metadata.genres || [], creators: metadata.creators || (metadata.creator ? [metadata.creator] : []), posterUri: metadata.posterUri || metadata.thumbnailUrl || '' }; return [4 /*yield*/, this.setMediaInfo(mintResult.tokenId, mediaData, options)]; case 2: setInfoTx = _a.sent(); return [4 /*yield*/, setInfoTx.wait()]; case 3: _a.sent(); // Wait for setMediaInfo to complete _a.label = 4; case 4: return [2 /*return*/, __assign(__assign({}, mintResult), { setInfoTx: setInfoTx })]; } }); }); }; /** * Legacy mint method (for backward compatibility) * @deprecated Use mintWithMetadata instead */ IPNFTWrapper.prototype.legacyMint = function (to_1, uri_1, title_1, description_1) { return __awaiter(this, arguments, void 0, function (to, uri, title, description, ipType, options) { var metadata, result; if (ipType === void 0) { ipType = 0; } return __generator(this, function (_a) { switch (_a.label) { case 0: metadata = { title: title, description: description, totalSeasons: 1, totalEpisodes: 1, genres: ["type_".concat(ipType)], creators: [], posterUri: uri }; return [4 /*yield*/, this.mintWithMetadata(to, uri, metadata, options)]; case 1: result = _a.sent(); return [2 /*return*/, { tx: result.tx, tokenId: result.tokenId }]; } }); }); }; /** * Get URI for a specific NFT * @param tokenId NFT ID * @returns NFT's metadata URI */ IPNFTWrapper.prototype.tokenURI = function (tokenId) { return __awaiter(this, void 0, void 0, function () { var ipnft, tokenIdBN; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _a.sent(); tokenIdBN = ethers_1.ethers.BigNumber.from(tokenId); return [2 /*return*/, ipnft.tokenURI(tokenIdBN)]; } }); }); }; /** * Check if a specific address is the owner of a specific NFT * @param address Address to check * @param tokenId NFT ID * @returns true if the address is the owner; otherwise false */ IPNFTWrapper.prototype.isOwnerOf = function (address, tokenId) { return __awaiter(this, void 0, void 0, function () { var ipnft, tokenIdBN, owner; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _a.sent(); tokenIdBN = ethers_1.ethers.BigNumber.from(tokenId); return [4 /*yield*/, ipnft.ownerOf(tokenIdBN)]; case 2: owner = _a.sent(); return [2 /*return*/, owner.toLowerCase() === address.toLowerCase()]; } }); }); }; /** * Get the owner of a specific NFT * @param tokenId NFT ID * @returns Owner address */ IPNFTWrapper.prototype.ownerOf = function (tokenId) { return __awaiter(this, void 0, void 0, function () { var ipnft, tokenIdBN; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIPNFT()]; case 1: ipnft = _a.sent(); tokenIdBN = ethers_1.ethers.BigNumber.from(tokenId); return [2 /*return*/, ipnft.ownerOf(tokenIdBN)]; } }); }); }; /** * Legacy getIPInfo method (for backward compatibility) * @deprecated Use getMediaInfo instead * @param tokenId NFT ID * @returns IP information */ IPNFTWrapper.prototype.getIPInfo = function (tokenId) { return __awaiter(this, void 0, void 0, function () { var mediaInfo; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getMediaInfo(tokenId)]; case 1: mediaInfo = _a.sent(); // Convert MediaInfo to legacy IPInfo format return [2 /*return*/, { title: mediaInfo.seriesTitle, description: mediaInfo.description, ipType: 0, // Default type since new contract doesn't have ipType createdAt: mediaInfo.createdAt }]; } }); }); }; /** * Convert contract NFT details to MintResult (Updated for new contract) * @param details NFT details from contract * @param tx Transaction object * @returns Formatted mint result */ IPNFTWrapper.prototype.formatMintResult = function (details, tx) { return { tokenId: details.tokenId.toString(), transactionHash: tx.hash, owner: details.owner, metadata: details.mediaInfo }; }; /** * Update connection information (when network changes) * @param provider New Provider * @param signer New Signer */ IPNFTWrapper.prototype.updateConnection = function (provider, signer) { _super.prototype.updateConnection.call(this, provider, signer); this._ipnft = null; // Clear cached contract instance, will be recreated on next use }; return IPNFTWrapper; }(base_transaction_wrapper_1.BaseTransactionWrapper)); exports.IPNFTWrapper = IPNFTWrapper;