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

tradelayer

v1.2.2

Published

NPM package for interacting with TradeLayer orderbook and building Litecoin/Token/Futures transactions.

Downloads

426

Readme

TradeLayer NPM Setup Guide

This NPM package helps you connect to the TradeLayer API, set up a Litecoin node, and perform peer-to-peer trading on decentralized orderbooks. Follow the steps below to initialize the environment and get everything running smoothly.

Prerequisites

  • Node.js: Ensure that you have Node.js installed. You can download it from here.
  • Git: Git must be installed on your system. You can download Git from here.
  • wget: Ensure you have wget installed to download the Litecoin binaries.

Installation and Setup

Step 1: Install the NPM package

In the directory of your choice, run the following command to install this package:


npm i tradelayer

Step 2: Setup

To run the TradeLayer setup script, use the following command from the root of your project where the NPM package is installed, chose the win or lin version for Windos and Linux formatting differences:


cd node_modules/tradelayer/

./setup-win.sh

./setup-winmain.sh

./setup-lin.sh

./setup-linmain.sh

Choose the 'main' versions if you're setting up for mainnet.

If you get an err "Permission denied" running the bash, try:

chmod +x setup-lin.sh

Run the included setup script to automatically:

  • Fetch and install Litecoin binaries.
  • defines litecoin.conf file with default user/pass credentials and 18332 testnet port
  • Start litecoind.
  • Clone the TradeLayer.js repository and check out the correct branch (txIndexRefactor).
  • waits for RPC to become available on litecoind
  • Generates an address.
  • Start the TradeLayer API.

The .sh output will end with something like this:

Wallet address created: tltc1q23fu03xr7m8muxrf3x8pvrvfhrlanct0lte9cu


Building TradeLayer API...

...<NPM install output>...

Setup complete!

Copy the address and fund it with a testnet faucet (https://testnet.help/en/ltcfaucet/testnet) or mainnet LTC. We're adding our own algo testnet faucet to support this, coming soon.

The API logic will grab your UTXOs from a loaded wallet using listunspent to build transactions.

Be sure to backup wallet.dat files, a clearlist automated-signing app is coming to make this more secure using NEAR Chain Signatures and will be integrated into this NPM.

Here's the rest of the example script to illustrate, copy this to a .js file and then drag it over from your local device to the ftp interface on your server, and drop it into the folder where you run npm i tradelayer. Run it after running the .sh and funding the new address:

const ApiWrapper = require('tradelayer/algoAPI.js');
const litecore = require('bitcoin-lib-ltc');
const litecoinClient = require('tradelayer/litecoinClient.js');
const api = new ApiWrapper('http://172.81.181.19', 9191);
const OrderbookSession = require('tradelayer/orderbook.js');
const io = require('socket.io-client');
const axios = require('axios')
const socket = new io('ws://172.81.181.19');
const myInfo = {address:'',otherAddrs:[]};

const client = litecoinClient(); // Use the litecoinClient for RPC commands

// Start listening for order matches and handle swaps
const orderbookSession = new OrderbookSession(socket, myInfo, client);
const savedOrderUUIDs = []; // Array to store UUIDs of orders

async function initializeApiAndStartSync() {
    await api.initUntilSuccess(); // Call the init function and wait for it to complete
    console.log('API Initialized successfully.');
    await getUTXOBalances('')
}

// Example usage: call the function to initialize the API and start periodic sync checks
initializeApiAndStartSync();
// Example of fetching UTXO balances for a test address
async function getUTXOBalances(address) {
    try {
        const utxos = await api.listUnspent(); // Fetch unspent transactions
        const totalBalance = utxos.reduce((sum, utxo) => {
            if (utxo.address === address) {
                return sum + utxo.amount; // Sum balances for the specific address
            }else if(address==''&&myInfo.address==''){
                myInfo.address=utxo.address
                console.log('Captured address'+myInfo.address)
                return sum+ utxo.amount
            }else if(address==''&&myInfo.address!=''){
                myInfo.otherAddrs.push(utxo.address)
                return sum+ utxo.amount
            }
            return sum;
        }, 0);
        console.log(`Total UTXO balance for address ${address}:`, totalBalance);
    } catch (error) {
        console.error('Error fetching UTXO balances:', error);
    }
}

// Example of calling token balances
async function getTokenBalances(address) {
    try {
        const response = await api.getAllTokenBalancesForAddress(address); // Assuming this method exists
        console.log(`Token balances for address ${address}:`, response);
    } catch (error) {
        console.error('Error fetching token balances:', error);
    }
}

// Define the async function
async function performTradeOperations(testAddress) {
    try {
          console.log("awaiting init and address load")
        await api.delay(10000);
        if(myInfo.address){
            testAddress=myInfo.address
        }else{
            console.log('awaiting UTXOs to mark the address')
            api.delay(10000);
            testAddress=myInfo.address
        }


        // Call getTokenBalances with your test address
        await getTokenBalances(testAddress);

        // Example of fetching spot markets
        try {
            const spotMarkets = await api.getSpotMarkets();
            console.log('Spot Markets:', spotMarkets);
        } catch (error) {
            console.error('Error fetching spot markets:', error);
        }

        // Example of fetching futures markets
        try {
            const futuresMarkets = await api.getFuturesMarkets();
            console.log('Futures Markets:', futuresMarkets);
        } catch (error) {
            console.error('Error fetching futures markets:', error);
        }

        // Example of sending an order
        const orderDetails = {
            type: 'SPOT',
            action: 'BUY',
            props: { id_for_sale: 0, id_desired: 1, price: 0.01, amount: 0.05 },
            keypair: { address: 'some-address', pubkey: 'some-pubkey' },
            isLimitOrder: true
        };

        try {
            const orderUUID = await api.sendOrder(orderDetails);
            console.log('Order sent, UUID:', orderUUID);
            savedOrderUUIDs.push(orderUUID); // Save UUID to the array

            // After saving, attempt to cancel the first order in the array
            if (savedOrderUUIDs.length > 0) {
                const orderToCancel = savedOrderUUIDs[0];
                console.log(`Attempting to cancel order with UUID: ${orderToCancel}`);

                try {
                    const cancelResponse = await api.cancelOrder(orderToCancel);
                    console.log(`Order with UUID: ${orderToCancel} canceled successfully!`);
                } catch (error) {
                    console.error(`Error canceling order with UUID: ${orderToCancel}`, error);
                }
            }
        } catch (error) {
            console.error('Error sending order:', error);
        }

        // Example of getting orderbook data
        const filter = { type: 'SPOT', first_token: 0, second_token: 1 };
        try {
            const orderbookData = await api.getOrderbookData(filter);
            console.log('Orderbook Data:', orderbookData);
        } catch (error) {
            console.error('Error fetching orderbook data:', error);
        }

    } catch (error) {
        console.error('An error occurred during trade operations:', error);
    }
}

// Example usage of the function
performTradeOperations(myInfo.address);