serde icon indicating copy to clipboard operation
serde copied to clipboard

ICRC3 problems

Open infu opened this issue 1 year ago • 3 comments

The ICRC3 example from https://raw.githubusercontent.com/NatLabs/serde/main/usage.md#icrc3-value doesn't work

There is one here https://github.com/NatLabs/serde/blob/main/tests/Candid.ICRC3.Test.mo that does work, but it's not converting the object to a ICRC3 Generic Value type, it's still Candid type. I am assuming it's only using the ICRC3 fields, but the type is incompatible when we try to use it. We need it to be with this type, without all the extra Candid variations

    public type ValueMap = (Text, Value);
    public type Value = { 
        #Blob : Blob; 
        #Text : Text; 
        #Nat : Nat;
        #Int : Int;
        #Array : [Value]; 
        #Map : [ValueMap]; 
    };

Also, is it possible to not have to write down a list of fields, so it does them automatically?

 Candid.decode(blob, ["ts", "payload"], ?options) else return [];
                       ^^^^^^^^^^^^^^

Will it convert the whole nested structure recursively? Or it can only convert flat records ?

The code we are trying to write is


    public type Action = {
        ts: Nat64;
        payload : {
            #received: Received;
            #sent: Sent;
        };
    };

    func encodeBlock(b: Action): [Rechain.ValueMap] {
        let blob = to_candid(b);
        let options = { Candid.defaultOptions with use_icrc_3_value_type = true };
        let #ok(rcd) = Candid.decode(blob, ["ts", "payload"], ?options) else return [];
        rcd
    };

infu avatar Aug 31 '24 14:08 infu

Hey @Infu,

Thanks for reporting the issue. I'll take a look and update the example.

For the type issue, I can add fromICRC3Value() and toICRC3Value() methods to convert the resulting Candid type to an ICRC3 type. It would be an additional call on the decoded data. Is this okay?

Also, is it possible to not have to write down a list of fields, so it does them automatically?

No, it's not currently possible to convert without the list of fields. The reason is the candid specification requires record fields to be hashed when encoding. So internally when we use to_candid() to get the serialized version of the given motoko type we only receive the hashed fields. The list of fields the user adds to the decoding request allows us to match them with their hashed versions and name them properly when decoding.

Will it convert the whole nested structure recursively? Or it can only convert flat records

Yes, it will convert any number of nested records or arrays.

tomijaga avatar Sep 05 '24 08:09 tomijaga

Thanks, it's okay to have another method

infu avatar Sep 05 '24 11:09 infu

Hey @infu, I've published the changes in version 3.2.1 on mops. Try it out and let me know if there are any issues.

tomijaga avatar Sep 06 '24 07:09 tomijaga