@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
85 lines • 3.26 kB
JavaScript
/**
* @purpose Simplified claimBorrower instruction wrapper
*
* Thin convenience wrapper around Codama-generated claimBorrower instruction.
* Allows claiming accumulated ATLAS from a borrower's managed token account.
* When close is true, bundles a closeBorrower instruction to also close the
* managed ATA and BorrowerState in a single transaction.
*/
import { address } from '@solana/kit';
import { getClaimBorrowerInstructionAsync, getCloseBorrowerInstructionAsync, } from '../generated/codama/instructions';
import { mergeConfig, getAddresses } from '../utils/config';
import { toTransactionSigner, getSignerAddress } from '../utils/signer';
import { prepareInstructions } from '../utils/instructions';
import { fetchConfig } from '../accounts/config';
/**
* Claim ATLAS refunds and bonuses owed to a borrower.
*
* Sweeps the borrower's managed ATA — accumulated bid refunds and
* knockoff bonuses from reservations — into their personal wallet.
*
* @param params - Borrower claim parameters
* @param config - Optional SDK configuration overrides
* @returns Kit instruction (or web3.js if PublicKey in config)
*
* @example
* ```typescript
* // Self-claim: payer is the borrower
* const ix = await claimBorrower({
* payer: borrowerWallet,
* });
*
* // Third-party claim: someone else pays
* const ix2 = await claimBorrower({
* payer: relayerWallet,
* borrower: "BORROWER_ADDRESS"
* });
*
* // Claim and close the BorrowerState account
* const ix3 = await claimBorrower({
* payer: borrowerWallet,
* close: true
* });
* ```
*/
export async function claimBorrower(params, config) {
const finalConfig = mergeConfig(config);
if (!params.payer) {
throw new Error('payer is required');
}
const payerSigner = toTransactionSigner(params.payer);
const borrowerAddress = params.borrower
? address(params.borrower)
: getSignerAddress(payerSigner);
const configAccount = await fetchConfig(finalConfig.rpcUrl);
const atlasMint = configAccount.data.atlasMint;
const addresses = getAddresses(config);
const programAddress = address(addresses.srsly);
// Build claim instruction
const claimIx = await getClaimBorrowerInstructionAsync({
payer: payerSigner,
borrower: borrowerAddress,
mint: atlasMint,
}, { programAddress });
// If close requested, bundle a closeBorrower instruction after the
// claim. closeBorrower requires the borrower as sole signer, so the
// payer must be the borrower.
if (params.close) {
if (borrowerAddress !== getSignerAddress(payerSigner)) {
throw new Error('close=true requires payer to be the borrower — only the borrower may authorize a close');
}
const closeIx = await getCloseBorrowerInstructionAsync({
borrower: payerSigner,
mint: atlasMint,
}, { programAddress });
return prepareInstructions([claimIx, closeIx], {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
return prepareInstructions(claimIx, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=claimBorrower.js.map