npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tinymanorg/tinyman-js-sdk

v3.1.1

Published

Tinyman JS SDK

Downloads

984

Readme

tinyman-js-sdk

JavaScript/TypeScript SDK for the Tinyman AMM Contracts.

Installation

npm i -S @tinymanorg/tinyman-js-sdk
import {tinymanJSSDKConfig} from "@tinymanorg/tinyman-js-sdk";
tinymanJSSDKConfig.setClientName("my-project");

Usage

🆕 AMM v2 Examples: Example scripts for v2 contracts can be found in examples folder.

For more details about the modules and functions, see "Module methods" section.

// Address of the account that will sign the transactions
const accountAddress = "...";
const account = await getAccountInformation(algodClient, accountAddress);
const isAppOptInRequired = isAccountOptedIntoApp({
  appID: getValidatorAppID("mainnet", CONTRACT_VERSION.V1_1),
  accountAppsLocalState: account["apps-local-state"]
});

if (!hasOptedIn) {
  const v1AppOptInTxns = await generateOptIntoValidatorTxns({
    client: algodClient,
    network: "mainnet",
    contractVersion: CONTRACT_VERSION.V1_1,
    initiatorAddr: accountAddress
  });
  // Sign the transactions using a wallet (or any other method)
  const signedTxns = await signTransactions(txGroups, accountAddress);
  // Send signed transactions to the network, and wait for confirmation
  const transactionData = await sendAndWaitRawTransaction(algodClient, [signedTxns]);
  // Log the transaction data to the consol
  console.log({transactionData});
}

Tinyman JS SDK does not provide an implementation for signTransactions as each app may have different integrations with the wallets. The implementation of signTransactions may use the account's secret key to sign or it can use an integration with an external wallet such as PeraConnect and use their signTransaction method. It should always return a Promise that resolves with an array of Unsigned Integer encoding of the signed transactions, ie. Promise<Uint8Array[]>.

Example implementation that uses only account's secret key:

/**
 * @param account account data that will sign the transactions
 * @returns a function that will sign the transactions, can be used as `initiatorSigner`
 */
export default function signerWithSecretKey(account: Account) {
  return function (txGroups: SignerTransaction[][]): Promise<Uint8Array[]> {
    // Filter out transactions that don't need to be signed by the account
    const txnsToBeSigned = txGroups.flatMap((txGroup) =>
      txGroup.filter((item) => item.signers?.includes(account.addr))
    );
    // Sign all transactions that need to be signed by the account
    const signedTxns: Uint8Array[] = txnsToBeSigned.map(({txn}) =>
      txn.signTxn(account.sk)
    );

    // We wrap this with a Promise since SDK's initiatorSigner expects a Promise
    return new Promise((resolve) => {
      resolve(signedTxns);
    });
  };
}

v2 Changes

Module structure

The new version of the sdk supports operations for both v1.1 and v2 contracts. Now, we have modules for different operations:

  • Bootstrap : Pool creation
    • Bootstrap.v1_1
    • Bootstrap.v2
  • AddLiquidity : Adding liquidity to the pools
    • AddLiquidity.v1_1
    • AddLiquidity.v2 This module has 3 sub-modules, according to the add liquidity type:
      • AddLiquidity.v2.initial : For adding liquidity to a pool that has been created, but doesn’t have any liquidity yet
      • AddLiquidity.v2.flexible : For adding liquidity using two assets, with arbitrary amounts, to a pool that already has some liquidity
      • AddLiquidity.v2.withSingleAsset : For adding liquidity using a single assets, to a pool that already has some liquidity
  • RemoveLiquidity : Removing previously added liquidity from the pools
    • RemoveLiquidity.v1_1
    • RemoveLiquidity.v2
      • RemoveLiquidity.v2.generateTxns (Generates txns for the default mode, multiple asset out)
      • RemoveLiquidity.v2.generateSingleAssetOutTxns (Generates txns for the new, single asset out mode)
  • Swap : Swapping assets; trading some portion of one of the owned assets for an another asset
    • Swap.v1_1
    • Swap.v2

Additional to the operation modules, now there is also a module for pool utilities:

  • poolUtils
    • poolUtils.v1_1
    • poolUtils.v2
    • Common utilities (The functions that are common for both versions, can be used like: poolUtils.isPoolEmpty(pool))

Module methods

And all of the operation modules have almost the same structure of functions:

  1. getQuote : Gets a quote for the desired operation, in which one can see the calculated amounts for the operation for example expected output amount, price impact, etc.

    ⚠️ Not available for Bootstrap

  2. generateTxns : Generates the transactions for the desired operation with desired amounts

    ⚠️ Note that additional to generateTxns, RemoveLiquidity.v2 module also has generateSingleAssetOutTxns method.

  3. signTxns : Signs the transactions using the given initiatorSigner

  4. execute : Sends the signed transactions to the blockchain, waits for the response and returns the operation data

And they can be used in the given order, to complete the operation.

Migrating to new sdk version from v1.3.0

Term changes

We now use clearer terms for operations, here are the changes (v1 -> v2):

  • Mint -> Add Liquidity
  • Burn -> Remove Liquidity
  • Liquidity Token -> Pool Token

Migrating the function calls

The new structure is actually pretty similar to the previous one, because the steps are the same, as mentioned in "Module methods" section. For example, for v1, to add liquidity (function arguments are left blank for simplicity):

// 1. get quote
getMintLiquidityQuote();
// 2. generate transactions
generateMintTxns();
// 3. sign generated transactions
signMintTxns();
// 4. execute the operation
mintLiquidity();

To migrate to the new version, you should find the corresponding module, and use the methods in that module:

// 1. get quote
AddLiquidity.v1_1.getQuote();
// 2. generate transactions
AddLiquidity.v1_1.generateTxns();
// 3. sign generated transactions
AddLiquidity.v1_1.signTxns();
// 4. execute the operation
AddLiquidity.v1_1.execute();

License

tinyman-js-sdk is licensed under a MIT license except for the exceptions listed below. See the LICENSE file for details.

Exceptions

asc.json files (src/contract/v1_1/asc.json and src/contract/v2/asc.json) are currently unlicensed. It may be used by this SDK but may not be used in any other way or be distributed separately without the express permission of Tinyman.