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
Requires Node >= 8.11.3 < 13.0.0
1
npm install @omisego/omg-js bn.js
2
Copy
Copied!
You can add omg-js and bn-js to a website using a script tag:
1
<script src="https://unpkg.com/@omisego/browser-omg-js"></script>
Copied!
You can easily integrate omg-js with React Native projects. First, add this postinstall script to your project's package.json:
1
"scripts": {
2
"postinstall": "omgjs-nodeify"
3
}
Copied!
Then install the react native compatible library:
1
npm install @omisego/react-native-omg-js
Copied!

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:
1
import BigNumber from "bn.js";
2
import { ChildChain, OmgUtil } from "@omisego/omg-js";const plasmaContractAddress = plasmaContractAddress;
3
const childChain = new ChildChain({
4
watcherUrl: watcherUrl,
5
watcherProxyUrl: '',
6
plasmaContractAddress: plasmaContractAddress
7
});
8
9
const ethTransfer = {
10
sender: "0x8CB0DE6206f459812525F2BA043b14155C2230C0",
11
senderPrivateKey: "CD55F2A7C476306B27315C7986BC50BD81DB4130D4B5CFD49E3EAF9ED1EDE4F7",
12
receiver: "0xA9cc140410c2bfEB60A7260B3692dcF29665c254",
13
currency: OmgUtil.transaction.ETH_CURRENCY,
14
feeCurrency: OmgUtil.transaction.ETH_CURRENCY,
15
amount: new BigNumber("12000000000000000"),
16
metadata: "eth transfer"
17
}
18
const erc20Transfer = {
19
sender: "0x8CB0DE6206f459812525F2BA043b14155C2230C0",
20
senderPrivateKey: "CD55F2A7C476306B27315C7986BC50BD81DB4130D4B5CFD49E3EAF9ED1EDE4F7",
21
receiver: "0xA9cc140410c2bfEB60A7260B3692dcF29665c254",
22
currency: OmgUtil.hexPrefix("0xd92e713d051c37ebb2561803a3b5fbabc4962431"),
23
feeCurrency: OmgUtil.transaction.ETH_CURRENCY,
24
amount: new BigNumber("3000000"),
25
metadata: "TUSDT transfer"
26
}
Copied!
    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:
1
[txType, inputs, outputs, txData, metaData]txType ::= uint256
2
inputs ::= [input]
3
input ::= bytes32
4
outputs ::= [output]
5
output ::= [outputType, outputData]
6
outputType ::= uint256
7
outputData ::= [outputGuard, token, amount]
8
outputGuard ::= bytes20
9
token ::= bytes20
10
amount ::= uint256
11
txData ::= uint256 (must be 0)
12
metadata ::= bytes32
Copied!
Transactions are signed using the EIP-712 method. The EIP-712 typed data structure is defined as follows:
1
{
2
types: {
3
EIP712Domain: [
4
{ name: 'name', type: 'string' },
5
{ name: 'version', type: 'string' },
6
{ name: 'verifyingContract', type: 'address' },
7
{ name: 'salt', type: 'bytes32' }
8
],
9
Transaction: [
10
{ name: 'txType', type: 'uint256' },
11
{ name: 'input0', type: 'Input' },
12
{ name: 'input1', type: 'Input' },
13
{ name: 'input2', type: 'Input' },
14
{ name: 'input3', type: 'Input' },
15
{ name: 'output0', type: 'Output' },
16
{ name: 'output1', type: 'Output' },
17
{ name: 'output2', type: 'Output' },
18
{ name: 'output3', type: 'Output' },
19
{ name: 'txData', type: 'uint256' },
20
{ name: 'metadata', type: 'bytes32' }
21
],
22
Input: [
23
{ name: 'blknum', type: 'uint256' },
24
{ name: 'txindex', type: 'uint256' },
25
{ name: 'oindex', type: 'uint256' }
26
],
27
Output: [
28
{ name: 'outputType', type: 'uint256' },
29
{ name: 'outputGuard', type: 'bytes20' },
30
{ name: 'currency', type: 'address' },
31
{ name: 'amount', type: 'uint256' }
32
]
33
},
34
domain: {
35
name: 'OMG Network',
36
version: '1',
37
verifyingContract: '',
38
salt: '0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83'
39
},
40
primaryType: 'Transaction'
41
}
Copied!
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.
1
async function transfer() {
2
3
const transactionBody = await childChain.createTransaction({
4
owner: erc20Transfer.sender,
5
payments: [
6
{
7
owner: erc20Transfer.receiver,
8
currency: erc20Transfer.currency,
9
amount: erc20Transfer.amount,
10
},
11
],
12
fee: {
13
currency: erc20Transfer.feeCurrency,
14
},
15
metadata: erc20Transfer.metadata,
16
});
17
18
const typedData = OmgUtil.transaction.getTypedData(
19
transactionBody.transactions[0], plasmaContractAddress);
20
const privateKeys = new Array(
21
transactionBody.transactions[0].inputs.length
22
).fill(ethTransfer.senderPrivateKey);
23
const signatures = childChain.signTransaction(typedData, privateKeys);
24
const signedTypedData = childChain.buildSignedTransaction(typedData, signatures);
25
const receipt = await childChain.submitTransaction(signedTypedData);
26
return receipt;
27
}
Copied!
Last modified 8mo ago