How to free CPU/NET for an EOS transaction

As we know that each transaction on EOS needs the amount of EOS tokens fee for CPU/NET. In which case of our business cases, we want to take care of the fee for all users. That’s meant the user does not care about the transaction fee each time they go to our dApp and make transactions. Today, I give you a guide to performing it by following steps:

  • Make an action which is called payforcpunet (or whatever name you want) on your smart contract.
[[eosio::action]] void payforcpunet(const name& recipient) {
      require_auth(get_self());
}
  • After you deployed your smart contract. You have to add a new payforcpunet permission for your EOS account (The EOS account you used for deploying the smart contract).
  • Almost done, at your backend side, each time you want to do your main action, you should include payforcpunet action on it (that’s mean we are using multiple actions on a transaction).

 const actionParams = {
            actions: [
                {
                    account: `${your EOS account}`,
                    name: 'payforcpunet',
                    authorization: [{
                        actor: `${your EOS account}}`,
                        permission: `payforcpunet`
                    }],
                    data: {recipient: actor}
                },
                {
                    account: `${your user account}`,
                    name: 'transfer',
                    authorization: [{
                        actor: `${your user account}`,
                        permission: 'active'
                    }],
                    data:  {
                        from: `${your user account}`,
                        to: `${your EOS account}}`,
                        quantity: "1.0000 TEST",
                        memo: "Covering CPU and NET for this transaction"
                    }
                },
                {
                    account: `${your user account}`,
                    name: `${action name},
                    authorization: [{
                        actor: `${your user account}`,
                        permission: active
                    }],
                    data : {...your action data here...}
                }
            ]
await this.eos.transact(
                actionParams,
                {
                    blocksBehind: 3,
                    expireSeconds: 60
                })

Well done!
Enjoy your coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s