TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor

huangapple go评论78阅读模式
英文:

TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor

问题

我试图运行这段代码来使用ethers.js库在JS中部署智能合约:

const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();

async function main() {
  const provider = new ethers.ethers.JsonRpcProvider(process.env.RPC_URL);
  // 从文件读取JSON私钥
  const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf-8");

  // ========= 开始失败的部分 ============
  let wallet = new ethers.Wallet.fromEncryptedJsonSync(
    encryptedJson,
    process.env.PRIVATE_KEY_PASSWORD
  );
  // ========= 结束失败的部分 ============

  wallet = await wallet.connect(provider);
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf-8"
  );

  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("部署中,请稍候...");
  const contract = await contractFactory.deploy({ gasPrice: 1000000000000 });
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

但我一直在控制台中收到以下错误:

TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor
at main (/home/vituz01/hh/ethers-simple-storage/deploy.js:36:16)
at Object.<anonymous> (/home/vituz01/hh/ethers-simple-storage/deploy.js:90:1)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
at Module.load (node:internal/modules/cjs/loader:1096:32)
at Module._load (node:internal/modules/cjs/loader:935:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:84:12)
at node:internal/main/run_main_module:23:47

有人能解释为什么会失败吗?我认为我正确地调用了fromEncryptedJsonSync()函数(如EthersJS v5文档中所解释的,参见以下链接:ethers.Wallet.fromEncryptedJsonSync);由于他们仍在构建ethers.js v6文档,我找不到它们。

英文:

I'm trying to run this piece of code to deploy a smart contract in JS with ethers.js library:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const ethers = require(&quot;ethers&quot;);
const fs = require(&quot;fs-extra&quot;);
require(&quot;dotenv&quot;).config();

async function main() {
  const provider = new ethers.ethers.JsonRpcProvider(process.env.RPC_URL);
  // reads the json private key from file
  const encryptedJson = fs.readFileSync(&quot;./.encryptedKey.json&quot;, &quot;utf-8&quot;);

// ========= START FAILING PART ============
let wallet = new ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
// ========= END FAILING PART ============

  wallet = await wallet.connect(provider);
  const abi = fs.readFileSync(&quot;./SimpleStorage_sol_SimpleStorage.abi&quot;, &quot;utf-8&quot;);
  const binary = fs.readFileSync(
    &quot;./SimpleStorage_sol_SimpleStorage.bin&quot;,
    &quot;utf-8&quot;
  );
  
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log(&quot;Deploying, please wait...&quot;);
  const contract = await contractFactory.deploy({ gasPrice: 1000000000000 });
}

main()
  .then(() =&gt; process.exit(0))
  .catch((error) =&gt; {
    console.error(error);
    process.exit(1);
  });

<!-- end snippet -->

but I keep getting this error in the console:

TypeError: ethers.Wallet.fromEncryptedJsonSync is not a constructor
at main (/home/vituz01/hh/ethers-simple-storage/deploy.js:36:16)
at Object.&lt;anonymous&gt; (/home/vituz01/hh/ethers-simple-storage/deploy.js:90:1)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
at Module.load (node:internal/modules/cjs/loader:1096:32)
at Module._load (node:internal/modules/cjs/loader:935:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:84:12)
at node:internal/main/run_main_module:23:47

Can someone explain why this is failing? I think I'm calling the function fromEncryptedJsonSync() correctly (as explained in the EthersJS v5 docs, see the following link: ethers.Wallet.fromEncryptedJsonSync); I couldn't find the ethers.js v6 docs since they are still building it.

答案1

得分: 4

移除 "new" 因为它创建了一个构造函数。

let wallet = ethers.Wallet.fromEncryptedJsonSync(
  encryptedJson,
  process.env.PRIVATE_KEY_PASSWORD
);
英文:

Remove new as it creates a constructor

let wallet = ethers.Wallet.fromEncryptedJsonSync(
  encryptedJson,
  process.env.PRIVATE_KEY_PASSWORD
);

huangapple
  • 本文由 发表于 2023年6月5日 20:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定