Search code examples
blockchainethereumsolidityweb3jsweb3py

Solidity transaction with an unsigned transaction object


I need to interact with another smart contract from my smart contract (several times), I have a transaction object that can be used with web3js or web3py, like this:

{
  from: '0x00000000',
    to: '0x0000000000',
      value: '1000000000000',
      data: '0x000000000000000000000000',
      gasPrice: '245000000000',
      gas: '163502',
      chainId: 3
}

how can I execute this transaction in solidity?


Solution

  • You can use the low-level .call() member of type address. More precisely of its extension address payable, since you're setting value.

    pragma solidity ^0.8;
    
    contract MyContract {
        function foo() external {
            address to = payable(address(0x0));
            bytes memory sendData = new bytes(12); // empty 12 byte array
            to.call{value: 1000000000000, gas: 163502}(sendData);
        }
    }
    

    Note that you cannot affect the gasPrice as its already set in the main transaction (executing the foo() function).

    from is always going to be the caller contract (in this case the MyContract) address.

    Also, EVM-compatible contracts are not able to perform cross-chain requests - that's why you cannot specify the chainId either.

    If you need to be able to affect any of these 3 properties, you'll need to send the transaction from an off-chain app - not from a contract.