Vit*_*nna -3 javascript node.js blockchain smartcontracts ethers.js
我正在尝试运行这段代码,以使用 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);
// reads the json private key from file
const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf-8");
// ========= 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("./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("Deploying, please wait...");
const contract = await contractFactory.deploy({ gasPrice: 1000000000000 });
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});Run Code Online (Sandbox Code Playgroud)
但我不断在控制台中收到此错误:
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
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会失败吗?我认为我正确调用了函数 fromEncryptedJsonSync() (如 EthersJS v5 文档中所述,请参阅以下链接:ethers.Wallet.fromEncryptedJsonSync);我找不到 ethers.js v6 文档,因为他们仍在构建它。
小智 7
删除 new 创建构造函数
let wallet = ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
process.env.PRIVATE_KEY_PASSWORD
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |