Solidity

如何在 Solidity 中將一個契約引用到另一個契約中

  • December 7, 2021

我有 3 個要部署的契約。2 個依賴於第三個契約,但由於某種原因,我無法從另一個契約呼叫函式。下面是我的程式碼。

ERC20 代幣

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0;

// Define your Smart Contract with the "Contract" keyword and an empty constructor
contract ERC20 {

   string public name = "Token";
   string public symbol = "Symbol";
   uint256 public totalSupply;

   // mapping of Balances
   mapping(address => uint256) public balanceOf;

   constructor(uint256 _initialSupply) {
       balanceOf[msg.sender] = _initialSupply;
       totalSupply = _initialSupply;
   }

}

NFT 市場

contract NFTMarket is ReentrancyGuard {
 using Counters for Counters.Counter;
 Counters.Counter private _itemIds;
 Counters.Counter private _itemsSold;

 address payable owner;
 uint256 listingPrice = 0.025 ether;

 constructor() {
   owner = payable(msg.sender);
 }
}

NFT 合約

   // Import the ERC20 Token contract
   import "./ERC20.sol";
   
   
   contract NFT is ERC721URIStorage {
       using Counters for Counters.Counter;
       Counters.Counter private _tokenIds;
       address contractAddress;
   
       // Declaring the ERC20 token contract variable
       ERC20 public tokenContract;
   
       constructor(address marketplaceAddress, ERC20 _tokenContract) ERC721("My NFT", "MNFT") {
           contractAddress = marketplaceAddress;
   
           // Assign Token Contract Reference to the NFT Contract
           tokenContract = _tokenContract;

 }
}

部署者

const ERC20 = artifacts.require("ERC20");
const NFT = artifacts.require("NFT");
const NFTMarket = artifacts.require("NFTMarket");

module.exports = function (deployer, network, accounts) {

   deployer.then(async () => {

       // Pass initial supply as argument of the deployer.
       await deployer.deploy(ERC20, 1000000); // 1000000 NZT tokens

       await deployer.deploy(NFTMarket);

       await deployer.deploy(NFT, NFTMarket.address, ERC20.address);

   })
};

測試和控制台

const balance = await tokenNFT.tokenContract().methods.balanceOf(await tokenNFT.address).call()
const balance2 = await tokenNFT.tokenContract.methods.balanceOf(await tokenNFT.address)
const balance3 = await tokenNFT.tokenContract.balanceOf(await tokenNFT.address)

這些都不返回任何東西。我的 NFT 合約無法呼叫 ERC20 代幣功能。我在這裡做錯了什麼。請幫我解決這個問題,因為我已經被困了好幾個小時了。

友好地,克里斯蒂安

在這種情況下,這些情況不起作用,因為您的NFT契約沒有繼承自,ERC20因此您可以使用這些函式的唯一地方是您聲明ERC20契約實例的地方。例如:

這是你的NFT契約:

// Import the ERC20 Token contract
   import "./ERC20.sol";
   
   
   contract NFT is ERC721URIStorage {
       using Counters for Counters.Counter;
       Counters.Counter private _tokenIds;
       address contractAddress;
   
       // Declaring the ERC20 token contract variable
       ERC20 public tokenContract;
   
       constructor(address marketplaceAddress, ERC20 _tokenContract) ERC721("My NFT", "MNFT") {
           contractAddress = marketplaceAddress;
   
           // Assign Token Contract Reference to the NFT Contract
           tokenContract = _tokenContract;
           }
   function RandomFunction() public returns(uint256){
   //EXAMPLE OF USING ERC20 FUNCTION
       return tokenContract.balanceOf(msg.sender);
   }

}

正如您在 a 中看到的,RandomFunction您可以使用您在部署時使用建構子創建的令牌實例呼叫任何 ERC20 函式。但是你不能從外部呼叫這些函式。

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