Skip to content

Commit

Permalink
Fixed missing status causing crashes changed to delay restart, typos,…
Browse files Browse the repository at this point in the history
… optional args via .env and more
  • Loading branch information
TrevorJTClarke committed Dec 4, 2021
1 parent 6801b3f commit 7583545
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
25 changes: 15 additions & 10 deletions bin/croncat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <account_id> <payable_account_id>',
command: 'register <account_id> [payable_account_id]',
desc: 'Add your agent to cron known agents',
builder: (yargs) => yargs
.option('account_id', {
Expand All @@ -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 <account_id> <payable_account_id>',
command: 'update <account_id> [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',
Expand All @@ -59,29 +61,31 @@ const unregisterAgent = {
};

const withdrawBalance = {
command: 'withdraw <account_id>',
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 <account_id>',
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);
}
};
Expand All @@ -96,15 +100,16 @@ const tasks = {
};

const go = {
command: 'go <account_id>',
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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
22 changes: 18 additions & 4 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/near.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.`)
Expand Down

0 comments on commit 7583545

Please sign in to comment.