/workshop

Repository for KyberDeveloper workshops

Primary LanguageSolidityMIT LicenseMIT

Kyber Workshop

This repository is used as complement to the workshops conducted by Kyber. It can also be used to quickly test the Kyber contracts in your local machine.

Useful Links

  1. Slides
  2. KyberDeveloper Portal
  3. KyberDeveloper Telegram
  4. Workshop Repository
  5. Ropsten ETH Faucets
  6. Ropsten Swap

What is Kyber?

Kyber is a widely used on-chain protocol that makes accessing liquidity simple for users, DApps and financial applications. The protocol has powered decentralised token swaps on popular wallets like MyEtherWallet and imToken, decentralised token payments (users can pay in any supported ERC20 token) in popular DApps like Etheremon and Peepeth and providing an on-chain liquidity source for decentralised financial applications like MelonPort, Set Protocol, b0x and many more.

Kyber protocol brings token inventories and prices on-chain, hence allowing developers to directly embed value exchanges into their smart contracts without any technical or security overhead. This allows for:

  • Instant confirmation. A transaction happens with instant confirmation if it's sent from on-chain entities like smart contracts. Otherwise, once the transaction is included on the blockchain, the execution triggered by the transaction is immediately confirmed.

  • Operation certainty. There is no transactional risk. Users know the rate and how much liquidity is available before they commit their transaction. There is also no settlement uncertainty or counterparty risk.

  • Global and diverse pool of different tokens. Kyber welcomes token holders to contribute their token to the liquidity pool. By having their token made available to the liquidity pool, the token will be available in all services integrated with Kyber.

Prerequisites

  1. Ganache

Install the Ganache AppImage by downloading here https://truffleframework.com/ganache. To use the provided Ganache snapshot, install ganache-cli.

sudo npm install -g ganache-cli
  1. Truffle

Install the latest Truffle v5 beta.

sudo npm install -g truffle@beta
  1. Install the rest of the NPM packages
npm install

Workshop Repository

Overview

workshop
├── config
│   ├── network.json
│   └── tokens.json
├── contracts
│   ├── ConversionRatesInterface.sol
│   ├── ConversionRates.sol
│   ├── ERC20Interface.sol
│   ├── examples
│   │   ├── SwapEtherToToken.sol
│   │   ├── SwapTokenToEther.sol
│   │   └── SwapTokenToToken.sol
│   │   └── Trade.sol
│   ├── ExpectedRateInterface.sol
│   ├── ExpectedRate.sol
│   ├── FeeBurnerInterface.sol
│   ├── FeeBurner.sol
│   ├── KyberNetworkInterface.sol
│   ├── KyberNetworkProxyInterface.sol
│   ├── KyberNetworkProxy.sol
│   ├── KyberNetwork.sol
│   ├── KyberReserveInterface.sol
│   ├── KyberReserve.sol
│   ├── LiquidityConversionRates.sol
│   ├── LiquidityFormula.sol
│   ├── Migrations.sol
│   ├── mockTokens
│   │   ├── KyberGenesisToken.sol
│   │   ├── KyberNetworkCrystal.sol
│   │   ├── OmiseGo.sol
│   │   ├── Salt.sol
│   │   └── Zilliqa.sol
│   ├── PermissionGroups.sol
│   ├── SanityRatesInterface.sol
│   ├── SanityRates.sol
│   ├── SimpleNetworkInterface.sol
│   ├── Utils2.sol
│   ├── Utils.sol
│   ├── VolumeImbalanceRecorder.sol
│   ├── WhiteListInterface.sol
│   ├── WhiteList.sol
│   └── Withdrawable.sol
├── db
├── examples
│   ├── solidity
│   │   ├── SwapEtherToToken.sol -> ../../contracts/examples/SwapEtherToToken.sol
│   │   ├── SwapTokenToEther.sol -> ../../contracts/examples/SwapTokenToEther.sol
│   │   └── SwapTokenToToken.sol -> ../../contracts/examples/SwapTokenToToken.sol
│   │   └── Trade.sol -> ../../contracts/examples/Trade.sol
│   ├── truffle
│   │   ├── getExpectedRate.js
│   │   ├── swapEtherToToken.js
│   │   ├── swapTokenToEther.js
│   │   ├── swapTokenToToken.js
│   │   └── trade.js
│   └── web3
│   ├── abi
│   │   ├── KyberNetworkProxy.abi
│   │   ├── KNC.abi
│   │   ├── OMG.abi
│   │   ├── SALT.abi
│   │   └── ZIL.abi
│   ├── getExpectedRate.js
│   ├── swapEtherToToken.js
│   ├── swapTokenToEther.js
│   └── swapTokenToToken.js
├── LICENSE
├── migrations
│   ├── 1_initial_migration.js
│   ├── 2_deploy_tokens.js
│   ├── 3_deploy_contracts.js
│   ├── 4_setup_permissions.js
│   ├── 5_setup_KyberNetworkProxy.js
│   ├── 6_setup_KyberReserve.js
│   ├── 7_setup_FeeBurner.js
│   ├── 8_setup_ExpectedRate.js
│   ├── 9_setup_ConversionRates.js
│   ├── 10_setup_SanityRates.js
│   ├── 11_setup_WhiteList.js
│   ├── 12_setup_KyberNetwork.js
│   ├── 13_transfer_tokens.js
│   └── 14_deployment_summary.js
├── package.json
├── README.md
├── test
└── truffle.js

