Skip to main content

SDK

The easiest way to integrate P2P Protocol into your application is via the TypeScript SDK (@p2pdotme/sdk). It provides pre-built modules for orders, user profiles, pricing, currency config, ZK-KYC, fraud detection, and QR parsing, plus an optional React provider and hooks.

Framework-agnostic. The core is pure TypeScript, with optional React hooks. Wallet-agnostic. Bring your own viem client. No exceptions. All methods return Result / ResultAsync types. Modular. Import only what you need.

Installation:

npm install @p2pdotme/sdk

Full source: https://github.com/p2pdotme/p2pdotme-sdk

Environment Setup​

Networks​

The SDK supports Base (mainnet and testnet). Choose one:

NetworkChain IDUse Case
Base Mainnet8453Production (real money)
Base Sepolia84532Development & testing

Contract Addresses​

You need three addresses for your network:

VariablePurpose
DIAMOND_ADDRESSP2P.me protocol contract
USDC_ADDRESSUSDC token contract
SUBGRAPH_URLGraphQL endpoint for order queries

Base Sepolia (Testnet)​

REACT_APP_DIAMOND_ADDRESS=0xce868398FDaDcA368EAc203222874D6888532aE2
REACT_APP_USDC_ADDRESS=0xDABa329Ed949f28F64019f22c33c3B253B2Ded60
REACT_APP_SUBGRAPH_URL=https://api.studio.thegraph.com/query/110312/indexer-one/version/latest

Base Mainnet (Production)​

REACT_APP_DIAMOND_ADDRESS=0x4cad6eC90e65baBec9335cAd728DDC610c316368
REACT_APP_USDC_ADDRESS=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
REACT_APP_SUBGRAPH_URL=<deploy-your-own>

For mainnet subgraph: Deploy your own using the P2P.me Subgraph repository.

RPC URLs​

You need an RPC endpoint. Options:

Public (free, rate-limited):

# Base Mainnet
REACT_APP_RPC_URL=https://mainnet.base.org

# Base Sepolia Testnet
REACT_APP_RPC_URL=https://sepolia.base.org

Recommended (commercial, faster, more reliable):

Setup .env.local File​

Create a .env.local in your project root. Copy the values from above based on your network:

For Base Sepolia Testnet:

# Network
REACT_APP_RPC_URL=https://sepolia.base.org
REACT_APP_CHAIN_ID=84532

# Contract Addresses
REACT_APP_DIAMOND_ADDRESS=0xce868398FDaDcA368EAc203222874D6888532aE2
REACT_APP_USDC_ADDRESS=0xDABa329Ed949f28F64019f22c33c3B253B2Ded60
REACT_APP_SUBGRAPH_URL=https://api.studio.thegraph.com/query/110312/indexer-one/version/latest

# Your Account (for testing; development only!)
REACT_APP_PRIVATE_KEY=<your-private-key-here>

For Base Mainnet (Production):

# Network
REACT_APP_RPC_URL=https://mainnet.base.org
REACT_APP_CHAIN_ID=8453

# Contract Addresses
REACT_APP_DIAMOND_ADDRESS=0x4cad6eC90e65baBec9335cAd728DDC610c316368
REACT_APP_USDC_ADDRESS=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
REACT_APP_SUBGRAPH_URL=<your-deployed-subgraph-url>

Load in your code:

const RPC_URL = import.meta.env.REACT_APP_RPC_URL;
const DIAMOND_ADDRESS = import.meta.env.REACT_APP_DIAMOND_ADDRESS;
const USDC_ADDRESS = import.meta.env.REACT_APP_USDC_ADDRESS;
const SUBGRAPH_URL = import.meta.env.REACT_APP_SUBGRAPH_URL;

Getting Testnet Funds​

To test SELL/PAY orders on Base Sepolia, you need ETH + USDC:

Setup​

Install the SDK:

npm install @p2pdotme/sdk viem

You need:

  • publicClient: viem PublicClient for reads
  • walletClient: viem WalletClient for writes
  • diamondAddress: P2P Protocol contract
  • usdcAddress: USDC token address
  • subgraphUrl: GraphQL endpoint

React Example​

import { SdkProvider, useOrders, useProfile } from "@p2pdotme/sdk/react";
import { createPublicClient, createWalletClient, http } from "viem";
import { baseSepolia } from "viem/chains";

