@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
88 lines • 3.45 kB
JavaScript
"use strict";
/**
* @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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.claimBorrower = claimBorrower;
const kit_1 = require("@solana/kit");
const instructions_1 = require("../generated/codama/instructions");
const config_1 = require("../utils/config");
const signer_1 = require("../utils/signer");
const instructions_2 = require("../utils/instructions");
const config_2 = require("../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
* });
* ```
*/
async function claimBorrower(params, config) {
const finalConfig = (0, config_1.mergeConfig)(config);
if (!params.payer) {
throw new Error('payer is required');
}
const payerSigner = (0, signer_1.toTransactionSigner)(params.payer);
const borrowerAddress = params.borrower
? (0, kit_1.address)(params.borrower)
: (0, signer_1.getSignerAddress)(payerSigner);
const configAccount = await (0, config_2.fetchConfig)(finalConfig.rpcUrl);
const atlasMint = configAccount.data.atlasMint;
const addresses = (0, config_1.getAddresses)(config);
const programAddress = (0, kit_1.address)(addresses.srsly);
// Build claim instruction
const claimIx = await (0, instructions_1.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 !== (0, signer_1.getSignerAddress)(payerSigner)) {
throw new Error('close=true requires payer to be the borrower — only the borrower may authorize a close');
}
const closeIx = await (0, instructions_1.getCloseBorrowerInstructionAsync)({
borrower: payerSigner,
mint: atlasMint,
}, { programAddress });
return (0, instructions_2.prepareInstructions)([claimIx, closeIx], {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
return (0, instructions_2.prepareInstructions)(claimIx, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=claimBorrower.js.map