/example/stargate-final.js

https://github.com/cosmostation/cosmosjs · JavaScript · 57 lines · 38 code · 12 blank · 7 comment · 0 complexity · 6d9c822aeab23dbe0b5e3e1f98acc5eb MD5 · raw file

  1. import { Cosmos } from "../src/index.js";
  2. import message from "../src/messages/proto";
  3. // [WARNING] This mnemonic is just for the demo purpose. DO NOT USE THIS MNEMONIC for your own wallet.
  4. const mnemonic = "swear buyer security impulse public stereo peasant correct cross tornado bid discover anchor float venture deal patch property cool wreck eight dwarf december surface";
  5. const chainId = "stargate-final";
  6. // This rest server URL may be disabled at any time. In order to maintain stable blockchain service, it is recommended to prepare your rest server.
  7. // (https://hub.cosmos.network/master/resources/service-providers.html#setting-up-the-rest-server)
  8. const lcdUrl = "https://lcd-office.cosmostation.io/stargate-final";
  9. const cosmos = new Cosmos(lcdUrl, chainId);
  10. cosmos.setBech32MainPrefix("cosmos");
  11. cosmos.setPath("m/44'/118'/0'/0/0");
  12. const address = cosmos.getAddress(mnemonic);
  13. const privKey = cosmos.getECPairPriv(mnemonic);
  14. const pubKeyAny = cosmos.getPubKeyAny(privKey);
  15. cosmos.getAccounts(address).then(data => {
  16. // signDoc = (1)txBody + (2)authInfo
  17. // ---------------------------------- (1)txBody ----------------------------------
  18. const msgSend = new message.cosmos.bank.v1beta1.MsgSend({
  19. from_address: address,
  20. to_address: "cosmos1jf874x5vr6wkza6ahvamck4sy4w76aq4w9c4s5",
  21. amount: [{ denom: "umuon", amount: String(100000) }] // 6 decimal places (1000000 uatom = 1 ATOM)
  22. });
  23. const msgSendAny = new message.google.protobuf.Any({
  24. type_url: "/cosmos.bank.v1beta1.MsgSend",
  25. value: message.cosmos.bank.v1beta1.MsgSend.encode(msgSend).finish()
  26. });
  27. console.log("msgSendAny: ", msgSendAny);
  28. const txBody = new message.cosmos.tx.v1beta1.TxBody({ messages: [msgSendAny], memo: "" });
  29. console.log("txBody: ", txBody);
  30. return;
  31. // --------------------------------- (2)authInfo ---------------------------------
  32. const signerInfo = new message.cosmos.tx.v1beta1.SignerInfo({
  33. public_key: pubKeyAny,
  34. mode_info: { single: { mode: message.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT } },
  35. sequence: data.account.sequence
  36. });
  37. const feeValue = new message.cosmos.tx.v1beta1.Fee({
  38. amount: [{ denom: "umuon", amount: String(5000) }],
  39. gas_limit: 200000
  40. });
  41. const authInfo = new message.cosmos.tx.v1beta1.AuthInfo({ signer_infos: [signerInfo], fee: feeValue });
  42. // -------------------------------- sign --------------------------------
  43. const signedTxBytes = cosmos.sign(txBody, authInfo, data.account.account_number, privKey);
  44. cosmos.broadcast(signedTxBytes).then(response => console.log(response));
  45. });