UNPKG

@wuwei-labs/srsly

Version:

[![npm version](https://img.shields.io/npm/v/ts-sdk-example.svg)](https://www.npmjs.com/package/ts-sdk-example)

139 lines 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deriveProfileFaction = deriveProfileFaction; exports.deriveSagePlayerProfile = deriveSagePlayerProfile; exports.deriveStarbase = deriveStarbase; exports.deriveStarbasePlayer = deriveStarbasePlayer; exports.deriveGameAccounts = deriveGameAccounts; const kit_1 = require("@solana/kit"); const constants_1 = require("./constants"); /** * Helper function to convert a string seed to a Uint8Array * This is necessary for @solana/kit's getBytesEncoder().encode() * * @param seed The string seed to convert * @returns A Uint8Array containing the UTF-8 encoded seed */ function seedToBytes(seed) { return new Uint8Array(Buffer.from(seed)); } /** * Helper function to derive the borrower's profile faction account * * @param profile The borrower's profile address * @returns Promise that resolves to the derived profile faction address */ async function deriveProfileFaction(profile) { const [address] = await (0, kit_1.getProgramDerivedAddress)({ programAddress: constants_1.PROFILE_FACTION_PROGRAM_ADDRESS, seeds: [ seedToBytes(constants_1.PROFILE_FACTION_SEED), (0, kit_1.getAddressEncoder)().encode(profile) ] }); return address; } /** * Helper function to derive the sage player profile * * @param borrowerProfile The borrower's profile address * @param gameId The game ID (defaults to the standard game ID) * @returns Promise that resolves to the derived sage player profile address */ async function deriveSagePlayerProfile(borrowerProfile, gameId = constants_1.DEFAULT_GAME_ID) { const [address] = await (0, kit_1.getProgramDerivedAddress)({ programAddress: constants_1.SAGE_PROGRAM_ADDRESS, seeds: [ seedToBytes(constants_1.SAGE_PLAYER_PROFILE_SEED), (0, kit_1.getAddressEncoder)().encode(borrowerProfile), (0, kit_1.getAddressEncoder)().encode(gameId) ] }); return address; } /** * Helper function to derive the starbase address based on faction * * @param faction The faction (1/2/3 or 'mud'/'oni'/'ustur') * @param gameId The game ID (defaults to the standard game ID) * @returns Promise that resolves to the derived starbase address */ async function deriveStarbase(faction, gameId = constants_1.DEFAULT_GAME_ID) { // Convert faction to string representation if it's a number const factionName = typeof faction === 'number' ? constants_1.FACTION_MAPPING[faction] : faction; // Validate faction if (!factionName || !constants_1.FACTION_SPECIFIC_CSS[factionName]) { throw new Error(`Unsupported faction: ${faction}`); } // Get coordinates for the faction const coords = constants_1.FACTION_SPECIFIC_CSS[factionName]; // Convert coordinates to bytes (little-endian, two's complement) const xCoord = new DataView(new ArrayBuffer(8)); const yCoord = new DataView(new ArrayBuffer(8)); xCoord.setBigInt64(0, BigInt(coords.x), true); yCoord.setBigInt64(0, BigInt(coords.y), true); // Create Uint8Array from DataView const xBytes = new Uint8Array(xCoord.buffer); const yBytes = new Uint8Array(yCoord.buffer); const [address] = await (0, kit_1.getProgramDerivedAddress)({ programAddress: constants_1.SAGE_PROGRAM_ADDRESS, seeds: [ seedToBytes(constants_1.STARBASE_SEED), (0, kit_1.getAddressEncoder)().encode(gameId), (0, kit_1.getBytesEncoder)().encode(xBytes), (0, kit_1.getBytesEncoder)().encode(yBytes) ] }); return address; } /** * Helper function to derive the starbase player address * * @param sagePlayerProfile The sage player profile address * @param starbase The starbase address * @param starbaseSeqId The starbase sequence ID (defaults to 0) * @returns Promise that resolves to the derived starbase player address */ async function deriveStarbasePlayer(sagePlayerProfile, starbase, starbaseSeqId = 0) { // Convert sequence ID to bytes (little-endian uint16) const seqIdView = new DataView(new ArrayBuffer(2)); seqIdView.setUint16(0, starbaseSeqId, true); const seqIdBytes = new Uint8Array(seqIdView.buffer); const [address] = await (0, kit_1.getProgramDerivedAddress)({ programAddress: constants_1.SAGE_PROGRAM_ADDRESS, seeds: [ seedToBytes(constants_1.STARBASE_PLAYER_SEED), (0, kit_1.getAddressEncoder)().encode(starbase), (0, kit_1.getAddressEncoder)().encode(sagePlayerProfile), (0, kit_1.getBytesEncoder)().encode(seqIdBytes) ] }); return address; } /** * Derives all three accounts needed: profileFaction, starbase, and starbasePlayer * * @param profile The borrower's profile address * @param faction The faction (1/2/3 or 'mud'/'oni'/'ustur') * @param gameId The game ID (defaults to the standard game ID) * @param starbaseSeqId The starbase sequence ID (defaults to 0) * @returns Promise that resolves to the three derived addresses */ async function deriveGameAccounts(profile, faction, gameId = constants_1.DEFAULT_GAME_ID, starbaseSeqId = 0) { // Derive the three accounts in parallel const [profileFaction, sagePlayerProfile, starbase] = await Promise.all([ deriveProfileFaction(profile), deriveSagePlayerProfile(profile, gameId), deriveStarbase(faction, gameId) ]); // Derive starbasePlayer using the results from above const starbasePlayer = await deriveStarbasePlayer(sagePlayerProfile, starbase, starbaseSeqId); return { profileFaction, starbase, starbasePlayer }; } //# sourceMappingURL=profiles.js.map