@saberhq/sail
Version:
Account caching and batched loading for React-based Solana applications.
99 lines • 3.68 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useUserATAs = exports.useATAs = void 0;
const token_utils_1 = require("@saberhq/token-utils");
const use_solana_1 = require("@saberhq/use-solana");
const react_1 = require("react");
const react_query_1 = require("react-query");
const native_1 = require("../native");
const splHooks_1 = require("../parsers/splHooks");
/**
* Cache of token/owner mapped to its ATA.
*/
const ataCache = {};
/**
* Loads ATAs owned by the provided owner.
* @param owner
* @param tokens
* @returns
*/
const useATAs = (owner, tokens) => {
const solBalance = (0, native_1.useSOLBalance)(owner);
const memoTokens = (0, react_1.useMemo)(() => tokens,
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(tokens.map((tok) => tok === null || tok === void 0 ? void 0 : tok.mintAccount.toString()))]);
const { data: userATAKeys } = (0, react_query_1.useQuery)([
"userATAKeys",
owner === null || owner === void 0 ? void 0 : owner.toString(),
...memoTokens.map((tok) => tok === null || tok === void 0 ? void 0 : tok.address),
], async () => {
return await Promise.all(memoTokens.map(async (token) => {
if (!token) {
return token;
}
if (!owner) {
return null;
}
const cacheKey = `${token.address}_${owner.toString()}`;
if (ataCache[cacheKey]) {
return ataCache[cacheKey];
}
const ata = await (0, token_utils_1.getATAAddress)({
mint: token.mintAccount,
owner,
});
ataCache[cacheKey] = ata;
return ata;
}));
}, {
staleTime: Infinity,
});
const { data: atas } = (0, splHooks_1.useBatchedTokenAccounts)(userATAKeys);
return (0, react_1.useMemo)(() => {
if (!owner) {
return null;
}
if (!atas) {
return atas;
}
return atas.map((datum, i) => {
var _a;
const token = memoTokens[i];
if (token === null || token === void 0 ? void 0 : token.mintAccount.equals(token_utils_1.RAW_SOL_MINT)) {
return {
key: owner,
balance: solBalance !== null && solBalance !== void 0 ? solBalance : new token_utils_1.TokenAmount(token, 0),
isInitialized: !!(solBalance && !solBalance.isZero()),
};
}
if (!token) {
return token;
}
const key = userATAKeys === null || userATAKeys === void 0 ? void 0 : userATAKeys[i];
if (!key) {
return key;
}
return {
key,
balance: new token_utils_1.TokenAmount(token, (_a = datum === null || datum === void 0 ? void 0 : datum.account.amount) !== null && _a !== void 0 ? _a : 0),
isInitialized: datum === null || datum === void 0 ? void 0 : datum.account.isInitialized,
};
});
}, [atas, memoTokens, owner, solBalance, userATAKeys]);
};
exports.useATAs = useATAs;
/**
* Loads ATAs owned by a user.
* @param tokens
* @returns
*/
const useUserATAs = (...tokens) => {
const wallet = (0, use_solana_1.useConnectedWallet)();
const atasList = (0, exports.useATAs)(wallet === null || wallet === void 0 ? void 0 : wallet.publicKey, tokens);
if (!atasList) {
return tokens.map(() => atasList);
}
return atasList;
};
exports.useUserATAs = useUserATAs;
//# sourceMappingURL=useUserATAs.js.map
;