Skip to content

horizonx-tech/demo-crosschain-lending

Repository files navigation

demo-crosschain-lending

A demo of crosschain lending, using chainsight.

Overview

This demo shows how to use the chainsight protocol to deposit assets on one chain, and then lend token on another chain, using deposit assets as collateral. Also, it shows how to liquidate the collateral when the loan is not repaid. If the collateral is liquidated on the second chain, the deposit assets are released on the first chain to the liquidator. This is one of a series of demos that show how to use chainsight to build cross-chain applications.

overview

A working demo is available here.

Pre-requisites

Setup

yarn

And .env file is needed, which contains the following information:

# .env
# mnemonic
MNEMONIC=test test test test test test test test test test test test
# alchemy api key
ALCHEMY_OPTIMISM_KEY=xxx
ALCHEMY_ARBITRUM_KEY=xxx
# etherscan api key
ETHERSCAN_OPTIMISM_KEY=xxx
ETHERSCAN_ARBITRUM_KEY=xxx

Deploy and setup

Before setup, you need some ETH to pay for the gas fee on both chains(Optimism Goerli and Arbitrum Goerli) to run demo. You can get some test ETH from Optimism faucet and Arbitrum faucet. Send ETH to the first and second addresses in the mnemonic. The first user is the borrower, and the second is the liquidator.

  1. Deploy contracts on the first chain
npx hardhat run scripts/0_deploy.ts --network optimismTest

And verify contracts on the first chain

npx hardhat run scripts/verify.ts --network optimismTest
  1. Deploy contracts on the second chain
npx hardhat run scripts/0_deploy.ts --network arbitrumGoerli

And vefiry contracts on the second chain

npx hardhat run scripts/verify.ts --network arbitrumGoerli
  1. Start dfx local network
dfx start 
  1. Depoloy chainsight canisters on mainnet
./chainsight/scripts/0_update_addresses.sh
./chainsight/scripts/1_deploy_canisters.sh
  1. Setup canisters
./chainsight/scripts/2_subscribe.sh
./chainsight/scripts/3_send_ether.sh

Run demo

  1. Mint DAI on the first chain(Optimism Goerli) and lock it to another chain(Arbitrum Goerli)
npx hardhat run scripts/2_mint_and_lock.ts --network optimismTest
  1. Borrow TUSD on the second chain, using the locked DAI as collateral
npx hardhat run scripts/3_borrow_on_dst.sh --network arbitrumGoerli
cd chainsight && dfx canister call lock_indexer_arbitrumGoerli save_logs && cd .. 

And you confirm onLockCreated tx like this on the second chain. This transaction is sent by the lock_relayer_optimism canister on local ICP, which is subscribed to the lock event of the lock_indexer_optimism canister.

  1. Make TUSD oracle price high in order to liquidate the TUSD on the second chain
npx hardhat run scripts/4_set_price.ts --network arbitrumGoerli
  1. Liquidate the TUSD on the second chain on behalf of the second user
npx hardhat run scripts/5_liquidate_on_dst.sh --network arbitrumGoerli
cd chainsight && dfx canister call unlock_indexer_arbitrumGoerli save_logs && cd ..

And you can confirm unlock asset tx like this

This transaction is sent by the unlock_relayer_arbitrum canister on local ICP, which is subscribed to the unlock event of the unlock_indexer_arbitrum canister.

More information

About

A demo for a cross-chain lending using chainsight

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published