0

I'm using this tool to get the keccak 256 hash: https://emn178.github.io/online-tools/keccak_256.html

However I'm trying to the 4bytes for executeOrder.

The executeOrder function signature is this:

function executeOrder(BaseOrderUtils.ExecuteOrderParams memory params) external 

The ExecuteOrderParams struct looks like the following:

 struct ExecuteOrderParams { ExecuteOrderParamsContracts contracts; bytes32 key; Order.Props order; Market.Props[] swapPathMarkets; uint256[] minOracleBlockNumbers; uint256[] maxOracleBlockNumbers; Market.Props market; address keeper; uint256 startingGas; Order.SecondaryOrderType secondaryOrderType; } 

How do I get the right 4bytes from keccak_256? What do I pass in?

2 Answers 2

1

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.

2
  • How would you pass in a struct? Like Market.Props? ``` struct Props { address marketToken; address indexToken; address longToken; address shortToken; } ``` Commented Mar 26, 2024 at 13:53
  • @caker, I've modified the answer to accommodate the change that you've mentioned, as well as adjusted the JS code to make it easier to obtain the signature hash using the contract's ABI. Commented Mar 26, 2024 at 14:36
0

In Solidity you can do:

import "contract.sol"; function getSelector() public view returns (bytes4) { return contract.executeOrder.selector; } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.