@aarc-dev/core-viem
Version:
305 lines • 12.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenPrice = exports.GetTransactionStatus = exports.GetSupportedChains = exports.GetSupportedTokens = exports.IsTokenSupported = exports.getMultiChainBalances = exports.fetchBalances = exports.executeForwardCallData = exports.generateCheckoutCallData = exports.generateForwardCallData = exports.generateDepositCallData = exports.executeGaslessCallData = exports.generateNonGaslessCallData = exports.generateGaslessCallData = void 0;
const Constants_1 = require("./Constants");
const HttpRequest_1 = require("./HttpRequest");
const Logger_1 = require("./Logger");
const generateGaslessCallData = async (forwardCallDataDto, dappApiKey) => {
try {
const txResponse = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.GENERATE_GASLESS_CALL_DATA_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": dappApiKey,
},
body: forwardCallDataDto,
});
Logger_1.Logger.log("body ", JSON.stringify(forwardCallDataDto));
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse;
}
catch (error) {
Logger_1.Logger.error("error while getting consuming forward endpoint");
throw error;
}
};
exports.generateGaslessCallData = generateGaslessCallData;
const generateNonGaslessCallData = async (forwardCallDataDto, dappApiKey) => {
try {
const txResponse = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.GENERATE_NON_GASLESS_CALL_DATA_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": dappApiKey,
},
body: forwardCallDataDto,
});
Logger_1.Logger.log("body ", JSON.stringify(forwardCallDataDto));
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse;
}
catch (error) {
Logger_1.Logger.error("error while getting consuming forward endpoint");
throw error;
}
};
exports.generateNonGaslessCallData = generateNonGaslessCallData;
const executeGaslessCallData = async (chainId, forwardCalldataSet, dappApiKey) => {
try {
const txResponse = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.EXECUTE_GASLESS_CALL_DATA_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": dappApiKey,
},
body: { chainId, trxList: forwardCalldataSet },
});
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse.data;
}
catch (error) {
Logger_1.Logger.error("error while getting consuming forward endpoint");
throw error;
}
};
exports.executeGaslessCallData = executeGaslessCallData;
const generateDepositCallData = async (depositCallDataDto, dappApiKey) => {
try {
// Convert depositCallDataDto to a query string using URLSearchParams
const params = new URLSearchParams();
for (const key in depositCallDataDto) {
if (depositCallDataDto.hasOwnProperty(key)) {
// Ensure the value is a string
params.append(key, String(depositCallDataDto[key]));
}
}
// Append query string to the endpoint URL
const url = `${Constants_1.GENERATE_DEPOSIT_CALL_DATA_ENDPOINT}?${params.toString()}`;
const txResponse = await (0, HttpRequest_1.sendRequest)({
url,
method: HttpRequest_1.HttpMethod.GET,
headers: {
"x-api-key": dappApiKey,
},
});
Logger_1.Logger.log("body ", JSON.stringify(depositCallDataDto));
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse;
}
catch (error) {
Logger_1.Logger.error("error while consuming deposit call data endpoint");
throw error;
}
};
exports.generateDepositCallData = generateDepositCallData;
const generateForwardCallData = async (forwardCallDataDto, dappApiKey) => {
try {
const txResponse = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.GENERATE_FORWARD_CALL_DATA_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": dappApiKey,
},
body: forwardCallDataDto,
});
Logger_1.Logger.log("body ", JSON.stringify(forwardCallDataDto));
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse.data;
}
catch (error) {
Logger_1.Logger.error("error while getting consuming forward endpoint");
throw error;
}
};
exports.generateForwardCallData = generateForwardCallData;
const generateCheckoutCallData = async (depositCallDataDto, dappApiKey) => {
try {
// Destructure checkoutPreferences and other properties from depositDto
const { outputTokenPreferences, ...rest } = depositCallDataDto;
const callDataDto = {
...rest,
...outputTokenPreferences,
};
// Convert callDataDto to a query string using URLSearchParams
const params = new URLSearchParams();
for (const key in callDataDto) {
if (callDataDto.hasOwnProperty(key)) {
params.append(key, String(callDataDto[key]));
}
}
// Append query string to the endpoint URL
const url = `${Constants_1.GENERATE_CHECKOUT_CALL_DATA_ENDPOINT}?${params.toString()}`;
const txResponse = await (0, HttpRequest_1.sendRequest)({
url,
method: HttpRequest_1.HttpMethod.GET,
headers: {
"x-api-key": dappApiKey,
},
});
Logger_1.Logger.log("body ", JSON.stringify(depositCallDataDto));
Logger_1.Logger.log("txResponse from server ", JSON.stringify(txResponse.data));
return txResponse;
}
catch (error) {
Logger_1.Logger.error("error while consuming checkout call data endpoint", error);
throw error;
}
};
exports.generateCheckoutCallData = generateCheckoutCallData;
const executeForwardCallData = async (chainId, forwardCalldataSet, dappApiKey) => {
try {
const txResponse = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.EXECUTE_FORWARD_CALL_DATA_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": dappApiKey,
},
body: {
chainId,
trxList: forwardCalldataSet.forwardCallDataResponse,
txIndexes: forwardCalldataSet.txIndexes,
},
});
return txResponse.data;
}
catch (error) {
Logger_1.Logger.error("error while getting consuming forward endpoint");
throw error;
}
};
exports.executeForwardCallData = executeForwardCallData;
/**
* @description this function will return balances of ERC-20, ERC-721 and native tokens
* @param balancesDto
* @returns
*/
const fetchBalances = async (apiKey, chainId, eoaAddress, fetchBalancesOnly = true, tokenAddresses) => {
try {
// Make the API call using the sendRequest function
const response = await (0, HttpRequest_1.sendRequest)({
url: Constants_1.BALANCES_ENDPOINT,
method: HttpRequest_1.HttpMethod.POST,
headers: {
"x-api-key": apiKey,
},
body: {
chainId: String(chainId),
address: eoaAddress,
onlyBalances: fetchBalancesOnly,
tokenAddresses: tokenAddresses,
},
});
Logger_1.Logger.log("Fetching API Response:", response);
return response;
}
catch (error) {
// Handle any errors that may occur during the API request
Logger_1.Logger.error("Error making backend API call:", error);
throw error;
}
};
exports.fetchBalances = fetchBalances;
const getMultiChainBalances = async (apiKey, walletAddress, destinationToken) => {
let payload = "";
if (destinationToken) {
if (destinationToken === null || destinationToken === void 0 ? void 0 : destinationToken.tokenAmount) {
payload += `tokenAddress=${destinationToken.tokenAddress}&tokenChainId=${destinationToken.tokenChainId}&tokenAmount=${destinationToken.tokenAmount}`;
}
else {
payload += `tokenAddress=${destinationToken.tokenAddress}&tokenChainId=${destinationToken.tokenChainId}`;
}
}
const response = await fetch(`${Constants_1.MULTI_CHAIN_BALANCES_ENDPOINT}/${walletAddress}?${payload}`, {
method: "GET",
headers: {
"x-api-key": apiKey,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const json = await response.json();
return json;
};
exports.getMultiChainBalances = getMultiChainBalances;
// Makes a GET request to Aarc APIs for supported token by address and chainId
const IsTokenSupported = async (apiKey, chainId, address) => {
const endPoint = `${Constants_1.IS_TOKEN_SUPPORTED_ENDPOINT}?chainId=${chainId}&address=${address}`;
const response = await fetch(endPoint, {
method: "GET",
headers: {
"x-api-key": apiKey,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const json = await response.json();
return json;
};
exports.IsTokenSupported = IsTokenSupported;
// Makes a GET request to Aarc APIs for supported token by chain
const GetSupportedTokens = async (apiKey, chainId, isShortList = true) => {
const endPoint = `${Constants_1.TOKENS_SUPPORTED_ENDPOINT}?chainId=${chainId}&isShortList=${isShortList}`;
const response = await fetch(endPoint, {
method: "GET",
headers: {
"x-api-key": apiKey,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const json = await response.json();
return json;
};
exports.GetSupportedTokens = GetSupportedTokens;
// Makes a GET request to Aarc APIs for supported chains
const GetSupportedChains = async (apiKey) => {
const endPoint = `${Constants_1.CHAINS_SUPPORTED_ENDPOINT}`;
const response = await fetch(endPoint, {
method: "GET",
headers: {
"x-api-key": apiKey,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const json = await response.json();
return json;
};
exports.GetSupportedChains = GetSupportedChains;
const GetTransactionStatus = async (taskId) => {
try {
const endpoint = `${Constants_1.TRX_STATUS_ENDPOINT + "/" + taskId}`;
const response = await fetch(endpoint, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
return response.json();
}
catch (error) {
Logger_1.Logger.error("Error getting transaction status:", error);
throw error;
}
};
exports.GetTransactionStatus = GetTransactionStatus;
const getTokenPrice = async (apiKey, tokenSymbol, tokenAddress, tokenChainId) => {
let url = `${Constants_1.TOKEN_PRICE_ENDPOINT}/${tokenSymbol}`;
if (tokenAddress)
url += `?tokenAddress=${tokenAddress}`;
if (tokenChainId)
url += `&tokenChainId=${tokenChainId}`;
const response = await fetch(url, {
method: "GET",
headers: {
"x-api-key": apiKey,
Accept: "application/json",
"Content-Type": "application/json",
},
});
const json = await response.json();
return json;
};
exports.getTokenPrice = getTokenPrice;
//# sourceMappingURL=Helper.js.map