Skip to content

Commit

Permalink
refactor: Decouple RainbowConfig (wip) (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
frol committed Nov 5, 2020
1 parent 3fcdfbc commit c2d250b
Show file tree
Hide file tree
Showing 17 changed files with 489 additions and 446 deletions.
13 changes: 6 additions & 7 deletions cli/commands/danger-deploy-myerc20.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const { Web3, RainbowConfig, normalizeEthKey } = require('rainbow-bridge-utils')
const { Web3, normalizeEthKey } = require('rainbow-bridge-utils')
const { BN } = require('ethereumjs-util')
const fs = require('fs')

class DangerDeployMyERC20 {
static async execute () {
const web3 = new Web3(RainbowConfig.getParam('eth-node-url'))
static async execute ({ ethNodeUrl, ethMasterSk, ethErc20AbiPath, ethGasMultiplier }) {
const web3 = new Web3(ethNodeUrl)
let ethMasterAccount = web3.eth.accounts.privateKeyToAccount(
normalizeEthKey(RainbowConfig.getParam('eth-master-sk'))
normalizeEthKey(ethMasterSk)
)
web3.eth.accounts.wallet.add(ethMasterAccount)
web3.eth.defaultAccount = ethMasterAccount.address
ethMasterAccount = ethMasterAccount.address

// use default ERC20 ABI
const abiPath = RainbowConfig.getParam('eth-erc20-abi-path')
const binPath = './test/MyERC20.full.bin'

const tokenContract = new web3.eth.Contract(
JSON.parse(fs.readFileSync(abiPath))
JSON.parse(fs.readFileSync(ethErc20AbiPath))
)
const txContract = await tokenContract
.deploy({
Expand All @@ -28,7 +27,7 @@ class DangerDeployMyERC20 {
from: ethMasterAccount,
gas: 3000000,
gasPrice: new BN(await web3.eth.getGasPrice()).mul(
new BN(RainbowConfig.getParam('eth-gas-multiplier'))
new BN(ethGasMultiplier)
)
})

Expand Down
1 change: 0 additions & 1 deletion cli/commands/near-dump.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs').promises
const Path = require('path')
const { RainbowConfig } = require('rainbow-bridge-utils')
const fetch = require('node-fetch')

async function getLatestBlock (nearNodeUrl) {
Expand Down
14 changes: 5 additions & 9 deletions cli/commands/prepare.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { exec } = require('child_process')
const path = require('path')
const { RainbowConfig } = require('rainbow-bridge-utils')
const { getScript } = require('rainbow-bridge-testing')

const { getScript } = require('rainbow-bridge-utils')

class PrepareCommand {
static execute () {
static execute ({ coreSrc, nearupSrc }) {
const scriptDir = getScript('prepare')

const shell = ['bash', scriptDir].join(' ')
Expand All @@ -14,13 +14,9 @@ class PrepareCommand {
env[e] = process.env[e]
}

env.LOCAL_CORE_SRC =
RainbowConfig.getParam('core-src') &&
path.resolve(RainbowConfig.getParam('core-src'))
env.LOCAL_CORE_SRC = coreSrc && path.resolve(coreSrc)

env.LOCAL_NEARUP_SRC =
RainbowConfig.getParam('nearup-src') &&
path.resolve(RainbowConfig.getParam('nearup-src'))
env.LOCAL_NEARUP_SRC = nearupSrc && path.resolve(nearupSrc)

// @ts-ignore
const prepareScript = exec(shell, { env: env })
Expand Down
34 changes: 20 additions & 14 deletions cli/commands/start/eth2near-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ const {
EthOnNearClientContract,
Eth2NearRelay
} = require('rainbow-bridge-eth2near-block-relay')
const { nearAPI, RainbowConfig } = require('rainbow-bridge-utils')
const { nearAPI } = require('rainbow-bridge-utils')
const path = require('path')

class StartEth2NearRelayCommand {
static async execute () {
if (RainbowConfig.getParam('daemon') === 'true') {
static async execute ({
daemon,
nearNetworkId,
nearNodeUrl,
nearMasterAccount,
nearMasterSk,
nearClientAccount,
ethNodeUrl
}) {
if (daemon === 'true') {
ProcessManager.connect((err) => {
if (err) {
console.log(
Expand All @@ -30,31 +38,29 @@ class StartEth2NearRelayCommand {
})
})
} else {
const masterAccount = RainbowConfig.getParam('near-master-account')
const masterSk = RainbowConfig.getParam('near-master-sk')
const keyStore = new nearAPI.keyStores.InMemoryKeyStore()
await keyStore.setKey(
RainbowConfig.getParam('near-network-id'),
masterAccount,
nearAPI.KeyPair.fromString(masterSk)
nearNetworkId,
nearMasterAccount,
nearAPI.KeyPair.fromString(nearMasterSk)
)
const near = await nearAPI.connect({
nodeUrl: RainbowConfig.getParam('near-node-url'),
networkId: RainbowConfig.getParam('near-network-id'),
masterAccount: masterAccount,
nodeUrl: nearNodeUrl,
networkId: nearNetworkId,
masterAccount: nearMasterAccount,
deps: {
keyStore: keyStore
}
})

const relay = new Eth2NearRelay()
const clientContract = new EthOnNearClientContract(
new nearAPI.Account(near.connection, masterAccount),
RainbowConfig.getParam('near-client-account')
new nearAPI.Account(near.connection, nearMasterAccount),
nearClientAccount
)
await clientContract.accessKeyInit()
console.log('Initializing eth2near-relay...')
relay.initialize(clientContract, RainbowConfig.getParam('eth-node-url'))
relay.initialize(clientContract, ethNodeUrl)
console.log('Starting eth2near-relay...')
await relay.run()
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/start/ganache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ProcessManager = require('pm2')
const { spawnProcess } = require('./helpers')
const { RainbowConfig, getScript } = require('rainbow-bridge-utils')
const { getScript } = require('rainbow-bridge-utils')

class StartGanacheNodeCommand {
static async execute () {
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/start/near.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const util = require('util')
const { execSync } = require('child_process')
const request = require('request')

const { getLocalNearNodeURL } = require('./helpers')
const { RainbowConfig } = require('rainbow-bridge-utils')

class StartLocalNearNodeCommand {
static execute () {
Expand Down
5 changes: 2 additions & 3 deletions cli/commands/start/near2eth-relay.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const ProcessManager = require('pm2')
const { spawnProcess } = require('./helpers')
const { Near2EthRelay } = require('rainbow-bridge-near2eth-block-relay')
const { RainbowConfig } = require('rainbow-bridge-utils')
const path = require('path')

class StartNear2EthRelayCommand {
static async execute () {
if (RainbowConfig.getParam('daemon') === 'true') {
static async execute ({ daemon }) {
if (daemon === 'true') {
ProcessManager.connect((err) => {
if (err) {
console.log(
Expand Down
5 changes: 2 additions & 3 deletions cli/commands/start/watchdog.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const ProcessManager = require('pm2')
const { spawnProcess } = require('./helpers')
const { Watchdog } = require('rainbow-bridge-watchdog')
const { RainbowConfig } = require('rainbow-bridge-utils')
const path = require('path')

class StartWatchdogCommand {
static async execute () {
if (RainbowConfig.getParam('daemon') === 'true') {
static async execute ({ daemon }) {
if (daemon === 'true') {
ProcessManager.connect((err) => {
if (err) {
console.log(
Expand Down
Loading

0 comments on commit c2d250b

Please sign in to comment.