Skip to content

Tokenize Uniswap v4 positions

From /tokenize or a script: previewPositions, an approval or a signed permit, then tokenize with the positions and a term.

On this page

Uniswap v4 positions go into doors the tokenizer makes for them, locked until a date or for good, and the revenue token's income is their LP fees. Positions on the Hookr hook go into one door and the rest into another, so only the second carries Hookr's leg.

In the app

  1. Open /tokenize. Your wallet lists the wallet's v4 positions with their pools, and its Hookr's leg column shows each one's leg. Tick them, press Tokenize, set the terms and sign: this tab approves the tokenizer's v4 door factory once with setApprovalForAll, then sends tokenize.
  2. Or open By id and its Uniswap v4 position NFTs card. Pick the positions, and the card previews Hookr's leg and the currencies they pay. Sign and tokenize asks for one signature: a permit for one position, or a permit for all of them for several. The tokenizer applies it before it moves them.

A wallet whose address has code gets Approve and tokenize instead, or Lock the positions and mint when the positions are approved already.

The calls

  1. predictToken(creator, t) gives the token's address, and previewPositions(token, tokenIds) on the tokenizer says, for each position, whether its pool is on the Hookr hook (no leg), whether its hook runs on liquidity removals and so could make the door's collection of it fail, and whether its pool holds the revenue token itself; and it gives the currencies the positions pay.
  2. setApprovalForAll(positionDoorFactory, true) on the PositionManager, both named by the tokenizer (positionDoorFactory(), positionManager()), or a permit in s.positionPermits.
  3. tokenize(t, s, m) with the positions in s.positionIds, up to 54 (more do not fit in one transaction; add the rest with addStreams), and their term in s.unlockAt: at least 7 days from now or the largest uint64 for good. The tokenizer emits PositionReported for each position, makes one door for the positions on the Hookr hook and one for the rest, moves the positions in, adds each door to the token and mints. With s.listAssets set, the positions' currencies are listed as the token's assets; without it, income in a currency the token does not list reaches holders through the drop box. No value is sent without a market.
  4. Once unlockAt has passed, anyone calls release() on a door, and each position goes back to the wallet that locked it after a last collection: Terms and endings. A door that holds more than 21 positions releases them with releaseSome(ids) in pages of 21, since one release() over more may not fit in a transaction.

In code

With client.ts and abis.ts:

import { createWalletClient, http, keccak256, parseUnits, toHex, zeroAddress } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { robinhood } from "viem/chains";
import { client, RULES, TOKENIZER } from "./client";
import { positionManagerAbi, tokenizerAbi } from "./abis";

const creator = privateKeyToAccount(process.env.CREATOR_KEY as `0x${string}`);
const wallet = createWalletClient({ account: creator, chain: robinhood, transport: http() });
const POSITIONS = [111n, 222n]; // your Uniswap v4 position NFTs

// 1. The terms. Hookr's leg comes out of each door's income at the sync, so the recipients share the whole supply.
const supply = parseUnits("1000000000", 18);
const terms = {
  name: "Example LP Revenue",
  symbol: "rLP",
  supply,
  streamSeconds: 86_400,
  tipBps: 100,
  assets: [],
  recipients: [creator.address],
  amounts: [supply],
  termsHash: keccak256(toHex("rLP: two v4 positions, 30 days")),
  streamManager: creator.address,
  openStreams: false,
};
const token = await client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "predictToken", args: [creator.address, terms] });

// 2. What the positions bring: which pools are on the Hookr hook, and the currencies they pay.
const [hookrPool, hookCanBlockCollection, holdsToken, currencies] = await client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "previewPositions", args: [token, POSITIONS] });
console.log({ hookrPool, hookCanBlockCollection, holdsToken, currencies });

// 3. Let the tokenizer's v4 door factory move the positions.
const [positionManager, doorFactory] = await Promise.all([
  client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "positionManager" }),
  client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "positionDoorFactory" }),
]);
if (!(await client.readContract({ address: positionManager, abi: positionManagerAbi, functionName: "isApprovedForAll", args: [creator.address, doorFactory] }))) {
  const hash = await wallet.writeContract({ address: positionManager, abi: positionManagerAbi, functionName: "setApprovalForAll", args: [doorFactory, true] });
  await client.waitForTransactionReceipt({ hash });
}

// 4. No market: with `open` false the tokenizer reads no other field of MarketTerms.
const noMarket = { ...(await client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "defaultMarket", args: [RULES, token, zeroAddress, 2n ** 96n, 1n] })), open: false };
const unlockAt = BigInt(Math.floor(Date.now() / 1000)) + 30n * 86_400n; // 30 days; at least 7, or FOREVER

// 5. Lock the positions in their doors, list the currencies they pay, and mint.
const streams = {
  launchParams: "0x" as const,
  families: [],
  rules: [],
  feebs: [],
  positionIds: POSITIONS,
  positionPermits: [],
  v3PositionIds: [],
  v3Permits: [],
  revenueTokens: [],
  sources: [],
  unlockBlock: 0n, // no family and no market here
  unlockAt,
  listAssets: true,
};
const { request } = await client.simulateContract({
  account: creator,
  address: TOKENIZER,
  abi: tokenizerAbi,
  functionName: "tokenize",
  args: [terms, streams, noMarket],
});
await client.waitForTransactionReceipt({ hash: await wallet.writeContract(request) });

To sign instead of sending the approval:

// Instead of step 3: one signature the tokenizer applies before it moves the positions.
// The PositionManager's nonces are unordered, a word and a bit: a fresh random word, bit 0.
const nonce = BigInt(toHex(crypto.getRandomValues(new Uint8Array(31)))) << 8n;
const deadline = BigInt(Math.floor(Date.now() / 1000)) + 3_600n;
const signature = await creator.signTypedData({
  domain: { name: "Uniswap v4 Positions NFT", chainId: robinhood.id, verifyingContract: positionManager },
  types: {
    PermitForAll: [
      { name: "operator", type: "address" },
      { name: "approved", type: "bool" },
      { name: "nonce", type: "uint256" },
      { name: "deadline", type: "uint256" },
    ],
  },
  primaryType: "PermitForAll",
  message: { operator: doorFactory, approved: true, nonce, deadline },
});
const positionPermits = [{ forAll: true, tokenId: 0n, deadline, nonce, signature }];

A permit for all of a wallet's positions stays in force after the mint; setApprovalForAll(doorFactory, false) ends it. For one position, sign a Permit(spender, tokenId, nonce, deadline) to the door factory and send it with forAll: false; the move uses it up. A permit that fails is skipped, and the move then needs an approval already in place. The PositionManager's domain has its name, the chain and its address, and no version.

Fee

Positions whose pools are on the Hookr hook already pay Hookr through it, so their door carries no leg. The door for any other positions carries Hookr's leg of 5%, taken from what it delivers at each sync. previewPositions says which pools are on the hook before the mint, and StreamAdded gives each door's legBps.