Or, if you use yarn as your module management tool.
yarn add @openocean.finance/openocean-sdk
Or, if you want to build up a jwallet and contract object by yourself, you will need web3 and bignumber.js.
npm install bignumber.js
npm install web3
How to use the sdk in your project
import { OpenoceanSdk } from '@openocean.finance/openocean-sdk'
const openoceanSdk = new OpenoceanSdk()
const { api, swapSdk, config } = openoceanSdk
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:
Choose a chain.
Choose your wallet.
Choose the token pair.
Input the amount you want to trade.
Swap. To make this process successful, we recommend using the Open API and Wallet plugin we provide to achieve DeFi trading.
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.
import { MetaMask } from "@openocean.finance/wallet";
const connectWallet( params ) {
const myWallet = new MetaMask()
const result = await myWallet.requestConnect(params.chainId);
// you can use the requestConnect function to trigger your wallet
}
Or you can trigger the wallet directly by the SDK.
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
const { sdk } = myWallet;
contract = new sdk.eth.Contract(Contract_abi, inToken_address);
// For example, by running this code, you get the contract to check the inToken's balance
Or, if you want to use ethers.
const { sdk } = myWallet;
const { currentProvider } = sdk;
myEtherWallet = new ethers.providers.Web3Provider(currentProvider);
//transfer your web3 wallet object to ether
signer = myEtherWallet.getSigner();
contract = new ethers.Contract(ContractAddress, contract.abi, signer);
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:
const { sdk } = myWallet;
const contract = new sdk.eth.Contract(ERC20_abi, inToken);
balance = await contract.methods.balanceOf(result.address).call();
//Save the result object which can be used here for balance checking.
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:
const gas = await contract.methods.approve(toContract, approveAmount).estimateGas({ from: account });
// get your gas fee
return await contract.methods.approve(toContract, approveAmount).send({
from: account,
gasPrice,
gas,
});
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.
async swap() {
if(this.address && this.inAmount > 0) {
let params = {
chain: 'bsc',
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 5,
gasPrice: 5,
slippage:100,
};
const res = await axios.get("https://open-api.openocean.finance/v3/bsc/swap_quote?inTokenAddress=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&outTokenAddress=0x55d398326f99059ff775485246999027b3197955&amount=5&gasPrice=5&slippage=100&account=0x929B44e589AC4dD99c0282614e9a844Ea9483C69");
if(res) {
const {estimatedGas,data,gasPrice} = res.data.data;
const swapParams = {
from:this.address,
to:'0x6352a56caadc4f1e25cd6c75970fa768a3304e64', //this is the only contract you can use if you decide to make transaction by our API.
gas: estimatedGas,
gasPrice: gasPrice,
data
};
const result = await this.myWallet.sdk.eth.sendTransaction(swapParams)
};
else {
return
},