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 🙏

© 2025 – Pkg Stats / Ryan Hefner

tran-p2pswap

v0.2.0

Published

SDK for P2P Asset Swap Protocol

Readme

Cross-Chain P2P Swap SDK

A TypeScript SDK for executing peer-to-peer asset swaps across different blockchains.

Features

  • Single-chain and cross-chain asset swaps
  • Support for ERC20, ERC721, ERC1155, and native tokens
  • Multi-relayer verification system
  • Automatic rollback on failed trades
  • Event monitoring and status tracking

Installation

npm install tran-p2pswap

Quick Start

import { createPublicClient, createWalletClient, http } from 'viem';
import { CrossChainSwapSDK } from 'tran-p2pswap';
import { mainnet, arbitrum } from 'viem/chains';
import { CONTRACT_ADDRESSES } from 'tran-p2pswap';

// Initialize SDK
const sdk = new CrossChainSwapSDK({
    sourceChain: {
        contractAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
        chainId: mainnet.id,
        publicClient: createPublicClient({
            chain: mainnet,
            transport: http()
        })
    },
    targetChain: {
        contractAddress: CONTRACT_ADDRESSES.B3_TESTNET.address,
        chainId: arbitrum.id,
        publicClient: createPublicClient({
            chain: arbitrum,
            transport: http()
        })
    },
    walletClient // Your viem wallet client
});

// Create cross-chain trade
const { sessionId } = await sdk.createTrade(
    takerAddress,
    [{ // Maker assets on source chain
        contractAddress: '0xTOKEN1',
        amount: 1000000000000000000n,
        assetType: 0, // ERC20
        chainId: mainnet.id
    }],
    [{ // Taker assets on target chain
        contractAddress: '0xTOKEN2',
        amount: 5000000000000000000n,
        assetType: 0, // ERC20
        chainId: arbitrum.id
    }],
    arbitrum.id
);

// Watch trade status
sdk.watchTrade(sessionId, (status) => {
    console.log('Trade status:', status);
});

Relayer Setup

import { CrossChainRelayer, CONTRACT_ADDRESSES } from 'tran-p2pswap';

const relayer = new CrossChainRelayer({
    sourceChain: {
        contractAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
        chainId: mainnet.id,
        publicClient: sourceClient
    },
    targetChain: {
        contractAddress: CONTRACT_ADDRESSES.B3_TESTNET.address,
        chainId: arbitrum.id,
        publicClient: targetClient
    },
    encryptedKey: 'your-encrypted-key',
    secretKey: 'your-secret-key',
    sourceAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
    targetAddress: CONTRACT_ADDRESSES.B3_TESTNET.address
});

// Execute cross-chain trade
await relayer.executeTradeAcrossChains(sessionId);

Trade Flow

  1. Lock Assets (Both maker and taker)
// Maker locks assets on source chain
const makerLockHash = await sdk.lockAssets(
    sessionId,
    makerAssets,
    sourceChainId
);

// Taker locks assets on target chain
const takerLockHash = await sdk.lockAssets(
    sessionId,
    takerAssets,
    targetChainId
);
  1. Create Trade (Relayer only)
// Relayer creates trade after assets are locked
const tradeHash = await relayer.createTrade(
    sessionId,
    maker,
    taker,
    makerAssets,
    takerAssets,
    targetChainId
);
  1. Execute Trade (Relayer)
// Relayer executes the trade
await relayer.executeTradeAcrossChains(sessionId);
  1. Handle Failures
// Users can unlock their assets if trade fails
await sdk.unlockAssets(sessionId, userAddress, chainId);

Example Usage

import { CrossChainSwapSDK } from 'tran-p2pswap';
import { CrossChainRelayer } from 'tran-p2pswap';

// Initialize SDK for users
const sdk = new CrossChainSwapSDK({
    sourceChain: {
        contractAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
        chainId: mainnet.id,
        publicClient: sourceClient
    },
    targetChain: {
        contractAddress: CONTRACT_ADDRESSES.B3_TESTNET.address,
        chainId: arbitrum.id,
        publicClient: targetClient
    },
    walletClient
});

// Initialize Relayer
const relayer = new CrossChainRelayer({
    sourceChain: config.sourceChain,
    targetChain: config.targetChain,
    encryptedKey: process.env.RELAYER_KEY,
    secretKey: process.env.SECRET_KEY,
    sourceAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
    targetAddress: CONTRACT_ADDRESSES.B3_TESTNET.address
});

// Custom sessionId (from frontend/backend)
const sessionId = 123n;

try {
    // Step 1: Users lock their assets
    await sdk.lockAssets(sessionId, makerAssets, sourceChainId);
    await sdk.lockAssets(sessionId, takerAssets, targetChainId);

    // Step 2: Relayer creates trade
    await relayer.createTrade(
        sessionId,
        maker,
        taker,
        makerAssets,
        takerAssets,
        targetChainId
    );

    // Step 3: Relayer executes trade
    await relayer.executeTradeAcrossChains(sessionId);

    // Monitor status
    sdk.watchTrade(sessionId, (status) => {
        console.log('Trade status:', status);
    });
} catch (error) {
    // Users can unlock their assets if something fails
    await sdk.unlockAssets(sessionId, userAddress, chainId);
    throw error;
}

Error Handling

try {
    await sdk.lockAssets(sessionId, assets, chainId);
} catch (error) {
    if (error instanceof ValidationError) {
        console.error('Asset validation failed:', error);
    } else if (error instanceof ChainError) {
        await sdk.unlockAssets(sessionId, userAddress, chainId);
    }
}

API Documentation

[Link to detailed API documentation]

License

Copyright © 2025 NPC Labs, Inc. All rights reserved.