OMG Network Docs
DocsAPIsEnvironmentsProduct
  • OMG Network Documentation
  • Table of Contents
  • Network
    • Getting Started
    • Retrieve Balances
    • Deposit Funds
    • Make a Transfer
    • Make an Atomic Swap
    • Make a Fee Relay Transfer
    • Start a Standard Exit
    • Start an In-flight Exit
    • Process an Exit
    • Manage UTXO
    • Challenge an Exit
  • Use Cases
    • Community Points Engine
    • Exchange Use Case
    • Wallet Use Case
  • Wallet
    • Web Wallet Quick Start
  • Contracts
    • MoreVP Technical Overview
    • Plasma Proposition
    • Smart Contract Administrative Accounts
  • Watcher
    • How to Run a Watcher
    • How to Manage a VPS
    • How to Manage a Watcher
  • Security
    • Bug Bounty Program
    • Plasma Contracts Audits
  • 3rd-Party
    • MetaMask
    • MultiBaas by Curvegrid
  • Concepts
    • Blockchain Design
    • Byzantine Conditions
    • Network Architecture
    • Challenge Period
    • Exit Bonds
    • Fees
  • Resources
    • Glossary
    • FAQ
    • Environments
    • API References
    • Release Notes
  • Migration Guides
    • Introducing v0.3 (ODP Edition) · OMG Network
    • v0.4 · OMG Network
Powered by GitBook
On this page
  • Merging UTXOs
  • Implementation
  • 1. Install omg-js
  • 2. Import dependencies, define constants
  • 3. Merge UTXOs
  • Lifecycle
  • Splitting UTXOs
  • Network Considerations
  • Demo Project
  • JavaScript

Was this helpful?

  1. Network

Manage UTXO

PreviousProcess an ExitNextChallenge an Exit

Last updated 4 years ago

Was this helpful?

are core to the logic of the OMG Network.

Merging UTXOs

Merging UTXOs is desirable in the following scenarios:

1. Exiting your funds as a single UTXO

Standard Exits are initiated on a single UTXO and not a specified amount. However, a user may want to exit an amount greater than the value of any UTXO one owns. For example:

  • Alice owns 4 UTXOs worth 1 ETH each.

  • Alice would like to withdraw all her funds from the network.

  • To exit her funds, Alice would need to initiate a Standard Exit (with an exit bond) on each UTXO.

In the above scenario, it would be more economical for Alice to merge these UTXOs and exit a single one.

2. Fitting Inputs into a Transaction

A transaction can have a maximum of four inputs but a user may not own four UTXOs that can cover the amount needed for a given transaction. For example:

  • Alice owns 5 UTXOs worth 1 ETH each.

  • Alice would like to send 5 ETH to Bob.

  • Alice cannot send 5 ETH to Bob in a single transaction as a transaction can take four inputs only.

In the above scenario, Alice can merge her UTXOs to send the desired amount to Bob in a single transaction.

Implementation

To access network features from your application, use our official libraries:

Requires Node >= 8.11.3 < 13.0.0

npm install @omisego/omg-js

Add omg-js to a website using a script tag:

Integrate omg-js with React Native projects. First, add this post-install script to your project's package.json:

"scripts": {
    "postinstall": "omgjs-nodeify"
}

Then install the react native compatible library:

npm install @omisego/react-native-omg-js

2. Import dependencies, define constants

Merging UTXOs involves using 2 omg-js objects. Here's an example of how to instantiate them:

import { ChildChain, OmgUtil } from "@omisego/omg-js";
const childChain = new ChildChain({ watcherUrl });
const plasmaContractAddress = plasmaContractAddress;const utxoMerge = {
  currency: OmgUtil.transaction.ETH_CURRENCY,
  address: "0x8CB0DE6206f459812525F2BA043b14155C2230C0",
  privateKey: "CD55F2A7C476306B27315C7986BC50BD81DB4130D4B5CFD49E3EAF9ED1EDE4F7"
}

3. Merge UTXOs

Note, the minimum number of UTXOs to merge is 2, the maximum — 4. You also can't mix ETH and ERC20 UTXOs while performing merging.

The merging UTXOs process is the same for both ETH and ERC20. This method demonstrates merging for ETH UTXOs. If you want to merge ERC20 tokens, change the currency value to a corresponding smart contract address.

async function mergeUtxo() {
  
  const addressUtxosAll = await childChain.getUtxos(utxoMerge.address);    
  const addressUtxos = addressUtxosAll.filter(
    (u) => u.currency === utxoMerge.currency
  );  
  const utxosToMerge = addressUtxos.slice(0, 4);  
  const mergedUtxo = await childChain.mergeUtxos({
    utxos: utxosToMerge,
    privateKey: utxoMerge.privateKey,
    verifyingContract: plasmaContractAddress,
  });  return utxo;
}

No fee is charged for merge transactions on the OMG Network since these are transactions that benefit the network.

Lifecycle

  1. A user calls the getUtxos function to retrieve the list of all available UTXOs.

  2. A user filters an array of UTXOs and returns an UTXOs for the desired currency (ETH_CURRENCY for ETH or ERC20_CONTRACT_ADDRESS for ERC20 tokens). This step is important because you can't merge ETH and ERC20 tokens together.

  3. A user calls the mergeUtxos function and returns an array of merged UTXOs.

Splitting UTXOs

A user may also want to split UTXOs if one would like to exit an amount smaller than the value of any UTXO one owns. For example:

  • Alice owns 1 UTXO worth 5 ETH.

  • Alice wants to withdraw 2 ETH back onto the root chain and keep 3 ETH on the child chain.

  • Alice cannot exit 2 ETH unless she splits her UTXO.

In the above scenario, Alice can split her UTXO to withdraw 2 ETH.

Network Considerations

Demo Project

This section provides a demo project that contains a detailed implementation of the tutorial. If you consider integrating with the OMG Network, you can use this sample to significantly reduce the time of development. It also provides step-by-step instructions and sufficient code guidance that is not covered on this page.

JavaScript

For running a full omg-js code sample for the tutorial, please use the following steps:

git clone https://github.com/omgnetwork/omg-js-samples.git
  1. Run these commands:

npm install
npm run start

Code samples for all tutorials use the same repository — omg-js-samples, thus you have to set up the project and install dependencies only one time.

1. Install

watcherUrl - the Watcher Info URL for defined (personal or from OMG Network).

plasmaContractAddress - CONTRACT_ADDRESS_PLASMA_FRAMEWORK for defined .

Currently, UTXO splitting is not available via omg-js lib but you can use the OMG Network to achieve the same result.

Users are highly encouraged to merge UTXOs continuously as a way of mitigating the vulnerability of OMG Network funds in a mass exit event. Read more in the .

Clone repository:

Create .env file and provide the .

Open your browser at .

Select , or on the left side, observe the logs on the right.

UTXOs
omg-js
environment
environment
Web Wallet
FAQ Section
omg-js-samples
required configuration values
http://localhost:3000
Show UTXOs
Merge UTXOs
Split UTXOs