const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(RPC_URL),
});

const walletClient = createWalletClient({
chain: baseSepolia,
transport: http(RPC_URL),
account: YOUR_ACCOUNT,
});

function App() {
return (
<SdkProvider
publicClient={publicClient}
diamondAddress={DIAMOND_ADDRESS}
usdcAddress={USDC_ADDRESS}
subgraphUrl={SUBGRAPH_URL}
>
<OrderFlow />
</SdkProvider>
);
}

function OrderFlow() {
const orders = useOrders();
const profile = useProfile();

async function buyUsdc() {
const result = await orders.placeOrder.execute({
walletClient,
orderType: 0, // BUY
currency: "INR",
user: userAddress,
recipientAddr: userAddress,
amount: 10_000_000n, // 10 USDC (6 decimals)
fiatAmount: 850_000_000n, // 850 INR (6 decimals)
fiatAmountLimit: 0n,
});

result.match(
({ hash, meta }) => console.log("Order placed:", hash),
(err) => console.error(`Error: ${err.code} - ${err.message}`),
);
}

return <button onClick={buyUsdc}>Buy USDC</button>;
}

Understanding Result Types​

All SDK methods return Result types from neverthrow (never throw exceptions):

const result = await orders.placeOrder.execute(params);

// Always use .match() to handle success and error
result.match(
(success) => {
// Handle success
console.log("Success:", success.hash);
},
(error) => {
// Handle error
console.error("Error:", error.code, error.message);
}
);

// Or check with .isOk()
if (result.isOk()) {
console.log("Hash:", result.value.hash);
} else {
console.log("Error:", result.error.code);
}

Orders​

The orders module handles all order lifecycle operations.

Order Types​

TypeValueDescription
BUY0User receives USDC, sends fiat
SELL1User sends USDC, receives fiat
PAY2User sends USDC to wallet

Place BUY Order​

const result = await orders.placeOrder.execute({
walletClient,
orderType: 0,
currency: "INR",
user: userAddress,
recipientAddr: userAddress,
amount: 10_000_000n,
fiatAmount: 850_000_000n,
fiatAmountLimit: 0n,
});

Place SELL Order​

SELL requires USDC approval first:

// 1. Approve
await orders.approveUsdc.execute({
walletClient,
amount: 10_000_000n,
});

// 2. Place order
const result = await orders.placeOrder.execute({
walletClient,
orderType: 1, // SELL
currency: "INR",
user: userAddress,
recipientAddr: userAddress,
amount: 10_000_000n,
fiatAmount: 850_000_000n,
fiatAmountLimit: 0n,
});

// 3. Set payment destination
await orders.setSellOrderUpi.execute({
walletClient,
orderId: result.value.meta.orderId,
paymentAddress: "user@upi",
});

Track Orders​

// Get all user orders (returns Result type)
const result = await orders.getOrders({
userAddress: userAddress,
limit: 20,
skip: 0,
});

result.match(
(ordersList) => {
console.log(`Found ${ordersList.length} orders`);
ordersList.forEach((order) => {
console.log(`Order ${order.orderId}: ${order.status}`);
});
},
(err) => console.error(`Error: ${err.code}`),
);

// Get single order
const singleResult = await orders.getOrder({ orderId: 42n });
singleResult.match(
(order) => console.log("Order:", order),
(err) => console.error("Error:", err),
);

// Cancel order
const cancelResult = await orders.cancelOrder.execute({
walletClient,
orderId: "0x123...",
});

cancelResult.match(
({ hash }) => console.log("Cancelled! Hash:", hash),
(err) => console.error("Error:", err.message),
);

// Raise dispute
const disputeResult = await orders.raiseDispute.execute({
walletClient,
orderId: "0x123...",
});

disputeResult.match(
({ hash }) => console.log("Dispute raised! Hash:", hash),
(err) => console.error("Error:", err.message),
);

Get Fees​

const result = await orders.getFeeConfig({ currency: "INR" });

result.match(
(feeConfig) => {
// FeeConfig fields are 6-decimal bigints
console.log(feeConfig.smallOrderThreshold, feeConfig.smallOrderFixedFee);
},
(err) => console.error("Error:", err.code),
);

Profile & Limits​

Check user balances, USDC allowance, and trading limits.

Check Balances​

// USDC balance
const balanceResult = await profile.getUsdcBalance({ address: userAddr });
if (balanceResult.isErr()) throw balanceResult.error;
const usdcBalance = balanceResult.value; // bigint

// USDC allowance (before SELL/PAY)
const allowance = await profile.getUsdcAllowance({
owner: userAddress,
});

// getBalances likewise returns a Result:
const balancesResult = await profile.getBalances({ address: userAddress, currency: "INR" });
balancesResult.match(
(b) => console.log(b.usdc, b.fiat),
(err) => console.error(err.code),
);

Check Limits​

const result = await profile.getTxLimits({
address: userAddress,
currency: "INR",
});

result.match(
(limits) => {
// limits has: buyLimit, sellLimit
console.log("Buy Limit:", limits.buyLimit);
console.log("Sell Limit:", limits.sellLimit);
},
(err) => console.error("Error:", err.code),
);

Limits depend on reputation, KYC level, and currency risk parameters.

Pre-flight Checks​

Before BUY:

const result = await profile.getTxLimits({
address: userAddr,
currency: "INR",
});

result.match(
(limits) => {
if (amount > limits.buyLimit) {
console.log("Exceeds buy limit");
} else {
console.log("Amount OK");
}
},
(err) => console.error("Error:", err.code),
);

Before SELL:

// Get USDC balance
const balanceResult = await profile.getUsdcBalance({ address: userAddr });
if (balanceResult.isErr()) throw balanceResult.error;
const usdcBalance = balanceResult.value; // bigint

// Get limits
const limitsResult = await profile.getTxLimits({
address: userAddr,
currency: "INR",
});

limitsResult.match(
(limits) => {
if (usdcBalance < amount) {
console.log("Insufficient USDC");
} else if (amount > limits.sellLimit) {
console.log("Exceeds sell limit");
} else {
console.log("Can sell");
}
},
(err) => console.error("Error:", err.code),
);

Error Handling​

Decode contract errors to user-friendly messages.

Error Structure​

const error = {
code: "INSUFFICIENT_BALANCE",
message: "User has insufficient USDC balance",
cause: rawError, // underlying error
};

Decode Contract Errors​

import {
parseContractError,
getContractErrorMessage,
} from "@p2pdotme/sdk/orders";

orders.placeOrder.execute(params).match(
({ hash }) => console.log("Placed:", hash),
(err) => {
const code = parseContractError(err.cause);
const message = getContractErrorMessage(code);
showToast(message); // "Order amount exceeds limit"
},
);

Common Error Codes​

CodeMeaningAction
INVALID_INPUTInvalid parametersCheck your inputs
VALIDATION_ERRORValidation failedFix the data format
NETWORK_ERRORRPC/subgraph errorRetry with backoff
INSUFFICIENT_ALLOWANCENeed USDC approvalCall approveUsdc.execute()

Error Handling Patterns​

Graceful degradation:

const result = await orders.placeOrder.execute(params);

result.match(
(success) => {
return { ok: true, hash: success.hash };
},
(error) => {
console.error(`Error [${error.code}]: ${error.message}`);
return { ok: false, message: error.message };
},
);

Retry with backoff:

async function retryOrder(params, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
const result = await orders.placeOrder.execute(params);

if (result.isOk()) {
return result.value;
}

const { code } = result.error;

// Only retry on network errors
if (code === "NETWORK_ERROR") {
await new Promise(r => setTimeout(r, 1000 * Math.pow(2, i)));
continue;
}

// Don't retry validation errors
throw result.error;
}
}

Advanced Patterns​

Use prepare/execute separation, relay identity, and custom storage.

Prepare vs Execute​

prepare() returns raw transaction without signing:

const result = await orders.placeOrder.prepare(params);
// Returns: { to, data, value, meta }
// Send via relayer, multisig, or custom signer

execute() signs and sends with viem:

const result = await orders.placeOrder.execute({
walletClient,
...params
});

Use Cases​

Gasless relaying:

const tx = await orders.placeOrder.prepare(params);
await relayerApi.send(tx.value);

Multi-sig:

const tx = await orders.placeOrder.prepare(params);
await multiSigWallet.queue(tx.value);

Server-side signing:

const tx = await orders.placeOrder.prepare(params);
const signed = await serverSignAndSend(tx.value);

Relay Identity​

SDK uses a keypair for sender anonymity. Default is in-memory (lost on refresh).

Persist to localStorage:

import { createLocalStorageRelayStore } from "@p2pdotme/sdk/orders";

<SdkProvider
orders={{
relayIdentityStore: createLocalStorageRelayStore({ key: "relay" })
}}
/>

Custom storage:

const store = {
get: async () => db.getRelayIdentity(),
set: async (id) => db.saveRelayIdentity(id),
};

<SdkProvider orders={{ relayIdentityStore: store }} />

Standalone (No React)​

Use factories directly:

import { createOrders } from "@p2pdotme/sdk/orders";
import { createProfile } from "@p2pdotme/sdk/profile";
import { createPublicClient, http } from "viem";
import { baseSepolia } from "viem/chains";

const publicClient = createPublicClient({
chain: baseSepolia,
transport: http("https://sepolia.base.org"),
});

const orders = createOrders({
publicClient,
diamondAddress: "0xce868398FDaDcA368EAc203222874D6888532aE2",
usdcAddress: "0xDABa329Ed949f28F64019f22c33c3B253B2Ded60",
subgraphUrl: "https://api.studio.thegraph.com/query/110312/indexer-one/version/latest",
});

const profile = createProfile({
publicClient,
diamondAddress: "0xce868398FDaDcA368EAc203222874D6888532aE2",
usdcAddress: "0xDABa329Ed949f28F64019f22c33c3B253B2Ded60",
});

// Use them
const orderResult = await orders.getOrder({ orderId: "0x123..." });
orderResult.match(
(order) => console.log("Order:", order),
(err) => console.error("Error:", err),
);

const balanceResult = await profile.getUsdcBalance({ address: "0x..." });
if (balanceResult.isErr()) throw balanceResult.error;
console.log("USDC:", balanceResult.value); // bigint

Examples​

Complete workflows for common patterns.

Buy Flow​

async function buyUsdc(userAddr, currency, fiatAmount) {
const limitsResult = await profile.getTxLimits({ address: userAddr, currency });
if (limitsResult.isErr()) throw limitsResult.error;
if (fiatAmount > limitsResult.value.buyLimit) throw new Error("Exceeds limit");

const priceClient = createPrices({ publicClient, diamondAddress });
const priceResult = await priceClient.getPriceConfig({ currency });
if (priceResult.isErr()) throw priceResult.error;
const usdcAmount = (fiatAmount * 1_000_000n) / priceResult.value.buyPrice;

return await orders.placeOrder.execute({
walletClient,
orderType: 0,
currency,
user: userAddr,
recipientAddr: userAddr,
amount: usdcAmount,
fiatAmount,
fiatAmountLimit: 0n,
});
}

Sell Flow​

async function sellUsdc(userAddr, currency, usdcAmount) {
const balanceResult = await profile.getUsdcBalance({ address: userAddr });
if (balanceResult.isErr()) throw balanceResult.error;
if (balanceResult.value < usdcAmount) throw new Error("Insufficient USDC");

const allowance = await profile.getUsdcAllowance({ owner: userAddr });
if (allowance < usdcAmount) {
await orders.approveUsdc.execute({ walletClient, amount: usdcAmount });
}

const priceClient = createPrices({ publicClient, diamondAddress });
const priceResult = await priceClient.getPriceConfig({ currency });
if (priceResult.isErr()) throw priceResult.error;
const fiatAmount = (usdcAmount * priceResult.value.sellPrice) / 1_000_000n;

const placed = await orders.placeOrder.execute({
walletClient,
orderType: 1,
currency,
user: userAddr,
recipientAddr: userAddr,
amount: usdcAmount,
fiatAmount,
fiatAmountLimit: 0n,
});

if (!placed.isOk()) return placed;

return await orders.setSellOrderUpi.execute({
walletClient,
orderId: placed.value.meta.orderId,
paymentAddress: "user@upi",
});
}

Monitor Order Status​

function useOrderStatus(orderId) {
const [order, setOrder] = useState(null);
const orders = useOrders();

useEffect(() => {
let poll;
const fn = async () => {
const result = await orders.getOrder({ orderId });
if (result.isOk()) setOrder(result.value);
};

fn();
poll = setInterval(fn, 3000);
return () => clearInterval(poll);
}, [orderId, orders]);

return order;
}