Velar-SDK is a user-friendly library to integrate swap functionality.
npm install @velarprotocol/velar-sdkVelar-SDK is a user-friendly library to integrate swap functionality.
``js
import { ContractPrincipalCV, PostCondition, PostConditionMode, UIntCV } from "@stacks/transactions";
export interface SwapConfig {
account: string;
inToken: string; // Token Contract Address
outToken: string; // Token Contract Address
}
export interface ISwapService {
swap(args: SwapPayload): Promise
getComputedAmount(args: ComputedAmountPayload): Promise
}
export interface ComputedAmountPayload {
amount: Number;
slippage?: Number;
}
interface DestinationToken {
symbol: string,
contractAddress: string,
tokenDecimalNum: Number,
assetName: string,
}
export interface AmountOutResponse {
value: Number,
amountOut: Number // raw amount out
path: Array
path2: Array
destinationToken: { symbol: string, contractAddress: string, tokenDecimalNum: Number, assetName: string } // destination token details
amountOutDecimal: Number // amountOutWithDecimals
route?: Array
result?: any // multihop routes results
}
export interface SwapResponse {
contractAddress: string,
contractName: string,
functionName: string,
functionArgs: [
UIntCV, // pool id
ContractPrincipalCV, // pool token0 address
ContractPrincipalCV, // pool token1 address
ContractPrincipalCV, // in token address
ContractPrincipalCV, // out token address
ContractPrincipalCV, // staking contract
UIntCV, // amount in
UIntCV // amount out
],
postConditions: Array
postConditionMode: PostConditionMode,
}
export declare class SwapService {
constructor(args: SwapConfig);
swap(args: SwapPayload): Promise
getComputedAmount(args: ComputedAmountPayload): Promise
}
`
`js
export interface VelarSDKConfig {
headless: boolean // for Node.js env default false
}
export declare class VelarSDK {
constructor(args?: VelarSDKConfig);
getPairs (symbol: string): Promise
getSwapInstance(args: SwapConfig): SwapService;
}
const sdk = new VelarSDK({
headless: false, // set to true to disable logging/UI-related behavior
});
`
`js
import {
VelarSDK,
getTokens,
ISwapService,
SwapResponse,
} from '@velarprotocol/velar-sdk';
import { openContractCall } from '@stacks/connect';
const sdk = new VelarSDK();
async () => {
const account = '';
// setup swap instance
const swapInstance: ISwapService = await sdk.getSwapInstance({
account: account,
inToken: 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.wstx',
outToken: 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token',
});
// to display amount out
const amount: AmountOutResponse = await swapInstance.getComputedAmount({
amount: 10,
});
// get swap calculated arguments for contract call
const swapOptions: SwapResponse = await swapInstance.swap({
amount: 10,
});
// build options for contract call
const options = {
...swapOptions,
network: AppService.getNetwork(),
appDetails: AppService.getAppDetails(),
anchorMode: AnchorMode.Any,
onFinish: data => {},
onCancel: ex => {},
};
// open contract call
await openContractCall(options);
};
`
js
import { VelarSDK } from '@velarprotocol/velar-sdk';
const sdk = new VelarSDK();
const CUSTOM_URL = 'https://api.mainnet.hiro.so';
sdk.setBlockChainApiUrl(CUSTOM_URL)
``