NFT SDK
How to Install the sdk in your project
npm i @openocean.finance/nft-sdk
npm i @openocean.finance/wallet
How to use the sdk in your project
import { tryWalletConnect } from '@openocean.finance/wallet';
import { openoceanNftSdk } from "@openocean.finance/nft-sdk";
Connect wallet
Connect your wallet using @openocean.finance/wallet. walletParams will be used later in the method.
let walletParams = {
provider: {},
account: '',
chainId: '',
chainKey: '',
}
// Connecting the wallet
async function connectWallet() {
let params = {
chainName: 'avax',
walletName: 'MetaMask'
}
let data = await tryWalletConnect(params)
walletParams = {
provider: data.wallet.sdk,
account: data.wallet.address,
chainId: data.chain.chainId,
chainKey: data.chain.key,
}
}
Buy NFT
import axios from "axios";
const requestUrl = 'https://open-api.openocean.finance/nft/v1'
// Buy NFT
async function sendBuyNFTTransaction() {
const GasPrice = 5000
const contractAddress = '0x511aee0146d766a9d93ceb389d231027da337aae'
const tokenId = 1
const amount = 1
const code = 'eyJvcmRlcklkIjoiVDNKa1pYSldNbFI1Y0dVNk9EWXhOekU1TWpjNU5BPT0iLCJvd25lciI6IjB4YjQzNzA4Zjg3ZGQwMzE4ZTllMDUzZGQxNmFiMzk5MWIwOTllODBmNiIsInByaWNlIjoiMTI0MDAwMDAwMDAwMDAwMDAwIiwibmZ0SWQiOiJRWE56WlhSVWVYQmxPakV4TURZek9USXlOVFE9IiwicGF5bWVudElkIjoiVUdGNWJXVnVkRUZ6YzJWMFZIbHdaVG8yTXpNPSIsIm1hcmtldCI6Im9wZW5zZWEifQ'
const payToken = {
code: "wrapped-avax",
name: "Avalanche Token",
address: "0x0000000000000000000000000000000000000000",
decimals: 18,
symbol: "AVAX",
balance: 0,
ethValue: 0,
gasValue: 0.1,
balanceDecimals: 1
}
const { data } = await axios.get(
`${requestUrl}/${walletParams.chainKey}/detail?address=${contractAddress}&tokenId=${tokenId}&code=${code}&amount=${amount}`,
);
let myNft = data
const order = myNft.orders[0];
const buyData = {
sender: walletParams.account,
balanceTokens: [
{
address: payToken.address,
amount: payToken.balanceDecimals,
to: order.paymentAsset.address
},
],
dustTokens: [payToken.address],
buy: [
{
market: order.market,
code: (order && order.code) || code,
price: order.price * (10 ** order.paymentAsset.decimals),
tokenType: myNft.schema_name,
collection: myNft.contract_address,
tokenId: myNft.token_id,
tokenAddress: myNft.token_address,
amount,
},
],
};
const params = await axios.post(`${requestUrl}/${walletParams.chainKey}/buy`, buyData);
return await openoceanNftSdk.sendBuyNFTTransaction(
walletParams,
params,
GasPrice
)
}
Sell NFT
// Sell NFT
async function sendSellNFTTransaction() {
const { data } = await axios.get(`${requestUrl}/${walletParams.chainKey}/nfts?address=${walletParams.account}&cursor=1`);
let myNft = data[0]
const GasPrice = 5000
const sellParams = {
market: 'opensea',
sender: walletParams.account,
sell: [
{
amount: 1,
times: '604800',
price: '1000000000000000000',
collection: myNft.contract_address,
tokenAddress: myNft.token_address,
tokenId: myNft.token_id,
tokenType: myNft.schema_name,
paymentAsset: myNft.paymentAsset,
collectionSlug: myNft.collection_slug
}
]
};
const params = await axios.post(`${requestUrl}/${walletParams.chainKey}/sell`, sellParams);
await openoceanNftSdk.sendSellNFTTransaction(
walletParams,
{ ...sellParams, ...params },
GasPrice
)
}
Make offer
// Make offer
async function sendOfferNFTTransaction() {
const { data } = await axios.get(`${requestUrl}/${walletParams.chainKey}/nfts?address=${walletParams.account}&cursor=1`);
let myNft = data[0]
const isCollection = true
let price = 1
const order = {}
const market = 'opensea'
const GasPrice = 5000
const sellParams = {
market,
sender: walletParams.account,
offer: isCollection
? [
{
price: price * (10 ** 18),
times: '604800',
collection: myNft.contract_address,
collectionSlug: myNft.collection_slug,
amount: 1,
paymentAsset: myNft.paymentAsset
},
]
: [
{
price: price * (10 ** 18),
times: '604800',
collection: myNft.contract_address,
collectionSlug: myNft.collection.slug,
tokenAddress: myNft.token_address,
tokenId: myNft.token_id,
tokenType: myNft.schema_name,
paymentAsset: myNft.paymentAsset,
amount: order.amount,
},
],
};
const params = await axios.post(`${requestUrl}/${walletParams.chainKey}/offer`, sellParams);
await openoceanNftSdk.sendOfferNFTTransaction(
walletParams,
{ market, ...params },
GasPrice
)
}
Cancel sell order
// Cancel sell order
async function sendCancelNFTTransaction() {
const { data } = await axios.get(`${requestUrl}/${walletParams.chainKey}/nfts?address=${walletParams.account}&cursor=1`);
let myNft = data[0]
let market = 'opensea'
let signature = ''
const GasPrice = 5000
if (market === "x2y2") {
signature = await openoceanNftSdk.signMessageX2Y2(walletParams.provider)
}
const sellParams = {
sender: walletParams.account,
address: myNft.contract_address,
tokenId: myNft.token_id,
code: myNft.code,
market,
signature
}
const params = await axios.post(`${requestUrl}/${walletParams.chainKey}/cancel`, sellParams);
await openoceanNftSdk.sendCancelNFTTransaction(
walletParams,
params,
GasPrice
)
}
Last updated