Getting started
Install module
Using NPM:
npm install @hop-protocol/sdkUsing Yarn:
yarn add @hop-protocol/sdkCDN
jsDeliver CDN:
<script src="https://cdn.jsdelivr.net/npm/@hop-protocol/sdk@latest/hop.js"></script>unpkg CDN:
<script src="https://unpkg.com/@hop-protocol/sdk@latest/hop.js"></script>Import module
Import as ES6 module (e.g. Using TypeScript, babel, webpack):
import { Hop } from '@hop-protocol/sdk'Import as commonJS module (e.g. Using Node.js directly or no ES6 modules):
const { Hop } = require('@hop-protocol/sdk')Instantiate SDK
Supported networks are mainnet and goerli, however the assets and chains available on goerli is limitted.
Instantiate with ethers Signer for sending transactions:
Example: Using window web3 provider as signer:
SDK with Node.js
Basic example that instantiates sdk with signer
Create .env file
Running example
Demo
A simple Hop SDK Demo:
https://sdk-demo.hop.exchange/
Examples
Unit conversion using ethers
The SDK accepts amounts as inputs as big number and most SDK methods return outputs as big numbers, so it's important to know how to convert to and from big numbers.
Token decimals
USDC
6
USDT
6
DAI
18
MATIC
18
ETH
18
WBTC
8
FRAX
18
HOP
18
Send tokens across chains
Note: when sending (or generally dealing with amounts in sdk), use values in big number format, e.g. 1 ETH = "1000000000000000000"
Send L1->L2
Note: there is no bonder fee when sending L1->L2 because transfers go through the native messaging bridge and don't require bonding.
Send L2->L1
Note: a bonder fee is required when sending L2->L1. It'll calculate it the bonder fee if don't is not explicitly specified. See specifying a custom fee for more info.
Send L2->L2
Note: a bonder fee is required when sending L2->L2. It'll calculate it the bonder fee if don't is not explicitly specified. See specifying a custom fee for more info.
Send ETH
Sending ETH is the same as sending any other ERC-20. The sdk handles inputting the tx value based on input amount.
Example
Get send calldata only
Using BigNumber as amount input
Specifying custom bonder fee
By default, the sdk will use the recommended bonder fee if one is not specified. You can set a custom bonder fee in the bonderFee field. See getSendData(...) for more info on the fee breakdown.
Sending to a custom recipient
Specifying custom min amount out
Note: The amountOutMin will be 0 if one is not specified. You can use the result from getSendData(...) to calculate the estimated amountOutMin value.
Note: Do not set destinationAmountOutMin and destinationDeadline when sending to L1. There is no AMM on L1 so these parameters should not be used, otherwise the calculated transferId will be invalid the transfer will be unbondable.
Specify custom deadline
Note: the deadline will be 7 days if one is not specified. The deadline is required for L2->L1 transfers and L2->L2 transfers for the AMM swap at the origin chain. Additionally, destinationDeadline is also required for L2->L2 transfers to swap the hTokens for the canonical tokens at the destination.
Note: Do not set destinationDeadline and destinationAmountOutMin for L2->L1 since there is no L1 AMM at the destination.
Get approval address for sending over bridge
Get approval address for sending hTokens over bridge
Get allowance for bridge approval address
Check allowance and send approval
Estimate tokens amount out from swap
Call this to estimate how many tokens the swap will provide (does not take account slippage or bonder fee):
Estimate tokens that will be received at destination
Call this to estimate how you'll receive at the destination. This is what the UI uses to show user the estimated tokens amount out.
The estimatedReceived value takes account the bonder fee and destination transaction fee.
Estimate amountOutMin
Call this to estimate the minimum amount of tokens you should receive at the destination when including slippage.
Note: Do not set destinationAmountOutMin and destinationDeadline when sending to L1 because there's is no AMM on L1.
Estimate total bonder fee
The total bonder fee is bonderFee+ destinationTxFee
Make sure to use the total bonder fee when sending a transfer over the Hop bridge.
The method bridge.getTotalFee(...) is a simple wrapper for const { totalFee } = await bridge.getSendData(...).
The following examples below are for retrieving the individual fees if you're interested in the breakdown of the total bonder fee.
Estimate base bonder fee:
The base bonder fee is the fee the bonder takes for fronting liquidity at the destination. The bonder receives this fee when bonding the transfer at the destination.
Estimate bonder destination transaction fee:
The bonder destination transaction fee is the regular chain transaction fee that the bonder pays to get transaction included in block. The sender of the transfer must compensate the bonder for this fee which is why this fee exists. The destination transaction fee returned here is in terms of the asset being transferred.
Get all send data info:
Return values of getSendData
amountOut
BigNumber
The estimated amount out but without taking account slippage or fees. Use the value estimatedReceived if you're looking for the final estimated out amount which takes account fees.
rate
Number
The ratio between amountIn and amountOut
priceImpact
Number
Price impact percentage. Depositing underpooled assets will give you bonus LP tokens. Depositing overpooled assets will give you less LP tokens (shown as negative price impact).
requiredLiquidity
BigNumber
The amount of required liquidity the bonder must have in order for this transfer to go through. Use getFrontendAvailableLiquidity(...) to check total available liquidity before sending.
lpFees
BigNumber
The AMM swap fees for amountIn
adjustedBonderFee
BigNumber
Small fee bonder takes for fronting liquidity at the destination when bonding transfer. There is a bonderFee going L2->L2 or L2->L1. There is no bonder fee going L1->L2.
adjustedDestinationTxFee
BigNumber
The destination transaction fee that the bonder has to pay when bonding transfer. The sender pays this fee in terms of the token being sent.
totalFee
BigNumber
The total fee consists of bonderFee + destinationTxFee. Use this totalFee value for the bonderFee parameter when sending transfer going L2->L2 or L2->L1.
estimatedReceived
BigNumber
The estimated amount that will be received at destination when slippage and all fees (bonderFee + destinationTxfee) are accounted for. This is what the UI displays.
The getSendData logic can viewed in github here if you would like to know how it works or implement in another language.
Get available liquidity for transfer
A transfer can only be bonded if the availale liquidity amount is greater than or equal to the transfer amount. The transfer bond at the destination may be delayed if it is sent when there is low or no available liquidity.
Get pool deposit price impact percent
Depositing underpooled assets will give you bonus LP tokens. Depositing overpooled assets will give you less LP tokens (shown as negative price impact).
Get transfer status
Watch for receipt events
Note: You can also utilize TheGraph for quering for transaction receipts at the destination. See Hop TheGraph docs.
Set custom provider RPC URLs
Withdraw an unbonded transfer at the destination
Get the populated withdraw transfer transaction data:
Send transaction to withdraw transfer:
Note: A transfer can be withdrawn only after the transfer root has been set at the destination chain, which may take a few hours or days depending on the chain.
The UI to withdraw transfers is found at https://app.hop.exchange/#/withdraw
More examples
If you'd like to see more examples or have any feedback, message us on Discord!
SDK API Reference
API ReferenceContract addresses
Contract AddressesLast updated