Router Smart Contract (1.0.0)
Download OpenAPI specification:Download
The Router Smart Contract serves as a convenient tool for efficiently managing and monitoring Pair contracts in a decentralized environment. It enables the deployer to easily keep track of the existing Pair contracts and offers a wide array of settings functions that make the management of the liquidity pools much easier.
The Router contract provides:
- Pair Management: Create and manage liquidity pairs
- LP Token Issuance: Issue LP tokens for liquidity providers
- Multi-Pair Swaps: Execute multiple swaps in a single transaction
- User Pair Creation: Allow users to create their own trading pairs
- Create the Pair: Use
createPairendpoint to create the liquidity pair - Issue LP Token: Use
issueLpTokenendpoint to create the LP token - Add Liquidity: Call
addInitialLiquidityon the pair, providing a value greater than configured inconfigEnableByUserParameters - Lock Tokens: After adding initial liquidity, lock the received tokens using
lockTokensfrom Simple-Lock contract. The lock period must be equal to or greater than the minimum configured. After locking, the user receives an SFT that can be used to redeem tokens after the lock period - Enable Swap: To activate swap on the created pair, send the SFT received from
lockTokenstosetSwapEnabledByUser. The Router validates the value and lock period and returns the SFT to the user
Create Pair
Creates a new liquidity pair and returns the pair contract address.
Process:
- Validates the token pair
- Deploys a new Pair contract based on the template
- Returns the new pair contract address in the response data
Response Decoding: The returned address is hex-encoded and needs to be decoded using the Klever decoder.
Example Response:
Raw: 00000000000000000500b7b4a14e4567a64b122ee61b8717ae11e210809ac3e6
Decoded: klv1qqqqqqqqqqqqqpgqk762znj9v7nyky3wucdcw9awz83ppqy6c0nqhe2qaz
Request Body schema: application/jsonrequired
| scAddress required | string Router contract address |
| funcName required | string Value: "createPair" |
| args required | Array of strings Arguments:
|
Responses
Request samples
- Payload
{- "scAddress": "klv1qqqqqqqqqqqqqpgq9fywm4wqawy8udszd3l66hs9vktdtl73c0nqwl5hlk",
- "funcName": "createPair",
- "args": [
- "string:KFI",
- "string:KLV"
]
}Response samples
- 200
{- "data": {
- "returnData": [
- "string"
], - "returnCode": "string",
- "returnMessage": "string"
}, - "error": "string",
- "code": "string"
}Issue LP Token
Creates the LP token that will be distributed to users who add liquidity to the pool.
Process:
- Validates the pair exists
- Creates a new LP token with the specified name and ticker
- Assigns the token to the pair contract
Prerequisites:
- The pair must be created first using
createPair - Caller must be the pair owner or have appropriate permissions
Note: After issuing the LP token, you need to set the local roles using setLocalRoles to allow minting.
Request Body schema: application/jsonrequired
| scAddress required | string Router contract address |
| funcName required | string Value: "issueLpToken" |
| args required | Array of strings Arguments:
|
Responses
Request samples
- Payload
{- "scAddress": "klv1qqqqqqqqqqqqqpgq9fywm4wqawy8udszd3l66hs9vktdtl73c0nqwl5hlk",
- "funcName": "issueLpToken",
- "args": [
- "address:klv1qqqqqqqqqqqqqpgqk762znj9v7nyky3wucdcw9awz83ppqy6c0nqhe2qaz",
- "string:KFIKLV-LP",
- "string:KFIKLVLP"
]
}Response samples
- 200
{- "data": {
- "returnData": [
- "string"
], - "returnCode": "string",
- "returnMessage": "string"
}, - "error": "string",
- "code": "string"
}Set Swap Enabled By User
Allows the creator of a pair to activate the swap functionality for that pair.
Process:
- After adding initial liquidity (
addInitialLiquidity), the user receives LP tokens - These LP tokens must be locked in the Simple-Lock contract
- After locking, the user receives an SFT (Semi-Fungible Token)
- To activate swap, send the SFT to this endpoint
- The Router validates the value and lock period
- The SFT is returned to the user after validation
Requirements:
- The SFT must have a value greater than
min_locked_token_value - The lock period must be greater than
min_lock_period_epochs - Both values are configured via
configEnableByUserParameters
Payment: The SFT received from locking LP tokens
Request Body schema: application/jsonrequired
| scAddress required | string Router contract address |
| funcName required | string Value: "setSwapEnabledByUser" |
| args required | Array of strings Arguments:
|
| value required | string SFT payment from Simple-Lock |
Responses
Request samples
- Payload
{- "scAddress": "klv1qqqqqqqqqqqqqpgq9fywm4wqawy8udszd3l66hs9vktdtl73c0nqwl5hlk",
- "funcName": "setSwapEnabledByUser",
- "args": [
- "address:klv1qqqqqqqqqqqqqpgqk762znj9v7nyky3wucdcw9awz83ppqy6c0nqhe2qaz"
], - "value": "LOCK-2U1O/1=1000000"
}Response samples
- 200
{- "data": {
- "returnData": [
- "string"
], - "returnCode": "string",
- "returnMessage": "string"
}, - "error": "string",
- "code": "string"
}Multi Pair Swap
Allows users to execute a list of swaps in a single transaction, routing through multiple pairs.
Use Case: When there's no direct pair between two tokens, you can route through intermediate pairs. For example, to swap KFI to ZART when only KFI-KLV and KLV-ZART pairs exist.
Swap Parameters (for each swap):
pair_address: The liquidity pair contract addressfunction: Swap type -swapTokensFixedInputorswapTokensFixedOutputtoken_wanted: The token to receive from the swapamount_wanted: The amount oftoken_wantedto receive
Supported Functions:
swapTokensFixedInput: Fixed input amount, variable outputswapTokensFixedOutput: Variable input, fixed output amount
Note: You can mix swapTokensFixedInput and swapTokensFixedOutput in the same multi-swap transaction.
Request Body schema: application/jsonrequired
| scAddress required | string Router contract address |
| funcName required | string Value: "multiPairSwap" |
| args required | Array of strings Arguments (repeated for each swap in the route):
|
| value required | string Initial token payment |
Responses
Request samples
- Payload
{- "scAddress": "klv1qqqqqqqqqqqqqpgq9fywm4wqawy8udszd3l66hs9vktdtl73c0nqwl5hlk",
- "funcName": "multiPairSwap",
- "args": [
- "address:klv1qqqqqqqqqqqqqpgqk762znj9v7nyky3wucdcw9awz83ppqy6c0nqhe2qaz",
- "string:swapTokensFixedInput",
- "string:KLV",
- "BigUint:9",
- "address:klv1qqqqqqqqqqqqqpgqlequ2zkergzrpzh5nzadduh0r80qau7mc0nqtwx8hz",
- "string:swapTokensFixedInput",
- "string:ZART-3RIP",
- "BigUint:8"
], - "value": "KFI=10"
}Response samples
- 200
{- "data": {
- "returnData": [
- "string"
], - "returnCode": "string",
- "returnMessage": "string"
}, - "error": "string",
- "code": "string"
}