@nosana/kit
Version:
Nosana KIT
54 lines • 2.35 kB
JavaScript
import { createNosanaApi, } from '@nosana/api';
import { isTransactionPartialSigner } from '@solana/kit';
const createApiSolanaIntegration = (wallet, { solana, nos }) => ({
getBalance: async (address) => {
const [SOL, NOS] = await Promise.all([solana.getBalance(address), nos.getBalance(address)]);
return { SOL: SOL / 1e9, NOS };
},
transferTokensToRecipient: async (recipient, { SOL, NOS, lamports }) => {
const instructions = [];
if (SOL && SOL > 0) {
instructions.push(await solana.transfer({
to: recipient,
amount: lamports ? SOL : SOL * 1e9,
}));
}
if (NOS && NOS > 0) {
instructions.push(...(await nos.transfer({
to: recipient,
amount: lamports ? NOS : NOS * 1e6,
})));
}
if (instructions.length === 0) {
throw new Error('At least one of SOL or NOS amount must be greater than zero.');
}
await solana.buildSignAndSend(instructions);
},
deserializeSignSendAndConfirmTransaction: async (transactionData) => {
if (!isTransactionPartialSigner(wallet)) {
throw new Error('Wallet is not a transaction partial signer.');
}
const deserializedTx = await solana.deserializeTransaction(transactionData);
const decompileTransaction = await solana.decompileTransaction(deserializedTx);
if (decompileTransaction.feePayer.address !== wallet.address) {
throw new Error('Fee payer of the transaction must match the wallet address.');
}
const fullySignedTx = await solana.signTransactionWithSigners(deserializedTx, [wallet]);
return await solana.sendTransaction(fullySignedTx);
},
});
export function createApiInstance(network, config, wallet, deps) {
const { apiKey, ...apiConfig } = config || {};
if (apiKey) {
return createNosanaApi(network, apiKey, apiConfig);
}
if (wallet) {
return createNosanaApi(network, {
identifier: wallet.address.toString(),
generate: deps.authorization.generate,
solana: createApiSolanaIntegration(wallet, deps),
}, apiConfig);
}
return createNosanaApi(network, undefined, apiConfig);
}
//# sourceMappingURL=createApiInstance.js.map