Developer Fundamentals

-- Fee Delegation

Learn how to offload gas fees using VeChain’s fee delegation system. This lesson covers delegated transaction setup, sponsor wallets, and backend service integration using VIP-201.

What is Fee Delegation?

Gas fees are usually paid by the address that signs the transaction. VeChain's fee delegation allows you to pass on this payment to another wallet, which can either reside as a private key in your realm or be shielded by a web service.

Fee delegation is particularly interesting for developers who want to make their apps more user-friendly, as it eliminates the friction of transaction fees for end-users.

This functionality is part of VeChain's broader strategy to make blockchain adoption easier, particularly for businesses and users unfamiliar with cryptocurrency.

How to use Fee Delegation

To use fee delegation, you need to:

  1. Enable it while building the transaction object

  2. Provide information about the Gas-Payer during transaction signing

To enable fee delegation as a feature, you need to set isDelegated to true while building the transaction body:

const tx = await thor.transactions.buildTransactionBody(clauses, gas.totalGas,
  { isDelegated: true }
);

To get the gas-payer involved, you'll pass either gasPayerPrivateKey or gasPayerServiceUrl to the signing wallet:

// if you're using a fee sponsor
const wallet = new ProviderInternalBaseWallet(
  [{ privateKey: privateKey, address: senderAddress }],
  {
    gasPayer: {
      gasPayerServiceUrl: 'https://sponsor-testnet.vechain.energy/by/90',
    },
  }
);

// if you're delegating fees to your wallet
const wallet = new ProviderInternalBaseWallet(
    [{privateKey, address: senderAddress}],
    {
        gasPayer: {
            gasPayerPrivateKey: gasPayerAccount.privateKey,
        },
    }
);

To shield your private key for paying gas fees into a backend service, you can set up a web service that receives a raw transaction and co-signs it to confirm gas payment (based on VIP-201).

The process requires you to rebuild a transaction object from a hex-encoded version:

const signedTx = Transaction.decode(
  HexUInt.of(rawSignedTx).bytes,
  true
);

Try It Yourself

The snippet below brings it all together, illustrating how you can build a transaction, estimate the cost, sign it to execute the transaction onchain, and delegate the required gas fees to a sponsor.

import { ThorClient, ProviderInternalBaseWallet, VeChainProvider } from '@vechain/sdk-network';
import { Clause, ABIFunction, Secp256k1, Address, Transaction, HexUInt } from '@vechain/sdk-core';

const thor = ThorClient.at('https://testnet.vechain.org/');

//Prepare Wallet
const privateKey = await Secp256k1.generatePrivateKey();
const senderAddress = Address.ofPrivateKey(privateKey);
console.log("Address: 0x" + senderAddress.digits)

// Clauses
const clauses = [
    Clause.callFunction(
        '0x8384738c995d49c5b692560ae688fc8b51af1059',
        new ABIFunction({
            name: 'increment',
            inputs: [],
            outputs: [],
            constant: false,
            payable: false,
            type: 'function',
        })
    ),
];

//Calculate Gas
const gasResult = await thor.transactions.estimateGas(clauses);

//Build Transaction with fee delegation
const tx = await thor.transactions.buildTransactionBody(clauses, gasResult.totalGas,
    { isDelegated: true }
);

// Sign Transaction. Needs 4 steps
//Step 1: Get signer
const wallet = new ProviderInternalBaseWallet(
  [{ privateKey: privateKey, address: senderAddress }],
  {
    gasPayer: {
      gasPayerServiceUrl: 'https://sponsor-testnet.vechain.energy/by/90',
    },
  }
);

const provider = new VeChainProvider(thor, wallet,
  // Enable fee delegation
 true
);

const signer = await provider.getSigner(0);

// Step 2: Sign transaction
const rawSignedTx = await signer.signTransaction(tx, privateKey);

// Step 3: Build Signed Transaction Object
const signedTx = Transaction.decode(
  HexUInt.of(rawSignedTx).bytes,
  true
);

// Step 4: Send Transaction
const sendTransactionResult = await thor.transactions.sendTransaction(signedTx);

// Wait for results
const txReceipt = await thor.transactions.waitForTransaction(sendTransactionResult.id);
console.log(txReceipt);