Ethereum
JellySwap ethereum package
What is JellySwap?
JellySwap is a cross-chain atomic swap protocol that supports many different blockchains. The Ethereum package is used to interact with the Ethereum 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
Using npm
Importing
JavaScript (ES3)
JavaScript (ES6) / TypeScript
Config Module
This module contains the main configuration for the whole package
Provider Module
A Provider abstracts a connection to the Ethereum blockchain, for issuing queries and sending signed state changing transactions.
JsonRpcProvider - allows you to connect to Ethereum nodes you control or have access to, including mainnet, testnets, proof-of-authority (PoA) nodes or Ganache by providing a URL to the node.
parameter
type
description
providerUrl
string
Can be infura or other provider on testnet or mainnet.
Web3Provider - used with Web3 application, or Web3-compatible Provider (e.g. MetaMask’s web3.currentProvider).
parameter
type
description
web3
object
Can be any web3 provider like the one from Metamask.
WalletProvider - manages a private/public key pair which is used to cryptographically sign transactions and prove ownership on the Ethereum network. Use when there is no Web3Provider available.
parameter
type
description
privateKey
string
Ethereum Private Key.
providerUrl
string
Can be infura or other provider on testnet or mainnet.
Contract Module
Contract module is used to interact with the Solidity Contract and get blockchain specific data (balance, blockNumber, subscribe to events, get past events)
Create contract instance:
parameter
type
description
provider
object
Provider instance that will be used for transaction signing.
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'
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
Ethereum Public Address.
getStatus
Returns the swaps statuses by given ids.
parameter
type
description
ids
array
List of ids.
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
ethContract
EthereumContract
Ethereum contract instance.
getPast
Returns past blockchain events.
parameter
type
description
eventType
string
can be 'new', 'withdraw' or 'refund'
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 ether amount to wei amount i.e. 0.1 to 100000000000000000
parameter
type
description
amount
any
String representation of the wei amount.
parseFromNative
Parse wei amount to ether amount i.e. 100000000000000000 to 0.1
parameter
type
description
amount
any
String representation of the ether 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