Salisu Bako
Salisu Bako

Reputation: 1

How do I check collators intent in polkadotjs/api like we check for validators?

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,
    // it indicates that the validator has bonded tokens and intends to participate in the     staking process and has intent to validate
    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 collators 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?

Upvotes: 0

Views: 38

Answers (0)

Related Questions