By 4bytes
for executeOrder
, you might be referring to the bytes corresponding to its function selector.
You can get the same, by using solidity (i.e., by adding a function within the same contract) as well as by using library like ethers.js
in javascript.
Using Solidity:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Market {
struct Props {
address marketToken;
address indexToken;
address longToken;
address shortToken;
}
}
contract YourContract {
// Define the ExecuteOrderParams struct
struct ExecuteOrderParams {
// Define struct properties here
string stringParam;
uint256 uintParam;
Market.Props market;
Market.Props[] swapPathMarkets;
}
// Function to execute order
function executeOrder(ExecuteOrderParams memory params) external {
// Implementation of the executeOrder function
}
// Function to get function signature bytes for executeOrder
function getExecuteOrderSignature() external pure returns (bytes4) {
return this.executeOrder.selector;
}
}
Using Javascript:
const { ethers } = require("ethers");
// ABI of the contract containing the executeOrder() function
const abi = [ { "inputs": [ { "components": [ { "internalType": "string", "name": "stringParam", "type": "string" }, { "internalType": "uint256", "name": "uintParam", "type": "uint256" }, { "components": [ { "internalType": "address", "name": "marketToken", "type": "address" }, { "internalType": "address", "name": "indexToken", "type": "address" }, { "internalType": "address", "name": "longToken", "type": "address" }, { "internalType": "address", "name": "shortToken", "type": "address" } ], "internalType": "struct Market.Props", "name": "market", "type": "tuple" }, { "components": [ { "internalType": "address", "name": "marketToken", "type": "address" }, { "internalType": "address", "name": "indexToken", "type": "address" }, { "internalType": "address", "name": "longToken", "type": "address" }, { "internalType": "address", "name": "shortToken", "type": "address" } ], "internalType": "struct Market.Props[]", "name": "swapPathMarkets", "type": "tuple[]" } ], "internalType": "struct YourContract.ExecuteOrderParams", "name": "params", "type": "tuple" } ], "name": "executeOrder", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "getExecuteOrderSignature", "outputs": [ { "internalType": "bytes4", "name": "", "type": "bytes4" } ], "stateMutability": "pure", "type": "function" } ]
// Create an instance of the Contract interface
const iface = new ethers.utils.Interface(abi);
// Get the function selector for the executeOrder function
const executeOrderSelector = iface.getSighash("executeOrder");
console.log(executeOrderSelector);
P.S., You can modify the properties of the struct ExecuteOrderParams
as per your use-case. Also, use the contract's ABI accordingly.