Skip to content

Commit

Permalink
Update local deployment guide. Improve CLI commands. (#485)
Browse files Browse the repository at this point in the history
* (#467) Minor CLI improvements. Update README.

* CLI: make transfer functions callable directly from CLI (without
  "TESTING" prefix).
* CLI: improve `getErc20Balance` method.
* CLI: add `getBridgeOnNearBalance` method.
* Update README.
* Minor improvements.

* (#467) Update README. CLI commands minor changes.

* CLI: make aux methods (transfer and view-balance functionality)
  to be called through the TESTING subcommand again.
* CLI: `getBridgeOnNearBalance` - don't provide private key to the view
  method.
* Update README.

* (#467) Update README.

* Update cli/index.js

Co-authored-by: Marcelo Fornet <mfornet94@gmail.com>

* Update README

Co-authored-by: Marcelo Fornet <mfornet94@gmail.com>

* Update README. Minor improvements and fixes.

* Update README: use `cli/index.js` as a rainbow cmd.

* Remove redundant code.

* Lint fixes.

Co-authored-by: Marcelo Fornet <mfornet94@gmail.com>
  • Loading branch information
sept-en and mfornet committed Feb 18, 2021
1 parent c5985d5 commit 4ee7472
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 50 deletions.
111 changes: 68 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,10 @@ pip3 install nearup

## Usage

You can install `rainbow-bridge-cli` from npm
Clone this repo, `yarn install`, then to learn the commands that you can use with the rainbow bridge run:

```
npm i -g rainbow-bridge-cli
```

To learn the commands that you can use with the rainbow bridge run

```
rainbow --help
```

Alternatively, clone this repo, `yarn install`, then you can see what commands you can use with:

```
./index.js --help
cli/index.js --help
```

Parameters of each command can be specified through environment variables, command line arguments, entries in the `~/.rainbow/config.json` config file, or the default value will be used -- in that priority.
Expand Down Expand Up @@ -122,12 +110,14 @@ We assume you have two accounts:

Make sure you have rainbow cli installed:
```bash
npm i -g rainbow-bridge-cli
yarn install
```

If you have already used the bridge on this machine run a cleanup:
```bash
rainbow clean
cli/index.js clean
```

If you're using rainbow-bridge-cli 1.x, create `~/.rainbow/config.json` file with the following content:
```json
{
Expand All @@ -148,6 +138,7 @@ If you're using rainbow-bridge-cli 1.x, create `~/.rainbow/config.json` file wit
"nearFunTokenAccount": "mintablefuntoken11"
}
```

If you are using rainbow-bridge-cli 2.x, create `~/.rainbow/config.json` file with the following content:
```json
{
Expand All @@ -169,12 +160,14 @@ If you are using rainbow-bridge-cli 2.x, create `~/.rainbow/config.json` file wi
"nearErc20Account": "21e7381368baa3f3e9640fe19780c4271ad96f37.ntf4.bridge2.testnet"
}
```

You can get infura project id, by registering at [infura.io](http://infura.io/).

To transfer ERC20 from ETH to NEAR run:
```bash
rainbow transfer-eth-erc20-to-near --amount 10 --eth-sender-sk <eth_token_holder_address> --near-receiver-account <near_token_holder_account>
cli/index.js TESTING transfer-eth-erc20-to-near --amount 10 --eth-sender-sk <eth_token_holder_address> --near-receiver-account <near_token_holder_account>
```

(If the command interrupts in the middle re-run it and it will resume the transfer. PoA RPC sometimes has issues)
Wait for the transfer to finish. You should see:
```
Expand All @@ -184,27 +177,28 @@ Balance of <near_token_holder_account> after the transfer is 10

To transfer ERC20 back from NEAR to ETH run:
```bash
rainbow transfer-eth-erc20-from-near --amount 1 --near-sender-account <near_token_holder_account> --near-sender-sk <near_token_holder_sk> --eth-receiver-address <eth_token_holder_address>
cli/index.js TESTING transfer-eth-erc20-from-near --amount 1 --near-sender-account <near_token_holder_account> --near-sender-sk <near_token_holder_sk> --eth-receiver-address <eth_token_holder_address>
```

You should see:
```
ERC20 balance of <eth_token_holder_address> after the transfer: 91
```
Congratulations, you have achieved a roundtrip of ERC20 token through the bridge!

<!---
### Deploying new bridge
If you used bridge before from your machine, then clean up the setup. We recommend using cloud instance for deploying and running the bridge. Go to a cloud instance and install dependencies from [Pre-requisites](#pre-requisites).
Then run:
```bash
rainbow clean
rainbow prepare
cli/index.js clean
cli/index.js prepare
```
Then initialize `EthOnNearClient` and `EthOnNearProver`:
```bash
rainbow init-near-contracts --near-network-id testnet --near-node-url <testnet_nodes_url> --eth-node-url https://ropsten.infura.io/v3/<infura_project_id> --near-master-account <near_master_account> --near-master-sk <near_master_sk> --near-client-account ethonnearclient01 --near-client-init-balance 2000000000000000000000000000 --near-prover-account ethonnearprover01
cli/index.js init-near-contracts --near-network-id testnet --near-node-url <testnet_nodes_url> --eth-node-url https://ropsten.infura.io/v3/<infura_project_id> --near-master-account <near_master_account> --near-master-sk <near_master_sk> --near-client-account ethonnearclient01 --near-client-init-balance 2000000000000000000000000000 --near-prover-account ethonnearprover01
```
* Make sure `ethonnearclient01` and `ethonnearprover01` do not exist yet. You can check it by going to https://explorer.testnet.near.org/accounts/ethonnearclient01 and https://explorer.testnet.near.org/accounts/ethonnearprover01 . If they exist, pick different names;
* You can get `<infura_project_id>` by creating a free [infura](http://infura.io/) account. If you are working in NEAR organization please ask max@near.org;
Expand Down Expand Up @@ -235,44 +229,44 @@ node index.js start bride-watchdog --eth-master-sk <watchdog_sk>
To locally test the bridge run:

```bash
rainbow clean
rainbow prepare
rainbow start near-node
rainbow start ganache
cli/index.js clean
cli/index.js prepare
cli/index.js start near-node
cli/index.js start ganache
```

### Initializing the contracts

First let's initialize the contracts that bridge needs to function:

```bash
rainbow init-near-contracts
rainbow init-eth-ed25519
rainbow init-eth-client --eth-client-lock-eth-amount 1000 --eth-client-lock-duration 10
rainbow init-eth-prover
cli/index.js init-near-contracts
cli/index.js init-eth-ed25519
cli/index.js init-eth-client --eth-client-lock-eth-amount 1000 --eth-client-lock-duration 10
cli/index.js init-eth-prover
```

Now, let's set up token on Ethereum blockchain that we can transfer to NEAR blockchain (this can be your own token).

```bash
rainbow init-eth-erc20
rainbow init-eth-locker
cli/index.js init-eth-erc20
cli/index.js init-eth-locker
```

Now, let's initialize token factory on NEAR blockchain.

```bash
rainbow init-near-token-factory
cli/index.js init-near-token-factory
```

### Starting the services

Now start the services that will relay the information between the chains:

```bash
rainbow start eth2near-relay
rainbow start near2eth-relay --eth-master-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501201
rainbow start bridge-watchdog --eth-master-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501202
cli/index.js start eth2near-relay
cli/index.js start near2eth-relay --eth-master-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501201
cli/index.js start bridge-watchdog --eth-master-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501202
```

Note, you can observe the logs of the relays by running:
Expand All @@ -283,38 +277,69 @@ pm2 logs

### Transferring tokens

Finally, let's transfer some tokens
Let's check the balance of bridged tokens from ETH to NEAR before starting the transfer. To this end let's use `node0` account, which is automatically created and funded on startup when localnet is started.
```bash
cli/index.js TESTING get-bridge-on-near-balance --near-receiver-account node0
```

Then transfer some tokens with:

```bash
rainbow transfer-eth-erc20-to-near --amount 1000 --eth-sender-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200 --near-receiver-account rainbow_bridge_eth_on_near_prover --near-master-account neartokenfactory
cli/index.js TESTING transfer-eth-erc20-to-near --amount 1000 --eth-sender-sk 0x2bdd21761a483f71054e14f5b827213567971c676928d9a1808cbfa4b7501200 --near-receiver-account node0 --near-master-account neartokenfactory
```

Now you check the balance of `node0` again. You should notice the balance was changed.

Note, when we deployed ERC20 to the Ethereum blockchain we have minted a large number of tokens to the default master
key of Ganache, so we have transferred ERC20 tokens from it to `alice.test.near`.
key of Ganache, so we have transferred ERC20 tokens from it to `node0` account.
Notice that we are using `neartokenfactory` account here to pay for the NEAR gas fees, any account for which we know a secret key would've worked too.
You must observe blocks being submitted.

You can also manually check the ERC20 balance of the receiver before and after receiving the transfer back from the NEAR side

```bash
cli/index.js TESTING get-erc20-balance 0xEC8bE1A5630364292E56D01129E8ee8A9578d7D8
```

Now let's try to transfer one token back to Ethereum

```bash
rainbow transfer-eth-erc20-from-near --amount 1 --near-sender-account rainbow_bridge_eth_on_near_prover --near-sender-sk ed25519:3D4YudUQRE39Lc4JHghuB5WM8kbgDDa34mnrEP5DdTApVH81af7e2dWgNPEaiQfdJnZq1CNPp5im4Rg5b733oiMP --eth-receiver-address 0xEC8bE1A5630364292E56D01129E8ee8A9578d7D8
cli/index.js TESTING transfer-eth-erc20-from-near --amount 1 --near-sender-account node0 --near-sender-sk ed25519:3D4YudUQRE39Lc4JHghuB5WM8kbgDDa34mnrEP5DdTApVH81af7e2dWgNPEaiQfdJnZq1CNPp5im4Rg5b733oiMP --eth-receiver-address 0xEC8bE1A5630364292E56D01129E8ee8A9578d7D8
```

You should observe the change of the ERC20 balance as reported by the CLI.

### Stopping the services

To stop relay services and node clients execute the following command:

```bash
cli/index.js stop all
```

Or you can stop them one by one using these commands:

```bash
cli/index.js stop near-node
cli/index.js stop ganache
cli/index.js stop eth2near-relay
cli/index.js stop near2eth-relay
cli/index.js stop bridge-watchdog
```

## Contract Development Workflow

Above steps are ways to run a local bridge and development workflows you need if make any changes to rainbow-bridge-cli. If you want to update any of solidity or rust contracts, they're not in this repo now and workflow is as following.

- Install dependencies:
```bash
rainbow clean
rainbow prepare
cli/index.js clean
cli/index.js prepare
```
- Start local NEAR network and Ganache
```bash
rainbow near-node
rainbow ganache
cli/index.js near-node
cli/index.js ganache
```
- If you want to modify solidity contracts, go to `node_modules/rainbow-bridge-sol`, make changes there and run `./build_all.sh` to recompile solidity contracts.
- If you want to modify rust contracts, go to `node_modules/ranbow-bridge-rs`, make changes there and run `./build_all.sh` to recompile rust contracts.
Expand Down
29 changes: 24 additions & 5 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
DeployToken,
mintErc20,
getErc20Balance,
getBridgeOnNearBalance,
getClientBlockHeightHash,
getAddressBySecretKey,
ethToNearApprove,
Expand Down Expand Up @@ -756,12 +757,12 @@ RainbowConfig.addOptions(

RainbowConfig.addOptions(
testingCommand
.command('get-erc20-balance <eth_account_address> <token_name>')
.command('get-erc20-balance <eth_account_address>')
.description('Get ERC20 balance on Ethereum for specific token (e.g. erc20).'),
async (ethAccountAddress, tokenName, args) => {
if (tokenName) {
args.ethErc20Address = RainbowConfig.getParam(`eth-${tokenName}-address`)
}
async (ethAccountAddress, args) => {
console.log(
`(Contract: ${args.ethErc20Address}) ERC20 balance of ${ethAccountAddress} is:`
)
await getErc20Balance({ ethAccountAddress, ...args })
},
[
Expand All @@ -771,6 +772,24 @@ RainbowConfig.addOptions(
]
)

RainbowConfig.addOptions(
testingCommand
.command('get-bridge-on-near-balance')
.option(
'--near-receiver-account <near_receiver_account>',
'The account on NEAR blockchain that owns bridged tokens.'
)
.description('Gets balance of bridged tokens from ETH to NEAR for the provided account.'),
async (args) => {
await getBridgeOnNearBalance(args)
},
[
'near-erc20-account',
'near-network-id',
'near-node-url'
]
)

RainbowConfig.addOptions(
testingCommand
.command('get-client-block-height-hash')
Expand Down
48 changes: 46 additions & 2 deletions testing/adapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const bs58 = require('bs58')
const { toBuffer } = require('eth-util-lite')
const {
nearAPI,
RobustWeb3,
remove0x,
normalizeEthKey,
Expand Down Expand Up @@ -64,8 +65,8 @@ async function getErc20Balance ({ ethAccountAddress, ethNodeUrl, ethErc20Address
JSON.parse(fs.readFileSync(ethErc20AbiPath)),
remove0x(ethErc20Address)
)
const result = await ethContract.methods.balanceOf(remove0x(ethAccountAddress)).call()
console.log(result)
const balance = await ethContract.methods.balanceOf(remove0x(ethAccountAddress)).call()
console.log(balance)
} catch (error) {
console.log('Failed', error.toString())
}
Expand Down Expand Up @@ -198,10 +199,53 @@ async function nearToEthUnlock ({
exit(0)
}

// View
async function getBridgeOnNearBalance ({
nearReceiverAccount,
nearErc20Account,
nearNetworkId,
nearNodeUrl
}) {
try {
const keyStore = new nearAPI.keyStores.InMemoryKeyStore()
const near = await nearAPI.connect({
nodeUrl: nearNodeUrl,
networkId: nearNetworkId,
masterAccount: nearReceiverAccount,
deps: { keyStore: keyStore }
})

const nearAccount = new nearAPI.Account(
near.connection,
nearReceiverAccount
)

const nearTokenContract = new nearAPI.Contract(
nearAccount,
nearErc20Account,
{
changeMethods: [],
viewMethods: ['get_balance']
}
)

const balance = await nearTokenContract.get_balance({
owner_id: nearReceiverAccount
})
console.log(
`[Rainbow-Bridge on Near] Balance of ${nearReceiverAccount} is ${balance}`
)
} catch (error) {
console.log('Failed', error.toString())
}
exit(0)
}

exports.ethToNearApprove = ethToNearApprove
exports.ethToNearLock = ethToNearLock
exports.nearToEthUnlock = nearToEthUnlock
exports.mintErc20 = mintErc20
exports.getErc20Balance = getErc20Balance
exports.getAddressBySecretKey = getAddressBySecretKey
exports.getClientBlockHeightHash = getClientBlockHeightHash
exports.getBridgeOnNearBalance = getBridgeOnNearBalance
2 changes: 2 additions & 0 deletions testing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
const {
mintErc20,
getErc20Balance,
getBridgeOnNearBalance,
getClientBlockHeightHash,
getAddressBySecretKey,
ethToNearApprove,
Expand All @@ -18,6 +19,7 @@ exports.TransferEthERC20FromNear = TransferEthERC20FromNear
exports.DeployToken = DeployToken
exports.mintErc20 = mintErc20
exports.getErc20Balance = getErc20Balance
exports.getBridgeOnNearBalance = getBridgeOnNearBalance
exports.getClientBlockHeightHash = getClientBlockHeightHash
exports.getAddressBySecretKey = getAddressBySecretKey
exports.ethToNearApprove = ethToNearApprove
Expand Down

0 comments on commit 4ee7472

Please sign in to comment.