initial commit

This commit is contained in:
emizzle 2019-05-30 20:00:31 +07:00
commit 8036986f65
No known key found for this signature in database
GPG Key ID: 1FD4BAB3C37EE9BA
10 changed files with 11759 additions and 0 deletions

18
.babelrc Normal file
View File

@ -0,0 +1,18 @@
{
"comments": false,
"compact": false,
"env": {
"node": {
"plugins": [
["@babel/plugin-transform-runtime", {
"corejs": 2
}]
],
"presets": [
["@babel/env", {
"targets": {"node": "8.11.3"}
}]
]
}
}
}

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
dist
package
.idea
.vscode
.eslintrc.json
embark-omg-*.tgz
NOTES
npm-debug.log
TODO

2
.npmrc Normal file
View File

@ -0,0 +1,2 @@
engine-strict = true
save-exact = true

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 Iuri Matias
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# Embark-Omg Setup
Embark-omg is a plugin for [Embark](https://github.com/embark-framework/embark) that ...
#### Please report any other issues you find, thank you!

55
package.json Normal file
View File

@ -0,0 +1,55 @@
{
"name": "embark-omg",
"version": "1.0.0",
"description": "OmiseGO plugin for Embark",
"main": "dist/index.js",
"files": [
"dist",
"src"
],
"scripts": {
"lint": "./node_modules/.bin/eslint src/",
"babel": "npm run babel:node",
"babel:node": "cross-env BABEL_ENV=node babel --out-dir dist src --source-maps",
"build": "npm run clean && npm run babel",
"clean": "rimraf dist embark-status-*.tgz package",
"prepare": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/emizzle/embark-omg.git"
},
"keywords": [
"Embark",
"OmiseGO",
"blockchain"
],
"author": "eric.mastro@gmail.com",
"license": "ISC",
"bugs": {
"url": "https://github.com/emizzle/embark-omg/issues"
},
"homepage": "https://github.com/emizzle/embark-omg#readme",
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"@omisego/omg-js": "1.2.2",
"@omisego/omg-js-childchain": "1.2.1",
"@omisego/omg-js-rootchain": "1.2.2",
"@omisego/omg-js-util": "1.2.1",
"embark-utils": "^4.1.0-beta.2",
"ethers": "4.0.28"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"@babel/plugin-transform-runtime": "7.4.0",
"@babel/preset-env": "7.4.1",
"cross-env": "5.2.0",
"eslint": "4.19.1",
"rimraf": "2.6.2"
},
"engines": {
"node": ">=8.11.3"
}
}

203
src/index.js Normal file
View File

