Icrc2_approve + agent-js

My code looks like this:

const agent = new HttpAgent({ host: ‘https : // nns.ic0 . app’ }); //no spaces in url, i cudnt post links
agent.fetchRootKey();
const actor = await window.Actor2.createActor(idlFactory, {
agent: agent,
canisterId: “ryjl3-tyaaa-aaaaa-aaaba-cai”,
});

            // Call the icrc1_balance_of() function
            const balanceOfArgs = {
                owner: Principal.fromText(window.curuser.key),
                subaccount: [],
            };
            try {
                const balance = await actor.icrc1_balance_of(balanceOfArgs);
                console.log('Balance:', balance);
            } catch (error) {
                console.error('Error calling icrc1_balance_of():', error);
            }
            //the above function works and returns the proper balance

            
            // Call the icrc2_approve() function
            const approveArgs = {
                fee: [],
                memo: [],
                from_subaccount: [],
                created_at_time: [],
                amount: 50,
                expected_allowance: [],
                expires_at: [],
                spender: {
                    owner: Principal.fromText(window.curuser.key), //i also tried the canisterId of the canister i launched
                    subaccount: [],
                },
            };

            try {
                const approveResult = await actor.icrc2_approve(approveArgs);
                if (approveResult.Ok) {
                    console.log('Approve successful, transaction ID:', approveResult.Ok);
                } else {
                    console.error('Approve failed:', approveResult.Err);
                }
            } catch (error) {
                console.error('Error calling icrc2_approve():', error);
            }

Now the “icrc1_balance_of” call works and returns the proper balance… but icrc2_approve doesnt work. I’m logged into Internet Identity and it gives me this error:

Error calling icrc2_approve(): Error: Call was rejected:
Request ID: xxxxxxxxxxxxxxxx
Reject code: 4
Reject text: Anonymous principal cannot approve token transfers on the ledger.

at pollForResponse 

does anyone know how to do icrc2_approve from javascript?

When you instantiate your actor, you need to pass in your identity that holds the tokens. Depending on what login method you’re using, there is generally a method to get the identity. For example, after you call the window.plug.connect There should be some more methods on window.plug that give you the identity.

You can use this library for easier interaction with ic agent and canisters.

you can find more about the library here Introducing @ic-reactor/core: Streamline Your Development on the Internet Computer

Thank, you were correct. I wasnt passing the identity argument when creating the actor

My new code looks like this and now it work:

            const authClient = await AuthClient.create();
            const identity = authClient.getIdentity();
            console.log('identity:', identity);

            //connect to nns-ledger with identity
            const jsagent = HttpAgent({ host: 'https://nns.ic0.app', identity: identity });
            jsagent.fetchRootKey();
            jsactor = await Actor.createActor(idlFactory, {
                agent: jsagent,
                canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
            });

One small question, do I have to do jsagent.fetchRootKey();? im not sure why i did that

in localhost deployment YES, in mainnet deployment NO.