SDK Reference
The OdinTrade SDK provides a simple and efficient way to interact with the OdinTrade Atomic Swap protocol. This guide will help you integrate the SDK into your application. The current version of the SDK supports onlyTestnet environment, with multiple initialization options including private key injection and browser wallet providers. For browser wallet providers, the SDK supports only Xverse for Bitcoin and MetaMask, or Rabby Wallet, for Ethereum.
The testnets are Testnet3 for Bitcoin and Sepolia for Ethereum.
For Bitcoin, only Native SegWit (Bech32) addresses are supported, which in Testnet should start with tb1.
Accessing the SDK
Since the SDK is private onnpm, only the authorized users can access it. This requires setting up an .npmrc file with an access token. This file will be provided by the team (contact us at [email protected]).
Installation
Initialization
The SDK can be initialized in two ways depending on your use case:Backend Initialization (Private Key)
For backend applications, you can initialize the SDK using private keys:tests/sdk.spec.ts.
Frontend Initialization (Browser Wallet)
For frontend applications, you can use browser wallet providers like MetaMask:User Requirements for Swapping with Browse Wallets
- The user must have the appropriate wallet installed:
- MetaMask or Rabby Wallet for Ethereum
- Xverse for Bitcoin
- The user must approve the connection request when
connect()is called.
Browser Wallet Interactions per Function
createSwap
- The user must sign a message containing the timestamp with
addressIn.
confirmSwap (only for User Claim on Bitcoin )
- The user must sign the user HTLC claim transaction with
addressOut, in order to claim the HTLC. The transaction will be broadcasted by the LiquidityNode.
confirmUserHTLC (Ethereum)
- If swapping an ERC20 token for the first time, the user must approve the ERC20 transfer (this transaction is broadcasted directly by the user).
- If swapping ETH, the user must sign the userDeposit transaction each time they perform an ETH swap.
- The user must sign the meta-transaction for HTLC creation.
confirmUserHTLC (Bitcoin)
- The user must sign the User HTLC creation transaction, which is broadcasted by the LiquidityNode.
Available Methods
Get Token Swap Details
Calculate estimated exchangerates and amountOut, for token pairs.
Get Maximum Amount Out
Calculate the maximum amount of tokens you can receive based on your available balance.Create Swap
Initialize a new atomic swap. The SDK will automatically handle signing and address management. If it is not clear whyamountOut is required, instead of amountIn, please refer to the Protocol Flow section.
Confirm Swap
Confirm and execute the swap. The SDK will handle the appropriate signing based on the chain.swap is the swap object returned by the createSwap method.
Create User HTLC
Generate HTLC for the user’s part of the swap.swap is the swap object returned by the createSwap method.
Confirm User HTLC
Submit the signed HTLC transaction.swap is the swap object returned by the createSwap method and htlc is the htlc object returned by the createUserHtlc method.
Get Swap Status
Get detailed information about a specific swap.Get Swaps History
Get all swaps history. If addressIn or addressOut is provided, it will return only the swaps for that address, and if both are provided, it will return the swaps for both addresses. At least one address (addressIn or addressOut) is required.Error Handling
The SDK throws errors in the following cases:- Missing required signers for specific chains
- Invalid parameters
- Network errors
- API errors
Best Practices
- Always initialize the SDK with the appropriate signers for the chains you plan to use
- Handle errors appropriately in your application
- Keep your private keys secure and never expose them
- Use environment variables for sensitive information like API URLs and private keys
- Test thoroughly on testnet before moving to mainnet
