UNPKG

@aibtc/types

Version:

TypeScript types for AIBTC

87 lines (86 loc) 3.08 kB
/** * @file Defines types and helper functions for AIBTC Agent Account contract interactions. * @packageDocumentation */ /** * Defines the approval types for contracts in the AIBTC Agent Account. * These correspond to the constants in the `aibtc-agent-account.clar` contract. */ export const AGENT_ACCOUNT_APPROVAL_TYPES = { VOTING: 1, SWAP: 2, TOKEN: 3, }; /** * Defines the default permissions for an agent account. * These values correspond to the initial data-var values in the contract. */ export const AGENT_ACCOUNT_DEFAULT_PERMISSIONS = { canManageAssets: true, canUseProposals: true, canApproveRevokeContracts: true, canBuySell: false, }; /** * Defines the default deployer principal for an agent account on each network. */ export const AGENT_ACCOUNT_DEFAULT_DEPLOYER = { mainnet: "SP2Z94F6QX847PMXTPJJ2ZCCN79JZDW3PJ4E6ZABY", testnet: "ST2Q77H5HHT79JK4932JCFDX4VY6XA3Y1F61A25CD", devnet: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM", mocknet: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM", }; /** * Validates and converts a user-provided approval type (string or number) into the correct numeric value. * Throws an error if the input is invalid. * * @param typeInput - The approval type to validate, can be a string (e.g., "VOTING") or a number (e.g., 1). * @returns The corresponding numeric approval type. * @throws {Error} If the `typeInput` is not a valid approval type. */ export function getAgentAccountApprovalType(typeInput) { let numericType; const validValues = Object.values(AGENT_ACCOUNT_APPROVAL_TYPES); if (typeof typeInput === "number") { if (validValues.includes(typeInput)) { numericType = typeInput; } } else if (typeof typeInput === "string") { const approvalTypeNumber = parseInt(typeInput, 10); if (!isNaN(approvalTypeNumber)) { if (validValues.includes(approvalTypeNumber)) { numericType = approvalTypeNumber; } } else { const upperApprovalType = typeInput.toUpperCase(); if (upperApprovalType in AGENT_ACCOUNT_APPROVAL_TYPES) { numericType = AGENT_ACCOUNT_APPROVAL_TYPES[upperApprovalType]; } } } if (numericType === undefined) { const validOptions = [ ...Object.keys(AGENT_ACCOUNT_APPROVAL_TYPES), ...validValues, ].join(", "); throw new Error(`Invalid approval type: "${typeInput}". Must be one of: ${validOptions}`); } return numericType; } /** * Retrieves the default agent permissions. * @returns The default permissions. */ export function getAgentAccountDefaultPermissions() { return AGENT_ACCOUNT_DEFAULT_PERMISSIONS; } /** * Retrieves the default deployer address for an agent account for a given network. * @param network - The Stacks network name. * @returns The default deployer address for that network. */ export function getAgentAccountDefaultDeployer(network) { return AGENT_ACCOUNT_DEFAULT_DEPLOYER[network]; }