Tutorial: pay for users' gas on NEAR Protocol
Volnov Peter
Posted on July 4, 2023
NEAR Protocol has introduced an exceptional feature since its version 0.17.0: Delegate Transactions. With this feature, you can create and sign a transaction from one account and send it on behalf of another account. Essentially, you pay for the gas for a third-party account.
This tutorial will guide you through creating and executing Delegate Transactions on NEAR Protocol using Python.
Prerequisites:
- Familiarity with Python
- A NEAR Protocol account
- Python NEAR library (
py_near
)
Step-by-Step Guide
Delegate Transactions are performed in three steps:
- Alice creates a
delegate_action
specifying all the actions, public key, and recipient. - Alice signs
delegate_action
with her private key and passes it to Bob. - Bob forms a normal transaction from one action -
delegate_action
, signs it, and sends it to the blockchain. Essentially, Alice's transaction is executed, but Bob pays for the gas.
Creating and Signing a Delegate Transaction
The following Python function creates and signs a delegate transaction. Follow the steps below:
- Install the
py_near
Python library. You can install it via pip:
pip install py_near
- Run the following Python code:
from py_near.account import Account
from py_near_primitives import TransferAction
import ed25519
async def create_and_sign_delegate():
account = Account(
"alice.near",
"ed25519::...",
"https://nrpc.herewallet.app",
)
action = await account.create_delegate_action(actions=[TransferAction(1)], receiver_id="bob.near")
sign = account.sign_delegate_transaction(action)
account_to_execute = Account(
"bob.near",
"ed25519:...",
"https://nrpc.herewallet.app",
)
result = account_to_execute.call_delegate_transaction(
delegate_action=action,
signature=sign,
)
return result
create_and_sign_delegate()
In this example, we transfer 1 yNEAR from alice.near
to bob.near
and pay for gas from bob.near
balance.
Note: Replace ed25519 public and private keys in the Account objects with your actual account keys. You also need to replace the receiver_id value in the
create_delegate_action
method with the actual NEAR Protocol account ID where you want to send the transaction.
Warning: Not all RPC support delegate transactions. You can use https://nrpc.herewallet.app for testing.
Executing the Delegate Transaction
To execute the delegate transaction, use the call_delegate_transaction
method as in the Python code above. This method requires two parameters:
-
delegate_action:
The DelegateActionModel object you created in the previous step. -
signature:
The signature of the delegate transaction that you created in the previous step.
You can execute this transaction from any NEAR Protocol account. In the example above, the transaction is executed from the bob.near
account.
And that's it! You have successfully created and executed a Delegate Transaction on NEAR Protocol, covering the gas costs on behalf of another user.
Posted on July 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.