Sponsoring Gas for Users

πŸ“˜

To learn more about gas sponsorship, please read the Gas Sponsorship section of Architecture + Concepts.

Using a Halliday paymaster, you can sponsor transaction fees for your users.

🚧

To enable gas sponsorship functionality, please reach out to the Halliday team in your dedicated slack channel or at [email protected].

The Halliday SDK allows you to sponsor gas on the following account methods: transferAsset, transferBalance, and callContract. This way, users no longer need to have native currency in their wallets before issuing transactions. Simply set the sponsor_gas boolean argument on any transaction method (transferAsset, transferBalance, or callContract) to true to sponsor a user's transaction once your paymaster is enabled!

Initialize the Halliday SDK

In this example we'll use the Halliday client from an external wallet. For more details on how to connect your external wallet to the Halliday SDK, please visit our Getting Started with an External Wallet guide.

import {HallidayViaSigner, BlockchainType} from "halliday-sdk";

const signer = ...                                // Retrieve the signer from your non-custodial provider of choice.

const hallidayClient = new HallidayViaSigner({
  hallidayPublicApiKey: "<YOUR_API_KEY>",
  signer: signer,
  // specifying blockchainType is optional - defaults to Mumbai
  blockchainType: BlockchainType.DFK, 
  sandbox: true                                   // Optional argument. If true, makes the SDK interact with your test environment
});


// To get or create the Halliday Smart Account, call getOrCreateHallidayAAWallet. 
// This will get the user's smart account for the blockchain you specified in the constructor.
const userInGameId = "user_in_game_id";           // The user's id in your application. Must be unique for each user.
const hallidayAccount = await hallidayClient.getOrCreateHallidayAAWallet(
  userInGameId,					
  "[email protected]"
);
const hallidayAccountAddress = hallidayAccount.account_address;

Sponsor a transferAsset transaction

Example of sponsoring a transaction where a player transfers an NFT.

const transferAssetTxInfo = await hallidayClient.transferAsset({
  from_in_game_player_id: userInGameId,                 // Your user's id in your application
  to_in_game_player_id: "other_player_id",              // Other user's id in your application
  collection_address: "0xeeaf9e39057002eae4bea8bb4e65b01a9cfd59be",
  token_id: "3988",
  
   /**
    * NOTE: This line enables the Halliday paymaster, 
    * allowing you to pay for this user's transaction.
    */
  sponsor_gas: true, 
});
console.log(transferAssetTxInfo.status, transferAssetTxInfo.on_chain_id);

Sponsor a transferBalance transaction

Example of sponsoring a transaction where a player transfers token (i.e. a native token or an ERC20) to another player.

const balanceTransferTxInfo = await hallidayClient.transferBalance({
  from_in_game_player_id: userInGameId,                   // Your user's id in your application
  to_in_game_player_id: "other_player_id",                // Other user's id in your application
  token_address: "0x0799ea468f812e40dbabe77b381cac105da500cd",
  value: "100000000000000000",
 	
   /**
    * NOTE: This line enables the Halliday paymaster, 
    * allowing you to pay for this user's transaction.
    */
  sponsor_gas: true,
});
console.log(balanceTransferTxInfo.status, balanceTransferTxInfo.on_chain_id);

Sponsor a contractCall transaction

Example of sponsoring a transaction where a player is calling an arbitrary smart contract.

// Call an arbitrary contract.
const contractAddress = '0x1f6557356bfb310a556300a36fb18f54fb4791b1';
// The contract's ABI
const contractAbi = [...];                         // Replace with your contract's ABI
// Create an instance of the contract
const contract = new ethers.Contract(contractAddress, contractAbi, signer);
// Get the calldata for a contract call
const calldata = contract.interface.encodeFunctionData('someFunction', ['arg1', 'arg2']);

// Call the contract, with gas sponsored by the paymaster.
const contractCallTxInfo = await hallidayClient.callContract({
  from_in_game_player_id: userInGameId,             // Your user's id in your application
  target_address: contractAddress,
  value: "0",
  calldata,
  
   /**
    * NOTE: This line enables the Halliday paymaster, 
    * allowing you to pay for this user's transaction.
    */
  sponsor_gas: true,
});
console.log(contractCallTxInfo.status, contractCallTxInfo.on_chain_id);