Integrate Blockchains easily
Full Installation (To be added)
Partial Installation
If you want to install one part of SwapKit SDK, you can install it separate instances of wallets & toolboxes.
For example, if you want to use SwapKit SDK with EVM chains and Ledger wallet, you can install @thorswap-lib/toolbox-evm
, @thorswap-lib/ledger
and @thorswap-lib/swapkit-core
packages.
pnpm
pnpm add @thorswap-lib/toolbox-evm @thorswap-lib/ledger @thorswap-lib/swapkit-core
yarn
yarn add @thorswap-lib/toolbox-evm @thorswap-lib/ledger @thorswap-lib/swapkit-core
npm
npm install @thorswap-lib/toolbox-evm @thorswap-lib/ledger @thorswap-lib/swapkit-core
Architecture of SwapKit SDK is pretty simple. It's based on the concept of toolboxes. Each toolbox is responsible for interacting with specific blockchain. For example, @thorswap-lib/toolbox-evm
is responsible for interacting with ETH, AVAX, BSC, etc. Toolboxes are extending SwapKitCore instance with methods to interact with specific blockchain. SwapKitCore is responsible for managing wallets and providing unified interface for interacting with them. To extend SDK with wallet support you need to pass array of wallets to extend
method. Wallets are responsible for interacting with specific wallet provider. After extend
method is called, you can start connecting to wallets and interacting with them.
import { Chain, FeeOption } from '@thorswap-lib/types';
import { SwapKitCore } from '@thorswap-lib/swapkit-core';
import { xdefiWallet } from '@thorswap-lib/xdefi';
import { evmWallet } from '@thorswap-lib/evm-web3-wallets';
import { keplr } from '@thorswap-lib/keplr';
import { keystoreWallet } from '@thorswap-lib/keystore';
import { ledgerWallet } from '@thorswap-lib/ledger';
import { trezorWallet } from '@thorswap-lib/trezor';
import { walletconnectWallet } from '@thorswap-lib/walletconnect';
const getSwapKitClient = () => {
const client = new SwapKitCore()
client.extend({
config: {
utxoApiKey: ''
covalentApiKey: '',
ethplorerApiKey: '',
walletConnectProjectId: '',
},
wallets: [
evmWallet, // MetaMask, BraveWallet, TrustWallet Web, Coinbase Wallet
keplrWallet,
keystoreWallet,
ledgerWallet,
trezorWallet,
walletconnectWallet,
xdefiWallet,
],
});
return SKClient;
}
// [44, 60, 2, 0, 0]
const llderivationPath = getDerivationPathFor({ chain: Chain.ETH, index: 2, type: 'ledgerLive' })
// [44, 60, 0, 0, 2]
const derivationPath = getDerivationPathFor({ chain: Chain.ETH, index: 2 })
const connectLedger = (chain: Chain) => {
await getSwapKitClient().connectLedger(Chain.ETH, derivationPath)
// { address: '0x...', balance: [], walletType: 'LEDGER' }
const walletData = await getSwapKitClient().getWalletByChain(Chain.ETH)
}
// quoteRoute is returned from `/quote` API endpoint
// https://dev-docs.thorswap.net/aggregation-api/examples/Swap#fetch-quote
const quoteParams = (sender: string, recipient: string) => {
sellAsset: 'ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044',
buyAsset: 'BTC.BTC',
sellAmount: '1000',
senderAddress: sender,
recipientAddress: recipient
}
const baseUrl = `https://api.thorswap.net/aggregator`;
const paramsStr = new URLSearchParams(quoteParams).toString();
const fetchQuote = (sender: string, recipient: string) => {
const params = quoteParams(sender, recipient)
const paramsStr = new URLSearchParams(params).toString();
return fetch(`${baseUrl}/tokens/quote?${paramsStr}`).then(res => res.json())
}
const swap = async () => {
const senderAddress = '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC'
const recipient = 'bc1qcalsdh8v03f5xztc04gzqlkqhx2y07dakv7f5c'
const { routes } = fetchQuote()
// select best route from routes -> it has `optimal` flag set to true
const route = routes[0]
if (await getSwapKitClient().validateAddress({ chain: Chain.BTC, address: recipient })) {
const txHash = await SKClient.swap({
route,
// Fee option multiplier -> it will be used if wallet supports gas calculation params
feeOptionKey: FeeOption.Fastest,
recipient
})
// txHash: '0x...'
}
}
This repo contains packages around SwapKit sdk and its integrations with different blockchains.
Package | Description | Chains |
---|---|---|
@thorswap-lib/swapkit-core | Core package for SwapKit | - |
@thorswap-lib/toolbox-evm | Toolkit to integrate EVM chain | ETH, AVAX, BSC |
@thorswap-lib/toolbox-utxo | Toolkit to integrate UTXO chain | BTC, LTC, DOGE, BCH |
@thorswap-lib/toolbox-cosmos | Toolkit to integrate Cosmos chains | THOR, ATOM, BNB |
@thorswap-lib/keystore | Keystore implementation | All chains supported by toolboxes |
@thorswap-lib/ledger | Ledger implementation | All chains supported by toolboxes |
@thorswap-lib/trezor | Trezor implementation | BTC, ETH, LTC, DOGE, BCH, AVAX |
@thorswap-lib/walletconnect | Walletconnect implementation | ETH, THOR, BNB, AVAX |
@thorswap-lib/keplr | Keplr implementation | ATOM |
@thorswap-lib/xdefi | XDEFI implementation | All chains |
@thorswap-lib/evm-web3-wallets | EVM Browser Extensions | See more |
npm install -g pnpm
Copy .env.example to .env and fill it with data
pnpm bootstrap;
main
- production branchdevelop
- development branch - all PRs should be merged here first
To run tests use pnpm test
command.
- PRs should be created from
develop
branch - PRs should be reviewed by at least Code Owner (see CODEOWNERS file)
- PRs should have scope in commit message (see commit messages section)
- PRs should have tests if it's possible
- PRs should have changeset file if it's possible (see release section)
To create new package use pnpm generate
and pick one of the options
It will setup the package with the necessary files for bundling and publishing.
New toolbox(TBA)
New wallet(TBA)
Packages are automatically published to npm when new PR is merged to main
& develop
branches.
To automate and handle process we use changesets and github action workflows.
Before running pnpm changeset
you have to pull main
& develop
To release new version of package you need to create PR with changes and add changeset file to your commit.
pnpm changeset
After PR is merged to develop
branch with changeset file, github action will create new PR with updated versions of packages and changelogs.