I am developing a dapp/smart contract, where users will be able to sell/buy NFTs from each other (market). Users grant authority to the smart contract to sell any of their items on their behalf using the setApprovalForAll openzeppelin function. Once this authority granted, users can put their items on sale. I am having difficulty understanding how to actually execute a sale. If another user accesses the front-end dapp with their wallet to purchase an NFT from another user, since they are initiating the token transfer, the transfer fails since they are obviously not authorised to initiate this transfer (but only the owner and the contract are). How is this logic usually implemented in such smart contract so that the purchaser (initiating the transfer) has the ability to buy an item from another owner. Do I need to transfer the token ownership to the smart contract? I thought that the setApprovalForAll function was meant to avoid this.
// Front-end Code
let totAmount = price + commission
this.contInst.methods.tokTransfer(tokOwner, this.account, aId, price, commission).send({
from: this.account,
value: this.web3.utils.toWei(totAmount, 'ether')
}).then((receipt) => {
...
}
// Smart Contract Code
function tokTransfer(address payable from, address to, uint256 tokenId, uint256 price, uint256 commission) payable external {
safeTransferFrom(from, to, tokenId);
...
}
Thank you. J
this.contInst.methods.setApprovalForAll(this.contInst.options.address, true).send({ from: tokOwner })
.