Skip to content

Commit 5844a4b

Browse files
committed
fix: Better error handling for the calculateRewards call
1 parent 7141f98 commit 5844a4b

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

lib/api/blockchain.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import console from 'console';
1010
import { differenceInSeconds } from 'date-fns';
1111
import Moralis from 'moralis';
1212
import { EvmAddress, EvmChain } from 'moralis/common-evm-utils';
13+
import type { ReadContractReturnType } from 'viem';
1314
import { isZeroAddress } from '../utils';
1415
import { getPublicClient } from './client';
1516

@@ -413,23 +414,30 @@ const getMndOrGndLicenseRewards = async (
413414
}
414415

415416
const publicClient = await getPublicClient();
416-
const result = await publicClient.readContract({
417-
address: config.mndContractAddress,
418-
abi: MNDContractAbi,
419-
functionName: 'calculateRewards',
420-
args: [
421-
[
422-
{
423-
licenseId,
424-
nodeAddress: license.nodeAddress,
425-
epochs: epochs.map((epoch) => BigInt(epoch)),
426-
availabilies: epochs_vals,
427-
},
417+
418+
let result: ReadContractReturnType<typeof MNDContractAbi, 'calculateRewards'> | undefined;
419+
420+
try {
421+
result = await publicClient.readContract({
422+
address: config.mndContractAddress,
423+
abi: MNDContractAbi,
424+
functionName: 'calculateRewards',
425+
args: [
426+
[
427+
{
428+
licenseId,
429+
nodeAddress: license.nodeAddress,
430+
epochs: epochs.map((epoch) => BigInt(epoch)),
431+
availabilies: epochs_vals,
432+
},
433+
],
428434
],
429-
],
430-
});
435+
});
436+
} catch {
437+
return undefined;
438+
}
431439

432-
if (result.length !== 1) {
440+
if (!result || result.length !== 1) {
433441
throw new Error('Invalid rewards calculation result');
434442
}
435443

0 commit comments

Comments
 (0)