1

I am trying to do a democracy vote using the API.

First I am getting the balance of my account:

  const balance = (await api.query.system.account(my_account)).data.free;
  const voteConfig: Vote = new GenericVote(api.registry, {                                                                                                                           
    aye: true,                                                                                                                                                                      
    conviction: 'None',                                                                                                                                                              
  });                                                                                                                                                                                
  let vote: PalletDemocracyVoteAccountVote = { asStandard: { vote: voteConfig, balance: balance } };

  let txHash = await api.tx.democracy.vote(i, vote).signAndSend(payoutKey, (({ events = [], status }: any) => {

But I am getting the type error below:

/home/nochem/.nvm/versions/node/v17.4.0/lib/node_modules/ts-node/src/index.ts:820
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
votebot.ts:102:48 - error TS2322: Type '{ vote: Vote; balance: u128; }' is not assignable to type '{ readonly vote: Vote; readonly balance: u128; } & Struct<TypesDef<Codec>, { [x: string]: any; }, { [x: string]: string; }>'.
  Type '{ vote: Vote; balance: u128; }' is missing the following properties from type 'Struct<TypesDef<Codec>, { [x: string]: any; }, { [x: string]: string; }>': #private, defKeys, getT, isEmpty, and 25 more.

102   let vote: PalletDemocracyVoteAccountVote = { asStandard: { vote: voteConfig, balance: balance } };
                                                   ~~~~~~~~~~

  node_modules/@polkadot/types-augment/lookup/types-substrate.d.ts:430:18
    430         readonly asStandard: {
                         ~~~~~~~~~~
    The expected type comes from property 'asStandard' which is declared here on type 'PalletDemocracyVoteAccountVote'

    at createTSError (/home/nochem/.nvm/versions/node/v17.4.0/lib/node_modules/ts-node/src/index.ts:820:12)

How can I compile the above code?

1 Answer 1

5

asStandard is when you receive the object back. In that case the is<Key> and as<Key> helpers allow you to convert the received enum to the internal view of a specific field.

To construct it, it needs the key Standard for the enum, so you need -

{ Standard: { vote: voteConfig, balance: balance } }

The API would always create the types, so you never need to do it manually via new. In this case I would suggest -

  let vote = { 
    Standard: {
      vote: {                                                                                                                           
        aye: true,                                                                                                                                                                      
        conviction: 'None',                                                                                                                                                              
      },
      balance: balance
    }
  };

  let unsub = await api.tx.democracy.vote(i, vote).signAndSend(payoutKey, ...);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.