Erc20

JellySwap erc20 package

What is JellySwap?

JellySwap is a cross-chain atomic swap protocol that supports many different blockchains. The erc20 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

$ yarn add @jelly-swap/erc20

Using npm

$ npm install @jelly-swap/erc20

Importing

JavaScript (ES3)

const jellyErc20 = require('@jelly-swap/erc20');

JavaScript (ES6) / TypeScript

import * as jellyErc20 from '@jelly-swap/erc20';

Config Module

This module contains the main configuration for the whole package.

parameter

type

description

token

string

Name of the token. Optional.

expiration

number

The Time Lock of the transaction. Optional

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.

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

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.

token

string

Token symbol.

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

Erc20ContractSwap

Input data for the swap.

checkAllowance

bool

Check ERC20 token allowance. Optional.

withdraw

Creates and broadcasts a withdraw transaction.

parameter

type

description

withdraw

Erc20ContractWithdraw

Input data for the withdraw.

refund

Creates and broadcasts a refund transaction.

parameter

type

description

contractRefund

Erc20ContractRefund

Input data for the refund.

getTokenContract

Returns new token contract instance.

parameter

type

description

tokenAddress

string

The address of desired ERC20 token

Event module

Event module is used to read past blockchain events and subscribe for new ones.

Initialize event module:

parameter

type

description

erc20Contract

Erc20Contract

Erc20 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

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

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

Erc20ContractSwap

Input data for the swap.

addressValid

Checks if the passed address is valid or not.

parameter

type

description

address

string

True if address is valid.

parseAddress

Returns the address in lower case.

parameter

type

description

address

string

Address in lower case.

parseToNative

Parse token amount to amount based on token decimals i.e. 0.1 to 100000000000000000

parameter

type

description

amount

number

String representation of the wei amount.

parseFromNative

Parse amount to token amount based on the token decimals i.e. 100000000000000000 to 0.1

parameter

type

description

amount

BigNumberish

String representation of the ether amount.

formatInput

Returns object of type UserInputSwap.

parameter

type

description

data

Erc20UserInputSwap

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?