Skip to content

Route.merchant

Creates a framework-agnostic server route for merchant RPC operations. This allows merchants to sponsor transaction fees for users and handle Porto RPC requests in various server environments.

Imports

Named
import { Route } from 'porto/server'

Usage

import { Route } from 'porto/server'
 
const route = Route.merchant({
  address: process.env.MERCHANT_ADDRESS,
  key: process.env.MERCHANT_PRIVATE_KEY,
})

Frameworks

Route.merchant is compatible with any server framework that supports the Fetch API or Node.js Request Listener.

Examples:

Conditional Sponsoring

The sponsor option can be used to conditionally sponsor calls. When a function is provided to sponsor, the first parameter is a request object and the return value is a boolean indicating whether to sponsor the call.

In the following example, the call will only be sponsored if the to address is the same as the target address.

import { Route } from 'porto/server'
 
const target = '0x...'
 
const route = Route.merchant({
  address: process.env.MERCHANT_ADDRESS,
  key: process.env.MERCHANT_PRIVATE_KEY,
  sponsor(request) {
    return request.calls.every((call) => call.to === target)
  },
})

Parameters

type Parameters = {
  /** Address of the Merchant Account. */
  address: Address.Address
  /** An Admin Key of the Merchant Account to use for signing. */
  key:
    | Hex.Hex
    | (Pick<OneOf<Key.Secp256k1Key | Key.P256Key>, 'type'> & {
        privateKey: Hex.Hex
      })
  /** Whether to sponsor calls or not, and the condition to do so. */
  sponsor?:
    | boolean
    | ((
        request: RpcSchema.wallet_prepareCalls.Parameters,
      ) => Promise<boolean>)
      | undefined
}