Home

Charge a fixed amount of ERC20 for gas payment

Learn how to charge fixed amount of ERC20 tokens to pay for gas fees.

Another interesting option Openfort powers is charging in-game (ERC20) tokens as a form of payment for transactions (ideal if you want to show your users a fix amount per gas no matter the real gas fee)..

By charging your users an X amount of ERC20, you'll be able to extract that in-game currency from the user, while under the hood, the transaction is sponsored with native tokens.

What you'll learn:#

  • How to charge ERC20 tokens to users to make a minting NFT transaction.
1

Select the contract you want to interact with

  • Add the collectible's smart contract.
  • Define the smart contract function you want to use. In our case 'mint'.
command-line

_10
curl https://api.openfort.xyz/v1/contracts \
_10
-u "$YOUR_SECRET_KEY:" \
_10
-d 'name=NFT Contract' \
_10
-d 'chainId=80002' \
_10
-d 'address=contract address'

2

Set up a Fixed ERC20 Policy

  • Create a new policy and select the strategy "charge fixed amount of ERC20".
  • Select the ERC20 Token Contract and the Tokens per Txn in WEI.
  • Select imported contract and the function you want to sponsor.
command-line

_10
curl https://api.openfort.xyz/v1/policies \
_10
-u "$YOUR_SECRET_KEY:" \
_10
-d 'name=My Policy' \
_10
-d 'chainId=80002' \
_10
-d 'strategy[0][sponsorSchema]=fixed_rate' \
_10
-d 'strategy[0][tokenContract]=con_...' \
_10
-d 'strategy[0][tokenContractAmount]=WEI'

command-line

_10
curl https://api.openfort.xyz/v1/policies/:id/policy_rules \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d type="contract_functions" \
_10
-d functionName="All functions" \
_10
-d contract="con_..."

3

Create a gasless transaction

  • Add chainId
  • Add contract
  • Add policy
  • Add function you want to interact with
  • Add arguments (in the case of mint, '_to' the person who receives the asset)

Note you can make a transaction happen without the need to have a registered user or account deployed at the time of interacting. Once you make the transaction a playerId and accountId will be created.

command-line

_10
curl https://api.openfort.xyz/v1/transaction_intents \
_10
-u "$YOUR_SECRET_KEY:" \
_10
-d 'chainId=80002' \
_10
-d 'optimistic=true' \
_10
-d 'policy=pol_...'
_10
-d 'interactions[0][contract]=con_...' \
_10
-d 'interactions[0][functionName]=mint' \
_10
-d 'interactions[0][functionArgs][0]=sender address or Id'

4

Optional: Add the Account or Player you're using

  • Add account or add player
command-line

_10
curl https://api.openfort.xyz/v1/transaction_intents \
_10
-u "$YOUR_SECRET_KEY:" \
_10
-d 'chainId=80002' \
_10
-d 'policy=pol_...' \
_10
-d 'account=acc...' or 'player=pla...' \
_10
-d 'optimistic=true' \
_10
-d 'interactions[0][contract]=con_...' \
_10
-d 'interactions[0][functionName]=mint' \
_10
-d 'interactions[0][functionArgs][0]=sender address or Id'