Encode Payload for ExecuteGenericNervousSystemFunction

I believe in this code I will need to encode my payload:

const createManageNeuronRequestForProposal = (neuronId, title, url, summary, function_id, payload) => {
return {
id: [{ NeuronId: neuronId }],
command: [{
MakeProposal: {
title: title,
url: url,
summary: summary,
action: {
ExecuteGenericNervousSystemFunction: {
function_id: function_id,
payload: payload
}
}
}
}],
neuron_id_or_subaccount: [{ NeuronId: neuronId }]
};
};

const revaluePlayerUp = async (playerId, userPrincipal) => {
    const proposalTitle = "Execute generic function for player";
    const proposalUrl = "https://openfpl.xyz/governance";
    const proposalSummary = `Proposal to execute a generic function for player ${playerId}.`;

    const payload = encodePayload(`(${playerId})`); //need encode payload function

    const neurons = await listNeurons({ principal: userPrincipal });
    for (const neuron of neurons) {
        const neuronId = neuron.id[0].NeuronId.toString();
        const manageNeuronRequest = createManageNeuronRequestForProposal(neuronId, proposalTitle, proposalUrl, proposalSummary, 1000, payload);
        await SnsGovernanceCanister.manageNeuron(manageNeuronRequest);
    }
};

Is there a built in function anywhere to encode the payload?

I think you’re looking for something like IDL.encode: https://github.com/dfinity/agent-js/blob/main/packages/candid/src/idl.test.ts#L11

2 Likes

Thank you Severin, appreciate it.

1 Like

I just wanted to check the setup to be sure:

So it’s import IDL

image

I’m encoding the payload arguments for a manageNeuronRequest. So I’m assuming I would put the governance canister Id as the InitArgs for the encoding:

image

image

Looks plausible to me, but I’ve never worked with JS before, so I think you just have to try it out

2 Likes