Fee relay transactions are those where fees are paid by another address. This can be useful when you split wallets that contain fees and payment funds. The process requires the construction of a custom transaction body that needs to undergo several manual checks due to certain features of the OMG Network.
Make a fee relay transaction on the OMG Network involves using ChildChain omg-js object. Here's an example of how to instantiate it:
import BN from "bn.js";
import { ChildChain, OmgUtil } from "@omisego/omg-js";const childChain = new ChildChain({ watcherUrl });
const ethUtil = require('ethereumjs-util');
const sigUtil = require('eth-sig-util');
watcherUrl - the Watcher Info URL for defined environment (personal or from OMG Network).
There are several ways to send a transaction on the OMG Network. We recommend using the first method but you may want to choose another approach for your specific use case.
UTXO helper filters UTXOs that can be used during a transaction. Currently, the OMG Network has a limitation of only 4 inputs and 4 outputs, thus you can have a maximum of 3 payment inputs and reserve the last one for a fee input. For simplicity purposes, we select UTXOs that have an amount that is greater or equal to the amount requested in a transaction.
UTXO change helper checks if the provided UTXO (payment or fee) needs a change. If the change is needed, the helper creates and pushes an additional output to the existing array of transaction outputs.
const checkUtxoForChange = (address, utxo, amount, transactionBody) => {
if (!utxo || utxo.length === 0) {
throw new Error(`No UTXO provided for ${address}`);
} if (transactionBody.outputs.length > 4) {
throw new Error(`The provided transaction body has 4 outputs. You need to have at least 1 spare output to proceed.`);
} if (utxo.amount.gt(amount)) {
const changeAmount = utxo.amount.sub(amount);
const changeOutput = {
outputType: 1,
outputGuard: address,
currency: utxo.currency,
amount: changeAmount
}
transactionBody.outputs.push(changeOutput);
}
}
Signature helper
Signature helper signs and returns the inputs by a sender and fee payer.
To construct a relay transaction, you need to create a custom transaction body. It should contain details about the sender, receiver, fee, fee payer, additional metadata.
The process of submitting a relay transaction is the same as submitting any other transaction, except you pass a custom transaction body created earlier.
NOTE, you can create a custom logic and include up to 3 payment inputs. This sample demonstrates the easiest option where you have only 1 payment input and 1 fee input.