JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q70466F
  • License MIT

SDK for interacting with GoTake blockchain contracts

Package Exports

  • @gotake/gotake-sdk

Readme

GoodTake SDK

GoodTake SDK is a JavaScript/TypeScript library for interacting with GoodTake smart contracts, supporting Token Bound Accounts (TBA) and IP NFT functionality.

Installation

Using npm:

npm install @gotake/gotake-sdk

Or using yarn:

yarn add @gotake/gotake-sdk

Browser & Frontend Compatibility ✨

The SDK is fully compatible with modern frontend frameworks and browsers:

  • ✅ React, Vue, Angular, Svelte
  • ✅ Vite, Webpack, Rollup, Parcel
  • ✅ Next.js, Nuxt.js, SvelteKit
  • ✅ Browser environments (Chrome, Firefox, Safari, Edge)
  • ✅ No Node.js polyfills required

Usage

Browser/Frontend Usage

For React, Vue, and other frontend frameworks:

import { GoTakeSDK } from '@gotake/gotake-sdk';
import { ethers } from 'ethers';

// Connect to user's wallet (MetaMask, WalletConnect, etc.)
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();

// Initialize SDK with signer from wallet
const sdk = new GoTakeSDK({
    network: 'sepolia',
    provider: provider,
    signer: signer  // Required in browser environment
});

// Ready to use!
const address = await sdk.account.getAddress();

Node.js Usage

For Node.js environments:

// ES Module import (Node.js with "type": "module")
import { GoTakeSDK } from '@gotake/gotake-sdk';

// CommonJS import (traditional Node.js)
const { GoTakeSDK } = require('@gotake/gotake-sdk');

// Initialize with private key from environment variables
const sdk = new GoTakeSDK({
    network: 'sepolia',
    // signer will be auto-loaded from PRIVATE_KEY env var
});

// Or provide private key directly
const sdk = new GoTakeSDK({
    network: 'sepolia',
    provider: 'https://sepolia.base.org',
    signer: '0x123...' // Your private key
});

Importing the SDK

The SDK supports both ES modules and CommonJS imports for maximum compatibility:

// ES Module import (recommended for React, Vue, modern bundlers)
import { GoTakeSDK } from '@gotake/gotake-sdk';

// CommonJS import (for Node.js, older projects)
const { GoTakeSDK } = require('@gotake/gotake-sdk');

// You can also import specific types and utilities
import { GoTakeSDK, NetworkId, VideoStatus } from '@gotake/gotake-sdk';

Initializing the SDK

// ES Module import (recommended for modern projects)
import { GoTakeSDK } from '@gotake/gotake-sdk';
import { ethers } from 'ethers';

// CommonJS import (for Node.js projects)
const { GoTakeSDK } = require('@gotake/gotake-sdk');

// Initialize with a private key
const sdk = new GoTakeSDK({
    provider: 'https://sepolia.base.org', // RPC URL or Provider instance
    signer: '0x123...', // Private key or Signer instance
});

// Or using environment variables from .env file
// Requires PRIVATE_KEY and RPC_URL to be set in .env
const sdk = new GoTakeSDK();

Network Support

The SDK supports multiple networks:

  • Ethereum Mainnet
  • Ethereum Goerli
  • Ethereum Sepolia
  • Base Mainnet
  • Base Goerli
  • Base Sepolia (default)

Switching networks:

// Switch by network ID
await sdk.switchNetwork(84532); // Switch to Base Sepolia

// Or switch by network name
await sdk.switchNetwork('Base Sepolia');

Account Operations (TBA)

Token Bound Accounts (TBA) are smart contract accounts linked to NFTs, providing additional storage and functionality:

// Create TBA
const { tbaAddress, tx } = await sdk.account.createTBA({
    tokenContract: '0x123...', // NFT contract address
    tokenId: 1, // NFT ID
});

// Initialize account
await sdk.account.initializeTBA(accountAddress, 84532, tokenContract, tokenId);

