JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 67
  • Score
    100M100P100Q57862F
  • License Copyright © 2025 NPC Labs, Inc. All rights reserved.

SDK for P2P Asset Swap Protocol

Package Exports

  • @b3dotfun/p2pswap
  • @b3dotfun/p2pswap/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@b3dotfun/p2pswap) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.