0

I am currently trying to create a simple program that does tax payments using Geth. I created a function in a smart contract that takes in a user's tax identification (don't mind this, I don't believe it is part of the problem), the amount they want to deposit, and the address to send the amount to.

My function is as below:

function payTaxes(string memory _taxID, uint _amount, address _to) public payable {
    if (msg.sender!= gov6) {
        if (taxStructs[msg.sender][_taxID].bracket != 1) {
            if (taxStructs[msg.sender][_taxID].taxessettled = false) {

                address payable to = payable(_to);
                to.transfer(_amount);
                taxStructs[msg.sender][_taxID].taxessettled = true;
                taxStructs[msg.sender][_taxID].taxdue = 0;
            }
        }
    }
}

The code compiles and all, but I don't see that an amount is being transferred to the destination address. Also, I put a flag that says if the payment is made, the variable taxessettled becomes true, but it is still always false.

Below is a screenshot of the transaction:

enter image description here

Can anybody advise what I'm doing wrong?

Thanks.

1 Answer 1

3

I think the problem is you are passing the amount as an argument but forgetting to really send the ether to the contract along with the function call.

The way transfer works is it sends the desired amount From the contract to the recepient.

But for that, the contract need to have that much amount in balance.

Now you want the same person to pay the ether who is calling the function, then you need to put the check inside the function.

require(msg.value == _amount, "Invalid amount of ether sent");

This require statement will check whether the caller has sent the ether or not.

And the person calling the function needs to send the ethers along with the function call.

Let me know if that was clear and solved the problem.

One more thing if you need to know. The way you send ether while calling the function is depicted below

enter image description here

Enter the value here and then call the function.

4
  • Wow, thanks a lot for explaining it so clearly. I understand. I think it works as it takes the balance from the account that is sending the ether. When I called the function, the balance in the account decreased by 30,349,002,400,000 wei. The gas expended said it was 30349 but I think in wei this is equal to 30.349 x 10^9. The value transfer was only 2,400,000 wei. Thing is, I dont think it transfers to the receiving account as the balance there did not change. The "taxessettled" field in the struct did not change either. Does the function end after calling the to.transfer()?
    – user119674
    Commented May 7, 2023 at 10:35
  • By the way, this is how I defined the gov6 address at the beginning: address gov6 = 0x59a3c77a099cB8A7EBE635D4c3AcE63cd475a016; //Taxation Office and then I convert it to payable in the function. Could there be something wrong there?
    – user119674
    Commented May 7, 2023 at 10:41
  • Never mind, I found out it was something wrong with the conditional statement! THANK YOU SO MUCH!
    – user119674
    Commented May 7, 2023 at 11:18
  • With the if statement? did I get your question correct btw? Commented May 7, 2023 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.