Skip to content

Commit

Permalink
covering faulty error handling on near balance retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorJTClarke committed Dec 14, 2021
1 parent 24e8e56 commit 5686235
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
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.6.0",
"version": "1.6.1",
"description": "cron.cat CLI and Agent Runner",
"main": "src/index.js",
"scripts": {
Expand Down
33 changes: 26 additions & 7 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import slack from './slack'

const log = console.log
export const env = process.env.NODE_ENV || 'development'
export const near_env = process.env.NEAR_ENV || 'testnet'
export const LOG_LEVEL = process.env.LOG_LEVEL || 'info'
export const WAIT_INTERVAL_MS = process.env.WAIT_INTERVAL_MS ? parseInt(`${process.env.WAIT_INTERVAL_MS}`) : 30000
export const AGENT_ACCOUNT_ID = process.env.AGENT_ACCOUNT_ID || 'croncat-agent'
Expand Down Expand Up @@ -66,6 +67,17 @@ export async function connect(options) {
}
}

export async function getAgentBalance() {
try {
const balance = await Near.getAccountBalance()
return balance
} catch (e) {
log(`${chalk.red('NEAR RPC Failed')}`)
notifySlack(`*Attention!* NEAR ${near_env} RPC Failed to retrieve balance!`)
return 0
}
}

export async function getCronManager(accountId, options) {
if (cronManager) return cronManager
await connect(options)
Expand Down Expand Up @@ -137,7 +149,7 @@ export async function getCroncatInfo(options) {
}

export async function checkAgentBalance(agentId) {
const balance = await Near.getAccountBalance()
const balance = await getAgentBalance()
const formattedBalance = utils.format.formatNearAmount(balance)
const hasEnough = Big(balance).gt(BASE_GAS_FEE)
log(`
Expand All @@ -157,7 +169,7 @@ export async function checkAgentBalance(agentId) {
}

export async function checkAgentTaskBalance(options) {
const balance = await Near.getAccountBalance()
const balance = await getAgentBalance()
const notEnough = Big(balance).lt(AGENT_MIN_TASK_BALANCE)
if (notEnough) {
log(`
Expand All @@ -174,9 +186,16 @@ export async function refillAgentTaskBalance(options) {
args: {},
gas: BASE_GAS_FEE,
})
const balance = await Near.getAccountBalance()
log(`Agent Refilled, Balance: ${chalk.blue(utils.format.formatNearAmount(balance))}`)
notifySlack(`Agent Refilled, Balance: *${utils.format.formatNearAmount(balance)}*`)
const balance = await getAgentBalance()
const notEnough = Big(balance).lt(AGENT_MIN_TASK_BALANCE)
if (notEnough) {
log(`${chalk.red('Balance too low.')}`)
notifySlack(`*Attention!* Not enough balance to execute tasks, refill please.`)
process.exit(1)
} else {
log(`Agent Refilled, Balance: ${chalk.blue(utils.format.formatNearAmount(balance))}`)
notifySlack(`Agent Refilled, Balance: *${utils.format.formatNearAmount(balance)}*`)
}
} catch (e) {
log(`${chalk.red('No balance to withdraw.')}`)
notifySlack(`*Attention!* No balance to withdraw.`)
Expand Down Expand Up @@ -315,7 +334,7 @@ export async function agentFunction(method, args, isView, gas = BASE_GAS_FEE, am
const payload = typeof res === 'object' ? res : JSON.parse(res)

if (method === 'get_agent') {
const balance = await Near.getAccountBalance()
const balance = await getAgentBalance()
const formattedBalance = utils.format.formatNearAmount(balance)
payload.wallet_balance = formattedBalance
}
Expand All @@ -337,7 +356,7 @@ export async function agentFunction(method, args, isView, gas = BASE_GAS_FEE, am

if (method === 'get_agent') {
// Check User Balance
const balance = await Near.getAccountBalance()
const balance = await getAgentBalance()

// ALERT USER is their balance is lower than they should be
if (!balance || balance < 3e24) {
Expand Down

0 comments on commit 5686235

Please sign in to comment.