@accret/bridge-sdk
Version:
53 lines • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenInfo = getTokenInfo;
exports.getQuote = getQuote;
const axios_1 = __importDefault(require("axios"));
const constants_1 = require("../../constants");
/**
* @description Fetches token information from the Jupiter API.
* @param mint {String} The mint address of the token.
* @returns {Promise<GetJupiterTokenResponse>} The token information from the Jupiter API.
*/
async function getTokenInfo(mint) {
try {
const response = await axios_1.default.get(`${constants_1.JUPITER_API_URL}/tokens/v2/search?query=${mint}`);
if (!response.data) {
throw new Error(`No token data found for mint: ${mint}`);
}
return response.data[0];
}
catch (error) {
throw new Error(`Token info request failed: ${error instanceof Error ? error.message : "Unknown error"}`);
}
}
/**
* @description Fetches a quote for a token swap from the Jupiter API.
* @param param0 {Object} The parameters for the quote request.
* @param param0.inputMint {String} The mint address of the input token.
* @param param0.outputMint {String} The mint address of the output token.
* @param param0.amount {Number} The amount of input tokens to swap.
* @returns {Promise<QuoteResponse>} The quote response from the Jupiter API.
*/
async function getQuote({ inputMint, outputMint, amount, }) {
const tokenInfo = await getTokenInfo(inputMint);
if (!tokenInfo.decimals && tokenInfo.decimals !== 0) {
throw new Error(`Invalid token decimals for ${inputMint}`);
}
const params = {
inputMint: inputMint,
outputMint: outputMint,
amount: amount * 10 ** tokenInfo.decimals,
};
const response = await axios_1.default.get(`${constants_1.JUPITER_API_URL}/swap/v1/quote`, {
params,
});
if (!response.data) {
throw new Error("No quote data received from Jupiter API");
}
return response.data;
}
//# sourceMappingURL=getQuote.js.map