KyberSwap Docs
Search
K

Environment Setup

KyberSwap Elastic Security Incident
On 22 Nov 2023, the Elastic protocol experienced a security incident. More details can be found via our official channels.
All other KyberSwap products (Aggregator, Limit Order, & Classic) continue to be fully operational.

Overview

All of the sample code corresponding to specific operations can be found in the /src/operations folder in the demo repo below:
To get the test environment up and running, you can run the following:
git clone https://github.com/KyberNetwork/ks-sdk-elastic-demo.git
npm install
npm run start // Run once
npm run start:dev // Run on save (Nodemon)

Provider And Signer Setup

In order for our application to communicate with the blockchain, we will first need to specify a provider as well as a signer.
Provider
Signer
A Provider is a read-only connection to the blockchain, which allows querying the blockchain state, such as account, block or transaction details, querying event logs or evaluating read-only code using call.
If you are coming from Web3.js, you are used to a Provider offering both read and write access. In Ethers, all write operations are further abstracted into another Object, the Signer.
A Signer wraps all operations that interact with an account. An account generally has a private key located somewhere, which can be used to sign a variety of types of payloads.
The private key may be located in memory (using a Wallet) or protected via some IPC layer, such as MetaMask which proxies interaction from a website to a browser plug-in, which keeps the private key out of the reach of the website and only permits interaction after requesting permission from the user and receiving authorization.
The examples utilize KyberSwap's own RPC provider for Polygon PoS but you configure your preferred provider in the provider.ts file. For simplicity, the Ethers Wallet instance was used to directly sign outgoing transactions using an imported private key specified in signer.ts.
Importing private keys
Your private keys are required for signing transactions which will result in changes to the blockchain state (i.e. token transfers, swaps, etc.). To avoid the complexity of frontend interfaces, the examples implements the Elastic operations in a pure Node.js environment thereby requiring your private keys to be imported.
You will need to replace the MATIC_PRIVATE_KEY constant with your own private key in order to execute trade and liquidity management operations. To export your private key from Metamask, open Metamask and go to Account Details > Export Private Key.
NEVER EXPOSE YOU PRIVATE KEYS (i.e. gommit to a public repo) AS ANYONE WITH YOUR KEYS WILL BE ABLE TO ACCESS YOUR ASSETS.

Important Folders And Files

/src/index.ts

Starting point for code execution. For each Elastic operation, a corresponding function has been created in the index.ts file. To execute a specific operation, please uncomment the relevant function.

/src/operations/

Contains individual .ts files that corresponds to each operation.

/src/libs/constants.ts

Specifies various constants that are required for the operation:
  • Tokens
  • Contract Addresses

/src/libs/provider.ts

Ethers.js provider configuration which specifies the chain as well as provider.

/src/libs/signer.ts

Ethers.js provider configuration which specifies the wallet which contains the signer methods required to execute a write operation.

/src/libs/abis/

Contains the various Elastic smart contract ABI definitions.