Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
How to serialize enum in borsh-js

Trying to serialize an object in Rust and deserialize it in JS We got 000100000031 hash, after serialization this:

pub enum Service {
    Stackoverflow,
    Twitter,
    Telegram,
}
pub struct ServiceId {
    pub service: Service,
    pub id: ExternalId,
}

When trying to deserialize in JS use this:

const Service = {
    Stackoverflow: 0,
    Twitter: 1,
    Telegram: 2
}

class ServiceId {
    constructor(service, id) {
        this.service = service
        this.id = id
    }
}
const value = new ServiceId(Service.Stackoverflow, userId)

const schema = new Map([
            [ServiceId,
                { kind: 'struct', fields: [['service', 'u8'], ['id', 'string']] }]
        ]);

After deserialization, we got this, but it is incorrect because we have an object inside an object and a redundant id parameter:

ServiceId { service: { service: undefined, id: '1' }, id: undefined }

Firstly it could be because in Rust we have enum type, so how we can use enum in borsh-js. Second if not, why do we have an incorrect results?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is hard to understand from documentation, but you need to create your class like this and all will be okay.

class ServiceId {
    constructor({ service, id }) {
        this.service = service
        this.id = id
    }
}
new ServiceId({ service: 'lol', id: 'kek' })

So you need to pass your params as object.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda