UNPKG

blazerjob

Version:

TypeScript library for scheduling, executing, and managing asynchronous tasks (custom, HTTP, Cosmos) with a SQLite backend.

127 lines (126 loc) 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeCosmosTaskFn = makeCosmosTaskFn; exports.getBalance = getBalance; exports.getTx = getTx; exports.sendTokens = sendTokens; exports.getLatestBlockHeight = getLatestBlockHeight; exports.getBlockByHeight = getBlockByHeight; exports.getAccountInfo = getAccountInfo; exports.getAllBalances = getAllBalances; exports.getChainId = getChainId; exports.getTransactionByHash = getTransactionByHash; exports.searchTxs = searchTxs; exports.broadcastTx = broadcastTx; exports.getDelegation = getDelegation; const client_1 = require("./client"); /** * Fabrique une fonction de tâche Cosmos pour BlazeJob (queryType, queryParams...) */ function makeCosmosTaskFn(cfg) { return async () => { const { queryType, queryParams } = cfg; console.log('[Cosmos] queryType:', queryType, 'queryParams:', queryParams); if (!queryType || !queryParams || !queryParams.address) throw new Error('Invalid Cosmos config: missing address'); const rpcEndpoint = process.env.COSMOS_RPC_URL; if (!rpcEndpoint) { throw new Error('COSMOS_RPC_URL is not set in environment variables'); } const client = await (0, client_1.getStargateClient)(rpcEndpoint); if (queryType === 'balance') { const balance = await client.getAllBalances(queryParams.address); console.log('[Cosmos][balance]', balance); } else if (queryType === 'txs') { const txs = await client.searchTx(queryParams); console.log('[Cosmos][txs]', txs); } else { throw new Error('Unknown Cosmos query type: ' + queryType); } }; } async function getBalance(rpcUrl, address) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getAllBalances(address); } async function getTx(rpcUrl, hash) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getTx(hash); } async function sendTokens({ rpcUrl, mnemonic, to, amount, denom, gas = '200000', memo = '', chainId }) { const { client, wallet } = await (0, client_1.getSigningClientAndWallet)(rpcUrl, mnemonic); const [account] = await wallet.getAccounts(); const fee = { amount: [{ amount: gas, denom }], gas, }; return client.sendTokens(account.address, to, [{ amount, denom }], fee, memo); } /** * Query the current block height */ async function getLatestBlockHeight(rpcUrl) { const client = await (0, client_1.getStargateClient)(rpcUrl); const status = await client.getHeight(); return status; } /** * Query a block by height */ async function getBlockByHeight(rpcUrl, height) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getBlock(height); } /** * Query account info (number, sequence, etc) */ async function getAccountInfo(rpcUrl, address) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getAccount(address); } /** * Query all balances for an address */ async function getAllBalances(rpcUrl, address) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getAllBalances(address); } /** * Query chain ID */ async function getChainId(rpcUrl) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getChainId(); } /** * Query a transaction by hash (alias for getTx) */ async function getTransactionByHash(rpcUrl, hash) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getTx(hash); } /** * Query all transactions for an address (using searchTx) */ async function searchTxs(rpcUrl, query) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.searchTx(query); } /** * Broadcast a signed transaction (raw tx) */ async function broadcastTx(rpcUrl, txBytes) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.broadcastTx(txBytes); } /** * Query staking delegation for (delegator, validator) */ async function getDelegation(rpcUrl, delegatorAddress, validatorAddress) { const client = await (0, client_1.getStargateClient)(rpcUrl); return client.getDelegation(delegatorAddress, validatorAddress); } // Note: Some advanced queries (validators, supply, node info) require REST endpoints or LCD clients, not StargateClient. // For those, you can use fetch or cosmjs/launchpad/lcd for more advanced needs.