UNPKG

@jagad/icsi

Version:

Internet Computer Subaccount Indexer Library - TypeScript SDK for ICP multi-token subaccount management, transaction tracking, and automated sweeping with webhook support

267 lines (266 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserVaultTransactions = getUserVaultTransactions; exports.getUserVaultInterval = getUserVaultInterval; exports.getTransactionsCount = getTransactionsCount; exports.getNonce = getNonce; exports.getSubaccountCount = getSubaccountCount; exports.getSubaccountId = getSubaccountId; exports.getWebhookUrl = getWebhookUrl; exports.getCanisterPrincipal = getCanisterPrincipal; exports.getIcrcAccount = getIcrcAccount; exports.getNetwork = getNetwork; exports.getNextBlock = getNextBlock; exports.getOldestBlock = getOldestBlock; exports.getRegisteredTokens = getRegisteredTokens; exports.getTransactionTokenType = getTransactionTokenType; const agent_1 = require("@dfinity/agent"); const utils_1 = require("./utils"); const userVault_did_1 = require("./userVault.did"); /** * Creates an actor for interacting with a user vault canister. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {_SERVICE} - The actor instance. */ function createUserVaultActor(agent, userVaultCanisterId) { (0, utils_1.isNotEmptyOrError)(userVaultCanisterId, 'User Vault Canister ID is undefined.'); return agent_1.Actor.createActor(userVault_did_1.idlFactory, { agent, canisterId: userVaultCanisterId, }); } /** * Calls the list_transactions function on a canister to fetch transactions. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @param {bigint} [upToIndex] - The number of blocks for the transactions to fetch. * @returns {Promise<Result_9>} - The list of transactions wrapped in a Result. */ async function getUserVaultTransactions(agent, userVaultCanisterId, upToIndex) { const actor = createUserVaultActor(agent, userVaultCanisterId); return await actor.list_transactions(upToIndex ? [upToIndex] : []); } /** * Calls the get_interval function on a canister to get the interval. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<bigint>} - The interval value. * @throws {Error} - Throws if the call returns an error. */ async function getUserVaultInterval(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_interval(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Calls the get_transactions_count function on a canister to get the transaction count. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<number>} - The transaction count. * @throws {Error} - Throws if the call returns an error. */ async function getTransactionsCount(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_transactions_count(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Calls the get_nonce function on a canister to get the nonce value. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<number>} - The nonce value. * @throws {Error} - Throws if the call returns an error. */ async function getNonce(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_nonce(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Calls the get_subaccount_count function on a canister to get the subaccount count. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<number>} - The subaccount count. * @throws {Error} - Throws if the call returns an error. */ async function getSubaccountCount(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_subaccount_count(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Calls the get_subaccountid function on a canister to get the subaccount ID by index. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @param {number} index - The index of the subaccount. * @param {TokenType} tokenType - The token type for the subaccount. * @returns {Promise<string>} - The subaccount ID. * @throws {Error} - Throws if the call returns an error. */ async function getSubaccountId(agent, userVaultCanisterId, index, tokenType) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_subaccountid(index, [tokenType]); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err.message); } } /** * Retrieves the webhook URL set for the user's vault. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<string>} - A promise that resolves to the webhook URL. * @throws {Error} - Throws if the call returns an error. */ async function getWebhookUrl(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_webhook_url(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Gets the principal of the user vault canister. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<string>} - A promise that resolves to the canister principal. * @throws {Error} - Throws if the call returns an error. */ async function getCanisterPrincipal(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_canister_principal(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Gets the ICRC-1 account representation for a subaccount. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @param {number} index - The index of the subaccount. * @returns {Promise<string>} - A promise that resolves to the ICRC-1 account. * @throws {Error} - Throws if the call returns an error. */ async function getIcrcAccount(agent, userVaultCanisterId, index) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_icrc_account(index); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err.message); } } /** * Gets the network the user vault is running on (Mainnet or Local). * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<"Mainnet" | "Local">} - A promise that resolves to the network. * @throws {Error} - Throws if the call returns an error. */ async function getNetwork(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_network(); if ('Ok' in result) { if ('Mainnet' in result.Ok) { return 'Mainnet'; } else { return 'Local'; } } else { throw new Error(result.Err); } } /** * Gets the next block to be processed by the user vault. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<bigint>} - A promise that resolves to the next block. * @throws {Error} - Throws if the call returns an error. */ async function getNextBlock(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_next_block(); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } } /** * Gets the oldest block processed by the user vault. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<bigint | undefined>} - A promise that resolves to the oldest block or undefined. * @throws {Error} - Throws if the call returns an error. */ async function getOldestBlock(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_oldest_block(); if ('Ok' in result) { return result.Ok.length > 0 ? result.Ok[0] : undefined; } else { throw new Error(result.Err); } } /** * Gets the token types registered with the user vault. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @returns {Promise<Result_7>} - The registered tokens wrapped in a Result. */ async function getRegisteredTokens(agent, userVaultCanisterId) { const actor = createUserVaultActor(agent, userVaultCanisterId); return await actor.get_registered_tokens(); } /** * Gets the token type for a specific transaction. * @param {HttpAgent} agent - The HTTP agent used for the call. * @param {string} userVaultCanisterId - The canister ID of the user vault. * @param {string} txHash - The transaction hash. * @returns {Promise<TokenType>} - A promise that resolves to the token type. * @throws {Error} - Throws if the call returns an error. */ async function getTransactionTokenType(agent, userVaultCanisterId, txHash) { const actor = createUserVaultActor(agent, userVaultCanisterId); const result = await actor.get_transaction_token_type(txHash); if ('Ok' in result) { return result.Ok; } else { throw new Error(result.Err); } }