EmerisHQ / demeris-backend

Monorepo containing all the Demeris backend code and infrastructure definitions.
GNU Affero General Public License v3.0
8 stars 1 forks source link

Metatransactions backend functionality #350

Open gamarin2 opened 2 years ago

gamarin2 commented 2 years ago

Context

Metatransactions are coming to the Cosmos SDK to solve a fundamental issue with cross-chain UX: transactions fees. Each chain accepts only a restricted set of denoms to pay for transactions fees (e.g. ATOM on the Cosmos Hub, AKT on Akash, LUNA and UST on Terra, etc.). The problem is that users generally own a few assets, and not necessarily the ones they need to pay for fees. For example, if a user does not own ROWAN, they can't trade using Sifchain DEX.

To solve this, the SDK will introduce a new type of transaction: metatransactions. Metatransactions are transactions that are signed by the end-user without a transaction fee. Instead of a fee, they can add a tip in whatever currency they own. Then, anyone (in most cases, it will be a backend service that we operate) can pick it up, add a transaction fee countersign the tx and broadcast it. When the tx is processed on chain, whoever signed the tx fee will automatically claim the provided tip.

Example: User A wants to do a simple swap on Sifchain DEX, but they only own ATOM. In this example, we assume the standard tx fee on Sifchain is 0.1 ROWAN, worth $0.1

(frontend)

  1. User A creates a metatransaction for the swap, and adds a tip of 0.05 ATOM, worth $0.11.
  2. User A signs the metatransaction, which is sent to Emeris backend.

(backend)

  1. The metatx functionality in Emeris backend picks up the tx, checks that the tip is sufficient.
  2. The metatx function adds a transaction fee of 0.1 ROWAN, countersigns the tx.
  3. The tx is broadcasted to Sifchain
  4. Transaction is processed, Emeris backend balance looses 0.1 ROWAN and gains 0.05 ATOM.

Description of the Metatx Backend Service

First, we will need a separate endpoint for broadcasting metatxs from the frontend. This endpoint relays the metatx to the Metatx Backend Service (called Service in the rest of this issue), which processes it.

In the first version, the Service will do the following:

  1. Run /simulate on the tx, which checks validity of the tx and returns gas estimation.
  2. If /simulate returns without error, fetch the fee token for the chain the tx needs to be submitted to from the CNS, as well as the gas-price for this token.
  3. Multiply the gas value returned by /simulate with the gas-price obtained previously. Convert the value to dollars using the price oracle.
  4. Convert the value of the tip provided in the metatx in dollars using the price oracle.
  5. If the dollar value of the tip is greater than the dollar value obtained at step 3., append the fees to the metatx using https://github.com/cosmos/cosmos-sdk/pull/10311 (use the gas and gas-prices values of step 3).
  6. Append our signature to the tx.
  7. Broadcast the tx to the chain.

Evolution of the Backend Service

In later versions, we will want to make this service a P2P service, i.e. enable third parties to act as metatx relayers themselves. This is needed primarily for legal reasons.

In essence, we "just" need to add a step 2.5. to the process above:

2.5. broadcast raw metatx to peers

Obviously, we would need to open source this service as a standalone software.

akc2267 commented 2 years ago

bug on tx flow is blocking this - it's reading 2 signatures but only 1 given - should be fixed in v46