UNPKG

@wuwei-labs/srsly

Version:
236 lines (172 loc) 6.53 kB
# @wuwei-labs/srsly > Simplified TypeScript SDK for SRSLY **Status**: pre-1.0. Surface area is stable for the current instruction set but may grow as new program features ship. ## What is this? This is a simplified TypeScript SDK for SRSLY. It's a thin convenience wrapper around Codama-generated TypeScript clients, focusing on simplicity and maintainability. ## Key Differences from `@wuwei-labs/srsly` ### Simpler (80% less code) - **Old**: 4,000+ lines of custom abstractions - **New**: ~800 lines of thin wrappers ### Clearer Purpose - **Old**: Framework with fluent APIs, factories, builders - **New**: Convenience wrapper around Codama ### Easier Configuration - **Old**: Fluent selectors, chainable configs, network modules - **New**: Simple global config + optional overrides ### Better Maintenance - **Old**: Complex abstractions break when Codama updates - **New**: Thin wrappers adapt easily to Codama changes ## Installation ```bash pnpm add @wuwei-labs/srsly ``` ## Quick Start ### With @solana/kit (default) ```typescript import { setSdkConfig, createContract } from '@wuwei-labs/srsly'; // Set global config once setSdkConfig({ network: 'atlasnet' }); // Create a contract - returns @solana/kit instruction const instruction = await createContract({ owner: walletAddress, // string address fleet: fleetAddress, ownerProfile: profileAddress, gameId: gameAddress, rate: 100, // 100 ATLAS per payment durationMax: { days: 30 }, paymentsFreq: '@daily' }); ``` ### With @solana/web3.js ```typescript import { PublicKey } from '@solana/web3.js'; import { setSdkConfig, createContract } from '@wuwei-labs/srsly'; // Set global config with PublicKey constructor setSdkConfig({ network: 'mainnet-beta', PublicKey // Enable web3.js mode }); // Create a contract - returns web3.js TransactionInstruction const instruction = await createContract({ owner: keypair, // Accepts web3.js Keypair fleet: fleetPubkey, // Accepts web3.js PublicKey ownerProfile: profilePubkey, gameId: gamePubkey, rate: 100, durationMax: { days: 30 }, paymentsFreq: '@daily' }); // instruction.keys[0].pubkey is a PublicKey instance ``` ## Configuration ### Basic Configuration ```typescript import { setSdkConfig, getSdkConfig, clearSdkConfig } from '@wuwei-labs/srsly'; // Set global config setSdkConfig({ network: 'atlasnet' | 'mainnet-beta' | 'localnet', rpcUrl: 'https://custom-rpc.com', // Optional programId: 'custom-program-id', // Optional commitment: 'confirmed', // Optional PublicKey: PublicKey // Optional for web3.js mode }); // Get current config const config = getSdkConfig(); // Clear config (reset to defaults) clearSdkConfig(); // Override per-call const ix = await createContract(params, { rpcUrl: 'https://temporary-rpc.com' }); ``` ### Network Addresses The SDK provides centralized address management for different networks: ```typescript import { getAddresses, ATLASNET_ADDRESSES, MAINNET_ADDRESSES } from '@wuwei-labs/srsly'; // Get addresses for current network (from global config) const addresses = getAddresses(); console.log(addresses.atlasMint); // ATLAS token mint console.log(addresses.sage); // SAGE program console.log(addresses.srsly); // SRSLY program // Override for specific network const mainnetAddrs = getAddresses({ network: 'mainnet-beta' }); // Custom SRSLY program const customAddrs = getAddresses({ programId: 'MyCustomSRSLY...' }); console.log(customAddrs.srsly); // 'MyCustomSRSLY...' // Direct access to network addresses console.log(ATLASNET_ADDRESSES); // All atlasnet addresses console.log(MAINNET_ADDRESSES); // All mainnet addresses ``` **Available Networks:** - `'atlasnet'` - Star Atlas testnet (default) - `'mainnet-beta'` - Solana mainnet / Star Atlas production - `'localnet'` - Local development **Network Addresses:** - `srsly` - SRSLY rental program - `sage` - Star Atlas SAGE program - `atlasMint` - ATLAS token mint - `profileFaction` - Profile Faction program - `gameId` - Star Atlas game ID ``` ## API Reference ### Instructions - `createContract(params, config?)` - Create a rental contract - `closeContract(params, config?)` - Close a rental contract - `acceptRental(params, config?)` - Accept a rental - `cancelRental(params, config?)` - Cancel a rental - _(More to come)_ ### Parameters All instruction params accept: - **Signers**: String addresses, web3.js Keypairs, or @solana/kit signers - **Addresses**: String addresses or web3.js PublicKeys - **Rates**: Numbers (ATLAS) or objects `{ atlas: 1.5 }` or `{ stardust: 150_000_000 }` - **Durations**: Objects `{ days: 7 }`, `{ hours: 24 }`, `{ weeks: 2 }`, etc. - **Schedules**: Strings `'@daily'`, `'@hourly'`, or cron expressions ### Return Types - **Without PublicKey in config**: Returns @solana/kit instruction - **With PublicKey in config**: Returns web3.js TransactionInstruction ### Discount Authorization The SDK provides a `createDiscount` helper for server-side discount authorization. This is useful when an affiliate wants to offer discounts to borrowers. #### Server-side ```typescript import { setSdkConfig, createDiscount } from '@wuwei-labs/srsly'; // Can be set once globally (e.g., at app startup) setSdkConfig({ rpcUrl: 'https://your-rpc-url.com' }); const discountAuth = await createDiscount({ address: 'AFFILIATE_MEMBER_PDA', discount: 10, // 10% discount expirySlots: 1000, // ~6-7 minutes from now discountAuthority: serverKeypair, }); // Already JSON-safe - return directly via API return discountAuth; ``` #### Client-side ```typescript import { acceptRental } from '@wuwei-labs/srsly'; // Pass directly from server response - no deserialization needed const ix = await acceptRental({ borrower: wallet, contract: contractAddress, duration: { days: 7 }, discountAuth: response.discountAuth, referrer: affiliateMemberAddress, }); ``` ## Development Status - ✅ Core config system - ✅ Centralized address management - ✅ Signer handling (web3.js + kit) - ✅ web3.js conversion - ✅ Parameter converters (amount, duration, schedule) - ✅ First instruction: `createContract` - 🚧 Additional contract instructions - 🚧 Rental instructions - 🚧 Other instructions - 🚧 Tests ## Migration from `@wuwei-labs/srsly` Coming soon - see migration guide once this package reaches 1.0.0. ## Contributing This package is currently in beta. Please report issues on GitHub. ## License ISC