SDK

Install OpenOcean Aggregator SDK

You can install the SDK by npm.n

Or, if you use yarn as your module management tool.

Or, if you want to build up a jwallet and contract object by yourself, you will need web3 and bignumber.js.

How to use the sdk in your project

You can then use all the functions explored by the SDK (API and swapSdk).

Start programing now!

Before you start developing a DeFi trading project, you should understand the industry’s most general DeFi trading workflow. Here is the process we designed for the user to make a transaction:

  1. Choose a chain.

  2. Choose your wallet.

  3. Choose the token pair.

  4. Input the amount you want to trade.

  5. Swap. To make this process successful, we recommend using the Open API and Wallet plugin we provide to achieve DeFi trading.

Full vue example

Choose Pair List

You can call the get Token List API to get all the available tokens we have on the blockchain you choose.

  • Method: Get

  • URL: https://open-api.openocean.finance/v3/:chain/tokenList

  • Parameters:

Parameter
Type
Example
Description

chain

string

avax

The chain name you want to search token

Your request will look like this:

And your response will look like this:

You need to save the token information you need for further operations.

Here is the SDK method for you to get the token list

Connect Wallet

Connect wallet is the first step you need to participate in DeFi trading. For example, you want to connect to MetaMask, so you have to get the MetaMask wallet constructor from OpenOcean wallet.

Please note that multiple browser wallet will have conflict, please only open a purse, (metaMask trustWallet, coin98Wallet) can't open at the same time.

Or you can trigger the wallet directly by the SDK.

Run the contract in project

Once you get your wallet connected, you can use Web3.js or ethers. These are tools to create the operable object for the contract, which serves for token approving, balance checking, and swapping.

  • Here is the example to init a contract object for you to call the abi in contract

Or, if you want to use ethers.

Get Balance

Once your wallet is connected and the address is displayed, you can use the SDK or directly use wallet object to get the balance from your wallet.

  • Use wallet API:

  • Use Open API:

  • Use SDK:

GetGasPrice

  • Use Open API:

  • Use SDK:

Approve

Approving assets is necessary for DeFi users to authorize the contract to use their tokens to swap. As with the getBalance method, you can use the wallet method or directly use our SDK to get a specific token approved for trading.

  • Use wallet Function:

  • Use SDK:

Quote

You can directly use Open API to quote the token exchange amount.

  • Method: Get

  • URL: https://open-api.openocean.finance/v3/:chain/quote

  • Parameters:

Parameter
Type
Example
Description

chain

string

bsc, avax, fantom

platform(openoceanv2,1inch,matcha,paraswap)

inTokenAddress

string

0x9029FdFAe9A03135846381c7cE16595C3554e10A

sell token address

outTokenAddress

string

0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE

buy token address

amount

number

10

sell amount

gasPrice

number

5

you can set it yourself or get it through GetGasPrice

slippage

number

1

1 equals 1%, ranges 0.01% to100%

For example, you want to pick up the price between OOE and BNB by Axios.

If you call it right, you will get a result like this:

or the api modules in SDK: For example we quote Ont chain

Swap

Here is the last step! Now you have several ways to swap the token you selected. You can directly use our swap API to trigger the trade, which will not awaken your personal wallet, but you have to provide your private key to the API. You can also use the swap_quote API to get the transaction body from our API server. Here is a case for you to make a transaction on BNB Chain.

The work flow we recommand for API users, is using Swapquote API to get transaction body, then use the wallet to request your transaction on chain.

On the SDK workflow:

For example, we want make a swap on terra chain.

Last updated