DEV Community

Cover image for How to use Tatum's gas pump to save on gas fees
evanVtatum
evanVtatum

Posted on

How to use Tatum's gas pump to save on gas fees

If you’ve built or want to build a custodial exchange like Binance or Kraken, a custodial wallet service, or a custodial NFT marketplace, there’s one major headache that you’re bound to encounter: how do you pay the gas fees for sending tokens from your users’ accounts?

Every time a user wants to send tokens from their account, you have to calculate the gas fees, send that amount to their account (a transaction that also incurs gas fees), and send the tokens on their behalf. Two transactions, both of which incur gas fees and can result in minuscule differences of crypto “dust” remaining in user accounts. If you have thousands or millions of addresses, this method creates an enormous amount of work for you as a developer, and astronomical gas fees to run everything.

custodial multiwallet current method.png

It can be a huge headache. But there's a workaround that simplifies the whole process and significantly reduces gas fees.

A smart contract instead of an address

Tatum's gas pump uses smart contracts that function as user wallets and automatically deduct gas fees from a master address. Every time you send tokens from a user wallet, the gas fees are deducted from the master balance, eliminating the need to send crypto to each user address to pay for gas fees. The smart contracts are inexpensive to deploy and pay for themselves in saved gas fees after transferring 1.5 ERC-20 tokens.

Here’s an example of the workflow:
tables_custodial_multi_wallet-16.png
tables_custodial_multi_wallet-17.png
tables_custodial_multi_wallet-18.png

Tatum's gas pump is available on the following blockchains:

  • Ethereum
  • Binance Smart Chain
  • Celo
  • Polygon (MATIC)
  • Tron

How to do it

In this guide, we'll be using REST API calls to perform operations. However, the same operations can be performed with Tatum's JavaScript library. Download and install the SDK here.

1. First, you’ll need to deploy a smart contract for each user.

This API call will do it for you. Each smart contract functions as a wallet/user address that can accept ERC-20, ERC-721, ERC-721, and native assets on the blockchain you deploy it on.

In addition, any combination of tokens can be transferred from a wallet in a batch transaction. This is significantly more efficient and saves enormously on gas fees compared to transferring assets one by one.

Let's deploy 200 gas pump wallets with one API call:

curl --location --request POST 'https://api-eu1.tatum.io/v3/blockchain/sc/custodial/batch' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY' \
--data-raw '{
    "owner": "0x80d8bac9a6901698b3749fe336bbd1385c1f98f2",
    "batchCount": 200,
    "chain": "MATIC",
    "fromPrivateKey": "0x37b091fc4ce46a56da643f021254612551dbe0944679a6e09cb5724d3085c9ab"
}'
Enter fullscreen mode Exit fullscreen mode

In the example above, the wallets will be deployed on the Polygon blockchain (as indicated in the by "MATIC" in the chain field). You can deploy on any other blockchain that supports the gas pump functionality by simply changing the name of the chain in the call.

The response will be a transaction ID:

{
    "txId": "0x6b9ecce4bf716a06c8dfad19feea13692b99737dc042ceaa1ca4204fdc1556a5"
}
Enter fullscreen mode Exit fullscreen mode

2. Next, let's get our gas pump wallet addresses.

Next, we'll get the addresses of the gas pump wallets we've just deployed. These will be the addresses you can assign to your users so that they can receive tokens.

We'll use the transaction ID returned from the previous call in the following endpoint to get the addresses of the wallets:

curl --location --request GET 'https://api-eu1.tatum.io/v3/blockchain/sc/custodial/MATIC/0x6b9ecce4bf716a06c8dfad19feea13692b99737dc042ceaa1ca4204fdc1556a5' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY'
Enter fullscreen mode Exit fullscreen mode

The response will be the addresses you generated in the first call:

[
    "0xc83779f2537fd40082c031fcef91bd6557ee2a13",
    "0xb67f45ea6c7466e25635f4154c671955df130977",
    "0x6dcc090c52e6427938c29a5dcf03274e5bdf0630",
    "0x29696548784515d2884fdd09ad7b4e689c56ed3f",
    "0xf12294780cedfa51dee310cba7f3a0968d881246",
    "0xe7b002b13a86d533abb74bbc7d5b6a16af2e6b13",
    "0x88cf6afd1dd665abaa03cac06e6bde554e1fffd5",
    "0xff79ff532723d56eb78c7547b53611e8dc73321e",
    "0x17b862cf61212013602290a2a7a1ee7775b51ff6",
    "0xba33fa91e471780db0e71771fd8af63aba6a1fb2",
    "0xbb5747dc66e35825b9a9da800de15d6743b66b55",
    .
    .
    .
]
Enter fullscreen mode Exit fullscreen mode

3. Now, we'll transfer some tokens.

Transferring tokens from the wallet you have generated can be done with one API call, whether you’re transferring one type of token or multiple types of tokens.

