@sky-mavis/mavis-market-core
v0.0.4
Published
Mavis Market Core
Downloads
52
Keywords
Readme
Mavis market core
Installation
With yarn
yarn add @sky-mavis/mavis-market-core
With npm
npm install @sky-mavis/mavis-market-core --save
Usage
Fetch data
Get all collections
import { ChainId, getCollections } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
from: 0,
size: 10,
};
const { erc721Collections, erc1155Collections } = await getCollections(params);
Get collection detail
import { ChainId, getCollection } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
};
const collection = await getCollection(params);
Get token metadata
import { ChainId, getTokenMetadata } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
showAttributes: true, // Optional
};
const tokenMetadata = await getTokenMetadata(params);
Get all tokens
import {
ChainId,
AuctionType,
ListingSortBy,
getAllTokens,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
owner: '0xce21e5ed74935379eda4d9120c3887423f960aac',
from: 0,
size: 1,
auctionType: AuctionType.ForSale, // Optional
sort: ListingSortBy.PriceAsc, // Optional
};
const { total, tokens } = await getAllTokens(params);
Get erc721 tokens
import {
ChainId,
AuctionType,
SortBy,
getErc721Tokens,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
from: 0,
size: 10,
owner: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional
auctionType: AuctionType.NotForSale, // Optional
criteria: [{ name: 'attribute_1', values: ['value_1', 'value_2'] }], // Optional
sort: SortBy.SortBy, // Optional
name: 'Cyberkongz', // Optional
priceRange: {
from: '1000000000000000000',
to: '1000000000000000000',
}, // Optional
rangeCriteria: [
{
name: 'attribute_1',
range: {
from: '1000000000000000000',
to: '1000000000000000000',
},
},
], // Optional
};
const { total, results } = await getErc721Tokens(params);
Get erc721 token detail
import { ChainId, getErc721Token } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '982',
};
const data = await getErc721Token(params);
Get erc721 token transfer history
import {
ChainId,
getErc721TokenTransferHistory,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '982',
from: 0,
size: 10,
};
const { total, results } = await getErc721TokenTransferHistory(params);
Get erc1155 tokens
import {
ChainId,
AuctionType,
SortBy,
getErc1155Tokens,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
from: 0,
size: 10,
owner: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional
auctionType: AuctionType.NotForSale, // Optional
criteria: [{ name: 'attribute_1', values: ['value_1', 'value_2'] }], // Optional
sort: SortBy.PriceAsc, // Optional
name: 'Cyberkongz', // Optional
};
const { total, results } = await getErc1155Tokens(params);
Get erc1155 token detail
import { ChainId, getErc1155Token } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
};
const data = await getErc1155Token(params);
Get erc1155 token transfer history
import {
ChainId,
getErc1155TokenTransferHistory,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
from: 0,
size: 10,
};
const { total, results } = await getErc1155TokenTransferHistory(params);
Get erc1155 balance
import { ChainId, getErc1155Balance } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
owner: '0xce21e5ed74935379eda4d9120c3887423f960aac',
};
const balance = await getErc1155Balance(params);
Get erc721 order
import { ChainId, getErc721Order } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '982',
};
const order = await getErc721Order(params);
Get erc721 active orders of tokens
import { ChainId, getActiveOrdersOfTokens } from '@sky-mavis/mavis-market-core';
const tokenIds = [
{
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '1460',
},
{
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '508',
},
];
const params = { chainId: ChainId.testnet, tokenIds };
const activeOrdersOfTokens = await getActiveOrdersOfTokens(params);
Get erc1155 orders
import {
ChainId,
Erc1155SortBy,
getErc1155Orders,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
from: 0,
size: 10,
maker: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional,
showInvalid: true, // Optional
sort: Erc1155SortBy.PriceAsc, // Optional
};
const orders = await getErc1155Orders(params);
Get order by hash
import { ChainId, getOrderByHash } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
hash: 'f7c91f6f72b3fc2c19efea6abf34a6e713abc67c3beab9b152fa1f591b4472c8',
};
const order = await getOrderByHash(params);
Get orders by address
import {
ChainId,
ListingSortBy,
Erc,
getOrdersByAddress,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
from: 0,
size: 10,
sort: ListingSortBy.PriceAsc, // Optional
isValid: true, // Optional
collectibleFilters: {
tokenAddresses: ['0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09'],
erc: Erc.Erc1155,
}, // Optional
};
const { total, quantity, data } = await getOrdersByAddress(params);
Get offers
import { ChainId, getOffers } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '982',
from: 0,
size: 10,
};
const offers = await getOffers(params);
Get offer detail
import { ChainId, getOffer } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
hash: '1c91a5a0d8ef022344166e60e3da96ba819278fe496c4008bc0d028e8c9ab690',
};
const offer = await getOffer(params);
Get sent offers
import {
ChainId,
getSentOffers,
Erc,
OfferSortBy,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
from: 0,
size: 10,
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
collectibleFilters: {
tokenAddresses: ['0x3fe52e39c3241ee5440a57edbb553563356b770c'],
erc: Erc.Erc721,
}, // Optional
isValid: true, // Optional
sort: OfferSortBy.ExpiredAtAsc, // Optional
};
const { total, data } = await getSentOffers(params);
Get received offers
import {
ChainId,
Erc,
OfferSortBy,
getReceivedOffers,
} from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
from: 0,
size: 10,
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
collectibleFilters: {
tokenAddresses: ['0x3fe52e39c3241ee5440a57edbb553563356b770c'],
erc: Erc.Erc721,
}, // Optional
isValid: true, // Optional
sort: OfferSortBy.ExpiredAtAsc, // Optional
};
const { total, data } = await getReceivedOffers(params);
Get offers amount
import { ChainId, getOffersAmount } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
};
const { receivedOffersAmount, sentOffersAmount } = await getOffersAmount(
params
);
Get offer by address
import { ChainId, getOfferByAddress } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '982',
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
};
const offer = await getOfferByAddress(params);
Get exchange rate
import { ChainId, getExchangeRate } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
};
const exchangeRate = await getExchangeRate(params);
Main actions
Buy token
import {
ChainId,
paymentTokens,
Token,
ApproveTokenType,
fetchPaymentTokenBalance,
getOrder,
checkIsErc721OrderValid,
getTokensNeedToApprove,
checkIsErc20Approved,
buyToken
} from '@sky-mavis/mavis-market-core';
const wethAddress = paymentTokens[chainId][Token.WETH].address;
const ronAddress = paymentTokens[chainId][Token.RON].address;
const account = '0xce21e5ed74935379eda4d9120c3887423f960aac';
const chainId = ChainId.testnet;
const amount = '1000000000000000000';
const hash = 'f7c91f6f72b3fc2c19efea6abf34a6e713abc67c3beab9b152fa1f591b4472c8';
const wallet = createWalletClient();
// token balances
const wethBalance = await fetchPaymentTokenBalance(
chainId,
wethAddress,
account
);
const isInsufficientBalance = await checkIsInsufficientBalance(
chainId,
wethAddress,
account,
amount
);
// check order is valid
const checkIsOrderValid = async () => {
const order = await getOrder({ chainId, hash });
const isValid = await checkIsErc721OrderValid(chainId, order);
};
// approve token
const checkIsTokenApproved = async () => {
const tokens = await getTokensNeedToApprove(
chainId,
account,
wethAddress,
ronAddress,
amount
);
const isFirstTokenApproved = await checkIsErc20Approved(
chainId,
account,
tokens[0].address,
amount
);
};
const params = {
wallet,
chainId,
address: wethAddress,
tokenType: ApproveTokenType.Erc20,
};
const tx = await approveToken(params);
// buy
const deadline = parseInt(`${new Date().getTime() / 1000 + 30 * 60}`); // 30 minutes
const params = {
wallet,
chainId,
hash,
selectedTokenAddress: wethAddress,
deadline, // seconds,
};
const tx = await buyToken(params);
Bulk buy ERC721 tokens
import {
BulkBuyOrderData,
bulkBuyToken,
ChainId,
Erc,
Erc1155SortBy,
getActiveOrdersOfTokens,
getErc1155Orders,
paymentTokens,
Token,
} from '@sky-mavis/mavis-market-core';
import { getOrdersByQuantity } from './src/services/order/getOrdersByQuantity';
const chainId = ChainId.testnet;
const wallet = createWalletClient();
const paymentTokenAddress = paymentTokens[chainId][Token.WETH].address;
const deadline = parseInt(`${new Date().getTime() / 1000 + 30 * 60}`).toString(); // 30 minutes
const tokenIds = [
{
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '1460',
},
{
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
tokenId: '508',
},
];
const getActiveOrdersOfTokenParams = { chainId: ChainId.testnet, tokenIds };
const activeOrdersOfTokens = await getActiveOrdersOfTokens(getActiveOrdersOfTokenParams);
const selectedOrders: BulkBuyOrderData[] = activeOrdersOfTokens.map(order => ({
order,
quantity: 1,
}));
const tokensNeedToApproveByOrders = await getTokensNeedToApproveByOrders(
chainId,
wallet,
selectedOrders,
paymentTokenAddress,
);
const bulkBuyErc721TokenParams = {
chainId,
wallet,
data: selectedOrders,
selectedTokenAddress: paymentTokenAddress,
deadline, // seconds,
tokenType: Erc.Erc721,
requiredAllSuccess: false,
};
const tx = await bulkBuyToken(bulkBuyErc721TokenParams);
Bulk buy ERC1155 tokens
import {
BulkBuyOrderData,
bulkBuyToken,
ChainId,
Erc,
Erc1155SortBy,
getErc1155Orders,
paymentTokens,
Token,
} from '@sky-mavis/mavis-market-core';
import { getOrdersByQuantity } from './src/services/order/getOrdersByQuantity';
const chainId = ChainId.testnet;
const wallet = createWalletClient();
const paymentTokenAddress = paymentTokens[chainId][Token.WETH].address;
const quantity = 20;
const deadline = parseInt(`${new Date().getTime() / 1000 + 30 * 60}`).toString(); // 30 minutes
const getErc1155OrdersParams = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
from: 0,
size: 10,
maker: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional,
showInvalid: true, // Optional
sort: Erc1155SortBy.PriceAsc, // Optional
};
const chosenErc1155Orders = await getErc1155Orders(getErc1155OrdersParams);
const selectedOrders: BulkBuyOrderData[] = getOrdersByQuantity(chosenErc1155Orders, quantity);
const tokensNeedToApproveByOrders = await getTokensNeedToApproveByOrders(
chainId,
wallet,
selectedOrders,
paymentTokenAddress,
);
const bulkBuyErc1155TokenParams = {
chainId,
wallet,
data: selectedOrders,
selectedTokenAddress: paymentTokenAddress,
deadline, // seconds,
tokenType: Erc.Erc1155,
requiredAllSuccess: false,
};
const tx = await bulkBuyToken(bulkBuyErc1155TokenParams);
Create order
import {
ChainId,
ApproveTokenType,
checkIsErc721Approved,
approveToken
} from '@sky-mavis/mavis-market-core';
import * as functions from '@sky-mavis/mavis-market-core';
const wethAddress = paymentTokens[chainId][Token.WETH].address;
const tokenAddress = '0x3fe52e39c3241ee5440a57edbb553563356b770c';
const chainId = ChainId.testnet;
const wallet = createWalletClient();
// check token is approved
const isApproved = await functions.checkIsErc721Approved(
chainId,
account,
tokenAddress
);
//approve token
const params = {
wallet,
chainId,
address: tokenAddress,
tokenType: ApproveTokenType.Erc721,
};
const tx = await approveToken(params);
// create order
const params = {
wallet,
chainId,
tokenAddress,
tokenId: '982',
paymentToken: wethAddress,
price: '1000000000000000000',
duration: 1000000, // seconds
};
const order = await createOrder(params);
Cancel order
import {
ChainId,
cancelOrder,
} from '@sky-mavis/mavis-market-core';
import * as functions from '@sky-mavis/mavis-market-core';
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
hash: 'f7c91f6f72b3fc2c19efea6abf34a6e713abc67c3beab9b152fa1f591b4472c8',
};
const tx = await cancelOrder(params);
Gift
import { ChainId, giftToken } from '@sky-mavis/mavis-market-core';
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
tokenId: '982',
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
receiverAddress: '0x1ef7a5fefc14fd7f48c31cca651741549a692183',
};
const tx = await giftToken(params);
Wrap ron, unwrap ron
import { ChainId, wrapRon, unwrapRon } from '@sky-mavis/mavis-market-core';
// wrap RON
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
amount: '10',
};
const tx = await functions.wrapRon(params);
// unwrap RON
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
amount: '10',
};
const tx = await functions.unwrapRon(params);
Create offer
import {
ChainId,
checkIsInsufficientOfferBalance,
checkIsWRonTokenApproved,
approveToken,
} from '@sky-mavis/mavis-market-core';
const chainId = ChainId.testnet;
const account = '0xce21e5ed74935379eda4d9120c3887423f960aac';
const amount = '1000000000000000000';
const wRonAddress = wRonToken[chainId].address;
const isInsufficientOfferBalance = await functions.checkIsInsufficientOfferBalance(chainId, account, amount);
const isWronApproved = await checkIsWRonTokenApproved(
chainId,
account,
amount,
);
if(!isWronApproved) {
const params = {
wallet,
chainId,
address: wRonAddress,
tokenType: ApproveTokenType.WRon,
};
const tx = await approveToken(params);
}
// create offer
const wallet = createWalletClient();
const params = {
wallet,
chainId,
tokenId: '529',
tokenAddress: '0x3fe52e39c3241ee5440a57edbb553563356b770c',
price: '1000000000000000000',
duration: 10000, // seconds
};
const offer = await functions.makeOffer(params);
Accept offer
import { ChainId, acceptOffer } from '@sky-mavis/mavis-market-core';
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
hash: '1c91a5a0d8ef022344166e60e3da96ba819278fe496c4008bc0d028e8c9ab690',
};
const tx = await functions.acceptOffer(params);
Cancel offer
import { ChainId, cancelOffer } from '@sky-mavis/mavis-market-core';
import * as functions from '@sky-mavis/mavis-market-core';
const cancelOffer = async () => {
const wallet = createWalletClient();
const params = {
wallet,
chainId: ChainId.testnet,
hash: '1c91a5a0d8ef022344166e60e3da96ba819278fe496c4008bc0d028e8c9ab690',
};
const tx = await functions.cancelOffer(params);
};
Refresh metadata
import { ChainId, refreshMetadata } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
};
refreshMetadata(params);
Utilities functions
Create wallet client
import { ChainId } from '@sky-mavis/mavis-market-core';
const createWalletClient = () => {
const web3Provider = new ethers.providers.Web3Provider(window.ronin.provider);
const { signer, provider } = web3Provider;
return {
account: '0xce21e5ed74935379eda4d9120c3887423f960aac',
signer,
provider,
};
};
Get rate
import {
ChainId,
paymentTokens,
Token,
getRate,
} from '@sky-mavis/mavis-market-core';
const exchangeRate = await getExchangeRate();
const ronSymbol = paymentTokens[chainId][Token.RON].symbol;
const rate = getRate(ronSymbol, exchangeRate);
Get config
import { getConfig } from '@sky-mavis/mavis-market-core';
const configs = getConfig(ChainId.testnet);
Get swap config
import {
ChainId,
paymentTokens,
Token,
getSwapConfig,
} from '@sky-mavis/mavis-market-core';
const chainId = ChainId.testnet;
const ronAddress = paymentTokens[chainId][Token.RON].address;
const swapConfig = getSwapConfig(chainId, ronAddress);
Get payment tokens
import { ChainId, getPaymentTokens } from '@sky-mavis/mavis-market-core';
const chainId = ChainId.testnet;
const tokens = getPaymentTokens(chainId);
Get offer token
import { ChainId, wRonToken } from '@sky-mavis/mavis-market-core';
const chainId = ChainId.testnet;
const offerToken = wRonToken[chainId].address;
Get swap token data
import {
ChainId,
paymentTokens,
Token,
getSwapTokenData,
} from '@sky-mavis/mavis-market-core';
const wethAddress = paymentTokens[chainId][Token.WETH].address;
const ronAddress = paymentTokens[chainId][Token.RON].address;
const params = {
chainId: ChainId.testnet,
inputTokenAddress: wethAddress,
outputTokenAddress: ronAddress,
amount: '1000000000000000000',
};
const {
swappedAmount,
maximumSent,
liquidityProviderFee,
liquidityProviderFeePercent,
priceImpactPercent,
slippageTolerance,
} = await getSwapTokenData(params);
Get orders by quantity
import { getOrdersByQuantity } from '@sky-mavis/mavis-market-core';
const params = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
from: 0,
size: 10,
maker: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional,
showInvalid: true, // Optional
sort: Erc1155SortBy.PriceAsc, // Optional
};
const quantity = 50;
const orders = await getErc1155Orders(params);
const ordersByQuantity = getOrdersByQuantity(orders, quantity);
Get orders total price
import {
BulkBuyOrderData,
ChainId,
Erc1155SortBy,
getErc1155Orders,
getOrdersByQuantity,
getOrdersTotalPrice,
paymentTokens,
Token,
} from '@sky-mavis/mavis-market-core';
const chainId = ChainId.testnet;
const wallet = createWalletClient();
const selectedToken = paymentTokens[chainId][Token.RON];
const quantity = 20;
const getErc1155OrdersParams = {
chainId: ChainId.testnet,
tokenAddress: '0xb987afb62f67a317b5ddbc05c4ddba528a5dbd09',
tokenId: '5',
from: 0,
size: 10,
maker: '0xce21e5ed74935379eda4d9120c3887423f960aac', // Optional,
showInvalid: true, // Optional
sort: Erc1155SortBy.PriceAsc, // Optional
};
const chosenErc1155Orders = await getErc1155Orders(getErc1155OrdersParams);
const selectedOrders: BulkBuyOrderData[] = getOrdersByQuantity(chosenErc1155Orders, quantity);
const { totalPrice, totalPriceOfEachOrder } = await getOrdersTotalPrice({
chainId,
wallet,
data: selectedOrders,
token: selectedToken,
});
Convert amount to usd
import {
ChainId,
paymentTokens,
Token,
getExchangeRate,
convertAmountToUsd,
} from '@sky-mavis/mavis-market-core';
import * as functions from '@sky-mavis/mavis-market-core';
const amount = '1000000000000000000';
const ronSymbol = paymentTokens[chainId][Token.RON].symbol;
const exchangeRate = await getExchangeRate(ChainId.testnet);
const usdPrice = convertAmountToUsd(amount, ronSymbol, exchangeRate);