-1

Whenever someone buys a smart-contract the amount (in Wei) get stored in SC(Smart Contract) address(this) += msg.value.

Now I am trying to create a function to redeem the balance of the SC

function redeemInvTokens(uint256 _amount) public returns(bool){
    require(_amount <= amount, "you cannot redeem the more tokens");
    balanceOf[msg.sender] -= _amount;
    msg.sender.call.value(address(this).balance - (address(this).balance - _amount))("Transferred ether to financier");
    return true;
}

but its not transferring any amount to the msg.sender.

How to transfer the specific amount to the msg.sender? so I don't have to use address(this).balance - (address(this).balanace - _amount).

7
  • 1
    "so I don't have to use address(this).balance - (address(this).balanace - _amount)"??? This expression is equivalent to _amount, what brought you to the conclusion that you need to use it in order to transfer the specific amount to the sender? You can simply do msg.sender.transfer(_amount). Jan 21, 2020 at 8:28
  • Also, what is amount in require(_amount <= amount, ...)? You should post a working example. This symbol (amount) is not defined anywhere in your code. Jan 21, 2020 at 8:30
  • msg.sender.call.value(_amount)() it is not transfering any amount to msg.sender so i used msg.sender.call.value(address(this).balance)(). but, again i want to send specific amount to msg.sender thats why that calculation to determine.
    – iamsujit
    Jan 21, 2020 at 8:48
  • x - (x - y) == y!!! Jan 21, 2020 at 8:50
  • And on top of that of that, why not simply do msg.sender.transfer(_amount)? Jan 21, 2020 at 8:51

2 Answers 2

0

You can use call with gas estimation.

for example,

require(address(receiverAdr).call.value(_amount).gas(35000)());

In your case:

uint256 transferableAmount = address(this).balance -(address(this).balance - _amount);
 require(msg.sender.call.value(transferableAmount).gas(35000)());;
2
  • but isn't this method should be avoided after Istanbul update ethereum.stackexchange.com/a/19343/50405
    – iamsujit
    Jan 21, 2020 at 6:25
  • Yes. but you don't mention the exact error msg or transaction hash of transferred amount so. Don't worry I have edited the answer. pls check and let me know. Jan 21, 2020 at 6:32
0

Here is the sample example

    // SPDX-License-Identifier: GPL-3.0
        pragma solidity >=0.7.0 <0.9.0;
        
        /**
         * @title SimpleVault
         * @dev Store & retrieve Funds from the contract
         * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
         */
        contract SimpleVault {
            address payable owner;
        
            constructor () payable {
                owner = payable(msg.sender);
            }
        
            function withdraw() public {
                   payable (msg.sender).transfer(getBalance());     // Transfer to msg.sender
       OR
                // payable (0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2).transfer(getBalance());  // Transfer to a specific address
       OR
               (bool success, ) = address(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2).call{value:getBalance()}("");
            require(success, "Transfer failed."); 
    
    }
        
            function getBalance() public view returns (uint) {
               return address(this).balance;
            }
        }    

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.