// Get account info
const accountInfo = await sdk.account.getTBAInfo(accountAddress);

// Execute transaction through TBA
const result = await sdk.account.executeTBATransaction(
    accountAddress,
    targetAddress,
    ethers.utils.parseEther('0.01'),
    '0x' // Transaction data
);

Video NFT Operations

// Create video NFT
const { tx, tokenId } = await sdk.video.uploadVideo(ownerAddress, {
    title: 'My Awesome Video',
    description: 'Description of the video',
    thumbnailUrl: 'ipfs://example/thumbnail.jpg',
    creator: ownerAddress
});

// Get video details
const videoDetails = await sdk.video.getVideoDetails(tokenId);

// Check if user is video owner
const isOwner = await sdk.video.isVideoOwner(tokenId, userAddress);

Complete Flow: Upload and Auto-Mint

// Upload video and automatically mint NFT when ready
const videoId = await sdk.uploadAndMintWhenReady(
  videoFile,
  {
    title: "My Awesome Video",
    description: "This is a description of my video",
    tags: ["tag1", "tag2"]
  },
  {
    statusCallback: (status) => {
      console.log(`Processing: ${status.status}`);
      updateUIProgress(status.progress || 0);
    },
    autoMint: true // Automatically mint when video is ready
  }
);

console.log(`Process started with video ID: ${videoId}`);

Content Management and Purchase

The SDK supports comprehensive content management and purchasing with multiple payment methods:

Content Management

// Create pay-per-view content (limited views)
await sdk.videoPayment.setContentConfig({
    contentId: 100,
    nativePrice: '0.01',      // Price in ETH
    defaultViewCount: 5,       // 5 views allowed
    viewDuration: 86400,       // 24 hours (in seconds)
    isActive: true
});

// Create lifetime access content (unlimited views)
await sdk.videoPayment.setContentConfig({
    contentId: 101,
    nativePrice: '0.02',       // Price in ETH
    defaultViewCount: 999999,  // Unlimited views
    viewDuration: 31536000,    // 1 year (in seconds)
    isActive: true
});

// Batch create multiple content items
await sdk.videoPayment.batchSetContentConfig({
    contentIds: [102, 103, 104],
    nativePrices: ['0.001', '0.002', '0.005'],
    defaultViewCounts: [3, 10, 999999],
    viewDurations: [86400, 172800, 31536000],
    isActiveArray: [true, true, true]
});

// Update content price
await sdk.videoPayment.updateContentPrice(
    100,                                    // Content ID
    ethers.utils.parseEther('0.015')       // New price
);

Purchase Content

// Check content information and supported payment methods
const contentInfo = await sdk.videoPayment.getContentInfo(100);
console.log('Price:', ethers.utils.formatEther(contentInfo.nativePrice), 'ETH');
console.log('View Count:', contentInfo.defaultViewCount);
console.log('Duration:', contentInfo.viewDuration / 3600, 'hours');
console.log('Supported Tokens:', Object.keys(contentInfo.tokenPrices).length);

// Purchase with ETH (native token)
const ethResult = await sdk.videoPayment.purchaseContent(100, 'ETH');
console.log('Purchase successful:', ethResult.transactionHash);

// Purchase with ERC20 token
const tokenResult = await sdk.videoPayment.purchaseContent(
    101, 
    'ERC20', 
    '0x4D1F4F683AB7122fBb77aB544aC03c5678acA968'  // Token address
);

// Batch purchase multiple content items
const batchResult = await sdk.videoPayment.batchPurchaseContent([102, 103], 'ETH');

// Check viewing permissions
const hasPermission = await sdk.videoPayment.hasViewPermission(100);
console.log('Can view content:', hasPermission);

// Get detailed permissions
if (hasPermission) {
    const permissions = await sdk.videoPayment.getMyPermissions(100);
    console.log('Remaining views:', permissions.remainingViews.toNumber());
    console.log('Purchase time:', new Date(permissions.purchaseTime.toNumber() * 1000));
    console.log('Valid until:', new Date(permissions.validUntil.toNumber() * 1000));
}

