@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
65 lines • 2.51 kB
JavaScript
/**
* @purpose Create a pending affiliate (Affiliate member) for SRSLY vault
*
* Wraps slyvault's initMember instruction with Affiliate type pre-configured.
* Auto-fetches vault and nonce from parent member state.
*/
import { address } from '@solana/kit';
import { initMember, fetchMember } from '@wuwei-labs/slyvault';
import { mergeConfig } from '../utils/config';
/**
* Register a pending affiliate under a parent Normal member. The
* affiliate must be activated via `approveAffiliate` before earnings flow.
*
* @param params - Affiliate creation parameters
* @param config - Optional SDK configuration overrides
* @returns Prepared instruction(s)
*
* @example
* ```typescript
* import { createAffiliate } from '@wuwei-labs/srsly';
*
* // Create affiliate linked to parent member
* const ix = await createAffiliate({
* affiliate: wallet,
* parentMemberState: "PARENT_MEMBER_STATE_PDA",
* discountAuthority: "OPTIONAL_DISCOUNT_SIGNER", // optional, defaults to main authority
* // vault and nonce are auto-fetched from parent state
* });
* ```
*/
export async function createAffiliate(params, config) {
const finalConfig = mergeConfig(config);
if (!params.affiliate)
throw new Error('affiliate is required');
if (!params.parentMemberState)
throw new Error('parentMemberState is required');
// Fetch parent member state to get vault and nonce
const { data: parentState } = await fetchMember(address(params.parentMemberState), finalConfig.rpcUrl);
// Validate parent is Normal type with affiliateEnabled
if (parentState.memberType.__kind !== 'Normal') {
throw new Error('Parent member is not a Normal member type');
}
if (!parentState.memberType.affiliateEnabled) {
throw new Error('Parent member does not have affiliates enabled');
}
// Get vault and nonce from parent state
const vaultAddress = parentState.vault;
const parentNonce = parentState.nonce;
// Call slyvault initMember with Affiliate type pre-configured
return initMember({
member: params.affiliate,
vault: vaultAddress,
nonce: params.nonce,
memberType: {
__kind: 'Affiliate',
parentMemberNonce: parentNonce,
percentageBps: 0,
claimableBalance: 0n,
isActive: false,
},
discountAuthority: params.discountAuthority,
computeUnits: params.computeUnits,
});
}
//# sourceMappingURL=createAffiliate.js.map