For the complete documentation index, see llms.txt. This page is also available as Markdown.

Subscription request

Approve the asset and create a subscription request on the shares contract.

Subscribing is a two-step flow, just like a standard deposit:

  1. Approve the shares contract to spend your subscription asset, a standard ERC-20 approve call.

  2. Create the request with requestSubscription on the shares contract:

function requestSubscription(
    uint256 assetsIn,         // amount of the asset to deposit
    uint256 minSharesOut,     // slippage protection (see Calculate share price)
    address subscriptionAsset,// must be approved for deposits
    address receiver          // who receives the minted shares
) external returns (uint256 requestId);

Example

Subscribing 1,000 USDC (6 decimals) into a fund. We read the fund's current share price (see Calculate share price), convert it to expected shares with assetsToShares, and apply a 0.5% example tolerance to get minSharesOut.

import { createWalletClient, createPublicClient, http, parseAbi, parseUnits } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";

const SHARES = "0x...";  // fund shares contract — see the fund's Policies and addresses
const USDC   = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";

const sharesAbi = parseAbi([
  "function assetsToShares(uint256 assetAmount, uint256 sharesPrice, address subscriptionAsset) view returns (uint256)",
  "function requestSubscription(uint256 assetsIn, uint256 minSharesOut, address subscriptionAsset, address receiver) returns (uint256)",
]);
const erc20Abi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);

const account = privateKeyToAccount("0x...");
const pub = createPublicClient({ chain: mainnet, transport: http() });
const wallet = createWalletClient({ account, chain: mainnet, transport: http() });

const assetsIn = parseUnits("1000", 6); // 1,000 USDC

// 1. Current share price in 8-decimal USD — compute it onchain per Calculate share price.
const sharePrice = await getSharePrice(pub); // bigint, 8 decimals — see Calculate share price

// 2. Convert to expected shares at that price, then apply your tolerance.
const expectedShares = await pub.readContract({
  address: SHARES, abi: sharesAbi, functionName: "assetsToShares",
  args: [assetsIn, sharePrice, USDC],
});
const minSharesOut = (expectedShares * 9950n) / 10000n; // 0.5% is only an example — set your own tolerance

// 3. Approve, then request.
await wallet.writeContract({ address: USDC, abi: erc20Abi, functionName: "approve", args: [SHARES, assetsIn] });
const hash = await wallet.writeContract({
  address: SHARES, abi: sharesAbi, functionName: "requestSubscription",
  args: [assetsIn, minSharesOut, USDC, account.address],
});

Events

Creating a subscription request emits a SubscriptionRequest event containing the requestId, cancelableFrom, and expiryAt. Track it as described in Monitor a request.

Subscription flow example, with USDC as subscription token.

Last updated