First, let’s look at how to transfer one type of token in the following API call:

curl --location --request POST 'https://api-eu1.tatum.io/v3/blockchain/sc/custodial/transfer' \
--header 'Content-Type: application/json' \
--header 'x-api-key: testnet' \
--data-raw '{
    "chain": "MATIC",
    "contractType": 0,
    "tokenAddress": "0x2d7882bedcbfddce29ba99965dd3cdf7fcb10a1e",
    "custodialAddress": "0x4eC40a4A0dA042d46cC4529f918080957003b531",
    "recipient": "0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA",
    "amount": "0.00006",
    "fromPrivateKey": "0x37b091fc4ce46a56da643f021254612551dbe0944679a6e09cb5724d3085c9ab"
}'
Enter fullscreen mode Exit fullscreen mode

In the call above, the contractType field specifies the type of token to be transferred. In this case, 0 refers to the fungible tokens we enabled in the previous call. Again, the chain is MATIC.

The addresses that must be specified are:

  • tokenAddress — the address from which the tokens will be sent
  • custodialAddress — the address of the wallet
  • recipient — the address to which the tokens will be sent

The amount to be sent is specified in the amount field, and the fromPrivateKey field contains the private key associated with the address.

4. And finally, let’s transfer multiple assets from the wallet.

curl --location --request POST 'https://api-eu1.tatum.io/v3/blockchain/sc/custodial/transfer/batch' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY' \
--data-raw '{
    "chain": "MATIC",
    "contractType": [0,1,2,3],
    "tokenId": ["0","100","1","0"],
    "amount": ["0.001","0","1","0.009"],
    "tokenAddress": [
        "0x2d7882bedcbfddce29ba99965dd3cdf7fcb10a1e",
        "0x6d8eae641416B8b79e0fB3a92b17448CfFf02b11",
        "0x664F97470654e8f00E42433CFFC0d08a5f4f7BC7",
        "0"
    ],
    "custodialAddress": "0x4eC40a4A0dA042d46cC4529f918080957003b531",
    "recipient": [
        "0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA",
        "0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA",
        "0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA",
        "0x8cb76aEd9C5e336ef961265c6079C14e9cD3D2eA"
    ],
    "fromPrivateKey": "0x37b091fc4ce46a56da643f021254612551dbe0944679a6e09cb5724d3085c9ab"
}'
Enter fullscreen mode Exit fullscreen mode

In the example above, all four contract types are enabled in the contractType field:

  • contractType 0 — ERC-20 token
  • contractType 1 — ERC-721 token
  • contractType 2 — ERC-1155 token
  • contractType 3 — native asset — MATIC

In the tokenId field, the token IDs of each respective type of asset are specified. For ERC-20 tokens and the native assets (contract types 0 and 3), the token IDs are 0 because both are fungible tokens and thus do not have unique token IDs.

The amounts of each token to be transferred are specified in the amount field:

  • 0.001 ERC-20 tokens
  • 1 ERC-721 token (the amount is not used for this token type, because each ERC-721 is unique, these the tokenID "100" in the tokenID field is what's relevant).
  • 1 ERC-1155 token with the tokenID 1
  • 0.009 MATIC

Finally, the addresses holding the different tokens are specified in the tokenAddress field, the address of the custodial multi-wallet in the custodialAddress field, and the addresses of the recipients in the recipient field.

The cost of deployment pays itself off quickly.

The cost of deploying one user wallet using the gas pump is the same as the cost of transferring 1.5 ERC-20 tokens. This means that after a user has transferred 1.5 ERC-20 tokens, they have already paid off the cost of the wallets, and from that point on you’ll just be saving on gas fees. When you’re dealing with hundreds, thousands, or millions of addresses, those fees can really add up.

You’ll never have to worry about the logistics of sending the correct amount to user accounts, crypto dust being left over in their wallets, or incurring extra gas fees while sending funds to cover your users’ transactions. The gas pump takes care of all of that for you.

How to get started

This gas pump feature is native to Tatum, so you'll need to sign up for an API key to communicate with our infrastructure. However, you can do everything with a free plan, and you don’t have to upgrade to a paid plan unless you require more than 5 API requests per second. For help getting started, check out our how-to guide.

You can use your API key to make direct REST API calls as we have in this guide, or you can download and install the Tatum JavaScript library here.

For more information about the gas pump feature, please refer to our guide and API documentation.

If you have any questions or need any help, please drop us a line on the Tatum Discord or subreddit, and one of our devs will get back to you asap.

About Tatum

Tatum is a development platform that unifies 40+ blockchain protocols into a single framework, allowing any developer to build apps with no previous blockchain experience. They provide blockchain infrastructure, SDKs, and a REST API, which eliminate many common obstacles to blockchain development. The platform is used by over 12,000 developers from around the world. Apps built on Tatum are used by tens of millions of end-users and process billions of dollars worth of transactions per month.

Top comments (0)