0

I used typegen to generate my types from definitions.

Definition

        Person: {
            status: 'Status',
            balance: 'u128',

        },
        Status: {
            _enum: ['Active', 'Suspended', 'Deactivated'],
        },

Generated types

/** @name Status */
export interface Status extends Enum {
  readonly isActive: boolean;
  readonly isSuspended: boolean;
  readonly isDeactivated: boolean;
  readonly type: 'Active' | 'Suspended' | 'Deactivated';
}

/** @name Person */
export interface Person extends Struct {
  readonly status: Status;
  readonly balance: u128;
}

Note that the enum type has fields isActive, isSuspended, isDeactivated. `

Decoding my type

I created a function in my contract that returns Result<Person, Error> I decode this result in JS.

const decoded = this.contract.api.registry.createType(`Result<Person, Error>`, result.data.toU8a(true))
if (decoded.isOk) {
    return decoded.asOk.toPrimitive()

This is where my problem starts. The toPrimitive function (and toJSON, toHuman, etc.) does not output the isActive, isSuspended, and isDeactivated fields of my Status enum. They are undefined.

How do I access the isActive, isSuspended, and isDeactivated fields after decoding my type?

1 Answer 1

3

Not able to reproduce your problem, but by looking at the code i am thinking it should be something like this:

if (result.isOk) {
  const decoded = this.contract.abi.registry.createType(`Result<Person, Error>`, [result.asOk.data]).toHuman()
}
console.log(decoded.isActive)

Your Answer

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

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