The owner of photo can transfer photo's ownership to another user. By sending transferOwnership transaction, new owner's address will be saved in ownership history, which keep tracks of past owner addresses.
2) Component code
2-1) Rendering TransferOwnership button
We are going to render TransferOwnership button on the FeedPhoto component only when photo's owner address matches with logged-in user's address (which means you are the owner).
3) Interact with contract: transferOwnership method
We already made transferOwnership function in Klaystagram contract at chapter 4. Write Klaystagram Smart Contract. Let's call it from application.
Invoke the contract method: transferOwnership
id: Photo's tokenId
to: Address to transfer photo's ownership
Set transaction options
from: An account that sends this transaction and pays the transaction fee.
gas: The maximum amount of gas that the from account is willing to pay for this transaction.
After sending the transaction, show progress along the transaction lifecycle using Toast component.
If the transaction successfully gets into a block, call updateOwnerAddress function to update new owner's address into the feed page.
// src/redux/actions/photo.jsexportconsttransferOwnership= (tokenId, to) => (dispatch) => {// 1. Invoke the contract method: transferOwnershiptry{KlaystagramContract.methods.transferOwnership(tokenId, to).send({// 2. Set transaction options from:getWallet().address, gas:'20000000', }, (error, txHash) => {if (error) throw error;// 3. After sending the transaction,// show progress along the transaction lifecycle using `Toast` component.ui.showToast({ status:'pending', message:`Sending a transaction... (transferOwnership)`, txHash, }) }).then((receipt) => {ui.showToast({ status:receipt.status ?'success':'fail', message:`Received receipt! It means your transaction is in klaytn block (#${receipt.blockNumber}) (transferOwnership)`, link:receipt.transactionHash, })// 4. If the transaction successfully gets into a block,// call `updateOwnerAddress` function to update new owner's address into the feed page.dispatch(updateOwnerAddress(tokenId, to)) }) } catch (error) {ui.showToast({ status:'error', message:error.toString(), }) }}
4) Update information in redux store: updateOwnerAddress action
After transferring ownership, FeedPhoto needs to be rerendered with new owner's address.
To update new owner's address, let's call feed data from store and find the photo that has the tokenId from the receipt. Then push new owner's address to photo's ownerHistory and setFeed.