我目前正在尝试按照https://docs.hedera.com/hedera/sdks-and-apis/sdks/set-up-your-local-network中的步骤设置本地网络。
我有代码:
const {
Client,
PrivateKey,
Hbar,
AccountId,
AccountCreateTransaction,
} = require("@hashgraph/sdk");
require('dotenv').config();
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = process.env.MY_PRIVATE_KEY;
const myPublicKey = process.env.MY_PUBLIC_KEY;
async function main() {
console.log("account id is: " + process.env.MY_ACCOUNT_ID);
const node = {"127.0.0.1:50211": new AccountId(3)};
const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600");
//set the transsaction fee paying account
client.setOperator(myAccountId, myPrivateKey);
console.log('success');
console.log(PrivateKey.fromString(myPublicKey));
const newAccount = await new AccountCreateTransaction()
.setKey(PrivateKey.fromString(myPublicKey))
.setInitialBalance(new Hbar(1))
.execute(client);
//Get receipt
const receipt = await newAccount.getReceipt(client);
//Get the account ID
const newAccountId = receipt.accountId;
console.log(newAccountId);
}
void main();
我始终得到的输出表示 AccountCreateTransaction 步骤失败:
/Users/{name}/Desktop/{org_name}/hello-hedera-js-sdk/node_modules/@hashgraph/sdk/lib/Executable.cjs:679
throw new Error(`max attempts of ${maxAttempts.toString()} was reached for request with last error being: ${persistentError != null ? persistentError.toString() : ""}`);
^
Error: max attempts of 10 was reached for request with last error being: GrpcServiceError: gRPC service failed with status: TIMEOUT
at AccountCreateTransaction.execute (/Users/{name}/Desktop/{org_name}/hello-hedera-js-sdk/node_modules/@hashgraph/sdk/lib/Executable.cjs:679:11)
at async main (/Users/drew/Desktop/Greensmith/hello-hedera-js-sdk/localtx.js:31:24)
我尝试直接复制并粘贴在常春藤网站上找到的代码,甚至在我的终端中返回相同的错误。我的 docker 桌面上运行了 hedera-local-node 集群,启动它没有任何问题。控制台在错误出现之前返回“成功”,表明
client.setOperator
功能正常工作。
我尝试使用给定的 private_key 和 account_id 直接从网站复制并粘贴代码。返回相同的错误。我也尝试过
rm -rf node_modules
,然后用 npm 重新安装,但没有成功。我预计不会返回任何内容并且交易会完成,这样我就可以通过检查 http://localhost:5551/api/v1/transactions
- 本地镜像端点 URL 来确认。
编辑 最终解决了,原来我需要在 Docker 上扩展我的 RAM
我遇到了同样的问题,为我解决的问题是我的 .env 变量没有字符串
所以在我拥有像这样的 .env 变量之前
.env 文件代码
MY_ACCOUNT_ID="0.0.5939242"
MY_PRIVATE_KEY="465654654654300706052b.."
我遇到了类型错误,所以查看其他人的代码,我发现他们使用了 来自 AccountId 和 PrivateKey 对象的 .fromString(..) 方法,所以我将其添加到我的代码中,如下所示:
index.js代码
const myAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
然后我得到了 抛出新错误(
max attempts of ${maxAttempts.toString()} was reached for request with last error being: ${persistentError != null ? persistentError.toString() : ""}
);
然后我从 .env 文件中删除了“...”,代码就工作了!
这是我的代码仓库
使用 object.functions() 时还要确保您的代码位于同一行
Hedera 开发者门户的格式如下,但这是错误的!
const newAccount = await new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance(Hbar.fromTinybars(1000))
.execute(client);
不要使用换行符
const newAccount = await new AccountCreateTransaction().setKey(newAccountPublicKey).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);