Gas Price Management

The SDK provides utilities for managing gas prices:

// Get current gas price recommendations
const gasPrices = await sdk.getGasPrice({
    multiplier: 1.5,         // Increase base fee by 50%
    priorityMultiplier: 1.2  // Increase priority fee by 20%
});

// Use gas prices in transaction options
const transactionOptions = {
    gasConfig: {
        gasLimit: 500000,
        maxFeePerGas: gasPrices.maxFeePerGas,
        maxPriorityFeePerGas: gasPrices.maxPriorityFeePerGas
    }
};

// Pass transaction options to any transaction method
const result = await sdk.account.createTBA(businessParams, transactionOptions);

Environment Variables

Create a .env file with the following variables:

# Network configuration
RPC_URL=https://sepolia.base.org

# Authentication
PRIVATE_KEY=your_private_key_here
INFURA_API_KEY=your_infura_api_key_here

# Optional alternative keys
DEVELOPER_KEY=alternative_private_key
USER_KEY=another_private_key

# API configuration (optional)
API_KEY=your_api_key_here
API_ENDPOINT=https://api.goodtake.io

Demo Scripts

The SDK includes several demo scripts that demonstrate common operations. These scripts are located in the scripts/ directory.

Setting Up the Demo Scripts

  1. Create a .env file in the project root with your PRIVATE_KEY
  2. Create an assets folder in the scripts directory and add a sample-video.mp4 file for the upload demos
  3. Run the demos using npm or yarn

Available Demo Scripts

1. Content Management (manage-content.ts)

npm run manage-content

This script demonstrates comprehensive content management capabilities:

Create Content:

# Create pay-per-view content (5 views, 24 hours)
npm run manage-content create 100 0.01 5 24 true

# Create lifetime access content (unlimited views)
npm run manage-content create 101 0.02 999999 8760 true

List and Check Content:

# List default content range
npm run manage-content list

# List specific content range
npm run manage-content list-range 1 20

# Generate comprehensive report
npm run manage-content report

Update Prices:

# Update single content price
npm run manage-content update-price 100 0.015

# Bulk update multiple prices (predefined)
npm run manage-content bulk-price-update

2. Content Purchase (purchase-content.ts)

npm run purchase-content

This script demonstrates content purchasing with multiple payment methods:

Check Content Information:

# Check content details and supported payment methods
npm run purchase-content check 100

Output includes:

  • Content activation status
  • View count and duration
  • Supported payment tokens (ETH/ERC20)
  • Current user access status
  • Smart purchase suggestions

Purchase with ETH:

# Purchase content with native ETH
npm run purchase-content buy 100

Purchase with ERC20 Token:

# Purchase content with ERC20 token
npm run purchase-content token 101 0x4D1F4F683AB7122fBb77aB544aC03c5678acA968

Batch Purchase:

# Purchase multiple content items at once
npm run purchase-content batch 100 101 102

3. Permission Checking (check-permissions.ts)

npm run check-permissions

This script demonstrates permission and access management:

Check User Permissions:

# Check current user permissions
npm run check-permissions check

# Check specific user permissions
npm run check-permissions check 0x742d35Cc6638C0532925a3b8A39c309b64D0b76b

# Check single content access
npm run check-permissions single 100

4. Create TBA (Token Bound Account)

npm run create-tba

This script demonstrates how to create a Token Bound Account for a specific NFT. It covers:

  • Setting up the SDK with the proper network
  • Getting gas price recommendations
  • Creating a TBA with optimized transaction options
  • Monitoring transaction confirmation

5. Upload Video

npm run upload-video

This script demonstrates the complete video upload flow:

  • Checking and creating a TBA if needed
  • Uploading a video file to the platform
  • Monitoring video processing status
  • Minting an IP NFT once processing is complete

