SDK
Javascript SDK for doing cross-chain transactions
Introduction
Hyphen SDK is a javascript-based SDK written in typescript that helps in integrating Hyphen services easily. It provides some helper methods that make the Hyphen integration very easy. Even though you can interact with the LiquidityPool smart contract directly and deposit tokens using the depositErc20
method from your DApp to do the deposit transactions, we recommend using the SDK as it provides methods like checking available liquidity before doing the transaction, checking exit transaction status, checking approvals, etc.
Let's get started
1. Installation
2. Importing and Instantiation
HYPHEN accepts a provider object as the first argument and an option
object as the second argument.
Note: <Provider Object> is the provider object exposed by your connected wallet. It can be window.ethereum
for Metamask or portis.provider
for Portis and so on. The first provider object should be a provider object with accounts information.
It can either be a JSON RPC Provider object or HttpProvider object with an RPC URL given that you have passed walletProvider
object in the options and Biconomy support is present. Check Network Agnostic Support section.
3. Initialization
After the SDK object is instantiated you need to initialize the SDK by calling its init
function. The initialization is kept separated from the constructor to give flexibility to the developer to use await statement for initializing the SDK.
Keep your code in a try-catch block to catch any error that might be thrown from the SDK during instantiation or initialization.
Time to do a Cross-Chain Transfer
Before we proceed let's define some terms which will be used frequently in this documentation
Terms
Definition
toChain
Target chain where the funds needs to be transferred.
fromChain
Source chain from where the funds needs to be transferred to toChain
toChainId
chain id of the target chain where funds needs to be transferred
fromChainId
chain id of the source chain from where funds needs to be transferred
LiquidityPool
Smart Contract where liquidity is present. It is pre-deployed on each supported chain by Biconomy.
Now that we have initialized the SDK and defined the terms we can do the cross-chain transfer transaction. In order to do a cross-chain transfer, some pre-checks need to be done and some pre-actions need to be taken if required.
SDK provides methods to do these pre-checks and do some pre-actions so you don't have to write extra code to do the same.
Pre-Checks
These checks can be done using the method preDepositStatus
that will check for a given fromChainId, toChainId, tokenAddress, userAddress and amount, whether there is enough liquidity on toChain, the given tokenAddress is supported, enough approval is given by the user to LiquidityPoolManager contract on fromChain and the networks are supported or not.
Pre-Actions
If the approval for the token to be transferred is not given to LiquidityPool contract on fromChain then the first approval transaction needs to be done. You can directly interact with the token contract and call its approve or permit method but to make things simple SDK also provides a method to do this.
Once the approvals are checked and if required approval transaction is done. All you need to do is make only one deposit transaction on LiquidityPool on fromChain and the rest will be done by Hyphen.
Let's do the Deposit Transaction
You can call the deposit
method provided in the SDK to initiate a deposit transaction. Make sure to call preDepositStatus
method before calling deposit
method.
The deposit
method checks if there's enough liquidity available on toChain for the given amount of tokens to be transferred considering all the pending transactions for the same token. It also checks if there's enough approval given to LiquidityPool contract by the sender address for the given amount of tokens to be transferred.
Once the deposit transaction is confirmed, rest of the transactions to make the cross-chain transfer will be done by off-chain Executor nodes run by Biconomy. Now you can wait for the cross-chain transfer transaction to be done.
In the next section, we'll see how to listen for the status of our cross-chain transfers once our deposit transaction is successful. Or what if you don't get the funds on the destination chain within 5 min after your successful deposit transaction.
Enable Network Agnostic Gasless Transactions
You can enable network agnostic gasless transactions using Biconomy so that the user can make Approve and Deposit transactions on a chain e.g., Polygon while still keeping his wallet pointing to the Ethereum chain.
To enable this, you need to pass walletProvider
object and biconomy
options in the second parameter of Hyphen constructor as mentioned below:
Here, Provider Object 1 is the JSON RPC provider object pointing to the 'fromChain' where the user will be depositing his tokens. It doesn't need to have any accounts information.
Provider Object 2 has to be a provider object from the connected user wallet. It doesn't matter on which network the User Wallet is because we'll be using this provider object to just get the user signatures to do gasless transactions on the chain pointed by Provider Object 1.
So when the user initiates a deposit transaction using deposit
method, Hyphen SDK will use Biconomy SDK to send the deposit transaction, internally Biconomy SDK will check the details of the called method and smart contract from Biconomy Dashboard. This step is mandatory and the called method needs to be registered on the Biconomy Dashboard.
Once the details are matched with the Biconomy dashboard data, Biconomy SDK will ask the user for the signature using walletProvider
object passed and the user will get a signature popup on his wallet.
Once the user provides his signatures, the transaction is routed via Biconomy and the transaction will be executed on the fromChain pointed by Provider Object 1.
Signing Transactions Using An Imported Private Key
In some cases, the SDK may be used outside of the browser and will require an automated experience. The following snippet demonstrates how to perform the same steps above but with your private key.
Instantiate Hyphen and an Ethers Wallet
Pre-Deposit Operations
These are the same as before except that the approveERC20
function receives the wallet
object.
Perform the Deposit Transaction
The wallet
object is passed as the last parameter to the deposit
function.
Last updated
Was this helpful?