在币安智能链中部署合约时面临错误

Posted

技术标签:

【中文标题】在币安智能链中部署合约时面临错误【英文标题】:Facing error on deploying contract in Binance Smart Chain 【发布时间】:2021-04-20 14:22:24 【问题描述】:

我正在尝试在币安智能链网络上部署测试合约。我正在使用元掩码和松露来创建和测试合同。我的文件夹结构是这样的

const HDWalletProvider = require('truffle-hdwallet-provider-privkey');
    const privateKeys = process.env.PRIVATE_KEYS || ""
    
    module.exports = 
      networks: 
        development: 
          host: "127.0.0.1",
          port: 7545,
          network_id: "*"
        ,
        binancetestnet: 
          provider: function() 
            return new HDWalletProvider(
              privateKeys.split(','), // Array of account private keys
              `https://data-seed-prebsc-1-s1.binance.org:8545/$process.env.INFURA_API_KEY`// Url to an Ethereum Node
            )
          ,
          gas: 5000000,
          gasPrice: 25000000000,
          network_id: 97
        
      ,
    
      // Set default mocha options here, use special reporters etc.
      mocha: 
        // timeout: 100000
      ,
    
      // Configure your compilers
      compilers: 
        solc: 
           version: "0.7.3",    // Fetch exact version from solc-bin (default: truffle's version)
          // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
          // settings:           // See the solidity docs for advice about optimization and evmVersion
          //  optimizer: 
          //    enabled: false,
          //    runs: 200
          //  ,
          //  evmVersion: "byzantium"
          // 
        
      
    ;

Truffle Deployment Error

Macbook-Air Oracle % truffle migrate --reset --network binancetestnet
Error: Cannot find module 'truffle-hdwallet-provider-privkey'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/macbbok/Desktop/Oracle/truffle-config.js:1:26)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at Object.require (internal/modules/cjs/helpers.js:25:18)
    at Function.load (/usr/local/lib/node_modules/truffle/build/webpack:/packages/config/dist/index.js:161:1)
    at Function.detect (/usr/local/lib/node_modules/truffle/build/webpack:/packages/config/dist/index.js:150:1)
    at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:199:1)
    at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:136:1)
    at Object.module.exports (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/cli.js:56:1)
    at __webpack_require__ (/usr/local/lib/node_modules/truffle/build/webpack:/webpack/bootstrap:19:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/webpack/bootstrap:83:1
    at Object.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:89:10)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Truffle v5.1.54 (core: 5.1.54)
Node v10.16.3

我已经附上了我面临的所有错误代码。有人可以帮我吗? 我还安装了 HD Wallet 提供程序。 Truffle 编译成功。

【问题讨论】:

您缺少名为 truffle-hdwallet-provider-privkey 的源代码模块,请确认您的文件位于正确的目录中 我已使用npm install @truffle/hdwallet-provider 安装它已成功安装。但是我在部署的时候遇到了一个新问题 pasteboard.co/JJNSH3F.png pasteboard.co/JJNTctb.png 【参考方案1】:

安装 Truffle HD 钱包提供程序后 现在面对

MacBook-Air Test % truffle migrate --reset --network binancetestnet

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

Error: Could not create addresses from your mnemonic or private key(s). Please check that your inputs are correct.
    at new HDWalletProvider (/Users/macbook/Desktop/Test/node_modules/@truffle/hdwallet-provider/src/index.ts:124:13)
    at Object.provider (/Users/macbook/Desktop/Test/truffle-config.js:12:16)
    at Object.getProvider (/usr/local/lib/node_modules/truffle/build/webpack:/packages/provider/index.js:20:1)
    at Object.create (/usr/local/lib/node_modules/truffle/build/webpack:/packages/provider/index.js:13:1)
    at TruffleConfig.get [as provider] (/usr/local/lib/node_modules/truffle/build/webpack:/packages/config/dist/configDefaults.js:200:1)
    at Object.detect (/usr/local/lib/node_modules/truffle/build/webpack:/packages/environment/environment.js:19:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate.js:206:1
Truffle v5.1.54 (core: 5.1.54)
Node v14.15.4

我的 Truffle.js 代码

const HDWalletProvider = require('@truffle/hdwallet-provider');
const privateKeys = process.env.PRIVATE_KEYS || ""
module.exports = 
  networks: 
    development: 
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    ,
    binancetestnet: 
      provider: function() 
        return new HDWalletProvider(
          privateKeys.split(','), // Array of account private keys
          `https://data-seed-prebsc-1-s1.binance.org:8545/$process.env.INFURA_API_KEY`// Url to an Ethereum Node
        )
      ,
      gas: 5000000,
      gasPrice: 25000000000,
      network_id: 97
    
  ,

  // Set default mocha options here, use special reporters etc.
  mocha: 
    // timeout: 100000
  ,

  // Configure your compilers
  compilers: 
    solc: 
       version: "0.7.3",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings:           // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: 
      //    enabled: false,
      //    runs: 200
      //  ,
      //  evmVersion: "byzantium"
      // 
    
  
;

我的 .env 文件

ETHERSCAN_API_KEY=https://data-seed-prebsc-1-s1.binance.org:8545/
INFURA_API_KEY=https://data-seed-prebsc-1-s1.binance.org:8545/
PRIVATE_KEYS="9xxxxx3fb486a57ad13e6db71xxxxx456e4128d51ecxxxxxxxd"
DEV_ADDRESS="0xxxxxxe80e3a43EBCab9A6CeC9d9e2a49100AxxxC"

【讨论】:

以上是关于在币安智能链中部署合约时面临错误的主要内容,如果未能解决你的问题,请参考以下文章

0基础教学_在BSC主链上部署智能合约并验证合约_BSC发币_币安发币

如何在币安智能链BSC上发行代币

我正在尝试编写恶意 ERC20 智能合约批准功能(用于学习目的),但这不起作用

如何将 BSCScan 中的合约部署到币安智能链上?

有没有办法检查交易是不是发生在币安智能链网络或以太坊智能链上?

0基础教学:在BSC主链上部署智能合约并验证合约|BSC发币|币安发币