near / borsh-js

TypeScript/JavaScript implementation of Binary Object Representation Serializer for Hashing
Apache License 2.0
112 stars 38 forks source link

How to serialize UnorderedMap (Map)? #52

Closed pvolnov closed 10 months ago

pvolnov commented 2 years ago

How I can deserialize in borsh UnorderedMap?

Now my schema is

 const schema = new Map([
 [
 Record,
 {
 kind: "struct",
 fields: [
 ["x2", 

 { kind: 'map', key: 'string', value: 'u128'} 

],
 ],
 },
 ],
 ]);

Rust struct:

 pub struct Counter 

 { x2: UnorderedMap<AccountId, Balance>, } 

But I have error: "Expected buffer length 2036811841 isn't within bounds"

When I try serialize same struct in js, it dont work too

 const value = new Test({ x2: 

 {"1":"1"} 

 });
 const schema = new Map([
 [
 Test,
 {
 kind: "struct",
 fields: [
 ["x2", 

 { kind: 'map', key: 'string', value: 'string'} 

],
 ]
 }
 ]
 ]);
 const buffer = borsh.serialize(schema, value);
 console.log(buffer.toString('base64'))

Error: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type number (0)

gagdiez commented 10 months ago

should be fixed now, please open again if not