safeTransferFrom does not transfer etherium to the target ERC-721

0 votes

I create a contract with ERC-721 and I try to transfer the token with money. I am using web3.js and openzeppelin. So, The transaction is confirmed, but no money is sent to the target wallet.

Here is the contract buy function;

function buy(uint256 _tokenId) public payable { address buyer = msg.sender; require(_isApprovedOrOwner(msg.sender, _tokenId), "ERC721: transfer caller is not owner nor approved"); approve(buyer, _tokenId); safeTransferFrom(ownerOf(_tokenId), buyer, _tokenId); }

And here is web3.js :

const buy = (2,2) => { //Token ID: 2 //Price: 2 ether contract.methods.buy(2).send({ from: account, value: web3.utils.toWei(2, 'ether') }) }


Here is transaction Response of Buy Function

{ "transactionHash": "0xb4267ac3da9a240dc007c118f710a0c9c1fab7d9eb1fab2097acacaec1ac56ee", "transactionIndex": 0, "blockHash": "0xf6b8b8456cc3ac6103f03e747e9ca92ada3b68cc86fd408e6e9e03c07d5bcc55", "blockNumber": 208, "from": "0xefd8c34cf80828bd9d7b12256421b0d10d75fa86", "to": "0x112c6dcedb141d2da934696bf8d6cd460eef111e", "gasUsed": 83787, "cumulativeGasUsed": 83787, "contractAddress": null, "status": true, "logsBloom": "0x04000000000000000000000000000000000000000000000000000000000000000000001000000000000010000000000000000000000000000000000000200000000000000000000000000008000000000000000000000000000000000000080000000000020000000000000000000800000000000000000000000010000000000100000000000000000000000000001000000000000000000000000000000000020000000000000000000110000002000000000000000000000000000000000000000002000000000080000000000000000000000000000000000000000020000010000000000000000000000000000000000000008000000000000000200000", "events": { "Approval": { "logIndex": 0, "transactionIndex": 0, "transactionHash": "0xb4267ac3da9a240dc007c118f710a0c9c1fab7d9eb1fab2097acacaec1ac56ee", "blockHash": "0xf6b8b8456cc3ac6103f03e747e9ca92ada3b68cc86fd408e6e9e03c07d5bcc55", "blockNumber": 208, "address": "0x112c6dCEDb141D2da934696BF8d6CD460EEf111e", "type": "mined", "id": "log_3514a43f", "returnValues": { "0": "0xf298d44e534E465727f8E6A02F0E95Fe0d360fbC", "1": "0x0000000000000000000000000000000000000000", "2": "2", "owner": "0xf298d44e534E465727f8E6A02F0E95Fe0d360fbC", "approved": "0x0000000000000000000000000000000000000000", "tokenId": "2" }, "event": "Approval", "signature": "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "raw": { "data": "0x", "topics": [ "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925", "0x000000000000000000000000f298d44e534e465727f8e6a02f0e95fe0d360fbc", "0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000002" ] } }, "Transfer": { "logIndex": 1, "transactionIndex": 0, "transactionHash": "0xb4267ac3da9a240dc007c118f710a0c9c1fab7d9eb1fab2097acacaec1ac56ee", "blockHash": "0xf6b8b8456cc3ac6103f03e747e9ca92ada3b68cc86fd408e6e9e03c07d5bcc55", "blockNumber": 208, "address": "0x112c6dCEDb141D2da934696BF8d6CD460EEf111e", "type": "mined", "id": "log_95b82cfe", "returnValues": { "0": "0xf298d44e534E465727f8E6A02F0E95Fe0d360fbC", "1": "0xEFD8C34Cf80828BD9D7B12256421b0d10d75fA86", "2": "2", "from": "0xf298d44e534E465727f8E6A02F0E95Fe0d360fbC", "to": "0xEFD8C34Cf80828BD9D7B12256421b0d10d75fA86", "tokenId": "2" }, "event": "Transfer", "signature": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "raw": { "data": "0x", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000f298d44e534e465727f8e6a02f0e95fe0d360fbc", "0x000000000000000000000000efd8c34cf80828bd9d7b12256421b0d10d75fa86", "0x0000000000000000000000000000000000000000000000000000000000000002" ] } } } }


Please help me. Thank you.

Apr 5, 2022 in Blockchain by Aditya
• 7,660 points
1,369 views

1 answer to this question.

0 votes

I changed my contract buy function like this;

function buy(uint256 _tokenId, address payable _owner, uint256 _amount) public payable { address payable buyer = msg.sender; address payable owner = _owner; //<-- *payable (important) _transfer(ownerOf(_tokenId), buyer, _tokenId); owner.transfer(_amount); //<--- this function send the amount to the token owner. }

The problem was solved!

answered Apr 7, 2022 by Rahul
• 9,670 points

Related Questions In Blockchain

+1 vote
1 answer

How does a miner get to know that a transaction is verified by all the nodes?

Contrary to the popular belief, it is ...READ MORE

answered Mar 27, 2018 in Blockchain by Johnathon
• 9,090 points
2,009 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,211 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
37,864 views
0 votes
1 answer
0 votes
1 answer

safeTransferFrom does not transfer etherium to the target. ERC-721

I changed my contract buy function like ...READ MORE

answered Apr 11, 2022 in Blockchain by Rahul
• 9,670 points
390 views
0 votes
1 answer

Where does Hyperledger fabric store the public key and private key of the user?

It signs the transaction (eg. initiated by ...READ MORE

answered Mar 24, 2022 in Blockchain by Rahul
• 9,670 points
143 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP