# Dawn Chain

## 🛠️ Development

Ensure you use `gofmt` so that `golang` files match expected language standards. The plugin for Sublime is here <https://packagecontrol.io/packages/Gofmt>

If you get errors compiling because it can't find modules, some may be private currently. This explains how to solve it <https://stackoverflow.com/a/44247040>

## 📦 Golang Modules

This repository uses `golang` modules. Learn more here <https://cosmos.network/docs/tutorial/gomod.html> and here <https://github.com/golang/go/wiki/Modules>

## 🏎️ Building and running the `dawn` application

If you want to build the `dawn` application in this repo to see the functionalities, `Go 1.12.1+` is required .

Add some parameters to environment is necessary if you have never used the `go mod` before.

```bash
mkdir -p $HOME/go/bin
echo "export GOPATH=$HOME/go" >> ~/.bash_profile
echo "export GOBIN=\$GOPATH/bin" >> ~/.bash_profile
echo "export PATH=\$PATH:\$GOBIN" >> ~/.bash_profile
echo "export GO111MODULE=on" >> ~/.bash_profile
source ~/.bash_profile
```

### 🛸 Example application

These modules can be added to any Cosmos-SDK based chain, but a demo application/blockchain is provided with example code for how to integrate them. It can be installed and built as follows:

```
# Clone the repository
mkdir -p $GOPATH/src/github.com/cosmos
cd $GOPATH/src/github.com/cosmos
git clone https://github.com/BlockKungPao/fbchain
cd fbchain && git checkout master

# Install dep, as well as your dependencies
make get_tools
dep ensure -v
go get -u github.com/kardianos/govendor

# Fetch the C file dependencies (this is a manual hack, as dep does not support pulling non-golang files used in dependencies)
govendor fetch -tree github.com/ethereum/go-ethereum/crypto/secp256k1

# Install the app into your $GOBIN
make install

# Now you should be able to run the following commands, confirming the build is successful:
fbd help
fbcli help
fbrelayer help
```

### 🔧 Running and testing the application

First, initialise a chain and create accounts to test sending of a random token.

```
# Initialize the genesis.json file that will help you to bootstrap the network
fbd init --chain-id=testing

# Create a key to hold your validator account and for another test account
fbcli keys add validator
# Enter password

fbcli keys add testuser
# Enter password

fbd add-genesis-account $(fbcli keys show validator -a) 1000000000stake,1000000000atom

# Now its safe to start `fbd`
fbd start

# Then, wait 10 seconds and in another terminal window, test things are ok by sending 10 tok tokens from the validator to the testuser
fbcli tx send $(fbcli keys show testuser -a) 10stake --from=validator --chain-id=testing --yes

# Confirm token balances have changed appropriately
fbcli query account $(fbcli keys show validator -a) --trust-node
fbcli query account $(fbcli keys show testuser -a) --trust-node

# Next, setup the staking module prerequisites
# First, create a validator and stake
fbcli tx staking create-validator \
  --amount=100000000stake \
  --pubkey=$(fbd tendermint show-validator) \
  --moniker="test_moniker" \
  --chain-id=testing \
  --commission-rate="0.10" \
  --commission-max-rate="0.20" \
  --commission-max-change-rate="0.01" \
  --min-self-delegation="1" \
  --gas=200000 \
  --gas-prices="0.001stake" \
  --from=validator

# Then wait 10 seconds then confirm your validator was created correctly, and has become Bonded status
fbcli query staking validators --trust-node

# See the help for the ethbridge create claim function
fbcli tx ethbridge create-claim --help

# Now you can test out the ethbridge module by submitting a claim for an ethereum prophecy
# Create a bridge claim (Ethereum prophecies are stored on the blockchain with an identifier created by concatenating the nonce and sender address)
fbcli tx ethbridge create-claim 0 0x7B95B6EC7fbd73572298cEf32Bb54FA408207359 $(fbcli keys show testuser -a) $(fbcli keys show validator -a --bech val) 3eth --from validator --chain-id testing --yes

# Then read the prophecy to confirm it was created with the claim added
fbcli query ethbridge prophecy 0 0x7B95B6EC7fbd73572298cEf32Bb54FA408207359 --trust-node

# And finally, confirm that the prophecy was successfully processed and that new eth was minted to the testuser address
fbcli query account $(fbcli keys show testuser -a) --trust-node
```

