Skip to content

tgntr/swapchain

Repository files navigation

Overview

The following repository contains a basic example of a module that interacts with Osmosis through IBC.


Prerequisites


Developer Documentation


Demo

Prerequisites


1. Start local swapchain network

make start-swapchain

2. Start local osmosis network

make start-osmosis

3. Wait until osmosis node starts producing blocks (~15 secs)


4. Start relayer

make start-relayer

5. Wait until relayer makes connection between the networks (~30 secs)


6. Create osmosis liquidity pool

# create liquidity pool for stake-uosmo with equal amounts
osmosisd tx gamm create-pool --pool-file=contrib/osmosis-pool.json --from validator --home=contrib/osmosis/.osmosis --node=tcp://localhost:26662 --chain-id=osmosis --fees=875stake -y

7. Query osmosis pool spot price through ICQ

# query spot price for stake
swapchaind tx interchainswap send-query-osmosis-spot-price channel-0 1 stake uosmo --from=validator --home=.swapchain --node=tcp://localhost:26660 -y

8. Wait until relayer relays packet (~10sec)


9. Query the result of the ICQ query

# price should be 1.00 since the pool has equal amounts for both tokens
swapchaind q interchainswap query-state 1 --home=.swapchain --node=tcp://localhost:26660

10. Create osmosis account through ICA

swapchaind tx interchainswap register-interchain-account connection-0 --from=validator --home=.swapchain --node=tcp://localhost:26660 -y

11. Wait until relayer relays packet (~10sec)


12. Query the created ICA account address

swapchaind q interchain-accounts controller interchain-account cosmos14y0kdvznkssdtal2r60a8us266n0mm97r2xju8 connection-0 --home=.swapchain --node=tcp://localhost:26660

13. Fund the ICA address in osmosis

  • Replace $INTERCHAIN_ADDRESS with the address queried in step 12
# fund address with 10000uosmo
osmosisd tx bank send faucet $INTERCHAIN_ADDRESS 10000uosmo --home=contrib/osmosis/.osmosis --node=tcp://localhost:26662 --chain-id=osmosis --fees=875stake -y

14. Swap tokens through ICA

  • Replace $INTERCHAIN_ADDRESS with the address queried in step 12
# swap 10uosmo for stake
swapchaind tx interchainswap send-msg-osmosis-swap '{"@type":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn","sender":"$INTERCHAIN_ADDRESS","routes":[{"poolId":"1","tokenOutDenom":"stake"}],"tokenIn":{"denom":"uosmo","amount":"10"},"tokenOutMinAmount":"1"}' connection-0 --from=validator --home=.swapchain --node=tcp://localhost:26660 -y

15. Wait until relayer relays packet (~10sec)


16. Verify swap was successful

  • Replace $INTERCHAIN_ADDRESS with the address queried in step 12
# interchain account should have 9990uosmo and some stake in their balance
osmosisd q bank balances $INTERCHAIN_ADDRESS --home=contrib/osmosis/.osmosis --node=tcp://localhost:26662 --chain-id=osmosis

# pool should have more uosmo than stake, therefore the spot price for uosmo should be less than 1.00 stake
osmosisd q gamm spot-price 1 stake uosmo --home=contrib/osmosis/.osmosis --node=tcp://localhost:26662 --chain-id=osmosis

Tests

./interchaintest contains example end-to-end IBC test using interchaintest - a package that provides test suite for complete interchain simulation


Prerequisites


1. Build binary image

make build-docker

2. Run tests

make interchaintest

Working with Docker

1. Build binary image

# create image named 'swapchaind'
make build-docker

2. Example interaction

# init config and persist config folder (/swapchain/.swapchain) to volume 'my-vol'
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind init testmoniker --chain-id=testchainid

# add account key named 'test'
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind keys add test --keyring-backend=test

# fund account and add to genesis
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind add-genesis-account test 999999999999999stake --keyring-backend=test

# generate genesis tx that sets 'test' as validator
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind gentx test 2222222222stake --keyring-backend=test --chain-id=testchainid

# collect the genesis tx and add to genesis
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind collect-gentxs

# start node
docker run --rm -it -v=my-vol:/swapchain/.swapchain swapchaind start

Releases

No releases published

Packages

No packages published

Languages