Tron

JellySwap tron package

What is JellySwap?

JellySwap is a cross-chain atomic swap protocol that supports many different blockchains. The tron package is used to interact with the Tron 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/tron

Using npm

$ npm install @jelly-swap/tron

Importing

JavaScript (ES3)

const jellyTron = require('@jelly-swap/tron');

JavaScript (ES6) / TypeScript

import * as jellyTron from '@jelly-swap/tron';

Config Module

This module contains the main configuration for the whole package.

parameter

type

description

expiration

number

The Time Lock of the transaction. Optional

Provider Module

A Provider abstracts a connection to the Tron blockchain, for issuing queries and sending signed state changing transactions.

HttpProvider - allows you to connect to Tron 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

address

string

Public tron address. Optional.

providerUrl

string

Can be trongrid or other provider on testnet or mainnet. Optional.

TronlinkProvider - used with tronWeb application, or tronWeb-compatible Provider.

parameter

type

description

tronWeb

object

Can be any tronWeb provider like the one from Tronlink.

WalletProvider - manages a private/public key pair which is used to cryptographically sign transactions and prove ownership on the Tron network. Use when there is no tronWebProvider available.

parameter

type

description

privateKey

string

Tron Private Key.

providerUrl

string

Can be trongrid or other provider on testnet or mainnet. Optional.

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.

getCurrentBlock

Returns the latest block.

getBalance

Returns the balance of the given address.

parameter

type

description

address

string

Tron 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

TronContractSwap

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

tronContract

object

Tron 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

ContractSwap

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.

parseOutputAddress

Returns tron address in hex format

parameters

type

description

address

string

Public tron address.

parseToNative

Parse tron amount to sun amount i.e. 1 to 1000000

parameter

type

description

amount

number

String representation of the sun amount.

parseFromNative

Parse sun amount to tron amount i.e. 1000000 to 1

parameter

type

description

amount

BigNumberish

String representation of the tron 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?