6. Mint to TBA

npm run mint-to-tba

This script shows how to mint an NFT directly to a Token Bound Account:

  • Calculating a TBA address using specific parameters
  • Creating the TBA if it doesn't exist
  • Minting an NFT to the TBA address
  • Retrieving NFT details after minting

7. Upload and Mint (One-Step Process)

npm run upload-and-mint

This script demonstrates the unified one-step method to upload a video and mint an NFT:

  • Preparing video metadata and file
  • Using the uploadAndMintWhenReady method to handle the entire process
  • Tracking status updates through callbacks
  • Automatic minting when video processing is complete

Verified Testing Results ✅

The following functionality has been successfully tested and verified:

Content Management:

  • ✅ Create pay-per-view content (Content 100: 5 views, 24 hours)
  • ✅ Create lifetime access content (Content 101: 999999 views, 8760 hours)
  • ✅ Admin rights verification and content configuration

Purchase & Payment:

  • ✅ ETH native token purchases (both content types successfully purchased)
  • ✅ ERC20 token purchase support (code verified, ready for use)
  • ✅ Batch purchase functionality

Query & Information:

  • ✅ Smart content information display
  • ✅ Automatic payment method detection (ETH/ERC20)
  • ✅ User access status and remaining views
  • ✅ Purchase suggestions with command examples

Contract Compatibility:

  • ✅ Full compatibility with gotake-contracts@0.1.24
  • ✅ Unified purchase interface with address(0) for native ETH
  • ✅ New getContentPurchaseInfo method integration
  • ✅ Removed deprecated native-only methods

API Reference

GoTakeSDK Class

Main SDK class providing:

  • video: Video API
  • account: Account API
  • ipnft: IP NFT API
  • networkId: Current network ID
  • provider: Current Provider
  • signer: Current Signer
  • getAddress(): Get current user address
  • switchNetwork(network): Switch network
  • getGasPrice(options): Get gas price recommendations
  • uploadAndMintWhenReady(file, metadata, options): Complete upload and mint process
  • checkAndCreateTBA(options): Check for existing TBA or create new one

AccountApi

Provides TBA-related functionality:

  • computeTBAAddress(params): Calculate TBA address
  • createTBA(params, options): Create TBA
  • initializeTBA(address, chainId, tokenContract, tokenId): Initialize TBA
  • getTBAInfo(address): Get TBA information
  • executeTBATransaction(address, to, value, data, options): Execute TBA transaction
  • isValidSigner(address, signer): Check signer validity

IPNFTApi

Provides IP NFT functionality:

  • mint(to, metadata, options): Mint new IP NFT
  • getNFTDetails(tokenId): Get NFT details
  • isOwner(tokenId, address): Check if address is NFT owner

VideoApi

Provides video-related functionality:

  • uploadVideo(file, metadata): Upload video metadata and file
  • getVideoInfo(videoId): Get video status and details
  • subscribeToVideoStatus(videoId, callback): Subscribe to status updates
  • pollVideoStatus(videoId, options): Poll for status updates

Testing

GoodTake SDK provides three types of tests:

Unit Tests

npm test

Unit tests verify individual components without connecting to blockchain networks.

Integration Tests

# Set environment variables
export RUN_INTEGRATION_TESTS=true
export TEST_PRIVATE_KEY=your_private_key
export TEST_NFT_CONTRACT=0x...
export TEST_NFT_TOKEN_ID=1
export TEST_VIDEO_TOKEN_ID=2

# Run integration tests
npm run test:integration

Integration tests require connection to a test network (like Base Sepolia) and interact with real contracts.

Interoperability Tests

# Set environment variables
export RUN_INTEGRATION_TESTS=true
export TEST_PRIVATE_KEY=your_private_key

# Run interoperability tests
npm run test:interop

Interoperability tests verify interactions between TBA and IPNFT components.

Contributing

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add some amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

License

MIT