Converting Text to Principal

How we can convert text to principal ?

Calling this method
public shared(msg) func transfer(to: Principal, value: Nat) : async Bool {

as
dfx canister call sgymv-uiaaa-aaaaa-aaaia-cai transfer '(“principal h6m2n-nrloa-igrxa-r3psa-6nvfe-e3qj2-aowjt-skmcn-z6i6j-sx5b7-pae”,1000)

resulting error what could be the issue here ?

your reply much appreciated

To pass the principal in your dfx command needs to be just slightly different, put the word principal outside of the quotes like this:

dfx canister call sgymv-uiaaa-aaaaa-aaaia-cai transfer '(principal “h6m2n-nrloa-igrxa-r3psa-6nvfe-e3qj2-aowjt-skmcn-z6i6j-sx5b7-pae”, 1000)'

Get the following error

The Replica returned an error: code 5, message: “Canister sgymv-uiaaa-aaaaa-aaaia-cai trapped explicitly: IDL error: unexpected IDL type when parsing Nat”

Use 1000:nat for the amount

1 Like

This is the method

public shared(msg) func transfer(to: Principal, value: Nat) : async Bool {
    switch (balances.get(msg.caller)) {
        case (?from_balance) {
            if (from_balance >= value) {
                var from_balance_new = from_balance - value;
                assert(from_balance_new <= from_balance);
                balances.put(msg.caller, from_balance_new);

                var to_balance_new = switch (balances.get(to)) {
                    case (?to_balance) {
                        to_balance + value;
                    };
                    case (_) {
                        value;
                    };
                };
                assert(to_balance_new >= value);
                balances.put(to, to_balance_new);
                return true;
            } else {
                return false;
            };
        };
        case (_) {
            return false;
        };
    }
};

Thanks bro

It is working

So, we need to pass the data type in the argument as well

Nat then value:nat
Nat32 then value:nat32

Am i right ?

I think it’s just a temp bug to do with how numbers are handled in dfx, there was a post about it before (can’t find it now). This is only when sending it from CLI

1 Like

Thanks Stephen.

Wondering how we can receive ICP in motoko method.
I can get the msg.caller for the callers principal.

How i can get ICP sent to the function ?

Basically want to send some tokens back to the callers based on the value of the ICP sent by the caller.

1 Like