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"
]
}
}
}
}
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!
Note: This is a simple contract. If you want to use , please be careful.