0

I am trying to develop a project in which balance transfer using smart contract is involved. I want to transfer balance to an address from the initiator address. I am using Remix IDE to write my contracts and I am using Ropsten testnet.

The problem that I am encountering is whether I use Solidity's transfer(), send() or call() functions, the balance transfer that is initiated is from msg.sender to the contract's address, instead of initiating to recipient's address. Doesn't matter what I put into the parameters, the result is the same.

Here is the code snippet:

function buy(uint256 tokenId) public payable {
        address payable recipient = payable(ownerOf(tokenId)); // Get the address and cast it to payable
        require(msg.value >= cost, "Insufficient bid.");
        require(msg.sender != address(recipient), "User already owns the NFT");
        require(recipient.send(msg.value)); // Problematic part: Transfer value
        _safeTransfer(recipient, msg.sender, tokenId, ""); // Unrelated: Transferring a NFT
}

When I execute the above code, the following screen appears on Metamask:

enter image description here

Now the problem is that the address 0x6A9...4691 is the contract's address and not the recipient's address. If I let it go forward, the balance isn't shown in the recipient's balance.

Please let me know if you spot the issue. Or do I have to use a front-end only to initiate transfer the funds, by referring the specific wallet's documentation? Which isn't ideal by the way in my case.

2 Answers 2

0

So make a parameter for the function called "address _userAddress".

That should work :)

Something like :

function buy(uint256 tokenId, address _userAddress) public payable {
        address payable recipient = _userAddress; // Get the address and cast it to payable
        require(msg.value >= cost, "Insufficient bid.");
        require(msg.sender != address(recipient), "User already owns the NFT");
        require(recipient.send(msg.value)); // Problematic part: Transfer value
        _safeTransfer(_userAddress, msg.sender, tokenId, ""); // Unrelated: Transferring a NFT
}

also there may be some allowanec issues, so make sure you gave the smart contract approval to spend funds in the token contract

1
  • Thanks for the suggestion. I found the the problem though, the transfer wasn't working because I was using deployment on another network (Ganache) on a different network (Ropsten). When I redeployed on the correct network, things worked. Also balance transfer to the contract address is the intended behaviour Dec 10, 2021 at 6:26
0

Actually I found out the solution myself. Transferring of the balance to the contract's address is the intended behavior. First the contract receives the balance and then sends the balance to the recipient's address.

The problem that I created was that I deployed the contract on Ganache and tried to call the function on Ropsten and vice versa, which was why the balance wasn't showing on recipient's address. When I deployed the contract to the current network, the balance transfer functions worked!

Silly mistake but make sure you avoid it. This can happen if you're testing on multiple networks.

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.