1

help me, I want to create a smart contract with the function send internal tx then self destruct like this example

this hash address polygon txhash enter image description here

M Akhtar
  • 11
  • 2

1 Answers1

0

Basically, as you can notice in the transaction Logs, the MATIC is sent to recipient (0xe6...) by the msg.sender as msg.sender -> contract -> recipient: Here is the completed code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SendAndSelfDestruct { address payable public recipient;

event EtherSentAndSelfDestructed(address indexed sender, address indexed recipient, uint256 amount);

constructor(address payable _recipient) {
    recipient = _recipient;
}

receive() external payable {}

function sendEtherAndSelfDestruct() public {
    require(msg.sender != recipient, "Sender cannot be the recipient");
    uint256 amountToSend = address(this).balance; // Send the entire balance to the recipient

    // Send Ether to the recipient
    require(recipient.send(amountToSend), "Failed to send Ether");

    // Self-destruct the contract
    selfdestruct(payable(msg.sender));

    // Emit an event
    emit EtherSentAndSelfDestructed(msg.sender, recipient, amountToSend);
}

}

Then, in your external code to call function sendEtherAndSelfDestruct, you should put the value (Ether) in the transaction. Some thing similar to this:

    txn = contract.functions.sendEtherAndSelfDestruct().buildTransaction({
        'from': owner,
        'value': 100000000, # fix the value you want
        'gasPrice': web3.toWei('15', 'gwei'),
        'nonce': web3.eth.get_transaction_count(owner),
    })

Hope it helps!!

tqdpham96
  • 608
  • 12