Skip to main content

createContractPermission

Creates a permission for a specific smart contract function.

Example

import { createContractPermission } from "@0xsequence/connect";

const AAVE_V3_POOL_ADDRESS_ARBITRUM = '0x794a61358D6845594F94dc1DB02A252b5b4814aD'

const permission = createContractPermission({
    address: AAVE_V3_POOL_ADDRESS_ARBITRUM,
    functionSignature: 'function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)',
    rules: [
        {
            param: 'asset',
            type: 'address',
            condition: 'EQUAL',
            value: USDC_ADDRESS_ARBITRUM
        },
        {
            param: 'amount',
            type: 'uint256',
            condition: 'LESS_THAN_OR_EQUAL',
            value: parseUnits('1', 6),
            cumulative: true
        }
    ]
});

Parameters

type CreateContractPermissionOptions = {
  /** The address of the target contract. */
  address: Address

  /**
   * The human-readable function signature.
   * Example: 'function transfer(address to, uint256 amount)'
   */
  functionSignature?: string

  /** An array of rules to apply to the function's arguments. */
  rules?: Rule[]

  /**
   * If true, this function can only be successfully called once during the session.
   * @default false
   */
  onlyOnce?: boolean
}

address

The address of the target contract.

functionSignature

The human-readable function signature.

rules

An array of rules to apply to the function’s arguments.
type Rule = {
  /** The name of the parameter from the function signature (e.g., 'to', 'amount'). */
  param: string

  /** The type of the parameter (address, uint256, etc.). */
  type: ParamType

  /** The comparison to perform on the parameter (EQUAL, NOT_EQUAL, GREATER_THAN_OR_EQUAL, LESS_THAN_OR_EQUAL). */
  condition: RuleCondition

  /** The value to compare against. Must match the `type`. */
  value: string | number | bigint | boolean

  /**
   * Determines if the rule's value is a cumulative total across all calls to this function.
   *
   * For example, for a `transfer` function, if `amount` is set to 10 with `cumulative: true`,
   * the dApp can make as many transfer calls as it wants but cannot transfer more than 10 tokens
   * in total. With `cumulative: false`, the dApp can transfer 10 tokens per call as many times
   * as conditions allow.
   *
   * @default true
   */
  cumulative?: boolean
}

onlyOnce

If true, this function can only be successfully called once during the session.

Return Type

Returns a Permission:
type Permission = {
  target: Address.Address
  rules: ParameterRule[]
}

createContractPermissions

Same as createContractPermission, but creates multiple permissions for the same contract address in a single call.

Example

import { createContractPermissions } from "@0xsequence/connect";

const permissions = createContractPermissions({
    address: AAVE_V3_POOL_ADDRESS_ARBITRUM,
    functions: [
        {
            functionSignature: 'function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)',
            rules: [
                {
                    param: 'asset',
                    type: 'address',
                    condition: 'EQUAL',
                    value: USDC_ADDRESS_ARBITRUM
                },
                {
                    param: 'amount',
                    type: 'uint256',
                    condition: 'LESS_THAN_OR_EQUAL',
                    value: parseUnits('1', 6),
                    cumulative: true
                }
            ]
        },
        {
            functionSignature: 'function withdraw(address asset, uint256 amount, address to)',
            rules: [
                {
                    param: 'asset',
                    type: 'address',
                    condition: 'EQUAL',
                    value: USDC_ADDRESS_ARBITRUM
                },
                {
                    param: 'amount',
                    type: 'uint256',
                    condition: 'LESS_THAN_OR_EQUAL',
                    value: parseUnits('1', 6),
                    cumulative: true
                }
            ]
        }
    ]
});

Parameters

type CreateContractPermissionsOptions = {
  /** The address of the target contract. */
  address: Address

  /** An array of permissions for one or more functions on this contract. */
  functions?: FunctionPermission[] | undefined | null
}

address

The address of the target contract.

functions

An array of FunctionPermission objects:
type FunctionPermission = {
  /**
   * The human-readable function signature.
   * Example: 'function transfer(address to, uint256 amount)'
   */
  functionSignature: string

  /** An array of rules to apply to the function's arguments. */
  rules?: Rule[]

  /**
   * If true, this function can only be successfully called once during the session.
   * @default false
   */
  onlyOnce?: boolean
}

Return Type

Returns Permission[]:
type Permission = {
  target: Address.Address
  rules: ParameterRule[]
}
See these utilities in action in the Smart Sessions Examples.