From 73a08276c48e7dab46b2b6e9ea57256e055b43a0 Mon Sep 17 00:00:00 2001 From: emizzle Date: Mon, 17 Jun 2019 12:33:38 +1000 Subject: [PATCH] Init modifcations Inits itself in the main process (for things like embark console commands). In addition, the EmbarkJS.Plasma object in the console is also inited. Package name update to `embark-plasma`. Moved child chain status to use the childChain function (and removed axios as a dependency as a result). --- package.json | 7 ++- src/index.js | 97 ++++++++++++++++----------------------- src/utils/plasma/index.js | 9 ---- yarn.lock | 27 ----------- 4 files changed, 42 insertions(+), 98 deletions(-) delete mode 100644 src/utils/plasma/index.js diff --git a/package.json b/package.json index 5743106..539e659 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "embark-omg", + "name": "embark-plasma", "version": "1.0.0", - "description": "OmiseGO plugin for Embark", + "description": "OmiseGO Plasma plugin for Embark", "main": "dist/index.js", "files": [ "src", @@ -45,7 +45,6 @@ "@omisego/omg-js-childchain": "2.0.0-v0.2", "@omisego/omg-js-rootchain": "2.0.0-v0.2", "@omisego/omg-js-util": "2.0.0-v0.2", - "axios": "0.19.0", "date-fns": "2.0.0-alpha.27", "embark-utils": "^4.1.0-beta.2", "embarkjs-omg": "1.0.0", @@ -72,4 +71,4 @@ "npm": ">=6.4.1", "yarn": ">=1.12.3" } -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 85c7ea9..a1da2e4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,6 @@ -import EmbarkJSOmg from "embarkjs-omg"; +import EmbarkJSPlasma from "embarkjs-omg"; import { dappPath } from "embark-utils"; import { formatDate } from "./utils"; -import { getWatcherStatus } from "./utils/plasma"; // Service check constants const SERVICE_CHECK_ON = "on"; @@ -11,7 +10,7 @@ const SERVICE_CHECK_OFF = "off"; * Plugin that allows Embark to connect to and interact with an existing Plama chain, * and provides an EmbarkJS.Plasma API to allow the DApp to interact with the chain. */ -class EmbarkOmg extends EmbarkJSOmg { +class EmbarkPlasma extends EmbarkJSPlasma { constructor(embark) { super(embark); @@ -24,21 +23,22 @@ class EmbarkOmg extends EmbarkJSOmg { this.events.once("config:load:contracts", this.addCodeToEmbarkJs.bind(this)); this.registerServiceCheck(); this.registerConsoleCommands(); + + this.events.request("blockchain:get", (web3) => { + this.events.request("blockchain:ready", () => { + this.init(web3, true); + }); + }); } generateSymlink(varName, location) { return new Promise((resolve, reject) => { - this.events.request( - "code-generator:symlink:generate", - location, - varName, - (err, symlinkDest) => { - if (err) { - return reject(err); - } - resolve(symlinkDest); + this.events.request("code-generator:symlink:generate", location, varName, (err, symlinkDest) => { + if (err) { + return reject(err); } - ); + resolve(symlinkDest); + }); }); } @@ -59,10 +59,7 @@ class EmbarkOmg extends EmbarkJSOmg { await this.codeGeneratorReady(); try { - embarkJsOmgSymlinkPath = await this.generateSymlink( - "embarkjs-omg", - embarkjsOmgPath - ); + embarkJsOmgSymlinkPath = await this.generateSymlink("embarkjs-omg", embarkjsOmgPath); } catch (err) { this.logger.error(__("Error creating a symlink to embarkjs-omg")); return this.logger.error(err.message || err); @@ -81,17 +78,16 @@ class EmbarkOmg extends EmbarkJSOmg { }, pluginConfig: ${JSON.stringify(this.pluginConfig)} };`; - //code += "\nEmbarkJS.onReady(() => {"; + code += "\nEmbarkJS.onReady(() => {"; code += "\n EmbarkJS.Plasma = new __embarkPlasma(opts);"; // code += `\n EmbarkJS.Plasma.init(${JSON.stringify(this.accounts)}, "${web3SymlinkPath}");`; code += `\n const embarkJsWeb3Provider = EmbarkJS.Blockchain.Providers["web3"]`; code += `\n if (!embarkJsWeb3Provider) { throw new Error("web3 cannot be found. Please ensure you have the 'embarkjs-connector-web3' plugin installed in your DApp."); }`; - code += `\n EmbarkJS.Plasma.init(embarkJsWeb3Provider.web3).catch((err) => console.error(err.message));`; // global.embarkjsOmg ? "${web3SymlinkPath}" : null);`; // pass the symlink path ONLY when we are in the node (VM) context - //code += "\n});"; + code += `\n if (global.embarkjsOmg) EmbarkJS.Plasma.init(embarkJsWeb3Provider.web3, true).catch((err) => console.error(err));`; // global.embarkjsOmg ? "${web3SymlinkPath}" : null);`; // pass the symlink path ONLY when we are in the node (VM) context + code += "\n});"; this.embark.addCodeToEmbarkJS(code); - } - ); + }); } registerConsoleCommands() { @@ -102,8 +98,7 @@ class EmbarkOmg extends EmbarkJSOmg { process: (cmd, callback) => { const force = cmd.endsWith("--force"); if (this.inited && !force) { - const message = - "The Plasma chain is already initialized. If you'd like to reinitialize the chain, use the --force option ('plasma init --force')."; + const message = "The Plasma chain is already initialized. If you'd like to reinitialize the chain, use the --force option ('plasma init --force')."; this.logger.error(message); return callback(message); // passes a message back to cockpit console } @@ -121,23 +116,18 @@ class EmbarkOmg extends EmbarkJSOmg { const depositRegex = /^plasma[\s]+deposit[\s]+([0-9]+)$/; this.embark.registerConsoleCommand({ - description: - "Deposits ETH from the root chain (Rinkeby) to the Plasma chain to be used for transacting on the Plasma chain.", + description: "Deposits ETH (or ERC20) from the root chain to the Plasma child chain to be used for transacting on the Plasma chain.", matches: cmd => { return depositRegex.test(cmd); }, usage: "plasma deposit [amount]", process: (cmd, callback) => { if (!this.inited) { - return callback( - "The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting." - ); // passes a message back to cockpit console + return callback("The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting."); // passes a message back to cockpit console } const matches = cmd.match(depositRegex) || []; if (matches.length <= 1) { - return callback( - "Invalid command format, please use the format 'plasma deposit [amount]', ie 'plasma deposit 100000'" - ); + return callback("Invalid command format, please use the format 'plasma deposit [amount]', ie 'plasma deposit 100000'"); } this.deposit(matches[1]) .then(message => { @@ -153,23 +143,18 @@ class EmbarkOmg extends EmbarkJSOmg { const sendRegex = /^plasma[\s]+transfer[\s]+(0x[0-9,a-f,A-F]{40,40})[\s]+([0-9]+)$/; this.embark.registerConsoleCommand({ - description: - "Sends an ETH tx on the Plasma chain from the account configured in the DApp's blockchain configuration to any other account on the Plasma chain.", + description: "Sends an ETH tx on the Plasma chain from the account configured in the DApp's blockchain configuration to any other account on the Plasma chain.", matches: cmd => { return sendRegex.test(cmd); }, usage: "plasma transfer [to_address] [amount]", process: (cmd, callback) => { if (!this.inited) { - return callback( - "The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting." - ); // passes a message back to cockpit console + return callback("The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting."); // passes a message back to cockpit console } const matches = cmd.match(sendRegex) || []; if (matches.length <= 2) { - return callback( - "Invalid command format, please use the format 'plasma transfer [to_address] [amount]', ie 'plasma transfer 0x38d5beb778b6e62d82e3ba4633e08987e6d0f990 555'" - ); + return callback("Invalid command format, please use the format 'plasma transfer [to_address] [amount]', ie 'plasma transfer 0x38d5beb778b6e62d82e3ba4633e08987e6d0f990 555'"); } this.transfer(matches[1], matches[2]) .then(message => { @@ -185,26 +170,24 @@ class EmbarkOmg extends EmbarkJSOmg { const exitRegex = /^plasma[\s]+exit[\s]+(0x[0-9,a-f,A-F]{40,40})$/; this.embark.registerConsoleCommand({ - description: "Exits the ETH from the Plasma chain to the Rinkeby chain.", + description: "Exits all UTXO's from the Plasma chain to the root chain.", matches: cmd => { return exitRegex.test(cmd); }, usage: "plasma exit [plasma_chain_address]", process: (cmd, callback) => { if (!this.inited) { - const message = - "The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting."; + const message = "The Plasma chain has not been initialized. Please initialize the Plamsa chain using 'plasma init' before continuting."; this.logger.error(message); return callback(message); // passes a message back to cockpit console } const matches = cmd.match(exitRegex) || []; if (matches.length <= 1) { - const message = - "Invalid command format, please use the format 'plasma exit [plasma_chain_address]', ie 'plasma exit 0x38d5beb778b6e62d82e3ba4633e08987e6d0f990'"; + const message = "Invalid command format, please use the format 'plasma exit [plasma_chain_address]', ie 'plasma exit 0x38d5beb778b6e62d82e3ba4633e08987e6d0f990'"; this.logger.error(message); return callback(message); } - this.exit(matches[1]) + this.exitAllUtxos(matches[1]) .then(message => { this.logger.info(message); callback(null, message); @@ -220,11 +203,10 @@ class EmbarkOmg extends EmbarkJSOmg { description: "Gets the status of the Plasma chain.", matches: ["plasma status"], process: (cmd, callback) => { - getWatcherStatus(this.pluginConfig.WATCHER_URL) + this.childChain.status() .then(status => { - const strStatus = JSON.stringify(status); - this.logger.info(strStatus); - callback(null, strStatus); + this.logger.info(status); + callback(null, status); }) .catch(e => { this.logger.error(e.message); @@ -247,11 +229,12 @@ class EmbarkOmg extends EmbarkJSOmg { "services:register", name, cb => { - getWatcherStatus(this.pluginConfig.WATCHER_URL) + if (!this.inited) { + return cb({ name: "Loading...", status: SERVICE_CHECK_OFF }); + } + this.childChain.status() .then(status => { - const serviceStatus = `Last block: ${formatDate( - status.last_mined_child_block_timestamp - )}`; + const serviceStatus = `Last block: ${formatDate(status.last_mined_child_block_timestamp)}`; return cb({ name: serviceStatus, status: status ? SERVICE_CHECK_ON : SERVICE_CHECK_OFF @@ -273,12 +256,10 @@ class EmbarkOmg extends EmbarkJSOmg { this.events.on("check:wentOffline:OmiseGO", () => { this.logger.error("------------------"); - this.logger.error( - "Couldn't connect or lost connection to the Plasma chain..." - ); + this.logger.error("Couldn't connect or lost connection to the Plasma chain..."); this.logger.error("------------------"); }); } } -export default EmbarkOmg; +export default EmbarkPlasma; diff --git a/src/utils/plasma/index.js b/src/utils/plasma/index.js deleted file mode 100644 index d3b1bd3..0000000 --- a/src/utils/plasma/index.js +++ /dev/null @@ -1,9 +0,0 @@ -import { post } from "axios"; - -export async function getWatcherStatus(watcherUrl) { - const response = await post(`${watcherUrl}/status.get`); - if (!(response.status === 200 && response.data && response.data.success)) { - throw new Error(`Error getting status of the Plasma watcher`); - } - return response.data.data; -} diff --git a/yarn.lock b/yarn.lock index 34a6348..6b2d82c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -977,14 +977,6 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f" integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ== -axios@0.19.0: - version "0.19.0" - resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8" - integrity sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ== - dependencies: - follow-redirects "1.5.10" - is-buffer "^2.0.2" - babel-code-frame@^6.22.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" @@ -1736,13 +1728,6 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3: dependencies: ms "2.0.0" -debug@=3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" - integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== - dependencies: - ms "2.0.0" - debug@^3.1.0, debug@^3.2.6: version "3.2.6" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" @@ -2588,13 +2573,6 @@ flat-cache@^1.2.1: rimraf "~2.6.2" write "^0.2.1" -follow-redirects@1.5.10: - version "1.5.10" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" - integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== - dependencies: - debug "=3.1.0" - follow-redirects@1.5.7: version "1.5.7" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.7.tgz#a39e4804dacb90202bca76a9e2ac10433ca6a69a" @@ -3161,11 +3139,6 @@ is-buffer@^1.1.5: resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== -is-buffer@^2.0.2: - version "2.0.3" - resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725" - integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw== - is-callable@^1.1.3, is-callable@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75"