K
K
konstantinst132022-03-24 22:49:25
blockchain
konstantinst13, 2022-03-24 22:49:25

How in Solidity to make it so that when money arrives in the Metamask wallet, they are automatically redirected to the required smart contract?

I wrote a smart contract on Solidity, which, when receiving money to its address, redirects 50% to the first wallet, 25% to the second wallet.

I wrote this in Remix IDE, but I need this smart contract on my Metamask wallet.
How can I make it so that when money arrives at the Metamask wallet (for example, if NFT was bought from me), they are automatically redirected to this smart contract? (And then this smart contract will distribute them to two necessary wallets).

PS Please, if it's not difficult, review the code, check it for safety and correctness. If something is not written correctly, please tell me how to rewrite it correctly. I am new to Solidity. Thank you for your attention.

Smart contract:

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

contract Demo {
        constructor() payable{}
        receive() external payable{}

        

        address addr1 = 0x583031D1113aD414F02576BD6afaBfb302140225;
        address addr2 = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;

        function withdrawFunds() external {
            //address target = payable(_to);
            address payable wallet1 = payable(addr1);
            address payable wallet2 = payable(addr2);

            uint _50 = address(this).balance/100*50; //50% of balance of contract
            uint _25 = address(this).balance/100*25; //25% of balance of contract

            wallet1.transfer(_50);
            wallet2.transfer(_25);
        }

        function receiveFunds() external payable {
            this.withdrawFunds();
        }

        function getBalance() public view returns(uint){
            return address(this).balance;
        }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Julia Bedrosova, 2022-04-18
@Bedrosova

In the formulation of the question, as you asked it - no way. Automatic execution of the program code is possible only when funds are received at the address of the smart contract, and not at the address of your account. Your account is not a smart contract.
I recommend you read Chris Dannen's book "Fundamentals of Cryptocurrency and Blockchain Programming for Beginners".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question