Coder Social home page Coder Social logo

dym-ts-sdk's Introduction

dym-ts-sdk

Dymenstion TypeScript SDK working with @cosmjs/stargate. There are some bugss when using the native library of @injectivelabs/sdk-ts together with @cosmjs/stargate, so I will separate the sdk-ts part and make some adjustments to make it compatible and consistent with @cosmjs/stargate.

Contributions and Bug Fixes:

This SDK is a work in progress, and as such, there may be bugs or areas for improvement. Hobbyists are encouraged to contribute to the project by identifying bugs, suggesting enhancements, or submitting fixes. Together, we can improve the SDK and create a more robust and enjoyable development experience for all hobbyists.

Getting Started:

Begin your hobby project by installing the SDK via npm:

npm install @mrtruongleo/dym-ts-sdk

Explore the SDK’s modules and start experimenting with blockchain features in your projects.

Example send dym transaction:

//1.Import
import {
  EthermintStargateClient,
  EthermintSecp256k1Wallet,
} from "@mrtruongleo/dym-ts-sdk";
import { BigNumberInBase } from "@injectivelabs/utils";
import { StdFee, calculateFee, GasPrice } from "@cosmjs/stargate";

//2.Main scripts
(async () => {
  //define chain's details.
  const network = {
    rest: "https://dymension-api.lavenderfive.com:443",
    chainId: "dymension_1100-1",
    rpc: "https://rpc.dymension.nodestake.org",
  };
  //define wallet info, I prefer to use private key on my transaction instead of mnemonic
  let privateKeyHex = "your_private_key";//replace here
  //check if private key started with '0x' (be cause it's evm account)
  privateKeyHex = privateKeyHex.startsWith("0x")
    ? privateKeyHex.substring(2)
    : privateKeyHex;

  //define transaction detail
  const recipient = "target_address";//replace here
  const realAmount = 0.001; //dym in real amount
  const amount = {
    denom: "adym",
    amount: new BigNumberInBase(realAmount).toWei().toFixed(), //you can just use amount string like: "1000000000000000"
  };
  const memo = "test dym transaction";

  //define signer and client : this part is only thing has different with other chain of cosmos ecosystem
  const signer = (await EthermintSecp256k1Wallet.fromKey(
    Buffer.from(privateKeyHex, "hex")
  )) as any;
  const signerAddress = (await signer.getAccounts())[0].address;
  const client =
    await EthermintStargateClient.EthSigningStargateClient.connectWithSigner(
      network.rpc as string,
      signer
    );

  //All the rest of the code is the same when working with @cosmjs/stargate
  //You just need define other type of Msg to execute different type of transaction like deleage, claim...
  const sendMsg = {
    typeUrl: "/cosmos.bank.v1beta1.MsgSend",
    value: {
      fromAddress: signerAddress,
      toAddress: recipient,
      amount: [amount],
    },
  };
  const simulate = await client.simulate(signerAddress, [sendMsg], memo);
  console.log("simulated gas limit: ", simulate);

  const usedFee: StdFee = calculateFee(
    Math.round(simulate * 1.5),
    GasPrice.fromString("5000000000000000adym") //this is avarage gasprice from cosmos chain registry
  );
  const txResponse = await client.signAndBroadcast(
    signerAddress,
    [sendMsg],
    usedFee ?? "auto",
    memo
  );
  // const txResponse = await client.sendTokens(
  //   signerAddress,
  //   recipient,
  //   [amount],
  //   usedFee,
  //   memo
  // );
  if (txResponse.code !== 0) {
    console.log(`Transaction failed: ${txResponse.rawLog}`);
  } else {
    console.log(`Broadcasted transaction hash: ${txResponse.transactionHash}`);
    console.log(`gas used: `, {
      gasUsed: txResponse.gasUsed,
      gasWanted: txResponse.gasWanted,
    });
  }
})();

dym-ts-sdk's People

Contributors

mrtruongleo avatar thejoven avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.