Skip to content

Commit

Permalink
SHARD-899: fetch canUnstake data from validator endpoint (#35)
Browse files Browse the repository at this point in the history
* fetch canUnstake data from validator endpoint

* rename unstakeable to stakeState

---------

Co-authored-by: Sam Sweet <[email protected]>
  • Loading branch information
urnotsam and Sam Sweet authored Nov 11, 2024
1 parent 581f43a commit 6bda7c0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
63 changes: 44 additions & 19 deletions src/node-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
getCommitHashForCLI,
getCommitHashForGUI,
getCommitHashForValidator,
fetchUnstakeableDetails,
} from './utils';
import {isValidPrivate} from 'ethereumjs-util';
import logger from './utils/logger';
Expand Down Expand Up @@ -315,6 +316,31 @@ export function registerNodeCommands(program: Command) {
]);
// TODO: Use Promise.allSettled. Need to update nodeJs to 12.9

let nodeInfo;
try {
nodeInfo = await fetchNodeInfo(config);
} catch (e) {
logger.error('Unable to fetch node info: ' + e);
nodeInfo = null;
}

let unstakable = {
unlocked: false,
reason: 'Could not fetch data',
remainingTime: -1,
};
if (accountInfo.nominator) {
const eoaData = await fetchEOADetails(config, accountInfo.nominator);

if (eoaData) {
const unstakableData = await fetchUnstakeableDetails(
config,
publicKey,
accountInfo.nominator
);
unstakable = unstakableData ?? unstakable;
}
}
if (descriptions.length === 0) {
// Node process not started
console.log(
Expand All @@ -333,6 +359,7 @@ export function registerNodeCommands(program: Command) {
? ethers.utils.formatEther(accountInfo.totalPenalty)
: '',
autorestart: nodeConfig.autoRestart,
stakeState: unstakable,
})
);
cache.writeMaps();
Expand All @@ -344,14 +371,6 @@ export function registerNodeCommands(program: Command) {
if (status.status !== 'stopped') {
// Node is started and active

let nodeInfo;
try {
nodeInfo = await fetchNodeInfo(config);
} catch (e) {
logger.error('Unable to fetch node info: ' + e);
nodeInfo = null;
}

const lockedStakeStr = accountInfo.lockedStake
? ethers.utils.formatEther(accountInfo.lockedStake)
: '';
Expand Down Expand Up @@ -384,6 +403,7 @@ export function registerNodeCommands(program: Command) {
: '',
autorestart: nodeConfig.autoRestart,
nodeInfo: nodeInfo,
stakeState: unstakable,
// TODO: Add fetching node info when in standby
})
);
Expand Down Expand Up @@ -414,6 +434,7 @@ export function registerNodeCommands(program: Command) {
? ethers.utils.formatEther(accountInfo.totalPenalty)
: '',
autorestart: nodeConfig.autoRestart,
stakeState: unstakable,
})
);
cache.writeMaps();
Expand All @@ -432,24 +453,28 @@ export function registerNodeCommands(program: Command) {
}

try {
const eoaData = await fetchEOADetails(config, address)
const stakeValue = eoaData?.operatorAccountInfo?.stake?.value
const nominee = eoaData?.operatorAccountInfo?.nominee ?? ''
const eoaData = await fetchEOADetails(config, address);
const stakeValue = eoaData?.operatorAccountInfo?.stake?.value;
const nominee = eoaData?.operatorAccountInfo?.nominee ?? '';

// Convert stake value to ether, handling potential hexadecimal input
const stakeOutput = stakeValue
? ethers.utils.formatEther(
ethers.BigNumber.from(stakeValue.startsWith('0x') ? stakeValue : '0x' + stakeValue).toString()
ethers.BigNumber.from(
stakeValue.startsWith('0x') ? stakeValue : '0x' + stakeValue
).toString()
)
: ''
: '';

console.log(yaml.dump({
stake: stakeOutput,
nominee: nominee
}));
console.log(
yaml.dump({
stake: stakeOutput,
nominee: nominee,
})
);
} catch (error) {
console.log(error)
console.error(`Error fetching stake details for ${address}: ${error}`)
console.log(error);
console.error(`Error fetching stake details for ${address}: ${error}`);
}
});

Expand Down
19 changes: 19 additions & 0 deletions src/utils/fetch-network-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,25 @@ export async function fetchEOADetails(

return eoaParams?.account;
}
export async function fetchUnstakeableDetails(
config: networkConfigType,
nominee: string,
nominator: string
) {
const unstakable = await fetchDataFromNetwork<{
stakeUnlocked: {
unlocked: boolean;
reason: string;
remainingTime: number;
};
}>(
config,
`/canUnstake/${nominee}/${nominator}`,
data => data?.stakeUnlocked == null
);

return unstakable?.stakeUnlocked;
}

export async function fetchValidatorVersions(config: networkConfigType) {
const validatorVersions = await fetchDataFromNetwork<{
Expand Down

0 comments on commit 6bda7c0

Please sign in to comment.