@teleportdao/teleswap-sdk
v1.9.0
Published
The Teleswap SDK provides comprehensive tools and utilities for integrating Bitcoin and Polygon blockchains. It empowers developers to build decentralized applications that enable users to transfer Bitcoin assets to the Polygon network and take advantage
Downloads
34
Keywords
Readme
How to Use TeleSwap SDK
First, create an instance of SDK and initialize it.
- default bitcoin connection is using MempoolSpace but you can also use you custom bitcoinConnection
BitcoinConnectionInfo = {
api: {
token?: string
provider: "BlockStream" | "MempoolSpace"
}
rpc?: {
enabled?: boolean
url: string
headers?: {
[key: string]: string
}
auth?: {
username: string
password: string
}
}
utxo?: {
token?: string
provider: "BlockStream" | "NowNodes" | "MempoolSpace" | "Unisat"
}
}
supported target networks
- polygon
- bsc
- bsquared
- bob
- polygon_testnet
Create Instance Using Web3 Endpoint:
let testnet = false
let networkName: "polygon_testnet" | "polygon" = testnet ? "polygon_testnet" : "polygon"
const bitcoinConnection = undefined // use default
const defaultTargetNetwork = {
networkName, // polygon bsc bsquared bob
web3: {
url: "https://polygon connection endpoint for web3 ",
},
}
const unisatToken = "" // only for brc20
let sdk = new TeleswapSDK(testnet, bitcoinConnection, defaultTargetNetwork)
to interact with other target networks, you need to add them to sdk
sdk.addNetwork({
networkName: "ethereum", //
web3: {
url: "https://ethereum",
},
})
to use with metamask add network like this
sdk.addNetwork({
networkName,
provider: window.ethereum,
})
After creating the instance, you need to initialize the account using Mnemonic for both Bitcoin and target networks, otherwise, you can just create unsigned transactions.
- If you want to sign Bitcoin transactions using other wallets, ignore initializing the Bitcoin account (just use SDK to create unsigned transactions).
Initialize Account Using Mnemonic
// Init Bitcoin account
let mnemonic = "..."
let bitcoinAddress = sdk.initBitcoinAccountUsingMnemonic(mnemonic)
// Init Polygon account
let polygonAddress = sdk.initEvmAccountUsingMnemonic(mnemonic)
using the following methods, you can get the addresses of Bitcoin and Polygon accounts
let bitcoinAddress = sdk.bitcoinAddress
let bitcoinAddressType = sdk.bitcoinAddressType
let targetNetworkAddress = sdk.evmAddress
Functions
Wrap
This function facilitates transferring BTC from Bitcoin to Polygon and receiving TELEBTC in return.
// If the Bitcoin account is initialized, use this function to create, sign and send wrap requests.
let amountInBTC = 0.001
let polygonRecipientAddress = "0x..."
let bitcoinTxId = await sdk.wrap(polygonRecipientAddress, amountInBTC)
// create unsigned transaction
// You need to provide Bitcoin account details to create an unsigned transaction
const signerInfo: {
addressType: "p2wpkh" | "p2pkh" | "p2sh-p2wpkh"
publicKey: string
address: string
}
const bitcoinChangeAddress = "" // Bitcoin change address
let unsignedTxResponse = await sdk.wrapUnsigned(
polygonRecipientAddress,
amountInBTC,
signerInfo,
networkName, // optional -> if not set use default target network
)
const unsignedPSBT = unsignedTxResponse.unsignedTransaction
// Sign unsignedTx
let signedTx = ...
// Send signed Tx
let bitcoinTxId = await sdk.sendSignedTransaction(signedTx)
Exchange (Bitcoin -> Polygon)
This function facilitates exchanging BTC on Bitcoin for other tokens on Polygon.
// You need to provide the relevant exchange information
let amountInBTC = 0.001
let polygonRecipientAddress = "0x..."
let {
txId: bitcoinTxId,
percentageFee,
lockerAddress,
} = await sdk.wrapAndSwap(polygonRecipientAddress,amountInBTC, , exchangeTokenAddress )
Unwrap
This function facilitates burning TELEBTC on Polygon and receiving BTC in return.
let amountInBTC = 0.001
let recipientBitcoinAddress = "..."
let {
burnTxId, // polygonTxId
approvedTxId, // null if teleBTC approved before (polygonTxId)
lockerAddress,
} = await sdk.unwrap(amountInBTC, recipientBitcoinAddress)
Swap And Unwrap
This function facilitates burning WMatic or other Tokens like USDC on Polygon and receiving BTC in return.
let amountInBTC = 0.001 // Expected received amount
let recipientBitcoinAddress = "..."
let exchangeInfo = {
inputToken: "Token Contract Address",
inputAmount: 50, // for example (50 WMatic)
isFixedToken: true, // True if the input token amount is fixed
}
let response = await sdk.swapAndUnwrap(exchangeInfo, recipientBitcoinAddress)