Introduction to Gas Station Network

Published by Marianne Poser on

The Gas Station Network (GSN) is a decentralized meta-transaction framework. It allows users without Ether to make an Ethereum call into the blockchain by having a third party pay the gas costs. This way, a DApp can be accessed by more users who have been deterred by buying Cryptocurrencies and the associated KYC Process. These users could pay with fiat or token in the application, but would not need Ether for the transaction fees. 

The protocol uses a set of public contracts. The decentralized network consists of relay nodes, which pay for the transactions and get compensated by a paymaster contract. The relay nodes listen on non-Ethereum interfaces and are rewarded for forwarding transactions. The protocol tries to operate with as little trust between components as possible. However, since not all attacks can be prevented directly in smart contracts, further precautions have been implemented. The relay nodes have to stake a deposit and watchdog the behavior of each other. For example, if they catch a node submitting two different transactions with the same nonce or with the wrong parameters, they get half of its stake (the other half is burnt). 

In addition to the relay nodes (Relayer) and the Paymaster contract (Paymaster), the protocol works with a RelayHub contract (RelayHub) and a TrustedForwarder contract (Forwarder). Both contracts are un-owned, un-modifiable contracts with audited code and can be verified by anyone. The Paymaster contract must be implemented for each application itself. It includes all access control and gas refund logic in two main functions. In the preRelayedCall-Function, a whitelist or balance sheet for users or transactions can be used and it is also possible to apply an (off-chain)approval to decide whether the fees for a transaction are taken over. This function is called by RelayHub before it makes the relayed call. In this contract, a small balance of ETH as gas prepayment should be deposited, and the owner of the contract should ensure a sufficient balance for the next transactions regularly.

The actual smart contract, with which the user should be able to interact without paying gas, is called RelayRecipient in the GSN. In oder to support meta transactions, this contract inherits from a simple base class and replaces all msg.sender and msg.data with _msgSender() and _msgData().

In addition to these two contracts, which must be implemented or modified to use the GSN, it can be useful to set up an app-owned relay node. This way, the fees for using third-party nodes do not accrue until the own relay node can no longer process the transactions. 

Source: https://docs.opengsn.org/assets/img/gsn_flow_full_layered.737dd3e4.jpg

Using the GSN protocol and network, the following procedure occurs when a gasless user wants to call a function in the RelayRecipient Contract:

1. The User signs a message with the necessary information about the transaction they would like to execute and sends this meta transaction to a relay node (3rd- party or app-owned).

2. The relay node checks if the fees will be refunded by the paymaster belonging to the RelayRecipient Contract, and then it sends the transaction to the RelayHub and pays for the gas cost.

3. The RelayHub ensures that the Paymaster pays back the relay server for gas fees and transaction fees by calling the preRelayedCall in the Paymaster Contract. Then it relays the transaction to the Forwarder.

4. The TrustedForwarder Contract validates the signature of the user and the uniqueness of the nonce of the transaction. Then it calls the target method in the RelayRecipient Contract and updates the nonce to prevent replaying the same request.

5. After the actual process has been completed, the second function “postRelayedCall” in the Paymaster can be called (if implemented), and the user can be informed about the success of his transaction by the Relay Hub.

For the exact process, further information and implementation guidance, you can visit the following sources:

• https://github.com/opengsn/gsn-protocol/blob/master/gsn-protocol.md

• https://github.com/opengsn/workshop

• https://docs.opengsn.org/

Categories: Blog

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *