
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@b3dotfun/p2pswap
Advanced tools
A TypeScript SDK for executing secure peer-to-peer asset swaps across different blockchains with relayer support.
The Cross-Chain P2P Swap SDK enables secure and trustless asset swaps between different blockchain networks. It supports:
yarn add tran-p2pswap
The main SDK for users to interact with the swap protocol:
lockAssets
: Lock assets on specified chaincreateTrade
: Create new trade with locked assetsunlockAssets
: Unlock assets if trade failswatchTrade
: Monitor trade execution statusgetTrade
: Get trade detailsgetTradeStatus
: Check current trade statusisTradeValid
: Validate trade conditionsgenerateLockProof
: Generate proof of locked assetsAutomated relayer service for executing cross-chain trades:
createTrade
: Create trades on both chainsexecuteTradeAcrossChains
: Execute cross-chain tradestrackExecutionStatus
: Monitor execution statushandleFailedExecution
: Handle and recover from failuresgenerateCancelProof
: Generate proof for trade cancellation// Contract addresses for supported networks
export const CONTRACT_ADDRESSES = {
MAINNET: {
address: '0x...',
chainId: 1
},
BASE_SEPOLIA: {
address: '0x37adC0e3fd070365CE7A0C385A5d0DB69F405Fa0',
chainId: 84532
},
B3_TESTNET: {
address: '0xcD5758669D2732B9a00d168329d55cE7Ff4B745B',
chainId: 1993
}
// ... other networks
}
import { CrossChainSwapSDK, CONTRACT_ADDRESSES } from 'tran-p2pswap';
import { createPublicClient, http } from 'viem';
const sdk = new CrossChainSwapSDK({
sourceChain: {
contractAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
chainId: 84532,
publicClient: createPublicClient({
chain: baseSepolia,
transport: http()
})
},
targetChain: {
contractAddress: CONTRACT_ADDRESSES.B3_TESTNET.address,
chainId: 1993,
publicClient: createPublicClient({
chain: b3Testnet,
transport: http()
})
},
walletClient
});
// Define assets
const makerAssets = [{
contractAddress: '0xTOKEN1',
amount: 1000000000000000000n,
assetType: 0, // ERC20
chainId: 84532n,
tokenId: 0n
}];
const takerAssets = [{
contractAddress: '0xTOKEN2',
amount: 5000000000000000000n,
assetType: 0, // ERC20
chainId: 1993n,
tokenId: 0n
}];
// Lock assets
await sdk.lockAssets(sessionId, makerAssets, 84532);
// Create trade
const hash = await sdk.createTrade(
sessionId,
makerAddress,
takerAddress,
makerAssets,
takerAssets,
1993n
);
// Monitor status
sdk.watchTrade(sessionId, (status) => {
console.log('Trade status:', status);
});
import { CrossChainRelayer } from 'tran-p2pswap';
// Initialize relayer with encrypted private key
const relayer = new CrossChainRelayer({
sourceClient,
targetClient,
sourceWalletClient,
targetWalletClient,
sourceAddress: CONTRACT_ADDRESSES.BASE_SEPOLIA.address,
targetAddress: CONTRACT_ADDRESSES.B3_TESTNET.address,
sourceChain: { id: 84532 },
targetChain: { id: 1993 }
});
// Execute cross-chain trade
await relayer.executeTradeAcrossChains(sessionId);
import { encrypt } from 'tran-p2pswap/utils';
// Encrypt relayer's private key
const password = process.env.KEY_PASSWORD;
const privateKey = process.env.RELAYER_PRIVATE_KEY;
const encryptedKey = await encrypt(privateKey, password);
// Store encryptedKey securely
import { decrypt } from 'tran-p2pswap/utils';
// Decrypt when needed
const decryptedKey = await decrypt(encryptedKey, password);
// Use decrypted key to create wallet client
const walletClient = createWalletClient({
account: privateKeyToAccount(decryptedKey),
chain: baseChain,
transport: http()
});
try {
await relayer.executeTradeAcrossChains(sessionId);
} catch (error) {
if (error.code === 'EXECUTION_FAILED') {
// Handle execution failure
await relayer.handleFailedExecution(sessionId);
} else if (error.code === 'INVALID_PROOF') {
// Handle invalid proof error
console.error('Invalid proof:', error);
}
// Handle other errors
}
// Watch trade execution events
sdk.watchTrade(sessionId, (status) => {
switch (status.status) {
case 'completed':
console.log('Trade completed successfully');
break;
case 'failed':
console.log('Trade failed:', status.reason);
break;
case 'pending':
console.log('Trade in progress');
break;
}
});
# Install dependencies
yarn install
# Build
yarn build
# Test
yarn test
# Lint
yarn lint
Copyright © 2025 NPC Labs, Inc. All rights reserved.
FAQs
SDK for P2P Asset Swap Protocol
The npm package @b3dotfun/p2pswap receives a total of 214 weekly downloads. As such, @b3dotfun/p2pswap popularity was classified as not popular.
We found that @b3dotfun/p2pswap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.