mirror of
https://github.com/embarklabs/embarkjs-plasma.git
synced 2025-01-11 00:26:02 +00:00
Removing PK, adding metamask support
WIP
This commit is contained in:
parent
23b67e8704
commit
9c85edf728
@ -13,6 +13,7 @@
|
|||||||
"> 0.2%"
|
"> 0.2%"
|
||||||
],
|
],
|
||||||
"files": [
|
"files": [
|
||||||
|
"src",
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
110
src/index.js
110
src/index.js
@ -1,4 +1,4 @@
|
|||||||
/* global Web3 */
|
/* global EmbarkJS */
|
||||||
import BigNumber from "bn.js";
|
import BigNumber from "bn.js";
|
||||||
import ChildChain from "@omisego/omg-js-childchain";
|
import ChildChain from "@omisego/omg-js-childchain";
|
||||||
import RootChain from "@omisego/omg-js-rootchain";
|
import RootChain from "@omisego/omg-js-rootchain";
|
||||||
@ -19,46 +19,60 @@ export default class BaseEmbarkOmg {
|
|||||||
|
|
||||||
// plugin opts
|
// plugin opts
|
||||||
this.plasmaContractAddress = pluginConfig.PLASMA_CONTRACT_ADDRESS;
|
this.plasmaContractAddress = pluginConfig.PLASMA_CONTRACT_ADDRESS;
|
||||||
this.web3ProviderUrl = pluginConfig.WEB3_PROVIDER_URL;
|
//this.web3ProviderUrl = pluginConfig.WEB3_PROVIDER_URL;
|
||||||
this.watcherUrl = pluginConfig.WATCHER_URL;
|
this.watcherUrl = pluginConfig.WATCHER_URL;
|
||||||
this.childChainUrl = pluginConfig.CHILDCHAIN_URL;
|
this.childChainUrl = pluginConfig.CHILDCHAIN_URL;
|
||||||
}
|
}
|
||||||
async init(accounts, web3Path) {
|
|
||||||
|
async init(web3) { //}, web3Path) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.initing) {
|
if (this.initing) {
|
||||||
const message = "Already intializing the Plasma chain, please wait...";
|
const message = "Already intializing the Plasma chain, please wait...";
|
||||||
this.logger.error(message);
|
//this.logger.error(message);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
this.initing = true;
|
this.initing = true;
|
||||||
|
|
||||||
if (!(accounts && accounts.length)) {
|
// if (!(accounts && accounts.length)) {
|
||||||
this.logger.error(ACCOUNT_CONFIG_ERROR);
|
// //this.logger.error(ACCOUNT_CONFIG_ERROR);
|
||||||
throw new Error(ACCOUNT_CONFIG_ERROR);
|
// throw new Error(ACCOUNT_CONFIG_ERROR);
|
||||||
}
|
// }
|
||||||
const { address, privateKey } = accounts[0];
|
//const { address, privateKey } = accounts[0];
|
||||||
this.address = address;
|
//this.address = address;
|
||||||
this.addressPrivateKey = privateKey;
|
//this.addressPrivateKey = privateKey;
|
||||||
|
|
||||||
|
// this.address = accounts[0];
|
||||||
|
|
||||||
|
|
||||||
// init Web3
|
// init Web3
|
||||||
const web3Lib = web3Path ? require(web3Path) : Web3;
|
// const web3Lib = web3Path ? require(web3Path) : Web3;
|
||||||
this.web3 = new web3Lib();
|
// this.web3 = new web3Lib();
|
||||||
const web3Provider = new web3Lib.providers.HttpProvider(this.web3ProviderUrl);
|
// if (!web3) {
|
||||||
this.web3.setProvider(web3Provider);
|
// web3 = EmbarkJS.Blockchain.providers["web3"];
|
||||||
|
// }
|
||||||
|
this.web3 = web3;
|
||||||
|
let accounts = await this.web3.eth.getAccounts();
|
||||||
|
this.address = accounts.length > 1 ? accounts[1] : accounts[0]; // ignore the first account because it is our deployer account, we want the manually added account
|
||||||
|
|
||||||
|
// if(!this.web3) {
|
||||||
|
// throw new Error("web3 cannot be found. Please ensure you have the 'embarkjs-connector-web3' plugin installed in your DApp.");
|
||||||
|
// }
|
||||||
|
// const web3Provider = new web3Lib.providers.HttpProvider(this.web3ProviderUrl);
|
||||||
|
//this.web3.setProvider(web3Provider);
|
||||||
|
|
||||||
// check account balance on the main chain
|
// check account balance on the main chain
|
||||||
try {
|
// try {
|
||||||
this.maxDeposit = await this.web3.eth.getBalance(this.address);
|
// this.maxDeposit = await this.web3.eth.getBalance(this.address);
|
||||||
if (!this.maxDeposit || new BigNumber(this.maxDeposit).lte(0)) {
|
// if (!this.maxDeposit || new BigNumber(this.maxDeposit).lte(0)) {
|
||||||
this.logger.error(ACCOUNT_BALANCE_ERROR);
|
// //this.logger.error(ACCOUNT_BALANCE_ERROR);
|
||||||
throw new Error(ACCOUNT_BALANCE_ERROR);
|
// throw new Error(ACCOUNT_BALANCE_ERROR);
|
||||||
}
|
// }
|
||||||
this.maxDeposit = new BigNumber(this.maxDeposit);
|
// this.maxDeposit = new BigNumber(this.maxDeposit);
|
||||||
}
|
// }
|
||||||
catch (e) {
|
// catch (e) {
|
||||||
this.logger.error(`Error getting balance for account ${this.address}: ${e}`);
|
// this.logger.warn(`Error getting balance for account ${this.address}: ${e}`);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// set up the Plasma chain
|
// set up the Plasma chain
|
||||||
this.rootChain = new RootChain(this.web3, this.plasmaContractAddress);
|
this.rootChain = new RootChain(this.web3, this.plasmaContractAddress);
|
||||||
@ -70,7 +84,7 @@ export default class BaseEmbarkOmg {
|
|||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
const message = `Error initializing Plasma chain: ${e}`;
|
const message = `Error initializing Plasma chain: ${e}`;
|
||||||
this.logger.error(message);
|
//this.logger.error(message);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,37 +92,37 @@ export default class BaseEmbarkOmg {
|
|||||||
async deposit(amount) {
|
async deposit(amount) {
|
||||||
if (!this.inited) {
|
if (!this.inited) {
|
||||||
const message = "Please wait for the Plasma chain to initialize...";
|
const message = "Please wait for the Plasma chain to initialize...";
|
||||||
this.logger.error(message);
|
// this.logger.error(message);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
amount = new BigNumber(amount);
|
amount = new BigNumber(amount);
|
||||||
if (!amount || amount.lte(0)) {
|
if (!amount || amount.lte(0)) {
|
||||||
const message = "You must deposit more than 0 wei.";
|
const message = "You must deposit more than 0 wei.";
|
||||||
this.logger.error(message);
|
// this.logger.error(message);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
if (amount.gt(this.maxDeposit)) {
|
// if (amount.gt(this.maxDeposit) && this.maxDeposit.gt(0)) {
|
||||||
// recheck balance in case it was updated in a recent tx
|
// // recheck balance in case it was updated in a recent tx
|
||||||
this.maxDeposit = await this.web3.eth.getBalance(this.address);
|
// this.maxDeposit = await this.web3.eth.getBalance(this.address);
|
||||||
if (amount.gt(this.maxDeposit)) {
|
// if (amount.gt(this.maxDeposit)) {
|
||||||
const message = `You do not have enough funds for this deposit. Please deposit more funds in to ${this.address} and then try again.`;
|
// const message = `You do not have enough funds for this deposit. Please deposit more funds in to ${this.address} and then try again.`;
|
||||||
this.logger.error(message);
|
// // this.logger.error(message);
|
||||||
throw new Error(message);
|
// throw new Error(message);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// const DEPOSIT_AMT = "100000";
|
// const DEPOSIT_AMT = "100000";
|
||||||
this.logger.info(`Depositing ${amount} wei...`);
|
this.logger.info(`Depositing ${amount} wei...`);
|
||||||
const depositTx = transaction.encodeDeposit(this.address, amount, transaction.ETH_CURRENCY);
|
const depositTx = transaction.encodeDeposit(this.address, amount, transaction.ETH_CURRENCY);
|
||||||
try {
|
try {
|
||||||
const receipt = await this.rootChain.depositEth(depositTx, amount, { from: this.address, privateKey: this.addressPrivateKey });
|
const receipt = await this.rootChain.depositEth(depositTx, amount, { from: this.address });//, privateKey: this.addressPrivateKey });
|
||||||
this.logger.trace(receipt);
|
this.logger.trace(receipt);
|
||||||
const message = `Successfully deposited ${amount} wei in to the Plasma chain.\nView the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
|
const message = `Successfully deposited ${amount} wei in to the Plasma chain.\nView the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
|
||||||
this.logger.info(message);
|
// this.logger.info(message);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
const message = `Error depositing ${amount} wei: ${e}`;
|
const message = `Error depositing ${amount} wei: ${e}`;
|
||||||
this.logger.error(message);
|
// this.logger.error(message);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,11 +133,11 @@ export default class BaseEmbarkOmg {
|
|||||||
const utxos = await this.childChain.getUtxos(this.address);
|
const utxos = await this.childChain.getUtxos(this.address);
|
||||||
const utxosToSpend = this.selectUtxos(utxos, val, transaction.ETH_CURRENCY);
|
const utxosToSpend = this.selectUtxos(utxos, val, transaction.ETH_CURRENCY);
|
||||||
if (!utxosToSpend) {
|
if (!utxosToSpend) {
|
||||||
return this.logger.error(`No utxo big enough to cover the amount ${val}`);
|
throw new Error(`No utxo big enough to cover the amount ${val}`);
|
||||||
}
|
}
|
||||||
val = new BigNumber(val);
|
val = new BigNumber(val);
|
||||||
if (!val || val.lte(0)) {
|
if (!val || val.lte(0)) {
|
||||||
return this.logger.error("Transaction value must be more than 0 wei.");
|
throw new Error("Transaction value must be more than 0 wei.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const txBody = {
|
const txBody = {
|
||||||
@ -151,7 +165,7 @@ export default class BaseEmbarkOmg {
|
|||||||
try {
|
try {
|
||||||
const unsignedTx = await this.childChain.createTransaction(txBody);
|
const unsignedTx = await this.childChain.createTransaction(txBody);
|
||||||
|
|
||||||
const signatures = await this.childChain.signTransaction(unsignedTx, [this.addressPrivateKey]);
|
const signatures = await this.childChain.signTransaction(unsignedTx);//, [this.addressPrivateKey]);
|
||||||
|
|
||||||
const signedTx = await this.childChain.buildSignedTransaction(unsignedTx, signatures);
|
const signedTx = await this.childChain.buildSignedTransaction(unsignedTx, signatures);
|
||||||
|
|
||||||
@ -159,11 +173,11 @@ export default class BaseEmbarkOmg {
|
|||||||
|
|
||||||
const message = `Successfully submitted tx on the child chain: ${JSON.stringify(result)}\nView the transaction: http://quest.ari.omg.network/transaction/${result.txhash}`;
|
const message = `Successfully submitted tx on the child chain: ${JSON.stringify(result)}\nView the transaction: http://quest.ari.omg.network/transaction/${result.txhash}`;
|
||||||
|
|
||||||
this.logger.info(message);
|
//this.logger.info(message);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
this.logger.error(e);
|
// this.logger.error(e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -191,12 +205,12 @@ export default class BaseEmbarkOmg {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const message = `Exited UTXO from address ${fromAddress} with value ${utxo.amount}. View the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
|
const message = `Exited UTXO from address ${fromAddress} with value ${utxo.amount}. View the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
|
||||||
this.logger.info(message);
|
// this.logger.info(message);
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
const message = `Error exiting the Plasma chain for UTXO ${JSON.stringify(utxo)}: ${e}`;
|
const message = `Error exiting the Plasma chain for UTXO ${JSON.stringify(utxo)}: ${e}`;
|
||||||
this.logger.error(message);
|
// this.logger.error(message);
|
||||||
errors.push(message);
|
errors.push(message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user