diff --git a/bin/croncat.js b/bin/croncat.js index 17a4c6b..b1899f2 100644 --- a/bin/croncat.js +++ b/bin/croncat.js @@ -5,8 +5,10 @@ import { utils } from 'near-api-js' import getConfig from '../src/configuration' const { agentFunction, bootstrapAgent, runAgentTick, registerAgent } = require('../src/actions') +const AGENT_ACCOUNT_ID = process.env.AGENT_ACCOUNT_ID + const registerAgentCmd = { - command: 'register ', + command: 'register [payable_account_id]', desc: 'Add your agent to cron known agents', builder: (yargs) => yargs .option('account_id', { @@ -21,18 +23,18 @@ const registerAgentCmd = { }), handler: async options => { // await agentFunction('register_agent', options, false, undefined, 1e25); - await registerAgent(options.account_id, options.payable_account_id, options); + await registerAgent(options.account_id, options.payable_account_id || options.account_id, options); } }; const updateAgent = { - command: 'update ', + command: 'update [payable_account_id]', desc: 'Update your agent to cron known agents', builder: (yargs) => yargs .option('account_id', { desc: 'Account to add', type: 'string', - required: true + required: true, }) .option('payable_account_id', { desc: 'Account that receives reward payouts', @@ -59,29 +61,31 @@ const unregisterAgent = { }; const withdrawBalance = { - command: 'withdraw ', + command: 'withdraw [account_id]', desc: 'Withdraw all rewards earned for this account', builder: (yargs) => yargs .option('account_id', { desc: 'Account that earned rewards.', type: 'string', - required: true + required: false }), handler: async options => { + if (!options.account_id) options.account_id = AGENT_ACCOUNT_ID await agentFunction('withdraw_task_balance', options); } }; const status = { - command: 'status ', + command: 'status [account_id]', desc: 'Check agent status and balance for this account', builder: (yargs) => yargs .option('account_id', { desc: 'Account to check', type: 'string', - required: true + required: false }), handler: async options => { + if (!options.account_id && AGENT_ACCOUNT_ID) options.account_id = AGENT_ACCOUNT_ID await agentFunction('get_agent', options, true); } }; @@ -96,15 +100,16 @@ const tasks = { }; const go = { - command: 'go ', + command: 'go [account_id]', desc: 'Run tasks that are available, if agent is registered and has balance', builder: (yargs) => yargs .option('account_id', { desc: 'Account to check', type: 'string', - required: true + required: false }), handler: async options => { + if (!options.account_id && AGENT_ACCOUNT_ID) options.account_id = AGENT_ACCOUNT_ID await bootstrapAgent(options.account_id, options) // MAIN AGENT LOOP diff --git a/package.json b/package.json index 87a9cf3..9697317 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "croncat", - "version": "1.5.2", + "version": "1.6.0", "description": "cron.cat CLI and Agent Runner", "main": "src/index.js", "scripts": { diff --git a/src/actions.js b/src/actions.js index 7b7c7dc..4cf0932 100644 --- a/src/actions.js +++ b/src/actions.js @@ -39,7 +39,7 @@ const pingHeartbeat = async () => { return Promise.resolve() } -function removeUneededArgs(obj) { +function removeUnneededArgs(obj) { const allowed = ['agent_account_id', 'payable_account_id', 'account', 'offset', 'accountId', 'account_id', 'payableAccountId'] const fin = {} @@ -225,6 +225,9 @@ export async function runAgentTick(options = {}) { agentSettings = await getAgent(agentId) } catch (ae) { agentSettings = {} + // if no status, trigger a delayed retry + setTimeout(() => { runAgentTick(options) }, WAIT_INTERVAL_MS) + return; } // Check agent is active & able to run tasks if (!agentSettings || !agentSettings.status || agentSettings.status !== 'Active') { @@ -283,7 +286,7 @@ export async function runAgentTick(options = {}) { export async function agentFunction(method, args, isView, gas = BASE_GAS_FEE, amount = BASE_ATTACHED_PAYMENT) { const account = args.account || args.account_id || args.agent_account_id || AGENT_ACCOUNT_ID const manager = await getCronManager(account, args) - const params = method === 'unregister' ? {} : removeUneededArgs(args) + const params = method === 'unregister' ? {} : removeUnneededArgs(args) let res if (LOG_LEVEL === 'debug') console.log(account, isView, manager[method], params, gas, amount); @@ -310,10 +313,21 @@ export async function agentFunction(method, args, isView, gas = BASE_GAS_FEE, am if (isView && res) { try { const payload = typeof res === 'object' ? res : JSON.parse(res) + + if (method === 'get_agent') { + const balance = await Near.getAccountBalance() + const formattedBalance = utils.format.formatNearAmount(balance) + payload.wallet_balance = formattedBalance + } + + if (payload.balance) { + payload.reward_balance = utils.format.formatNearAmount(payload.balance) + delete payload.balance + } + log('\n') Object.keys(payload).forEach(k => { - const value = k === 'balance' ? utils.format.formatNearAmount(payload[k]) : payload[k] - log(`${chalk.bold.white(k.replace(/\_/g, ' '))}: ${chalk.white(value)}`) + log(`${chalk.bold.white(k.replace(/\_/g, ' '))}: ${chalk.white(payload[k])}`) }) log('\n') } catch (ee) { diff --git a/src/near.js b/src/near.js index fcfa76f..0a8e2aa 100644 --- a/src/near.js +++ b/src/near.js @@ -1,6 +1,6 @@ import "core-js/stable" import "regenerator-runtime/runtime" -import { connect, KeyPair, keyStores, Contract, WalletConnection, WalletAccount } from 'near-api-js' +import { connect, KeyPair, keyStores, Contract, utils, WalletConnection, WalletAccount } from 'near-api-js' // import fs from 'fs' import path from 'path' import { homedir } from 'os' @@ -34,7 +34,7 @@ class NearProvider { const keyPair = KeyPair.fromRandom('ed25519') const publicKey = keyPair.publicKey.toString() - const id = accountId || implicitAccountId(publicKey) + const id = accountId || utils.PublicKey.fromString(publicKey).data.hexSlice() this.accountId = id await keyStore.setKey(this.config.networkId, id, keyPair) console.log(`NEW AGENT CREATED: "${id}", Public Key ${publicKey}\n Requires funds to start processing tasks.`)