Nft

如何從乙太坊上部署的智能合約呼叫函式?

  • March 13, 2022

我是一名初級區塊鏈開發人員,我在乙太坊區塊鏈上部署了一個智能合約來收集 NFT。如何使用 remix 或 ethers js 呼叫顯示函式來顯示 NFTS?

function reveal() public onlyOwner {
       revealed = true;
   }

在帶有 ethers.js 的 JavaScript 中,您可以這樣做:

import yourDeployedContract from '../build/contracts/yourDeployedContract.json'
import {ethers} from 'ethers'
import Web3Modal from "web3modal"

async callReveal() {
   // Get ethereum provider with Web3Modal
   const web3Modal = new Web3Modal();
   const connection = await web3Modal.connect();
   const provider = new ethers.providers.Web3Provider(connection);
   const signer = provider.getSigner();

   // Get contract data on the network you deployed your contract on
   const network = await provider.getNetwork()
   const contractData = yourDeployedContract.networks[network.chainId]

   // Get smart contract
   let contract = new ethers.Contract(contractData.address, yourDeployedContract.abi, signer);

   // Call reveal method
   let revealTransaction = await contract.reveal();

   // Wait for transaction to be complete 
   await voteTransaction.wait();

   // Rest of the code here
}

yourDeployedContract.json是呼叫“truffle migrate”時生成的 JSON 文件。

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