### 🎛️ Using the application from rest-server

First, run the cli rest-server

```
fbcli rest-server --trust-node
```

An api collection for Postman (<https://www.getpostman.com/>) is provided [here](https://app.gitbook.com/s/-LuCpA9fbn7Okffas0LT/docs/peggy.postman_collection.json) which documents some API endpoints and can be used to interact with it. Note: For checking account details/balance, you will need to change the cosmos addresses in the URLs, params and body to match the addresses you generated that you want to check.

### 🕹️ Running the relayer service

For automated relaying, there is a `relayer` service that can be run that will automatically watch and relay events.

```
# Check fbrelayer connection to fbd
fbrelayer status

# Initialize the Relayer service for automatic claim processing
fbrelayer init testing wss://ropsten.infura.io/ws ec6df30846baab06fce9b1721608853193913c19 "LogLock\(bytes32,address,bytes,address,uint256,uint256\)" validator

# Enter password and press enter
# You should see a message like:  Started ethereum websocket... and Subscribed to contract events...
```

The `relayer` will now watch the contract on `Ropsten` and create a claim whenever it detects a lock event.

### 🌉 Using the bridge

With the application set up and the `relayer` running, you can now use Peggy by sending a `lock` transaction to the smart contract. You can do this from any Ethereum wallet/client that supports smart contract transactions.

The easiest way to do this for now, assuming you have Metamask setup for Ropsten in the browser is to use `remix` or `mycrypto` as the frontend, for example:

* 1. Go to [remix.ethereum.org](http://remix.ethereum.org/)
* 1. Compile `Peggy.sol` with `solc v0.5.0`
* 1. Set the environment as `Injected Web3 Ropsten`
* 1. On 'Run' tab, select Peggy and enter `"0x22BFD920Ac64B0A90088D07f84137EA79bbb8Ca1"` in 'At Address' field
* 1. Select 'At Address' to load the deployed contract
* 1. Enter the following for the variables under function `lock()`:

```
 _recipient = [HASHED_COSMOS_RECIPIENT_ADDRESS] *(for testuser cosmos1pjtgu0vau2m52nrykdpztrt887aykue0hq7dfh, enter "0x636f736d6f7331706a74677530766175326d35326e72796b64707a74727438383761796b756530687137646668")*
 _token = [DEPLOYED_TOKEN_ADDRESS]
 _amount = [WEI_AMOUNT]
```

* 1. Enter the same number from `_amount` as the transaction's value (in wei)
* 1. Select "transact" to send the `lock()` transaction

Then, wait for the transaction to confirm and mine, and for the `relayer` to pick it up. You should see the successful output in the `relayer` console. You can also confirm the tokens have been minted by using the CLI again:

```
fbcli query account cosmos1pjtgu0vau2m52nrykdpztrt887aykue0hq7dfh --trust-node
```

## 👨‍💻 Using the modules in other projects

The `ethbridge` and oracle modules can be used in other `cosmos-sdk` applications by copying them into your application's modules folders and including them in the same way as in the example application. Each module may be moved to its own repo or integrated into the core Cosmos-SDK in future, for easier usage.

There are 2 nuances you need to be aware of when using these modules in other Cosmos-SDK projects.

* A specific version of `golang.org/x/crypto` (ie `tendermint/crypto`) is needed for compatability with `go-ethereum`. See the `Gopkg.toml` for constraint details. There is an open pull request to `tendermint/crypto` to add compatibility, but until that is merged you need to use the customized version (<https://github.com/tendermint/crypto/pull/1>)
* The `govendor` steps in the application as above are needed

For instructions on building and deploying the smart contracts, see [here](http://daw.org/docs/eth-bridge).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dawn.org/start/firstblood-chain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
