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

@cennznet/crml-cennzx-spot

v1.2.1

Published

the javascript sdk for cennznet runtime module: CennzxSpot. also a plugin of @cennznet/api

Downloads

76

Readme

cennznet-js/crml-cennzx-spot

A sdk providing additional features for cennzx spot runtime module

Install

It is installed along with @cennznet/api

$> npm i --save @cennznet/api

USAGE

@cennznet/api will create a instance of cennzxSpot after it finishes initialization.

// node --experimental-repl-await
// initialize Api and connect to dev network
const {Api} = require('@cennznet/api')
const api = await Api.create({provider: 'wss://rimu.unfrastructure.io/public/ws'});

const cennzxSpot = api.cennzxSpot;

// for Rxjs
const {ApiRx} = require('@cennznet/api')
const apiRx = await ApiRx.create({provider: 'wss://rimu.unfrastructure.io/public/ws'});

const cennzxSpotRx = apiRx.cennzxSpot;

Derives

All derives related to crml-cennzx-spot are defined in this library, which can be access from both CennzxSpot instance and api.derives.cennzxSpot.*

  • exchangeAddress
  • inputPrice / inputPriceAt
  • outputPrice / outputPriceAt
  • liquidityBalance / liquidityBalanceAt
  • totalLiquidity / totalLiquidityAt

check API Docs for more information

Cookbook

Add liquidity

const coreAssetId = 16001;
const tradeAssetA = 16000;
const tradeAssetB = 16002;

const investAmount: number = 1000;
const maxAssetAmount = '1000';
await cennzxSpot
    .addLiquidity(tradeAssetA, 0, maxAssetAmount, investAmount)
    .signAndSend(investor.address, ({events, status}: SubmittableResult) => {
        if (status.isFinalized && events !== undefined) {
            let isCreated = false;
            for (let i = 0; i < status.events.length; i += 1) {
                const event = events[i];
                if (event.event.method === 'AddLiquidity') {
                    // Liquidity added      
                }
            }
        }
    });

Remove liquidity

#liquidity -> amount to remove
await cennzxSpot.removeLiquidity(tradeAssetA, liquidity, 1, 1)
    .signAndSend(investor.address, ({events, status}: SubmittableResult) => {
        if (status.isFinalized && events !== undefined) {
        let isRemoved = false;
        for (let i = 0; i < status.events.length; i += 1) {
            const event = events[i];
            if (event.event.method === 'RemoveLiquidity') {
            }
        }
    }
});

Buy Asset with Another Asset

Given a certain amountBought, assetBought and assetSold

Paying no more than maxPayingAmount of assetSold in order to trade for amountBought of assetBought

//1) query current exchange rate
const expectCost = await cennzxSpot.getOutputPrice(assetSold, assetBought, amountBought);

//2) add a buffer in case price goes up, let's say 2%
const maxPayingAmount = expectCost.muln(1.02);

//3) commit the exchange tx
await cennzxSpot
    .assetSwapOutput(assetSold, assetBought, amountBought, maxPayingAmount)
    .signAndSend(trader.address, ({events, status}: SubmittableResult) => {
        if (status.isFinalized && events !== undefined) {
            let trade = false;
            for (let i = 0; i < status.events.length; i += 1) {
                const event = events[i];
                if (event.event.method === 'TradeAssetPurchase') { // check if ExtrinsicFailed or successful
                }
            }
        }
    });

Buy Asset and transfer to a 3rd party account

await cennzxSpot
    .assetTransferOutput(recipient, assetSold, assetBought, amountBought, maxPayingAmount)
    .signAndSend(trader.address, ({events, status}: SubmittableResult) => {
        if (status.isFinalized && events !== undefined) {
            let trade = false;
            for (let i = 0; i < status.events.length; i += 1) {
                const event = events[i];
                if (event.event.method === 'TradeAssetPurchase') { // check if ExtrinsicFailed or successful
                }
            }
        }
    });

Sell Asset with Another Asset

Given a certain amountSell, assetSold and assetBought

Sell amountSell of assetSold to gain no less than minReceive amount of AssetBought

//1) query current exchange rate
const expectReceive = await cennzxSpot.getInputPrice(assetSold, assetBought, amountSell);

//2) add a buffer in case price goes down, let's say 2%
const minReceive = expectReceive.muln(0.98);

//3) commit the exchange tx
await cennzxSpot
    .assetSwapInput(assetSold, assetBought, amountSell, minReceive)
    .signAndSend(trader.address, ({events, status}: SubmittableResult) => {
        if (status.isFinalized && events !== undefined) {
            let trade = false;
            for (let i = 0; i < status.events.length; i += 1) {
                const event = events[i];
                if (event.event.method === 'TradeAssetPurchase') { // check if ExtrinsicFailed or successful
                }
            }
        }
    });

Sell Asset and transfer the gained Asset to a 3rd party account

await cennzxSpot
    .assetTransferInput(recipient, assetSold, assetBought, amountSell, minReceive)
    .signAndSend(trader.address, ({events, status}: SubmittableResult) => {
            if (status.isFinalized && events !== undefined) {
            let trade = false;
            for (let i = 0; i < status.events.length; i += 1) {
                const event = events[i];
                if (event.event.method === 'TradeAssetPurchase') { // check if ExtrinsicFailed or successful
                }
            }
        }
    });