@ -0,0 +1,203 @@
import { embarkPath, dappPath } from "embark-utils";
import { BigNumber } from "ethers/utils";
import ChildChain from "@omisego/omg-js-childchain";
import RootChain from "@omisego/omg-js-rootchain";
import { transaction } from "@omisego/omg-js-util";
// const WEB3_PROVIDER_URL = "https://rinkeby.infura.io/";
// const WATCHER_URL = "https://watcher.ari.omg.network/";
// const CHILDCHAIN_URL = "https://ari.omg.network/";
// const PLASMA_CONTRACT_ADDRESS = "0x44de0ec539b8c4a4b530c78620fe8320167f2f74";
// Service check constants
const SERVICE_CHECK_ON = 'on';
const SERVICE_CHECK_OFF = 'off';
// var web3 = new Web3()
let globalKeystore;
let rootChain;
let childChain;
const ADDRESS = "0x1e8df8b7d4212084bf5329fddc730b9e5aaba238";
const ADDRESS_PK = "0x0f7aa58edd2758334a819516b3421953b6c453c3e8ed85b071ce1edf3aedfab8";
/**
* Plugin that connects an Embark dApp to the Status app, and allows the dApp
* to be run in the Status browser.
*/
class EmbarkOmg {
constructor(embark) {
this.embark = embark;
this.events = this.embark.events;
this.pluginConfig = this.embark.pluginConfig;
this.deviceIp = this.pluginConfig.deviceIp;
this.logger = this.embark.logger;
this.fs = embark.fs;
// plugin opts
this.plasmaContractAddress = this.pluginConfig.PLASMA_CONTRACT_ADDRESS;
this.web3ProviderUrl = this.pluginConfig.WEB3_PROVIDER_URL;
this.watcherUrl = this.pluginConfig.WATCHER_URL;
this.childChainUrl = this.pluginConfig.CHILDCHAIN_URL;
this.webServerConfig = {};
this.blockchainConfig = {};
// gets hydrated webserver config from embark
this.events.on('config:load:webserver', webServerConfig => {
this.webServerConfig = webServerConfig;
});
// gets hydrated blockchain config from embark
this.events.on('config:load:blockchain', blockchainConfig => {
this.blockchainConfig = blockchainConfig;
});
// register service check
//this._registerServiceCheck();
this.init();
}
async txChildChain() {
const val = "555";
const toAddress = "0x38d5beb778b6e62d82e3ba4633e08987e6d0f990";
const utxos = await this.childChain.getUtxos(ADDRESS);
const utxosToSpend = this.selectUtxos(utxos, val, transaction.ETH_CURRENCY);
if (!utxosToSpend) {
return console.error(`No utxo big enough to cover the amount ${val}`);
}
const txBody = {
inputs: utxosToSpend,
outputs: [{
owner: toAddress,
currency: transaction.ETH_CURRENCY,
amount: Number(val)
}]
};
if (utxosToSpend[0].amount > val) {
// specify the change amount back to yourself
const CHANGE_AMOUNT = utxosToSpend[0].amount - val;
txBody.outputs.push({
owner: ADDRESS,
currency: transaction.ETH_CURRENCY,
amount: CHANGE_AMOUNT
});
}
try {
const unsignedTx = await this.childChain.createTransaction(txBody);
const signatures = await this.childChain.signTransaction(unsignedTx, [ADDRESS_PK]);
const signedTx = await this.childChain.buildSignedTransaction(unsignedTx, signatures);
const result = await this.childChain.submitTransaction(signedTx);
console.log(`Submitted tx: ${JSON.stringify(result)}`);
}
catch (e) {
console.error(e);
}
}
async deposit() {
const DEPOSIT_AMT = "100000";
const depositTx = transaction.encodeDeposit(ADDRESS, DEPOSIT_AMT, transaction.ETH_CURRENCY);
try {
const receipt = await this.rootChain.depositEth(depositTx, DEPOSIT_AMT, { from: ADDRESS, privateKey: ADDRESS_PK });
console.log(receipt);
}
catch (e) {
console.log(e);
}
}
async init() {
let web3Location = await this.getWeb3Location();
web3Location = web3Location.replace(/\\/g, '/');
const Web3 = require(web3Location);
this.web3 = new Web3;
const web3Provider = new Web3.providers.HttpProvider(this.web3ProviderUrl);
this.web3.setProvider(web3Provider);
this.rootChain = new RootChain(this.web3, this.plasmaContractAddress);
this.childChain = new ChildChain(this.watcherUrl, this.childChainUrl);
await this.deposit();
await this.txChildChain();
}
selectUtxos(utxos, amount, currency) {
const correctCurrency = utxos.filter(utxo => utxo.currency === currency)
// Just find the first utxo that can fulfill the amount
const selected = correctCurrency.find(utxo => new BigNumber(utxo.amount).gte(new BigNumber(amount)));
if (selected) {
return [selected];
}
}
getWeb3Location() {
return new Promise((resolve, reject) => {
this.events.request("version:get:web3", (web3Version) => {
if (web3Version === "1.0.0-beta") {
const nodePath = embarkPath('node_modules');
const web3Path = require.resolve("web3", { paths: [nodePath] });
return resolve(web3Path);
}
this.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
if (err) {
return reject(err);
}
const locationPath = embarkPath(location);
resolve(locationPath);
});
});
});
}
/**
* Registers this plugin for Embark service checks and sets up log messages for
* connection and disconnection events. The service check pings the Status app.
*
* @returns {void}
*/
_registerServiceCheck() {
// const serviceCheckQueue = queue((task, callback) => {
// this.statusApi.ping((err, isOnline) => {
// if (!err && isOnline) this.events.emit('embark-status:connect');
// const stateName = (isOnline ? SERVICE_CHECK_ON : SERVICE_CHECK_OFF);
// task.cb({ name: `Status.im (${this.deviceIp})`, status: stateName });
// });
// callback();
// }, 1);
this.embark.registerServiceCheck('OmiseGO', (cb) => {
//serviceCheckQueue.push({ cb });
cb({ name: `OmiseGO network (${this.deviceIp})`, status: SERVICE_CHECK_ON });
});
this.embark.events.on('check:backOnline:OmiseGO', () => {
this.logger.info("------------------");
this.logger.info("Connected to the OmiseGO network!");
this.logger.info("------------------");
});
this.embark.events.on('check:wentOffline:OmiseGO', () => {
this.logger.error("------------------");
this.logger.error("Couldn't connect or lost connection to the OmiseGO network...");
this.logger.error("------------------");
});
}
}
export default EmbarkOmg;

10
utils/utils.js Normal file
View File

@ -0,0 +1,10 @@
// import { BN } from "we";
// function selectUtxos (utxos, amount, currency) {
// const correctCurrency = utxos.filter(utxo => utxo.currency === currency)
// // Just find the first utxo that can fulfill the amount
// const selected = correctCurrency.find(utxo => new BigNumber(utxo.amount).gte(new BigNumber(amount)))
// if (selected) {
// return [selected]
// }
// }

5764
yarn-error.log Normal file

File diff suppressed because it is too large Load Diff

5667
yarn.lock Normal file

File diff suppressed because it is too large Load Diff