Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Authorization

Learn how to authorize the VifRouter to act on your behalf in the Vif protocol.

Why Authorization?

Before the VifRouter can execute trades or manage orders for you, it needs to be authorized by your account in the Vif core contract. This is a security feature that gives you control over which contracts can interact with your positions.

Two Authorization Methods

You can authorize the router in two ways:

  1. Standalone Transaction - Simple one-time authorization transaction
  2. Batch with Signature - Include authorization in the same transaction as your first action

Method 1: Standalone Transaction

The simplest way to authorize the router is with a dedicated transaction.

Setup

import { VifRouter, nonce } from "vifdk";
import { createWalletClient, http, publicActions } from "viem";
import { mainnet } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
 
const router = new VifRouter(VIF_ROUTER_ADDRESS, VIF_CORE_ADDRESS, chainId);
 
const client = createWalletClient({
  chain: mainnet,
  transport: http(),
  account: privateKeyToAccount(privateKey),
}).extend(publicActions);

Authorize

// Authorize with standalone transaction
await client.writeContract({
  address: VIF_CORE_ADDRESS,
  abi: [
    {
      type: "function",
      name: "authorize",
      inputs: [
        { name: "authorized", type: "address" },
        { name: "value", type: "bool" },
      ],
      outputs: [],
      stateMutability: "nonpayable",
    },
  ],
  functionName: "authorize",
  args: [VIF_ROUTER_ADDRESS, true],
});

This sends a transaction to the Vif core contract authorizing the router to act on your behalf.

Method 2: Batch with Typed Signature

For a better user experience, you can include authorization in your first transaction using a typed signature (EIP-712).

Get Current Nonce

First, retrieve your current authorization nonce from the contract:

// Get current nonce for the user
const userNonce = await client.readContract({
  address: VIF_CORE_ADDRESS,
  ...nonce(client.account.address),
});

Create and Sign Authorization

Create authorization data and get the user's signature:

// Create authorization data with current nonce
const authData = router.authorizationData(
  client.account.address,
  userNonce,
  new Date(Date.now() + 1000 * 60 * 60 * 24 * 30) // 30 days
);
 
// Sign the authorization
const typedData = router.singatureDataForAuthorization(authData);
const signature = await client.signTypedData(typedData);

Include in Transaction

Add the authorization action to your transaction, then chain it with your first action:

// Use authorization in a batch with other actions
const actions = router
  .createTypedActions()
  .authorize({ authorization: authData, signature })
  // ... other actions ...
  .build({
    addRecommendedActions: true,
    receiver: client.account.address,
  });

You would then add your trading action (like orderSingle, limitSingle, etc.) before calling .build().

Checking Authorization Status

You can check if the router is already authorized:

// Check if router is authorized
import { authorized } from "vifdk";
 
const isAuthorized = await client.readContract({
  address: VIF_CORE_ADDRESS,
  ...authorized(client.account.address, VIF_ROUTER_ADDRESS),
});
 
console.log("Router authorized:", isAuthorized);

Revoking Authorization

To revoke authorization:

await client.writeContract({
  address: VIF_CORE_ADDRESS,
  abi: [
    {
      type: "function",
      name: "authorize",
      inputs: [
        { name: "authorized", type: "address" },
        { name: "value", type: "bool" },
      ],
      outputs: [],
      stateMutability: "nonpayable",
    },
  ],
  functionName: "authorize",
  args: [VIF_ROUTER_ADDRESS, false], // false to revoke
});

Best Practices

  1. Check First - Always check if authorization is needed before requesting it
  2. Use Batch for UX - Batch authorization provides better user experience
  3. Nonce Management - Always use the current nonce from the contract when creating authorization data
  4. Deadline - Set reasonable deadlines for authorizations (e.g., 30 days)
  5. Revoke When Done - Consider revoking authorization if you're no longer using the router

Next Steps