Directory Details

config - contains JSON files that hold configuration details of the Kyber contracts used for migrations
contracts - contains all the Kyber contracts, plus some mock tokens and solidity examples for testing
examples - contains truffle and web3 example scripts to interact with Kyber's smart contracts, and also contains solidity examples for Kyber contract interactions
migrations - contains the truffle migration scripts to deploy and setup the Kyber contracts in a test environment

Interacting with the Kyber contracts locally

1A. Run Ganache with local snapshot

A Ganache snapshot has already been pre-made with the Kyber contracts deployed. You can immediately interact with the contracts without having to do migrations. The snapshot is stored in db folder.

We use the mnemonic gesture rather obey video awake genuine patient base soon parrot upset lounge for the accounts. The user wallet (0x47a793D7D0AA5727095c3Fe132a6c1A46804c8D2) already contains some ETH and test ERC20 tokens.

NOTE: The mnemonic provided is used only for testing. DO NOT use the accounts generated for your own personal use in mainnet, as you can potentially lose these funds.

To run the snapshot locally, run the command:

ganache-cli --db db --accounts 10 --mnemonic 'gesture rather obey video awake genuine patient base soon parrot upset lounge' --networkId 5777 --debug

1B. Run Ganache and deploy the Kyber contracts from scratch

If you wish to deploy the Kyber contracts yourself, you can run the following commands:

Run ganache-cli in one terminal session

ganache-cli --accounts 10 --mnemonic 'gesture rather obey video awake genuine patient base soon parrot upset lounge' --networkId 5777 --debug

In a new terminal session, connect to the ganache network, and run the truffle migration scripts

truffle migrate --network development

2. Running the example scripts

You can directly interact with the Kyber contracts on the Ganache network. We have provided some example scripts in the example directory.

For the Truffle examples:

truffle exec examples/truffle/<SCRIPT>

e.g.

truffle exec examples/truffle/swapEtherToToken.js

For the Web3 examples:

node examples/web3/<SCRIPT>

e.g.

node examples/web3/swapEtherToToken.js

For the Solidity examples, they are already deployed in the Ganache network using the Truffle migration scripts. You can interact with the Solidity examples using truffle console, or write your own Truffle/Web3 scripts to interact with the Solidity example contracts.

Ganache network details

Network

development

Permissions

(admin) 0x2B522cABE9950D1153c26C1b399B293CaA99FcF9
(operator) 0x3644B986B3F5Ba3cb8D5627A22465942f8E06d09
(alerter) 0x9e8f633D0C46ED7170EF3B30E291c64a91a49C7E

Wallets

(user) 0x47a793D7D0AA5727095c3Fe132a6c1A46804c8D2
(reserve) 0x0d95EBB4874f17157e40635C19dBC6E9b0BFdb03
(tax) 0x5243B5970f327c328B2739dEc88abC46FaE8931A
(bob) 0xe1a1d3637eE02391ac4035e72456Ca7448c73FD4
(alice) 0x1cF1919d91cebAb2E56a5c0cC7180bB54eD4f3F6

Tokens

(KNC) 0x8c13AFB7815f10A8333955854E6ec7503eD841B7
(OMG) 0x3750bE154260872270EbA56eEf89E78E6E21C1D9
(SALT) 0x7ADc6456776Ed1e9661B3CEdF028f41BD319Ea52
(ZIL) 0x400DB523AA93053879b20F10F56023b2076aC852

Contracts

(KyberNetwork) 0x58A21f7aA3D9D83D0BD8D4aDF589626D13b94b45
(KyberNetworkProxy) 0xA46E01606f9252fa833131648f4D855549BcE9D9
(ConversionRates) 0xF6084Ad447076da0246cD28e104533f9f51dbD2F
(SanityRates) 0x738d8Ef6AcaE15660E467AB2B2cF3a488e40FF64
(KyberReserve) 0xd44B9352e4Db6d0640449ed653983827BD882885
(FeeBurner) 0xd3add19ee7e5287148a5866784aE3C55bd4E375A
(WhiteList) 0x6E9b241Eec2C4a80485c1D2dF750231AFaf1A167
(ExpectedRate) 0x8b3BdEcEac3d23A215300A3df19e1bEe43A0Ac9C
(SwapEtherToToken) 0xf71D305142eC1aC03896526D52F743959db01624
(SwapTokenToEther) 0x63D556067eDbCD97ACc3356314398F70d4CcF948
(SwapTokenToToken) 0xE16d27F08e94D9d6f05C988169E388068C790B75
(Trade) 0x19F18bde9896890f161DeD31B05b58dc0ffD911b