Skip to content

Fee Sponsoring

This guide will demonstrate how you can leverage Fee Sponsoring in your Application to cover fees for your users.

Template

We will base this guide off the following template:

pnpx gitpick ithacaxyz/porto/tree/main/examples/sponsoring-vite

Steps

Connect Account

Follow the Onboard & Discover Accounts guide to get this set up.

Set up Merchant Account

Run the following command to onboard a new Porto Merchant (Sponsor) Account.

pnpx porto onboard --admin-key --testnet

Place the address and private key of the merchant account into the .dev.vars file.

MERCHANT_ADDRESS=0x...
MERCHANT_PRIVATE_KEY=0x...

Set up Merchant API Route

Next, we will set up our Merchant API Route at a /porto/merchant endpoint.

For this example, we will use a Cloudflare Worker, however, you could also use a server framework of your choice (Next.js, Deno, Express, etc).

There are usages for each of the frameworks in the Router API reference.

import { env } from 'cloudflare:workers'
import { Router, Route } from 'porto/server'
 
export default Router({ basePath: '/porto' })
  .route('/merchant', Route.merchant({
    address: env.MERCHANT_ADDRESS as `0x${string}`,
    key: env.MERCHANT_PRIVATE_KEY as `0x${string}`,
    // Optionally handle which requests should be sponsored.
    // sponsor(request) {
    //   return true
    // },
  }),
) satisfies ExportedHandler<Env>

Hook up API

Now that we have our Merchant API Route set up, we need to hook it up to our app. We will do this by passing the URL to it on Porto.create().

const porto = await Porto.create({
  merchantUrl: '/porto/merchant'
})

Done

That's it! Now you can start sponsoring calls for your users.