Socket
Book a DemoInstallSign in
Socket

@b3dotfun/p2pswap

Package Overview
Dependencies
Maintainers
0
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@b3dotfun/p2pswap

SDK for P2P Asset Swap Protocol

npmnpm
Version
0.1.7
Version published
Weekly downloads
33
560%
Maintainers
0
Weekly downloads
 
Created
Source

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

  • 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
);
  • Create Trade (Relayer only)
// Relayer creates trade after assets are locked
const tradeHash = await relayer.createTrade(
    sessionId,
    maker,
    taker,
    makerAssets,
    takerAssets,
    targetChainId
);
  • Execute Trade (Relayer)
// Relayer executes the trade
await relayer.executeTradeAcrossChains(sessionId);
  • 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.

Keywords

p2p

FAQs

Package last updated on 05 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts