Skip to main content

Examples

Simple examples for common wallet providers.

import { OwneySDK } from "@owney/sdk";

const owney = new OwneySDK({
apiKey: "your-api-key",
});

async function activateWithMetaMask() {
const provider = window.ethereum;
await provider.request({ method: "eth_requestAccounts" });
await owney.connect(provider);
await owney.activateAgent(8453); // Base chain ID
}

Deposit​

import { OwneySDK } from "@owney/sdk";

const owney = new OwneySDK({
apiKey: "your-api-key",
});

async function deposit(provider: any) {
if (!owney.isConnected()) {
await owney.connect(provider);
}

await owney.activateAgent(8453);

return owney.deposit({
amount: "100000000", // 100 USDC (6 decimals)
asset: "USDC",
});
}

Gasless WETH deposit​

WETH deposits use the same gasless flow, but the first deposit on a chain needs a one-time (user-paid) Permit2 approval. The SDK triggers it automatically; you can also pre-approve it. See Gasless deposits.

import { OwneySDK } from "@owney/sdk";

const owney = new OwneySDK({
apiKey: "your-api-key",
});

async function depositWeth(provider: any) {
if (!owney.isConnected()) {
await owney.connect(provider);
}

await owney.activateAgent(8453);

// Optional: front-load the one-time Permit2 approval (user pays gas).
await owney.approvePermit2("WETH");

return owney.deposit({
amount: "1000000000000000", // 0.001 WETH (18 decimals)
asset: "WETH",
});
}

Swap and deposit​

Swap a wallet asset into an agent-supported asset and deposit the proceeds. See Swapping for asset discovery and quote previews.

async function swapAndDeposit(provider: any) {
if (!owney.isConnected()) await owney.connect(provider);
await owney.activateAgent(8453);

return owney.swapAndDeposit({
from: {
chainId: 1,
symbol: "USDT",
amount: "100000000", // 100 USDT with 6 decimals
},
to: { chainId: 8453, symbol: "USDC" },
onSwapProgress: (stage) => console.log(stage),
});
}

Withdraw and swap​

async function withdrawAndSwap(provider: any) {
if (!owney.isConnected()) await owney.connect(provider);
await owney.activateAgent(8453);

return owney.withdrawAndSwap({
from: { chainId: 8453, symbol: "USDC" },
to: { chainId: 1, symbol: "ETH" },
amount: "25", // Human units; omit to withdraw the full balance
onSwapProgress: (stage) => console.log(stage),
});
}

Resume swaps​

// This can run before wallet connection.
await owney.resumePendingSwaps();

// After connecting the wallet, activate the destination chain before depositing.
for (const pending of owney.listPendingDeposits()) {
await owney.activateAgent(pending.chainId);
await owney.completePendingDeposit(pending.id);
}

Customize pools​

Load the catalogue, restore existing choices, and save the user's selection before depositing. See Pool customization for the complete field reference.

Enable user-level pool customization when constructing the SDK. This prevents an organization-level pool policy from overwriting the user's saved selection on a later activation.

const owney = new OwneySDK({
apiKey: "your-api-key",
userPoolCustomization: true,
});

const provider = window.ethereum;
await provider.request({ method: "eth_requestAccounts" });
await owney.connect(provider);
await owney.activateAgent(8453);

const catalog = await owney.getPoolCatalog({
asset: "USDC",
strategy: "aggressive",
});

const pairs = catalog.flatMap((protocol) =>
[...new Set(protocol.pools.map((pool) => pool.chainId))].map((chainId) => ({
protocolId: protocol.protocolId,
chainId,
})),
);

const saved = await owney.getPoolSelection({
pairs,
assets: ["USDC"],
});

// Render the catalogue using `saved`, then save the user's choices.
await owney.setPoolSelection({
selection: {
strategies: [{ asset: "USDC", strategy: "aggressive" }],
selections: [
{
asset: "USDC",
protocolId,
chainId,
pools: selectedPoolNames,
availablePools: displayedPoolNames,
},
],
},
});

await owney.deposit({ amount: "100000000", asset: "USDC" });