motoko icon indicating copy to clipboard operation
motoko copied to clipboard

Obtain actor principal without going through async methods

Open infu opened this issue 1 year ago • 5 comments

There is currently no way of passing 'this' - the current actor to a library inside the body during installation. Can we add a system function similar to postupgrade that will trigger on new install only. https://forum.dfinity.org/t/devefi-ledger-icrc-ledger-client-library/27274/3

infu avatar Feb 05 '24 15:02 infu

When writing libraries - middleware, we have to do something like this:

    public shared({caller}) func start() { 
        assert(Principal.isController(caller));
        config.canister := Principal.fromActor(this);
        ledgerOne.setOwner(this); 
    };

infu avatar Feb 27 '24 13:02 infu

c.f https://github.com/dfinity/motoko/issues/3718

crusso avatar Mar 08 '24 12:03 crusso

@crusso In our libraries we have a lot of code trying to distribute the current actor principal ~ 30 places. It feels like 5% of the code is about that, which is a bit strange. If we had Principal.currentActorPrincipal() it would be even better than when you added Principal.isController(..)

infu avatar Sep 03 '24 21:09 infu

Do you think we can have this feature soon? If not I could make our libraries/middleware create a new canister with whoami and call it to obtain their principal. Like - the worst solution on the planet, but it will be tucked in a library, so nobody will see it, or have to call 'start'. It is the only solution that works in local and mainnet.

infu avatar Sep 03 '24 21:09 infu

image image image image image image and a lot more. All 4 libraries (middleware) all using timers a lot - devefi-icrc-ledger, devefi-icp-ledger, devefi, rechain And all the pocket ic tests testing the libraries

infu avatar Sep 04 '24 10:09 infu