Make a Transfer

A transfer involves one wallet sending tokens to another wallet on the OMG Network.

Implementation

1. Install omg-js, bn.js

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

Node
Browser
React Native
Node

Requires Node >= 8.11.3 < 13.0.0

npm install @omisego/omg-js bn.js
Copy
Browser

You can add omg-js and bn-js to a website using a script tag:

<script src="https://unpkg.com/@omisego/browser-omg-js"></script>
React Native

You can easily integrate omg-js with React Native projects. First, add this postinstall 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

Transferring funds to the OMG Network involves using ChildChain and OmgUtil omg-js objects. Here's an example of how to instantiate them:

import BigNumber from "bn.js";
import { ChildChain, OmgUtil } from "@omisego/omg-js";const plasmaContractAddress = plasmaContractAddress;
const childChain = new ChildChain({
watcherUrl: watcherUrl,
watcherProxyUrl: '',
plasmaContractAddress: plasmaContractAddress
});
const ethTransfer = {
sender: "0x8CB0DE6206f459812525F2BA043b14155C2230C0",
senderPrivateKey: "CD55F2A7C476306B27315C7986BC50BD81DB4130D4B5CFD49E3EAF9ED1EDE4F7",
receiver: "0xA9cc140410c2bfEB60A7260B3692dcF29665c254",
currency: OmgUtil.transaction.ETH_CURRENCY,
feeCurrency: OmgUtil.transaction.ETH_CURRENCY,
amount: new BigNumber("12000000000000000"),
metadata: "eth transfer"
}
const erc20Transfer = {
sender: "0x8CB0DE6206f459812525F2BA043b14155C2230C0",
senderPrivateKey: "CD55F2A7C476306B27315C7986BC50BD81DB4130D4B5CFD49E3EAF9ED1EDE4F7",
receiver: "0xA9cc140410c2bfEB60A7260B3692dcF29665c254",
currency: OmgUtil.hexPrefix("0xd92e713d051c37ebb2561803a3b5fbabc4962431"),
feeCurrency: OmgUtil.transaction.ETH_CURRENCY,
amount: new BigNumber("3000000"),
metadata: "TUSDT transfer"
}
  • watcherUrl - the Watcher Info URL for defined environment (personal or from OMG Network).

  • plasmaContractAddress - CONTRACT_ADDRESS_PLASMA_FRAMEWORK for defined environment.

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.

3. Send a payment transaction

Transactions are composed of inputs and outputs. An input is simply a pointer to the output of another transaction. An output is a transaction that hasn't been spent yet (also known as UTXO). Each transaction should be signed by the owner of funds (UTXOs), have a specific format, and encoded with RLP encoding according to the following rules:

[txType, inputs, outputs, txData, metaData]txType ::= uint256
inputs ::= [input]
input ::= bytes32
outputs ::= [output]
output ::= [outputType, outputData]
outputType ::= uint256
outputData ::= [outputGuard, token, amount]
outputGuard ::= bytes20
token ::= bytes20
amount ::= uint256
txData ::= uint256 (must be 0)
metadata ::= bytes32

Transactions are signed using the EIP-712 method. The EIP-712 typed data structure is defined as follows:

{
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'verifyingContract', type: 'address' },
{ name: 'salt', type: 'bytes32' }
],
Transaction: [
{ name: 'txType', type: 'uint256' },
{ name: 'input0', type: 'Input' },
{ name: 'input1', type: 'Input' },
{ name: 'input2', type: 'Input' },
{ name: 'input3', type: 'Input' },
{ name: 'output0', type: 'Output' },
{ name: 'output1', type: 'Output' },
{ name: 'output2', type: 'Output' },
{ name: 'output3', type: 'Output' },
{ name: 'txData', type: 'uint256' },
{ name: 'metadata', type: 'bytes32' }
],
Input: [
{ name: 'blknum', type: 'uint256' },
{ name: 'txindex', type: 'uint256' },
{ name: 'oindex', type: 'uint256' }
],
Output: [
{ name: 'outputType', type: 'uint256' },
{ name: 'outputGuard', type: 'bytes20' },
{ name: 'currency', type: 'address' },
{ name: 'amount', type: 'uint256' }
]
},
domain: {
name: 'OMG Network',
version: '1',
verifyingContract: '',
salt: '0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83'
},
primaryType: 'Transaction'
}

Note, the Childchain server collects fees for sending a transaction. The fee can be paid in a variety of supported tokens by the Network. For more details on how the fees are defined, please refer to Fees.

3.1 Method A

The most "granular" implementation of transfer includes creating, typing, signing, and submitting the transaction. Such an approach will have the following structure of the code:

This method demonstrates a transfer made in ETH. If you want to make an ERC20 transfer, replace erc20Transfer with the corresponding ethTransfer values.

async function transfer() {
const transactionBody = await childChain.createTransaction({
owner: erc20Transfer.sender,
payments: [
{
owner: erc20Transfer.receiver,
currency: erc20Transfer.currency,
amount: erc20Transfer.amount,
},
],
fee: {
currency: erc20Transfer.feeCurrency,
},
metadata: erc20Transfer.metadata,
});
const typedData = OmgUtil.transaction.getTypedData(
transactionBody.transactions[0], plasmaContractAddress);
const privateKeys = new Array(
transactionBody.transactions[0].inputs.length
).fill(ethTransfer.senderPrivateKey);
const signatures = childChain.signTransaction(typedData, privateKeys);
const signedTypedData = childChain.buildSignedTransaction(typedData, signatures);
const receipt = await childChain.submitTransaction(signedTypedData);
return receipt;
}