The Swap SDK provides functionality to fetch the best trade and execute trades on the cronos evm (VVS). This SDK is designed to be used in decentralized finance (DeFi) applications.
To install the Swap SDK, run the following command:
npm install @vvs-finance/swap-sdk
To fetch the best trade, you can use the fetchBestTrade
function. This function takes the following required parameters:
chainId
: The ID of the blockchain network.inputTokenAddressArg
: The address of the input token or "NATIVE" for the native token.outputTokenAddressArg
: The address of the output token or "NATIVE" for the native token.amount
: The amount to be used as input or output.
import { fetchBestTrade, SupportedChainId } from "@vvs-finance/swap-sdk";
const inputToken = "NATIVE";
const outputToken = "0x2D03bECE6747ADC00E1a131BBA1469C15fD11e03";
const amount = "2.5";
fetchBestTrade(SupportedChainId.CRONOS_MAINNET, inputToken, outputToken, amount)
.then((trade) => {
console.log(trade);
})
.catch((error) => {
console.error(error);
});
To execute a trade, you can use the executeTrade
function. This function takes the following required parameters:
chainId
: The ID of the blockchain network.trade
: The trade object obtained fromfetchBestTrade
.signer
: The signer object to sign the transaction.
import {
fetchBestTrade,
executeTrade,
SupportedChainId,
} from "@vvs-finance/swap-sdk";
const inputToken = "NATIVE";
const outputToken = "0x2D03bECE6747ADC00E1a131BBA1469C15fD11e03";
const amount = "2.5";
fetchBestTrade(SupportedChainId.CRONOS_MAINNET, inputToken, outputToken, amount)
.then(async (trade) => {
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
return executeTrade(SupportedChainId.CRONOS_MAINNET, trade, signer);
})
.catch((error) => {
console.error(error);
});
Fetches the best trade for the given parameters.
chainId
: The ID of the blockchain network. can be:SupportedChainId.CRONOS_MAINNET
SupportedChainId.CRONOS_TESTNET
(please setargOpts.quoteApiEndpoint
orQUOTE_API_ENDPOINT_338
env)
inputTokenAddressArg
: The address of the input token or "NATIVE" for the native token.outputTokenAddressArg
: The address of the output token or "NATIVE" for the native token.amount
: The amount to be used as input or output.argOpts
:Partial<BestAMMTradeOpts>
- Optional arguments for the trade.tradeType
:TradeType.EXACT_INPUT
orTradeType.EXACT_OUTPUT
- By default
tradeType
isTradeType.EXACT_INPUT
- When set to
TradeType.EXACT_OUTPUT
,amount
will be used as output amount
- By default
maxHops
: how much steps to consider between input and output token- default:
1
- default:
maxSplits
: for example swapping 100 CRO to VVS,{ maxSplits: 2 }
means the algorithm will consider splitting 100 CRO into 2 parts: 5 and 95 CRO to VVS, then 10 and 90 CRO to VVS...to see which one is most beneficial,- default:
1
- default:
poolTypes
: pool types to consider- possible types:
PoolType.V2
,PoolType.V3_100
,PoolType.V3_500
,PoolType.V3_3000
andPoolType.V3_10000
- default:
[PoolType.V2]
PoolType.V3_*
types are not available for now
- possible types:
slippageTolerance
,- When
TradeType.EXACT_INPUT
: this is used to calculate minimal output amount, transaction will revert if contract can't send out such amount - When
TradeType.EXACT_OUTPUT
: this is used to calculate maximum input amount, transaction will revert if contract is using more than such amount
- When
quoteApiEndpoint
: URL of routing API endpoint to overwrite
Promise<Trade>
- A promise that resolves to the trade object. This object containsamountIn
,amountOut
,routes
Executes the given trade on the blockchain.
chainId
: The ID of the blockchain network. can be:SupportedChainId.CRONOS_MAINNET
SupportedChainId.CRONOS_TESTNET
trade
:Trade
- The trade object obtained fromfetchBestTrade
.signer
:Signer
- The signer object to sign the transaction.options
:Partial<ExecuteTradeOptions>
- Optional arguments for executing the trade.recipient
: where output token should be sentdeadlineOrPreviousBlockhash
fee
: compensation for token taxesrouterAddress
: SmartRouter contract address to overwrite
Promise<TransactionResponse>
- A promise that resolves to the transaction response.
git clone https://github.com/vvs-finance/swap-sdk
cd swap-sdk
pnpm install
cp .env.example .env
pnpm test
- package source entrypoint:
packages/swap-sdk/src/index.ts
- tests:
packages/swap-sdk/test/
This project is licensed under the GNU GPLv3 License. See the LICENSE
file for details.