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?