Solidity

通過合約和代幣 ID 獲取 NFT 所有者

  • May 17, 2021

我正在嘗試通過呼叫solidity和獲取 NFT的所有者web3``the Contract``the token id

Web3.js-v1.3.5到目前為止,我已經嘗試過了,但結果從未被呼叫過!

我該如何處理?我怎麼能把它寫進去solidity

       var abi_ = [ {
       "constant": true,
       "inputs": [
           {
               "name": "_tokenId",
               "type": "uint256"
           }
       ],
       "name": "ownerOf",
       "outputs": [
           {
               "name": "",
               "type": "address"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   }]

   const smartContractAddress = "0x495f947276749ce646f68ac8c248420045cb7b5e"
   const contract = new web3.eth.Contract(abi_, smartContractAddress)
   let owner = await contract.methods.ownerOf('91338127118827779192862936396890434745733899848224782300810335984505495486465').call()
   //Code never reaches here
   console.log(owner)

試試這個 abi:

{"constant":true,"inputs":
 [{"internalType":"uint256","name":"tokenId","type":"uint256"}],
"name":"ownerOf",
"outputs":[{"internalType":"address","name":"","type":"address"}],
"payable":false,"stateMutability":"view","type":"function"}

您忘記將合約的所有者地址放在“發件人”位置:

var myContract = new web3.eth.Contract(abi, '0x495f947276749ce646f68ac8c248420045cb7b5e', {
   from: '0x1234567890123456789012345678901234567891', //<--- here 
   gasPrice: '20000000000' 
});

引用自:https://ethereum.stackexchange.com/questions/97895