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.1
Version published
Weekly downloads
26
52.94%
Maintainers
0
Weekly downloads
 
Created
Source

P2P Swap SDK

A TypeScript SDK for peer-to-peer asset swaps on EVM-compatible networks. Enables direct and open-ended swaps of ERC20, ERC721, and ERC1155 tokens.

Installation

npm install @b3dotfun/p2pswap

Features

  • Direct Swaps: Specify exact assets to offer and request
  • Open Swaps: Post available assets and receive multiple offers
  • Multi-Asset Support: Combine different token types in a single swap
  • Cross-Chain Compatible: Works with any EVM-compatible network
  • Type Safety: Full TypeScript support

Swap Workflows

Direct Swap Workflow

User A                     Contract                    User B
  |                           |                          |
  |------ List Assets ------->|                          |
  |------ Set Terms --------->|                          |
  |------ Lock Assets ------->|                          |
  |                           |<---- View Listing -------|
  |                           |<---- Check Terms --------|
  |                           |<---- Provide Assets -----|
  |                           |                          |
  |                           |------ Validate --------->|
  |                           |                          |
  |<---- Transfer Assets -----|---- Execute Swap ------->|
  |                           |                          |

Open Swap Workflow

Initiator                  Contract                  Offerers
  |                           |                          |
  |------ List Assets ------->|                          |
  |------ Lock Assets ------->|                          |
  |                           |<---- View Assets --------|
  |                           |<---- Make Offer ---------|
  |                           |<---- Lock Assets --------|
  |                           |                          |
  |<---- Review Offers -------|                          |
  |                           |                          |
  |------ Accept Offer ------>|                          |
  |<---- Transfer Assets -----|---- Execute Trade ------>|
  |                           |                          |

Key Differences:

  • Direct Swap

    • Fixed terms
    • Single responder
    • Immediate execution
    • No negotiation
  • Open Swap

    • Flexible terms
    • Multiple offers
    • Review period
    • Cancellable offers

Usage

Direct Swap

import { createPublicClient, createWalletClient, http } from 'viem';
import { b3 } from 'viem/chains';
import { SwapSDK } from '@b3dotfun/p2pswap';

const sdk = new SwapSDK({
  contractAddress: '0x...',
  publicClient,
  walletClient
});

// User A: Propose a swap
const { swapId } = await sdk.direct.proposeDirectSwap(
  [{ // Offered assets
    tokenAddress: '0x...',
    amount: 1000000000000000000n,
    tokenType: 'ERC20'
  }],
  [{ // Requested assets
    tokenAddress: '0x...',
    tokenId: 123n,
    tokenType: 'ERC721'
  }],
  deadline
);

// User B: Accept the swap
await sdk.direct.acceptDirectSwap(swapId, requestedAssets);

Open Swap

The Open Swap feature allows users to:

  • Post assets without specifying what they want in return
  • Receive multiple offers from different users
  • Choose which offer to accept
import { createPublicClient, createWalletClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { SwapSDK } from '@b3dotfun/p2pswap';

const sdk = new SwapSDK({
  contractAddress: '0x...',
  publicClient,
  walletClient
});

// User A: Create open swap
const { swapId } = await sdk.open.proposeOpenSwap(
  [{ // Available assets
    tokenAddress: '0x...',
    tokenId: 456n,
    tokenType: 'ERC721'
  }],
  deadline
);

// User B: View available swaps and make an offer
const { offerId } = await sdk.open.makeOfferForOpenSwap(
  swapId,
  [{ // Offered assets
    tokenAddress: '0x...',
    amount: 5000000000000000000n,
    tokenType: 'ERC20'
  }]
);

// User C: Make another offer for the same swap
const { offerId: secondOfferId } = await sdk.open.makeOfferForOpenSwap(
  swapId,
  [{ // Different offer
    tokenAddress: '0x...',
    tokenId: 789n,
    tokenType: 'ERC721'
  }]
);

// User A: View all offers for their swap
const offers = await sdk.open.getSwapOffers(swapId);

// User A: Accept their preferred offer
await sdk.open.acceptOffer(swapId, offerId);

// User C: Cancel their offer if not accepted
await sdk.open.cancelOffer(swapId, secondOfferId);

// User A: Cancel the entire swap if needed
await sdk.open.cancelOpenSwap(swapId);

Open Swap Features

  • Multiple Offers: Receive various offers for your assets
  • Offer Management: View, accept, or cancel offers
  • Flexible Assets: Accept any combination of ERC20/ERC721/ERC1155 tokens
  • Deadline Control: Set expiration time for the swap
  • Event Notifications: Track swap and offer status through events

Supported Networks

Compatible with all EVM-based networks including:

  • Ethereum
  • BNB Chain
  • Arbitrum
  • Optimism
  • Polygon
  • Avalanche
  • And other EVM-compatible chains

Documentation

For detailed documentation, visit docs.b3.fun

License

MIT

Keywords

p2p

FAQs

Package last updated on 08 Jan 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