smart contract의 sale 과 purchase 관련 메소드를 다음과 같이 작성하였습니다.
// 판매 등록
function setForSale(uint256 _tokenId, uint256 _price) public {
address tokenOwner = nftAddress.ownerOf(_tokenId);
require(tokenOwner == msg.sender, "caller is not token owner");
//토큰 소유자만 판매가능
require(_price > 0 , "price is zero or lower");
//판매시 가격은 0보다 커야함
require(nftAddress.isApprovedForAll(tokenOwner, address(this)), "token owner did not approve TokenSales contract");
//토큰 컨트랙이 대신 승인하도록 승인했는지 확인
tokenPrice[_tokenId] = _price;
}
// 구매
function purchaseToken(uint256 _tokenId, address _tokenAddress, uint256 _price) public payable {
ERC20 tokenForPay = ERC20(_tokenAddress);
uint256 price = tokenPrice[_tokenId];
address tokenSeller = nftAddress.ownerOf(_tokenId);
require(tokenForPay.balanceOf(msg.sender) >= price, "caller does not have enough tokens");
require(_price >= price, "caller sent token lower than price");
require(msg.sender != tokenSeller, "caller is token seller");
address payable payableTokenSeller = address(uint160(tokenSeller));
tokenForPay.transferFrom(msg.sender, payableTokenSeller, price);
nftAddress.safeTransferFrom(tokenSeller, msg.sender, _tokenId);
tokenPrice[_tokenId] = 0;
}
이를 실행하기 위하여
프론트쪽에서
const { rawTransaction: senderRawTransaction } = await caver.klay.signTransaction({
type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
from: sender,
to: DEPLOYED_ADDRESS_TOKENSALES,
data : tsContract.methods.purchaseToken(tokenId, TokenAddress, price).encodeABI(),
gas: '500000',
}, signedMessage)
caver.klay.sendTransaction({
senderRawTransaction: senderRawTransaction,
feePayer: feePayer.address,
})
이런 방식으로 요청하려고 합니다.
하지만 error를 반환합니다.
Uncaught (in promise) Error: evm: execution reverted
{
"blockHash": "0xd9a7a45121a73c0e365b4e712d0c43069d731cb5084b85e27e4deabb8c46ad07",
"blockNumber": 85675225,
"contractAddress": null,
"feePayer": "0xda22d2127a3a9b6f218fd975f2ee4312726626e7",
"feePayerSignatures": [
{
"V": "0x7f5",
"R": "0x5ff2da40d894f04f1cfb87f5416ce76dfc8295823d03c3165cc15346be55eaf4",
"S": "0x19c532dea9dc491b50b86704a6b57a7af11e68d968ccae9770042ca708d3aa4d"
}
],
"from": "0x6915b0828a99072d885aeaeb549d45978bdba02a",
"gas": "0x7a120",
"gasPrice": "0xae9f7bcc00",
"gasUsed": 88758,
"input": "0x9b60cc97000000000000000000000000000000000000000000000000000000000000000100000000000000000000000014078e02e3ea9cb650826431a08856c607a72ab50000000000000000000000000000000003c2f7086aed236c807a1b5000000000",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x21",
"senderTxHash": "0x37bb49f6a9b8a815282c6cbb45fde440af52436b418c341e21d4279f01aac37e",
"signatures": [
{
"V": "0x7f5",
"R": "0xffbcfd22cfc949034f18b27f47f7c8d643d3880c3cba99d11c00784f8efb7704",
"S": "0x445d7496acc3ea37fef22ea8fb760b3dfa1b3b346bbfa895eb01573e13d3a72"
}
],
"status": false,
"to": "0xdedbb16b00d62a9ea71cb1e62ed461c3371e9810",
"transactionHash": "0xe32d3d0a0e37f15a8c3027e632c90118f37b009b7dc4c427c8f7eb032a02bcf2",
"transactionIndex": 1,
"txError": "0x9",
"type": "TxTypeFeeDelegatedSmartContractExecution",
"typeInt": 49,
"value": "0x0"
}
at checkForNormalTx (index.js:765)
at eval (index.js:642)
checkForNormalTx @ index.js:765
eval @ index.js:642
Promise.then (async)
buyToken @ index.js:516
async function (async)
buyToken @ index.js:496
onclick @ (index):1
프론트쪽에서 smart Contract를 실행시키려면 어떤 caver js 메소드 중 klay.sendTransaction()을 사용하고 있는데 다른걸 사용해야 될까요? 아니면 내부적 데이터 값들을 변경해야 할까요?
잘못된 부분 지적해주시면 감사하겠습니다!!