简体   繁体   English

如何使用 wave-transaction JS 库签署和发送转账交易?

[英]How to sign and send transfer transaction using waves-transaction JS library?

Please help me understand https://testnodes.wavesnodes.com/api-docs/index.html I use this api and this library https://github.com/wavesplatform/waves-transactions I cannot send a transaction using the manual to the library or directly by POST request for api.请帮助我理解https://testnodes.wavesnodes.com/api-docs/index.html我使用这个 api 和这个库https://github.com/wavesplatform/waves-transactions我无法使用手册发送交易库或直接通过 POST 请求获取 api。

common mistakes:常见错误:

  • Error: State check failed.错误:状态检查失败。 Reason: Script doesn't exist and proof原因:脚本不存在并证明

  • Error: State check failed.错误:状态检查失败。 Reason: Transactions from non-scripted accounts must have exactly 1 proof原因:来自非脚本账户的交易必须恰好有 1 个证明

A POST request for url / addresses also gives an error.对 url / 地址的 POST 请求也会出错。 Provided API key is not correct.提供的 API 密钥不正确。 Here is my code:这是我的代码:

const { transfer, broadcast } = require("@waves/waves-transactions");
const seed =
  "ride flee tenant tuna share buyer work west amateur review time kick";
const signedTranserTx = transfer(
  {
    amount: 1,
    recipient: "3NBVqYXrapgJP9atQccdBPAgJPwHDKkh6A8"
  },
  seed
);
const nodeUrl = "http://testnodes.wavesnodes.com";

broadcast(signedTranserTx , nodeUrl)
  .then(resp => console.log(resp))
  .catch(err => console.error(err));

If you use Waves transactions api, the request should be signed already and you can post it to /transactions/broadcast.如果您使用 Waves 交易 api,则该请求应该已经签名,您可以将其发布到 /transactions/broadcast。 Then you don't need your own node and you don't need your own API Key.那么你不需要自己的节点,也不需要自己的 API Key。 in your code, I see several mistakes here:在您的代码中,我在这里看到了几个错误:

  1. You're transferring to MAINNET address using testnet node.您正在使用 testnet 节点转移到 MAINNET 地址。 you should use TESTNET address instead.您应该改用 TESTNET 地址。 in the reciepent change the address to an address in testnet and let me know if you still get any errors.在 reciepent 中将地址更改为 testnet 中的地址,如果您仍然遇到任何错误,请告诉我。 you can create new accounts here https://testnet.ide.wavesplatform.com/ in the tab accounts on the top right.您可以在https://testnet.ide.wavesplatform.com/在右上角的选项卡帐户中创建新帐户。
  2. Use https instead of http, const nodeUrl = "https://testnodes.wavesnodes.com/";使用 https 代替 http, const nodeUrl = "https://testnodes.wavesnodes.com/";
  3. Add the chain id('T' for testnet and 'W' for mainnet)添加链 id('T' 代表测试网,'W' 代表主网)

Here is the code:这是代码:

const { transfer, broadcast } = require("@waves/waves-transactions");
const seed =
"ride flee tenant tuna share buyer work west amateur review time kick";
const signedTranserTx = transfer(
  {
    amount: 100,
    recipient: "3N3pJ8xAnbaSBFdAbnaKe4yu4ZXbYkatMcN"
  },
  seed
);
const nodeUrl = "https://testnodes.wavesnodes.com";
broadcast({ ...signedTranserTx, chainId: "T" }, nodeUrl)
  .then(resp => console.log(resp))
  .catch(err => console.error(err));

[Update] [更新]

The above code working good.上面的代码运行良好。 Just a quick update since I see the new testnet URI is below link:只是一个快速更新,因为我看到新的 testnet URI 在链接下面:

https://nodes-testnet.wavesnodes.com https://nodes-testnet.wavesnodes.com

I mean I have replace from https://testnodes.wavesnodes.com to https://nodes-testnet.wavesnodes.com then it's working maybe because we created the account from the different place.我的意思是我已经从https://testnodes.wavesnodes.com替换为https://nodes-testnet.wavesnodes.com然后它可以工作,也许是因为我们从不同的地方创建了帐户。

So this is the final code:所以这是最终的代码:

const { transfer, broadcast } = require("@waves/waves-transactions");
const seed =
"ride flee tenant tuna share buyer work west amateur review time kick";
const signedTranserTx = transfer(
  {
    amount: 100,
    recipient: "3N3pJ8xAnbaSBFdAbnaKe4yu4ZXbYkatMcN"
  },
  seed
);
const nodeUrl = "https://nodes-testnet.wavesnodes.com";
broadcast({ ...signedTranserTx, chainId: "T" }, nodeUrl)
  .then(resp => console.log(resp))
  .catch(err => console.error(err));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM