0

I want to monitor the intent of my collators using @polkadotjs/api like I am monitoring for my validators using the below codes :

async function checkValidatorIntent(url, validatorAddress) {
  try {
    // connect to Polkadot API
    const provider = new WsProvider(url);
    const api = await ApiPromise.create({ provider });

    // get the specified validator's intent
    const validator = await api.query.staking.validators(validatorAddress);

    const stashAccount = await api.query.staking.bonded(validatorAddress);
        
    // if a stash account exists for a validator,
    const hasIntent = !!stashAccount && !stashAccount.isEmpty;

    const commission = validator.commission.toString()

   
    if (commission == '0' && !hasIntent) {
      console.log(`Validator ${validatorAddress} has no intent to validate!`)
     
    }else{
          console.log(`Validator ${validatorAddress} has intent to validate with commission ${commission / Math.pow(10, 7)}%`)

    }
    
    // disconnect from Polkadot API
    await provider.disconnect();
  } catch (error) {
    console.error(error);
  }
}

I tried the above code for my collator's addresss but it was returning undefined. What changes do I need to make to the above code to make it work for both collators and validators?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.