Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICRC3 problems #31

Open
infu opened this issue Aug 31, 2024 · 3 comments
Open

ICRC3 problems #31

infu opened this issue Aug 31, 2024 · 3 comments

Comments

@infu
Copy link

infu commented Aug 31, 2024

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
    };
@tomijaga
Copy link
Member

tomijaga commented Sep 5, 2024

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.

@infu
Copy link
Author

infu commented Sep 5, 2024

Thanks, it's okay to have another method

@tomijaga
Copy link
Member

tomijaga commented Sep 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants