@wuwei-labs/srsly
Version:
[](https://www.npmjs.com/package/ts-sdk-example)
132 lines • 5.36 kB
JavaScript
import { getBytesEncoder, getAddressEncoder, getProgramDerivedAddress } from '@solana/kit';
import { DEFAULT_GAME_ID, FACTION_MAPPING, FACTION_SPECIFIC_CSS, PROFILE_FACTION_PROGRAM_ADDRESS, PROFILE_FACTION_SEED, SAGE_PLAYER_PROFILE_SEED, SAGE_PROGRAM_ADDRESS, STARBASE_PLAYER_SEED, STARBASE_SEED } from './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
*/
export async function deriveProfileFaction(profile) {
const [address] = await getProgramDerivedAddress({
programAddress: PROFILE_FACTION_PROGRAM_ADDRESS,
seeds: [
seedToBytes(PROFILE_FACTION_SEED),
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
*/
export async function deriveSagePlayerProfile(borrowerProfile, gameId = DEFAULT_GAME_ID) {
const [address] = await getProgramDerivedAddress({
programAddress: SAGE_PROGRAM_ADDRESS,
seeds: [
seedToBytes(SAGE_PLAYER_PROFILE_SEED),
getAddressEncoder().encode(borrowerProfile),
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
*/
export async function deriveStarbase(faction, gameId = DEFAULT_GAME_ID) {
// Convert faction to string representation if it's a number
const factionName = typeof faction === 'number'
? FACTION_MAPPING[faction]
: faction;
// Validate faction
if (!factionName || !FACTION_SPECIFIC_CSS[factionName]) {
throw new Error(`Unsupported faction: ${faction}`);
}
// Get coordinates for the faction
const coords = 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 getProgramDerivedAddress({
programAddress: SAGE_PROGRAM_ADDRESS,
seeds: [
seedToBytes(STARBASE_SEED),
getAddressEncoder().encode(gameId),
getBytesEncoder().encode(xBytes),
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
*/
export 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 getProgramDerivedAddress({
programAddress: SAGE_PROGRAM_ADDRESS,
seeds: [
seedToBytes(STARBASE_PLAYER_SEED),
getAddressEncoder().encode(starbase),
getAddressEncoder().encode(sagePlayerProfile),
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
*/
export async function deriveGameAccounts(profile, faction, gameId = 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