Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encode call data #1

Merged
merged 13 commits into from
Mar 22, 2024
52 changes: 52 additions & 0 deletions helpers/encodeCallData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Web3 = require('web3');
dragos-rebegea marked this conversation as resolved.
Show resolved Hide resolved

/**
* Encodes call data for the deposit function with simplified argument handling.
* Automatically attempts to detect and encode integers and strings.
*
* @param {string} endpointName - The name of the endpoint for the cross-chain call.
* @param {number} gasLimit - The gas limit for the cross-chain call.
* @param {any[]} args - The arguments for the cross-chain call, assumed to be strings or numbers.
* @returns {string} The encoded callData string.
*/
function encodeCallData(endpointName, gasLimit, args) {
const web3 = new Web3();

// Convert endpoint name to bytes and prepend its length
const endpointNameBytes = web3.utils.asciiToHex(endpointName);
const endpointNameLength = web3.utils.leftPad(web3.utils.numberToHex(endpointNameBytes.length / 2), 8); // 4 bytes length

// Convert gasLimit to 8 bytes hex string
const gasLimitHex = web3.utils.leftPad(web3.utils.numberToHex(gasLimit), 16);

// Encode arguments, automatically handling as strings or numbers
let encodedArgs = '';
args.forEach(arg => {
let argHex;
if (typeof arg === 'number' || (!isNaN(arg) && !isNaN(parseFloat(arg)))) {
// Treat as number
argHex = web3.utils.numberToHex(arg);
} else {
// Default to treating as string
argHex = web3.utils.asciiToHex(arg);
}
const argLength = web3.utils.leftPad(web3.utils.numberToHex(argHex.length / 2), 8); // 4 bytes length
encodedArgs += argLength.substr(2) + argHex.substr(2); // Remove '0x' prefix
});
dragos-rebegea marked this conversation as resolved.
Show resolved Hide resolved

// Number of arguments in 4 bytes hex string
const numArgsHex = web3.utils.leftPad(web3.utils.numberToHex(args.length), 8);

// Combine everything
const callData = '01' + // Custom prefix
endpointNameLength.substr(2) + // Remove '0x' prefix
endpointNameBytes.substr(2) +
gasLimitHex.substr(2) +
numArgsHex.substr(2) +
encodedArgs;

return '0x' + callData;
}


module.exports = encodeCallData;
5 changes: 5 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const encodeCallData = require('./encodeCallData');

module.exports = {
encodeCallData,
};
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { encodeCallData } = require('./helpers');

module.exports = {
encodeCallData,
};
Loading