사진 소유자는 사진의 소유권을 다른 사용자에게 양도할 수 있습니다. transferOwnership 트랜잭션을 전송하여 새로운 소유자의 주소가 소유권 기록에 저장되고, 이를 통해 과거 소유자들의 주소를 추적합니다.
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).
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.
트랜잭션이 블록에 잘 담기게 되면 updateOwnerAddress 함수를 호출하여 피드 페이지에 새로운 소유자의 주소를 업데이트합니다.
// 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. 트랜잭션 전송 후 // 'Toast' 컴포넌트를 사용하여 트랜잭션 생명 주기로 진행 상황을 나타냅니다.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.