# Relayer IBC Testing
## Faucet Server Setup
To test IBC commands, you need to start a server first. Once after starting chain, enable faucet relayer server in same server. While setting up the server, ensure that ports :8000 (faucet) and :26657 (gaiad) are open and accessable. Run below commands in same system where your are running server:
```bash
# Write chain config into a json file
$ echo "{\"key\":\"$RLYKEY\",\"chain-id\":\"$CHAINID\",\"rpc-addr\":\"$NODE_RPC\",\"account-prefix\":\"$ACCOUNT_PREFIX\",\"gas-adjustment\":1.5,\"gas-prices\":\"0.025$DENOM\",\"trusting-period\":\"3306h\"}" > $CHAINID.json
# Above,
# $RLYKEY represents key name of chain
# $CHAINID represents chain-id
# $NODE_RPC represents rpc-address of node
# $ACCOUNT_PREFIX represents account address prefix
# $DENOM represents denom of chain
# NOTE: you will want to save the content from this JSON file
# install the relayer
$ export RELAYER=$GOPATH/src/github.com/cosmos/relayer
$ mkdir -p $(dirname $RELAYER) && git clone git@github.com:cosmos/relayer $RELAYER && cd $RELAYER
$ make install
# Now its time to configure relayer and start faucet server
$ cd
$ rly config init
# Add chain to relayer using json created above
$ rly chains add -f $CHAINID.json
$ rly keys add $CHAINID $RLYKEY
# Setup the faucet service definition
rly dev faucet $USER $HOME $CHAINID $RLYKEY 100000$DENOM > faucet.service
# Example for above command: $ rly dev faucet vitwit /home/vitwit ibc-0 testkey 100000000stake
sudo mv faucet.service /etc/systemd/system/faucet.service
sudo systemctl daemon-reload
sudo systemctl start faucet
# Faucet server _should_ be ready to go!
# Be sure you have the text from ~/$CHAINID.json for the next step
```
## Relayer Setup
Once you have your server (you could deploy the relayer on a different machine as above server)
```bash
# install the relayer
export RELAYER=$GOPATH/src/github.com/cosmos/relayer
mkdir -p $(dirname $RELAYER) && git clone git@github.com:cosmos/relayer $RELAYER && cd $RELAYER
make install
# then to configure your local relayer to talk to your remote chain
# get the json from $CHAINID.json on your server
rly cfg init
rly ch add -f {{chain_id}}.json
# create a local rly key for the chain
rly keys add {{chain_id}} testkey
# configure the chain to use that key by default
rly ch edit {{chain_id}} key testkey
# initialize the light client for {{chain_id}}
rly light init {{chain_id}} -f
# request funds from the faucet to test it
rly tst request {{chain_id}} testkey
# you should see a balance for the rly key now
rly q bal {{chain_id}}
```
Note that most of these instructions would also work directly on the
server on which you deployed your chain on (not recommended though).
## Creating a connection
Once you have your chain configured on your relayer, follow these steps to send tokens between the chains:
```bash
# Add destination chain
# do it either individually...
rly ch a -f destination-chain.json
# or add multiple chains in folder at once
rly chains add-dir ~/<chains-folder>/
# ensure the light clients are created locally...
rly light init {{src_chain_id}} -f
rly l i {{dst_chain_id}} -f
# ensure each chain has its appropriate key...
rly keys add {{src_chain_id}}
rly k a {{dst_chain_id}}
# ensure you have funds on both chains...
# this adds tokens to your addresses from each chain's faucet
rly testnets request {{src_chain_id}}
rly tst req {{dst_chain_id}}
# either manually add a path by following the prompts...
rly paths add {{src_chain_id}} {{dst_chain_id}} {{path_name}}
# or run below two commands with replacing json values with your chain values
$ echo "{\"src\":{\"chain-id\":\"{{src_chain_id}}\",\"port-id\":\"transfer\",\"order\":\"unordered\",\"version\":\"ics20-1\"},\"dst\":{\"chain-id\":\"{{dst_chain_id}}\",\"port-id\":\"transfer\",\"order\":\"unordered\",\"version\":\"ics20-1\"},\"strategy\":{\"type\":\"naive\"}}" > {{path_name}}.json
$ rly pth add {{src_chain_id}} {{dst_chain_id}} {{path_name}} -f {{path_name}}.json
# create connections and channels to the path
rly tx link {{path_name}}
# then send some funds back and forth!
rly q bal {{src_chain_id}}
rly q bal {{dst_chain_id}}
rly tx transfer {{src_chain_id}} {{dst_chain_id}} {{amount}} $(rly ch addr {{dst_chain_id}})
# check balance again
rly q bal {{src_chain_id}}
rly q bal {{dst_chain_id
# relay packets to complete tokens transfer
rly tx relay-packets {{path_name}}
rly q bal {{src_chain_id}}
rly q bal {{dst_chain_id}}
```
## Test relayer between regen-devnet-4 and aplikigo-1
Here, we will consider `regen-devnet-4` chain as source chain and `aplikigo-1` chain as destination chain.
```bash
# init relayer config
rly config init
# Write regen-devnet-4 and aplikigo chains config to separate json files.
# Note: Please change gas-prices value based on your rpc-node
$ echo "{\"key\":\"testkey\",\"chain-id\":\"regen-devnet-4\",\"rpc-addr\":\"http://18.220.101.192:26657\",\"account-prefix\":\"regen:\",\"gas-adjustment\":1.5,\"gas-prices\":\"0.025uregen\",\"trusting-period\":\"336h\"}" > regen-devnet-4.json
$ echo "{\"key\":\"testkey\",\"chain-id\":\"aplikigo-1\",\"rpc-addr\":\"http://public-rpc1.regen.vitwit.com:26657\",\"account-prefix\":\"regen:\",\"gas-adjustment\":1.5,\"gas-prices\":\"0.025utree\",\"trusting-period\":\"336h\"}" > aplikigo-1.json
# Add above both chains to relayer
# do it either individually...
rly ch a -f regen-devnet-4.json
rly ch a -f aplikigo-1.json
# or add multiple chains at once by copying both json files into one folder
rly chains add-dir ~/<chains-folder>/
# export source and destination chain-ids to variables
export SRC=regen-devnet-4
export DST=aplikigo-1
export PTH=demo-path
# ensure the light clients are created locally...
rly light init $SRC -f
rly l i $DST -f
# ensure each chain has its appropriate key...
# either add key by below command
rly keys add $SRC
rly keys add $DST
# or restore key using mnemonic
rly keys restore $SRC testkey "{{mnemonic-words}}"
rly keys restore $DST testkey "{{mnemonic-words}}"
# show key will return address of chain's default key i.e., testkey
rly keys show $SRC
rly keys show $DST
# ensure you have funds on both chains...
rly query bal $SRC
rly query bal $DST
# if there is no balance found, then request chain faucet for tokens to chain's addresses.
# Next, Add path between chains
# either manually add a path by following the prompts...
rly paths add $SRC $DST $PTH
# or run below two commands
$ echo "{\"src\":{\"chain-id\":\"$SRC\",\"port-id\":\"transfer\",\"order\":\"unordered\",\"version\":\"ics20-1\"},\"dst\":{\"chain-id\":\"$DST\",\"port-id\":\"transfer\",\"order\":\"unordered\",\"version\":\"ics20-1\"},\"strategy\":{\"type\":\"naive\"}}" > $PTH.json
$ rly pth add $SRC $DST $PTH -f $PTH.json
# create connections and channels to the path
rly tx link $PTH
# then send some funds back and forth!
rly q bal $SRC
rly q bal $DST
rly tx transfer $SRC $DST {{amount}} $(rly ch addr $DST)
# check balance again
rly q bal $SRC
rly q bal {{dst_chain_id
# relay packets to complete tokens transfer
rly tx relay-packets $PTH -d
rly q bal $SRC
rly q bal $DST
# You can send back ibc tokens from dst to src again
rly tx transfer $DST $SRC {{amount}} $(rly ch addr $SRC)