Rsk Swap SDK acts eases the integration between client applications and the RSK Swap API. Which is an aggregator that providers several ways to perform swaps to obtain RBTC through different 3rd party providers.
For most users, simply install the SDK from npm:
npm install @rsksmart/rsk-swap-sdkFor installing private packages from the GitHub registry (requires authentication), see the Building from Source section below.
Now that you have installed the SDK in your project, you can start using it as shown in the Usage section.
Create RskSwapSDK client instance. For this SDK, the blockchain connection is mandatory. There are 3 ways to create an RSK connection, you can check them in [BlockchainConnection class](TODO url) documentation.
const blockchainConnection: BlockchainConnection = await BlockchainConnection.createUsingStandard(window.ethereum)
const sdk: RskSwapSDK = new RskSwapSDK('Local', blockchainConnection)To know which providers can facilitate a specific swap you should perform an estimation. The amount must be in the lowest unit for the origin currency. For example, if you're swapping BTC -> RBTC the amount must be in sats and if you're swapping RBTC -> USDT the amount must be in wei.
const amount = 250_000_000
const estimations = await sdk.estimateSwap({
fromChainId: '1',
toChainId: '30',
fromToken: 'ETH',
toToken: 'RBTC',
fromAmount: amount
})🚧 Important
Notice that to identify the networks we use the Chain ID, currently there are two special cases where we don't use it. These are Bitcoin and the Lightning Network. For Bitcoin it must be passed
BTCas Chain ID and for the Lightning NetworkLN. The differentiation between testnet and mainnet is done through the token (BTCortBTC).
You can also fetch the limits of a specific pair to know what is the minimum and maximum amounts you can swap for a specific pair.
const args: SwapLimitsArgs = {
fromToken: 'ETH',
toToken: 'RBTC',
toNetwork: '30',
fromNetwork: '1',
}
const limits = await sdk.getSwapLimits(args)Then, once you know which provider to use, you can start doing operations with the client by indicating the providerId
const args: CreateSwapArgs = {
fromAmount: amount,
fromToken: 'ETH',
toToken: 'RBTC',
toNetwork: '30',
fromNetwork: '1',
providerId: estimations[1].providerId,
address: '<your address in the destination network>',
refundAddress: '<your address in the origin network>'
}
const result = await sdk.createNewSwap(args)Finally, the created swap will contain a [SwapAction](TODO url) indicating the SDK the required actions to perform your swap. In some cases, like EVM transactions, the RskSwapSDK executes the transaction by using the connected provider. But in cases like Bitcoin or Lightning the executeSwap function will just return the proper BIP21 or BOLT11 string so the consumer application can present it for the user.
const txHash = await sdk.executeSwap(result.action)
const receipt = await blockchainConnection.getTransactionReceipt(txHash)Depending on your provider, the swap might not arrive automatically to your wallet. In that case a claim transaction is required, this information will be available in the [SwapAction](TODO url) object. This doesn't necessarily mean that a EVM transaction will be produced, as the claim might happen in a non EVM chain, for the current integrations that RskSwap has, there is not action required from the consumer besides executing claimSwap when the claim is not in a EVM chain.
if (result.action.requiresClaim) {
const claimTxHash = await sdk.claimSwap(result)
}
⚠️ The [swap object](TODO url) contains a context field with all the information specific to the provider performing the swap. It's very important that the SDK consumer stores this object securely as it might contain private information used to claim or refund swaps. E.g. private keys for atomic swaps. It's also important to remark that when this is the case, the private information is always securely generated by the SDK in client side and is not sent to the server.
This SDK only requires the [BlockchainConnection class](TODO url) instance and the environment name to be used. Regarding the BlockchainConnection it mustn't be readonly as the SDK will try to broadcast transactions during the execute step.
If you want to contribute to the SDK or build it from source, you'll need to set up your local development environment.
Before setting up the project locally, ensure you have the following requirements:
This project uses Node.js version 19.6.0 as specified in the .nvmrc file. To install and use the correct version:
-
If you're using
nvm(Node Version Manager), run:nvm use
This will automatically switch to the version specified in
.nvmrc. -
Alternatively, you can manually install Node.js v19.6.0 from nodejs.org.
The prepare script requires Python 3 to install and configure pre-commit hooks. Ensure Python 3 is installed on your system:
- Check if Python 3 is installed:
python3 --versionorpython --version - Install Python 3 from python.org if needed
If you need to install private versions of this package from the GitHub registry, you'll need a GitHub personal access token with the following configuration:
- Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
- Generate a new classic token with the
read:packagesscope - Use this token when prompted for authentication when running
npm login
⚠️ Important: The token must be a classic token with theread:packagespermission. Fine-grained tokens are not supported.
To build the SDK locally:
-
Clone the repository:
git clone [email protected]:rsksmart/rsk-swap-sdk.git cd rsk-swap-sdk
-
Install dependencies (this will trigger the prepare script which requires Python 3):
npm i
-
Build the SDK:
npm run build:clean
If you need to install private versions from the GitHub registry, authenticate first:
npm login --scope=@rsksmart --auth-type=legacy --registry=https://npm.pkg.github.comYou'll need to provide your GitHub username and the classic token with read:packages scope mentioned in the Development Prerequisites section above.
To see the full API of this package please refer to the the docs folder of this project