UNPKG

@gotake/gotake-sdk

Version:

SDK for interacting with GoTake blockchain contracts

493 lines (492 loc) 22.5 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 __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.VideoPaymentApi = void 0; var ethers_1 = require("ethers"); var base_api_1 = require("./base-api"); var wrappers_1 = require("../wrappers"); /** * High-level API for VideoPayment contract interactions * Provides user-friendly interfaces for purchasing and managing video content */ var VideoPaymentApi = /** @class */ (function (_super) { __extends(VideoPaymentApi, _super); /** * Create VideoPaymentApi instance * @param provider Provider instance * @param signer Signer instance */ function VideoPaymentApi(provider, signer) { var _this = _super.call(this, provider, signer) || this; _this._videoPaymentWrapper = null; return _this; } /** * Initialize VideoPayment wrapper */ VideoPaymentApi.prototype.getVideoPaymentWrapper = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!!this._videoPaymentWrapper) return [3 /*break*/, 2]; this._videoPaymentWrapper = new wrappers_1.VideoPaymentWrapper(this._provider, this._signer); return [4 /*yield*/, this._videoPaymentWrapper.init()]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, this._videoPaymentWrapper]; } }); }); }; /** * Initialize the API */ VideoPaymentApi.prototype.init = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; // Purchase Functions /** * Purchase content with specified payment method * @param contentId Content ID to purchase * @param paymentMethod Payment method: 'ETH' or 'ERC20' * @param tokenAddress ERC20 token address (required for ERC20 payment) * @param options Transaction options * @returns Purchase result */ VideoPaymentApi.prototype.purchaseContent = function (contentId, paymentMethod, tokenAddress, options) { return __awaiter(this, void 0, void 0, function () { var wrapper, tx, receipt; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); if (!(paymentMethod === 'ETH')) return [3 /*break*/, 4]; return [4 /*yield*/, wrapper.purchaseWithNative(contentId, options)]; case 2: tx = _a.sent(); return [4 /*yield*/, tx.wait()]; case 3: receipt = _a.sent(); return [2 /*return*/, { transactionHash: tx.hash, contentId: contentId, gasUsed: receipt.gasUsed }]; case 4: if (!tokenAddress) { throw new Error('Token address is required for ERC20 payment'); } // For ERC20, we need to get the token amount first // This is a simplified implementation - in practice you might want to expose this throw new Error('ERC20 payment requires token amount - use wrapper directly for ERC20 payments'); } }); }); }; /** * Batch purchase multiple content items * @param contentIds Array of content IDs to purchase * @param paymentMethod Payment method: 'ETH' or 'ERC20' * @param tokenAddress ERC20 token address (required for ERC20 payment) * @param options Transaction options * @returns Purchase result */ VideoPaymentApi.prototype.batchPurchaseContent = function (contentIds, paymentMethod, tokenAddress, options) { return __awaiter(this, void 0, void 0, function () { var wrapper, tx, receipt; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); if (!(paymentMethod === 'ETH')) return [3 /*break*/, 4]; return [4 /*yield*/, wrapper.batchPurchaseWithNative(contentIds, options)]; case 2: tx = _a.sent(); return [4 /*yield*/, tx.wait()]; case 3: receipt = _a.sent(); return [2 /*return*/, { transactionHash: tx.hash, contentId: contentIds[0], // First content ID as reference gasUsed: receipt.gasUsed }]; case 4: if (!tokenAddress) { throw new Error('Token address is required for ERC20 payment'); } throw new Error('ERC20 batch payment requires total amount - use wrapper directly for ERC20 payments'); } }); }); }; // Permission Functions /** * Check if current user has view permission for content * @param contentId Content ID * @returns True if user has valid view permission */ VideoPaymentApi.prototype.hasViewPermission = function (contentId) { return __awaiter(this, void 0, void 0, function () { var wrapper, userAddress; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [4 /*yield*/, this._signer.getAddress()]; case 2: userAddress = _a.sent(); return [2 /*return*/, wrapper.hasViewPermission(userAddress, contentId)]; } }); }); }; /** * Get current user's permission details for content * @param contentId Content ID * @returns View permission details */ VideoPaymentApi.prototype.getMyPermissions = function (contentId) { return __awaiter(this, void 0, void 0, function () { var wrapper, userAddress, details; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [4 /*yield*/, this._signer.getAddress()]; case 2: userAddress = _a.sent(); return [4 /*yield*/, wrapper.getPermissionDetails(userAddress, contentId)]; case 3: details = _a.sent(); return [2 /*return*/, { purchaseTime: details.purchaseTime, remainingViews: details.remainingViews, isValid: details.isValid }]; } }); }); }; /** * Get content configuration information * @param contentId Content ID * @returns Content configuration */ VideoPaymentApi.prototype.getContentInfo = function (contentId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { // Suppress unused parameter warning for incomplete implementation void contentId; // Note: This method doesn't exist in the contract interface we saw // We need to implement it or use individual calls throw new Error('getContentInfo method needs to be implemented based on available contract methods'); }); }); }; /** * Batch get content information for multiple items * @param contentIds Array of content IDs * @returns Array of content configurations */ VideoPaymentApi.prototype.batchGetContentInfo = function (contentIds) { return __awaiter(this, void 0, void 0, function () { var results, _i, contentIds_1, contentId, info, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: results = []; _i = 0, contentIds_1 = contentIds; _a.label = 1; case 1: if (!(_i < contentIds_1.length)) return [3 /*break*/, 6]; contentId = contentIds_1[_i]; _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4 /*yield*/, this.getContentInfo(contentId)]; case 3: info = _a.sent(); results.push(info); return [3 /*break*/, 5]; case 4: error_1 = _a.sent(); throw error_1; // Fail fast as per user requirements case 5: _i++; return [3 /*break*/, 1]; case 6: return [2 /*return*/, results]; } }); }); }; /** * Batch check user permissions for multiple content items * @param userAddress User address * @param contentIds Array of content IDs * @returns Array of permission results */ VideoPaymentApi.prototype.batchCheckUserPermissions = function (userAddress, contentIds) { return __awaiter(this, void 0, void 0, function () { var wrapper, permissions, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4 /*yield*/, wrapper.getUserPermissions(userAddress, contentIds)]; case 3: permissions = _a.sent(); return [2 /*return*/, permissions.map(function (permission) { return ({ hasPermission: permission.isValid, permissionDetails: permission }); })]; case 4: error_2 = _a.sent(); throw error_2; // Fail fast as per user requirements case 5: return [2 /*return*/]; } }); }); }; // Admin Functions /** * Get current user's admin role information * @returns Admin role information */ VideoPaymentApi.prototype.getMyAdminRole = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { // This would need to be implemented based on available contract methods // For now, returning a placeholder return [2 /*return*/, { isOwner: false, isAdmin: false, canManageContent: false, canManageTokens: false, canManageAdmins: false }]; }); }); }; /** * Set content configuration (Admin only) * @param config Content configuration * @param options Transaction options * @returns Transaction hash */ VideoPaymentApi.prototype.setContentConfig = function (config, options) { return __awaiter(this, void 0, void 0, function () { var wrapper, tx; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [4 /*yield*/, wrapper.setContentConfig(config.contentId, ethers_1.ethers.utils.parseEther(config.nativePrice), config.defaultViewCount, config.viewDuration, config.isActive, options)]; case 2: tx = _a.sent(); return [2 /*return*/, tx.hash]; } }); }); }; /** * Batch set content configurations (Admin only) * @param configs Array of content configurations * @param options Transaction options * @returns Transaction hash */ VideoPaymentApi.prototype.batchSetContentConfig = function (configs, options) { return __awaiter(this, void 0, void 0, function () { var wrapper, nativePricesWei, tx; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); nativePricesWei = configs.nativePrices.map(function (price) { return ethers_1.ethers.utils.parseEther(price); }); return [4 /*yield*/, wrapper.batchSetContentConfig(configs.contentIds, nativePricesWei, configs.defaultViewCounts, configs.viewDurations, configs.isActiveArray, options)]; case 2: tx = _a.sent(); return [2 /*return*/, tx.hash]; } }); }); }; /** * Update content price (Admin only) * @param contentId Content ID * @param newPrice New price in ETH * @param options Transaction options * @returns Transaction hash */ VideoPaymentApi.prototype.updateContentPrice = function (contentId, newPrice, options) { return __awaiter(this, void 0, void 0, function () { var wrapper, tx; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [4 /*yield*/, wrapper.updateNativePrice(contentId, newPrice, options)]; case 2: tx = _a.sent(); return [2 /*return*/, tx.hash]; } }); }); }; /** * Set content active status (Admin only) * @param contentId Content ID * @param isActive Active status * @param options Transaction options * @returns Transaction hash */ VideoPaymentApi.prototype.setContentActive = function (contentId, isActive, options) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { // Suppress unused parameter warnings for incomplete implementation void contentId; void isActive; void options; // This method doesn't exist in the contract interface we saw // We might need to use setContentConfig with current values throw new Error('setContentActive method needs to be implemented'); }); }); }; /** * Check if user has view permission (wrapper for external address) * @param userAddress User address * @param contentId Content ID * @returns True if user has valid view permission */ VideoPaymentApi.prototype.userHasViewPermission = function (userAddress, contentId) { return __awaiter(this, void 0, void 0, function () { var wrapper; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [2 /*return*/, wrapper.hasViewPermission(userAddress, contentId)]; } }); }); }; /** * Get user permissions (wrapper for external address) * @param userAddress User address * @param contentId Content ID * @returns View permission details */ VideoPaymentApi.prototype.getUserPermissions = function (userAddress, contentId) { return __awaiter(this, void 0, void 0, function () { var wrapper, details; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getVideoPaymentWrapper()]; case 1: wrapper = _a.sent(); return [4 /*yield*/, wrapper.getPermissionDetails(userAddress, contentId)]; case 2: details = _a.sent(); return [2 /*return*/, { purchaseTime: details.purchaseTime, remainingViews: details.remainingViews, isValid: details.isValid }]; } }); }); }; /** * Process connection update */ VideoPaymentApi.prototype.onConnectionUpdated = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, _super.prototype.onConnectionUpdated.call(this)]; case 1: _a.sent(); if (this._videoPaymentWrapper) { this._videoPaymentWrapper.updateConnection(this._provider, this._signer); } return [2 /*return*/]; } }); }); }; return VideoPaymentApi; }(base_api_1.BaseApi)); exports.VideoPaymentApi = VideoPaymentApi;