Bitcoin
JellySwap Bitcoin package
What is JellySwap?
JellySwap is a cross-chain atomic swap protocol that supports many different blockchains. The Bitcoin package is used to interact with the Bitcoin Hash Time Locked Contracts and to handle blockchain events. The main components of the module are Providers, Contract, Event, Adapter and Config.
Install package
Using yarn
$ yarn add @jelly-swap/bitcoinUsing npm
$ npm install @jelly-swap/bitcoinImporting
JavaScript (ES3)
const jellyBtc = require('@jelly-swap/bitcoin');JavaScript (ES6) / TypeScript
import * as jellyBtc from '@jelly-swap/bitcoin';Config Module
This module contains the main configuration for the whole package
Provider Module
A Provider abstracts a connection to the Bitcoin blockchain, for issuing queries and sending signed state changing transactions.
BitcoinProvider - allows you to connect to Bitcoin nodes you control or have access to, including mainnet, testnets, etc.
Contract Module
Create contract instance:
parameter
type
description
btcWallet
BtcWallet object
A bitcoin wallet object, with network, mnemonic, type & provider.
network
Network object
Can be mainnet, testnet or regtest (optional).
provider
object
Provider instance that will be used for transaction signing (optional).
subscribe
parameter
type
description
onEvent
function
Called when an event is received.
filter
function
Used to filter any received event (optional).
getPastEvents
Returns past blockchain events.
parameter
type
description
eventType
string
Can be 'new', 'withdraw' or 'refund'. If not passed all events will be returned.
filter
function
Filter the received events (optional). If not passed all events will be returned.
currentBlock
number or string
Read events up to the currentBlock (optional). If not passed the currentBlock will be assigned to the latest mined block.
getCurrentBlock
Returns the latest block.
getBalance
Returns the balance of the given address.
parameter
type
description
address
string
Bitcoin Public Address.
network
Network Object
Can be any testnet or mainnet.
newContract
Creates and broadcasts a new swap transaction.
parameter
type
description
swap
ContractSwap
Input data for the swap.
withdraw
Creates and broadcasts a withdraw transaction.
parameter
type
description
withdraw
ContractWithdraw
Input data for the withdraw.
refund
Creates and broadcasts a refund transaction.
parameter
type
description
contractRefund
ContractRefund
Input data for the refund.
Event module
Event module is used to read past blockchain events and subscribe for new ones
Initialize event module:
parameter
type
description
btcContract
BitcoinContract
Bitcoin contract instance.
getPast
Returns past blockchain events.
parameter
type
description
eventType
string
Can be 'new', 'withdraw' or 'refund'. If not passed all events will be returned.
filter
function
Filter the received events (optional). If not passed all events will be returned.
currentBlock
number or string
Read events up to the currentBlock (optional). If not passed the currentBlock will be assigned to the latest mined block.
subscribe
parameter
type
description
onEvent
function
Called when an event is received.
filter
function
Used to filter any received event. Optional.Adapter module
Adapter module
The Adapter module is used to format the client data and prepare the input for the swaps.
Initialize adapter module:
generateId
Generate swap id based on the input parameters. The same id will be generated by the Solidity Contract.
parameter
type
description
input
ContractSwap
Input data for the swap.
addressValid
Checks if the passed address is valid or not.
parameter
type
description
address
any
True if address is valid.
parseAddress
Returns the address in lower case.
parameter
type
description
address
any
Address in lower case.
parseToNative
Parse BTC amount to satoshi amount i.e. 0.1 to 10000000
parameter
type
description
amount
any
String representation of the satoshi amount.
parseFromNative
Parse satoshi amount to BTC amount i.e. 10000000 to 0.1
parameter
type
description
amount
any
String representation of the BTC amount.
formatInput
Returns object of type UserInputSwap.
parameter
type
description
data
UserInputSwap
User input data.
receiver
string
Address of the receiver (optional). If not passed uses the receiver from the config.
Example
Last updated
Was this helpful?