nearprotocolnear-sdk-rsnear-api-js

nearprotocol - best way to pay transaction fees for contract users


I want to pay all costs for some contract users (probably forever). The idea is to incorporate more users in NEAR ecosystem and provide them the ability to use my contract for free for easier onboarding.

The scenario similar to the one described in the docs:

https://docs.near.org/docs/concepts/gas#what-about-prepaid-gas

I've read and re-read this section several times and still don't get how exactly to achieve this.

Let's take this pseudo-contract:

impl Pseudo {
  pub fn do_something(&mut self) {
    ... does something valuable ...
  }
}

How to pay gas fees for the user given that the user calls my near-sdk-rs contract from their own account using near-api-js? Please, provide some example(s). The goal is to make calling pseudo.do_something() completely free for the user.


Solution

  • What you can do is generate a set of keypairs locally and then add them all as function call access keys to your contract using something like this. This will allow people with the keypair to sign transactions (as your contract) and you can specify which methods they can call on the contract itself.

    Doing this will require you to sign a txn in order to add the access key to your contract but you can do this infrequently and add hundreds (or more) keys at once. Users can then navigate to your dApp and behind the scenes, they use those access keys to sign transactions on your contract (acting as your contract as well).