Solidity

ImportError:無法從“brownie”導入名稱“LinkToken”

  • October 10, 2022

我正在使用 freecodecamp.org 的教程,但出現以下錯誤:ImportError: cannot import name 'LinkToken' from 'brownie 它也無法導入我在 help_script 中使用的 VRFCoordinatorMock:

from web3 import Web3
from brownie import accounts, network, config, Contract, LinkToken, VRFCoordinatorMock
#...
contract_to_mock = {"link_token": LinkToken, "vrf_coordinator": VRFCoordinatorMock}
#...
def deploy_mocks():
   """
   Use this script if you want to deploy mocks to a testnet
   """
   print(f"The active network is {network.show_active()}")
   print("Deploying mocks...")
   account = get_account()
   print("Deploying Mock LinkToken...")
   link_token = LinkToken.deploy({"from": account})
   print(f"Link Token deployed to {link_token.address}")
   print("Deploying Mock VRF Coordinator...")
   vrf_coordinator = VRFCoordinatorMock.deploy(link_token.address, {"from": account})
   print(f"VRFCoordinator deployed to {vrf_coordinator.address}")
   print("All done!")
# ...

來自 freecodecamp.org 的 David 已經創建了這個 browie-config.yaml 文件,其中不包括有關合約的報告:

dependencies:
 - OpenZeppelin/openzeppelin-contracts@3.4.0
 - smartcontractkit/chainlink-brownie-contracts@1.1.1
reports:
 exclude_contracts:
   - LinkToken
   - VRFCoordinatorMock
   - ERC721
   - EnumerableMap
   - Address
   - EnumerableSet
compiler:
 solc:
   remappings:
     - "@openzeppelin=OpenZeppelin/openzeppelin-contracts@3.4.0"
     - "@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1"
dotenv: .env
wallets:
 from_key: ${PRIVATE_KEY}
networks:
 development:
   keyhash: "0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311"
   fee: 100000000000000000
 goerli:
   vrf_coordinator: "0x2bce784e69d2Ff36c71edcB9F88358dB0DfB55b4"
   link_token: "0x326C977E6efc84E512bB9C30f76E30c160eD06FB"
   keyhash: "0x0476f9a745b61ea5c0ab224d3a6e4c99f0b02fce4da01143a4f70aa80ae76e8a"
   fee: 100000000000000000 # 0.1

當我執行以下deploy_and_create文件時發生錯誤:

from scripts.helpful_scripts import get_account, OPENSEA_URL, get_contract
from brownie import AdvancedCollectible, network, config

sample_token_uri = "https://ipfs.io/ipfs/Qmd9MCGtdVz2miNumBHDbvj8bigSgTwnr4SbyH6DNnpWdt?filename=0-PUG.json"


def deploy_and_create():
   account = get_account()
   # We want to be able to use the deployed contracts if we are on a testnet
   # Otherwise, we want to deploy some mocks and use those
   # Goerli
   advanced_collectible = AdvancedCollectible.deploy(
       get_contract("vrf_coordinator"),
       get_contract("link_token"),
       config["networks"][network.show_active()]["keyhash"],
       config["networks"][network.show_active()]["fee"],
       {"from": account},
   )

def main():
   deploy_and_create()

您可以在https://www.youtube.com/watch?v=M576WGiDBdQ&list=PLFLVQ12OWlUJ0EvLOdcJs-bNY5rqW_rXq&index=5&t=38144s 和 github 儲存庫中找到 YouTube 影片:https ://github.com/PatrickAlphaC/nft-demo

希望有人可以幫助我,在此先感謝!

據我所知,基於您在此處發布的程式碼的邏輯。看來您的問題是因為您沒有deploy_mockshelpful_script.

代替:

def main():
   deploy_and_create()

您將需要以下內容:

def main():
   deploy_mocks()
   deploy_and_create()

簡而言之,您的deploy_and_create文件應該像這樣執行:

from scripts.helpful_scripts import get_account, OPENSEA_URL, get_contract, deploy_mocks
   from brownie import AdvancedCollectible, network, config
   
   sample_token_uri = "https://ipfs.io/ipfs/Qmd9MCGtdVz2miNumBHDbvj8bigSgTwnr4SbyH6DNnpWdt?filename=0-PUG.json"
   
   
   def deploy_and_create():
       account = get_account()
       # We want to be able to use the deployed contracts if we are on a testnet
       # Otherwise, we want to deploy some mocks and use those
       # Goerli
       advanced_collectible = AdvancedCollectible.deploy(
           get_contract("vrf_coordinator"),
           get_contract("link_token"),
           config["networks"][network.show_active()]["keyhash"],
           config["networks"][network.show_active()]["fee"],
           {"from": account},
       )
   
   def main():
       deploy_mocks()
       deploy_and_create()

試試這個,我們會看看它是怎麼回事。

在腳本的頂部,您有

from brownie import ... LinkToken

這意味著您正在從您的契約文件夾中導入 LinkToken。

您需要將 LinkToken 合約添加到您的合約文件夾中。

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