@gotake/gotake-sdk
Version:
SDK for interacting with GoTake blockchain contracts
581 lines (580 loc) • 26.7 kB
JavaScript
"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.VideoPaymentWrapper = void 0;
var VideoPayment__factory_1 = require("gotake-contracts/dist/typechain/factories/contracts/VideoPayment__factory");
var base_transaction_wrapper_1 = require("./base-transaction-wrapper");
/**
* Wrapper for VideoPayment contract, providing purchase and content management functionality
* Uses TypeChain generated types for full type safety
*/
var VideoPaymentWrapper = /** @class */ (function (_super) {
__extends(VideoPaymentWrapper, _super);
/**
* Create VideoPayment wrapper instance
* @param provider Provider instance
* @param signer Signer instance
*/
function VideoPaymentWrapper(provider, signer) {
var _this = _super.call(this, provider, signer) || this;
_this._videoPayment = null;
return _this;
}
/**
* Get VideoPayment contract instance
* @returns VideoPayment contract instance
*/
VideoPaymentWrapper.prototype.getVideoPayment = function () {
return __awaiter(this, void 0, void 0, function () {
var address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.init()];
case 1:
_a.sent();
if (!this._videoPayment) {
address = this.getContractAddress('videoPayment');
this._videoPayment = VideoPayment__factory_1.VideoPayment__factory.connect(address, this._signer);
}
return [2 /*return*/, this._videoPayment];
}
});
});
};
// Purchase Functions
/**
* Purchase content with native currency (ETH)
* @param contentId Content ID to purchase
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.purchaseWithNative = function (contentId, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.purchaseWithNative(contentId, overrides)];
}
});
});
};
/**
* Purchase content with ERC20 token
* @param contentId Content ID to purchase
* @param paymentToken ERC20 token address
* @param amount Token amount to pay
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.purchaseContent = function (contentId, paymentToken, amount, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.purchaseContent(contentId, paymentToken, amount, overrides)];
}
});
});
};
/**
* Batch purchase multiple content with native currency (ETH)
* @param contentIds Array of content IDs to purchase
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchPurchaseWithNative = function (contentIds, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchPurchaseWithNative(contentIds, overrides)];
}
});
});
};
/**
* Batch update native prices for multiple content (Admin only)
* @param contentIds Array of content IDs
* @param prices Array of new prices in wei
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchUpdateNativePrice = function (contentIds, prices, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchUpdateNativePrice(contentIds, prices, overrides)];
}
});
});
};
/**
* Batch update token prices for multiple content (Admin only)
* @param contentIds Array of content IDs
* @param token Token address
* @param prices Array of new prices
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchUpdateTokenPrice = function (contentIds, token, prices, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchUpdateTokenPrice(contentIds, token, prices, overrides)];
}
});
});
};
/**
* Batch consume views for multiple users and content (Admin only)
* @param users Array of user addresses
* @param contentIds Array of content IDs
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchConsumeView = function (users, contentIds, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchConsumeView(users, contentIds, overrides)];
}
});
});
};
/**
* Batch purchase multiple content with ERC20 token
* @param contentIds Array of content IDs to purchase
* @param paymentToken ERC20 token address
* @param totalAmount Total token amount to pay
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchPurchase = function (contentIds, paymentToken, totalAmount, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchPurchase(contentIds, paymentToken, totalAmount, overrides)];
}
});
});
};
// Query Functions
/**
* Check if user has view permission for content
* @param user User address
* @param contentId Content ID
* @returns True if user has valid view permission
*/
VideoPaymentWrapper.prototype.hasViewPermission = function (user, contentId) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
return [2 /*return*/, videoPayment.hasViewPermission(user, contentId)];
}
});
});
};
/**
* Get detailed permission information for user and content
* @param user User address
* @param contentId Content ID
* @returns View permission details
*/
VideoPaymentWrapper.prototype.getPermissionDetails = function (user, contentId) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
return [2 /*return*/, videoPayment.getPermissionDetails(user, contentId)];
}
});
});
};
/**
* Get user permissions for multiple content items
* @param user User address
* @param contentIds Array of content IDs
* @returns Array of view permission details
*/
VideoPaymentWrapper.prototype.getUserPermissions = function (user, contentIds) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
return [2 /*return*/, videoPayment.getUserPermissions(user, contentIds)];
}
});
});
};
/**
* Check if content is active
* @param contentId Content ID
* @returns True if content is active
*/
VideoPaymentWrapper.prototype.isContentActive = function (contentId) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
return [2 /*return*/, videoPayment.isContentActive(contentId)];
}
});
});
};
/**
* Check if token is supported
* @param tokenAddress Token address
* @returns True if token is supported
*/
VideoPaymentWrapper.prototype.isSupportedToken = function (tokenAddress) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
return [2 /*return*/, videoPayment.isSupportedToken(tokenAddress)];
}
});
});
};
// View Consumption
/**
* Consume a view for user (Admin only)
* @param user User address
* @param contentId Content ID
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.consumeView = function (user, contentId, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.consumeView(user, contentId, overrides)];
}
});
});
};
// Admin Functions
/**
* Set content configuration (Admin only)
* @param contentId Content ID
* @param nativePrice Native price in wei
* @param defaultViewCount Default view count
* @param viewDuration View duration in seconds
* @param isActive Whether content is active
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.setContentConfig = function (contentId, nativePrice, defaultViewCount, viewDuration, isActive, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.setContentConfig(contentId, nativePrice, defaultViewCount, viewDuration, isActive, overrides)];
}
});
});
};
/**
* Batch set content configurations (Admin only)
* @param contentIds Array of content IDs
* @param nativePrices Array of native prices in wei
* @param defaultViewCounts Array of default view counts
* @param viewDurations Array of view durations in seconds
* @param isActiveArray Array of active statuses
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.batchSetContentConfig = function (contentIds, nativePrices, defaultViewCounts, viewDurations, isActiveArray, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.batchSetContentConfig(contentIds, nativePrices, defaultViewCounts, viewDurations, isActiveArray, overrides)];
}
});
});
};
/**
* Update native price for content (Admin only)
* @param contentId Content ID
* @param price New price in wei
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.updateNativePrice = function (contentId, price, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.updateNativePrice(contentId, price, overrides)];
}
});
});
};
/**
* Update token price for content (Admin only)
* @param contentId Content ID
* @param token Token address
* @param price New price
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.updateTokenPrice = function (contentId, token, price, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.updateTokenPrice(contentId, token, price, overrides)];
}
});
});
};
/**
* Add supported token (Owner only)
* @param tokenAddress Token address to add
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.addSupportedToken = function (tokenAddress, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.addSupportedToken(tokenAddress, overrides)];
}
});
});
};
/**
* Remove supported token (Owner only)
* @param tokenAddress Token address to remove
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.removeSupportedToken = function (tokenAddress, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.removeSupportedToken(tokenAddress, overrides)];
}
});
});
};
/**
* Add admin (Owner only)
* @param adminAddress Admin address to add
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.addAdmin = function (adminAddress, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.addAdmin(adminAddress, overrides)];
}
});
});
};
/**
* Remove admin (Owner only)
* @param adminAddress Admin address to remove
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.removeAdmin = function (adminAddress, options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.removeAdmin(adminAddress, overrides)];
}
});
});
};
/**
* Pause contract (Owner only)
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.pause = function (options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.pause(overrides)];
}
});
});
};
/**
* Unpause contract (Owner only)
* @param options Transaction options
* @returns Transaction result
*/
VideoPaymentWrapper.prototype.unpause = function (options) {
return __awaiter(this, void 0, void 0, function () {
var videoPayment, overrides;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVideoPayment()];
case 1:
videoPayment = _a.sent();
overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
return [2 /*return*/, videoPayment.unpause(overrides)];
}
});
});
};
/**
* Update connection when provider or signer changes
* @param provider New provider instance
* @param signer New signer instance
*/
VideoPaymentWrapper.prototype.updateConnection = function (provider, signer) {
_super.prototype.updateConnection.call(this, provider, signer);
this._videoPayment = null;
};
return VideoPaymentWrapper;
}(base_transaction_wrapper_1.BaseTransactionWrapper));
exports.VideoPaymentWrapper = VideoPaymentWrapper;