🚀 Quick start

Starting guide on how to quickly integrate the @txfusion/txsync-viem🚀

📥 Installation & setup

Your API requests are authenticated using API keys. Any request that doesn't include an API key will return an error.

You can generate an API key from your Dashboard at any time.

Install the @txfusion/txsync-viem package

The first step to interact with our API is to install the official library:

# Install via NPM
npm install @txfusion/txsync-viem

Get your paymaster object

To be able to work with the paymaster and call the methods of the Paymaster object, you first need to get it by passing the address of the paymaster contract.

import { getPaymaster, createRestriction } from "@txfusion/txsync-viem";
import { createWalletClient, custom } from 'viem';
import { zkSync } from 'viem/chains';
import { eip712WalletActions } from 'viem/zksync';

// Get the account
const [account] = await window.ethereum!.request({ method: "eth_requestAccounts" });

// Create walletClient object
const walletClient = createWalletClient({
  account,
  chain: zkSync,
  transport: custom(window.ethereum),
}).extend(eip712WalletActions());

// Create publicClient object
const publicClient = createPublicClient({
  chain: zkSync,
  transport: http(),
});

// Get paymaster object
const paymaster = await getPaymaster(
  paymasterAddress,
  walletClient,
  publicClient
);

console.log(`PaymasterType: ${paymaster.paymasterType}`);
console.log(`Token Address: ${paymaster.token}`);

Using paymaster methods

Now that we have the paymaster object, we can call all sorts of methods upon it.

import { ERC20Token } from "@txfusion/txsync-viem/src/abi/ERC20Token";

const tokenAddress = '0x...'; // Replace with the actual token address on L2
const args = ['0x...', parseEther(amount)]; // Recipient address and amount to send

// Sending transaction:
const txHash = await paymaster.sendPaymasterTransaction(
  tokenAddress as Address, // Contract address
  ERC20Token, // ABI
  "transfer", // Function to call
  args,
  {
    gasLimit: gasLimit, // Override values
  },
);

// Estimate gas
const estimatedGas = paymaster.estimateGas(
  tokenL2Address as Address,
  ERC20Token,
  "transfer",
  [recipientAddress, parseEther(amount)],
  {
    gasLimit: gasLimit,
  },
);


// Getting restrictions:
const restrictions = await paymaster.getExtensions()

Above are just quick examples.

You can find how these and many other methods work, which arguments they accept, and how they interact deeply explained in the API section and the Examples section.

Last updated