Skip to main content
SKALE offers native gasless transactions that allow users to send transactions without needing to hold gas tokens. This is achieved through SKALE’s Proof-of-Work mechanism that replaces traditional gas payments with computational work done client-side. For more information about SKALE’s gasless transaction technology, see the SKALE Gasless Transactions documentation. This guide demonstrates how to implement gasless transactions using the @dirtroad/gasless library and viem.

Installation

# NPM
npm install viem @dirtroad/gasless

# Yarn
yarn add viem @dirtroad/gasless

# PNPM
pnpm add viem @dirtroad/gasless

# Bun
bun add viem @dirtroad/gasless

Example

typescript.ts
import { mineGasForTransaction } from "@dirtroad/gasless";
import { createPublicClient, createWalletClient, http } from "viem";
import { skaleCalypsoTestnet } from "viem/chains";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";

async function main() {
    const privateKey = generatePrivateKey();
    
    const client = createPublicClient({
        chain: skaleCalypsoTestnet,
        transport: http()
    });

    const wallet = createWalletClient({
        chain: skaleCalypsoTestnet,
        transport: http(),
        account: privateKeyToAccount(privateKey)
    });

    // Generate gasless transaction parameters
    const { gasPrice } = await mineGasForTransaction(100_000, wallet.account.address, 0);

    // Send transaction with computed gas price
    const transactionHash = await wallet.sendTransaction({
        to: "0x62Fe932FF26e0087Ae383f6080bd2Ed481bA5A8A",
        data: `0x0c11dedd000000000000000000000000${wallet.account.address.substring(2)}`,
        gas: BigInt(100_000),
        gasPrice: BigInt(gasPrice)
    });

    const receipt = await client.waitForTransactionReceipt({
        hash: transactionHash
    });

    console.log("Gasless transaction receipt: ", receipt);
    return receipt;
}

main().catch(console.error);


Libraries & Resources