SDK
Chains and Tools
Wallet connection, contract interaction, and gas price utilities.
Connect Wallet
Connect to popular wallets like MetaMask, TrustWallet, and more.
Important
Multiple browser wallets can conflict — only open one at a time (e.g., MetaMask, TrustWallet, Coin98Wallet cannot be open simultaneously).
Using wallet directly
Terminal
JS
1 import { MetaMask } from "@openocean.finance/wallet"; 2
3 const connectWallet = async (params) => { 4 const myWallet = new MetaMask() 5 const result = await myWallet.requestConnect(params.chainId); 6 // you can use the requestConnect function to trigger your wallet 7 }
Using the SDK
Terminal
JS
1 async connectWallet() { 2 try { 3 let AllChainNames = config.chains.chainNames 4 let AllWalletNames = config.wallets.walletList.map(item => item.key) 5 // ["MetaMask","CryptoCom","TrustWallet",...] 6 // ["eth","ropsten","rinkeby","bsc","solana","flow","polygon","avax",...] 7 8 let data = await swapSdk.connectWallet({ 9 chainName: this.chainName, 10 walletName: this.walletName 11 }) 12 13 if (data) { 14 this.myWallet = data.wallet 15 this.chain = data.chain 16 } 17 } catch (error) { 18 this.myWallet = null 19 this.chain = null 20 } 21 }
Run Contract
Create operable contract objects for token approving, balance checking, and swapping.
Using Web3.js
Terminal
JS
1 const { sdk } = myWallet; 2 contract = new sdk.eth.Contract(Contract_abi, inToken_address); 3 // For example, by running this code, you get the contract to check the inToken's balance
Using ethers.js
Terminal
JS
1 const { sdk } = myWallet; 2 const { currentProvider } = sdk; 3 myEtherWallet = new ethers.providers.Web3Provider(currentProvider); 4 // transfer your web3 wallet object to ether 5 signer = myEtherWallet.getSigner(); 6 contract = new ethers.Contract(ContractAddress, contract.abi, signer);
Get Gas Price
Using Open API
Terminal
JS
1 getGasPrice() { 2 let chainName = 'bsc'; 3 let data = await axios.get(`https://open-api.openocean.finance/v3/${chainName}/gasPrice`) 4 }
Using SDK
Terminal
JS
1 async getGasPrice() { 2 this.gasPrice = await api.getGasPrice({ 3 chain: this.chainName, 4 }) 5 }