This guide will introduce the flow of using the marketplace sdk library
Prerequisite: Installing dependencies
Add the sdk to your repository
npm i @uniblock/launcher-marketplace
or
yarn add @uniblock/launcher-marketplace
Create SDK instance
To initialize the SDK, you should first create your presale contract through the Uniblock dashboard. Then you have to provide Uniblock API key which can be found after creating a project on the site. The API key can be seen inside the project page Then collect the contract address of your deployed contract and chain id and pass them into the initialize function with the Uniblock API key:
- Nodejs:
import { initializeMarketplace } from "@uniblock/launcher-marketplace"
const uniblockApiKey = <YOUR_UNIBLOCK_API_KEY>
const marketplaceContractAddress = <YOUR_CONTRACT_ADDRESS>
const chainId = <YOUR_CHAIN_ID>
const option = {
rpc: <YOUR_RPC_URL> // You can assign your own rpc url
}
const marketplaceSdk = await initializeMarketplace(uniblockApiKey, {contractAddress: marketplaceContractAddress, chainId}, option)
// or if you don't have your own rpc url, you can use uniblock sdk default url
// and use the following code:
// const marketplaceSdk = await initializeMarketplace(uniblockApiKey, {contractAddress, chainId})
// also chainId is optional and the default chainId of the following code will be 1:
// const marketplaceSdk = await initializeMarketplace(uniblockApiKey, {contractAddress})
- Web browser:
<script type="module">
import { initializeMarketplace } from "https://cdn.jsdelivr.net/npm/@uniblock/launcher-market/index.esm.min.js"
const uniblockApiKey = <YOUR_UNIBLOCK_API_KEY>
const marketplaceContractAddress = <YOUR_CONTRACT_ADDRESS>
const chainId = <YOUR_CHAIN_ID>
const option = {
rpc: <YOUR_RPC_URL> // You can assign your own rpc url
}
const marketplaceSdk = await initializeMarketplace(uniblockApiKey, {contractAddress: marketplaceContractAddress, chainId}, option)
</script>
Create a Signer instance
We first need to create two signers for owner
of sale or auction and buyer
:
- Nodejs:
const provider = new ethers.providers.JsonRpcProvider("RPC_URL", 5);
// needs the RPC Url of the specific chain, which can be found on chainlist:
// https://chainlist.org/chain/5
const owner = new ethers.Wallet("WALLET_PRIVATE_KEY", provider);
// similar processes for buyer
- Web browser
import { initializeWagmi } from "@uniblock/wagmi"
const chainId = <CHAIN_ID>
const wagmi = await initializeWagmi(uniblock_api_key);
const { chains, provider, webSocketProvider } = wagmi.configureChains([chainId]);
const client = wagmi.createClient(
provider,
webSocketProvider,
chains,
[
connector.METAMASK_CONNECTOR,
],
);
await wagmi.connectWallet(client, connector.METAMASK_CONNECTOR);
const signer = await wagmi.getSigner();
// similar processes for buyer
Note: To use the wagmi, install it through
yarn add @uniblock/wagmi
ornpm install @uniblock/wagmi
Sale launch
Step 1: Create sale
To launch a sale, owner
needs to call createSale
to create a sale using their signer
Ensure allowance from the listing token contract:
const listingToken = await marketplaceSdk.getListingFeeToken()
// listingToken is not native token
if(listingToken !== ZERO_ADDRESS){
const listingFee = await marketplaceSdk.getListingFee()
await marketplaceSdk.increaseAllowance(listingToken, listingFee, owner)
}
Ensure approval from nft contract:
const nftAddress = <NFT_ADDRESS>
const nftId = <token_ID>
// if nft address is an ERC721 token contract
await marketplaceSdk.approve(marketplaceContractAddress, nftId, owner)
// if nft address is an ERC1155 token contract
await marketplaceSdk.setApproveForAll(marketplaceContractAddress, true, owner)
Create sale:
const paymentToken = <DESIRED_CRYPTO_TOKEN> // can be native token or any ERC20 token
const createSaleParam = {
contractAddress: nftAddress,
tokenId: nftId,
startTime: <SALE_START_TIME>,
endTime: <SALE_END_TIME>,
paymentToken,
price: 300,
tokenType: 1, // 0 if nftAddress is an ERC721 address, 1 if nftAddress is ERC1155 address
amount: 2,
};
const result = await marketplaceSdk.createSale(createSaleParam, owner);
const saleId = await marketplaceSdk.getTotalSales()
Step 2: Buy
Ensure allowance from the payment token and buyer can call buy method to purchase using buyer
signer
const { paymentToken, amount, price } = await marketplaceSdk.getSale(saleId)
const paymentAmount = amount.mul(price).toString()
// if paymentToken is an ERC20 token address
if(paymentToken !== ZERO_ADDRESS){
await marketplaceSdk.increaseAllowance(paymentToken, paymentAmount, buyer)
}
await marketplace.buy(saleId, '1', paymentToken, paymentAmount, buyer)
Step 3: Cancel Sale
If there are still nft tokens left in the marketplace contract, the sale owner could cancel the sale and the remaining nft tokens stored in the marketplace contract will be returned back to the sale owner
const { amount, purchasedAmount, endTime } = await marketplaceSdk.getSale(saleId);
if(!amount.sub(purchasedAmount).eq(BigNumber.from('0') && endTime.gt(Math.floor(Date.now() / 1000))){
await marketplaceSdk.cancelSale(saleId, owner);
}
Auction launch
Step 1: Create auction
To launch an auction, owner
needs to call the createAuction
method to create an auction using their signer
Ensure allowance from the listing token contract
// listingToken is not native token
if(listingToken !== ZERO_ADDRESS){
const listingFee = await marketplaceSdk.getListingFee()
await marketplaceSdk.increaseAllowance(listingToken, listingFee, owner)
}
Ensure approval from nft contract
const nftAddress = <NFT_ADDRESS>
const nftId = <token_ID>
// if nft address is an ERC721 token contract
await marketplaceSdk.approve(marketplaceContractAddress, nftId, owner)
// if nft address is an ERC1155 token contract
await marketplaceSdk.setApproveForAll(marketplaceContractAddress, true, owner)
Create auction:
const paymentToken = <DESIRED_CRYPTO_TOKEN>
// can be native token or any ERC20 token
const createAuctionParam = {
contractAddress: nftAddress,
tokenId: nftId,
startTime: <SALE_START_TIME>,
endTime: <SALE_END_TIME>,
paymentToken: paymentToken,
minimumPrice: 0,
maximumPrice: 100000,
tokenType: 1, // 0 if nftAddress is an ERC721 address, 1 if nftAddress is ERC1155 address
amount: 1,
};
await marketplaceSdk.createAuction(createAuctionParam, owner)
const auctionId = await marketplaceSdk.getTotalAuctions()
Step 2: Bid
Ensure allowance from the payment token and buyers can call the bid
method to bid on the nft. The bidder who has placed the highest bid or is the first to bid the maximum price will be able to claim the nft.
const price = <BID_PRICE>
if(paymentToken !== ZERO_ADDRESS){
await marketplaceSdk.increaseAllowance(paymentToken, price, buyer)
}
await marketplaceSdk.bid(auctionId, price, buyer)
Step 3: Claim
After the auction finishes, the auction owner
can call the method of claimAuctionNft
to end the auction so that the nft can be sent to the bidder and the payment to the nft will be sent to the feeReceiver
and the seller
.
await marketplaceSdk.claimAuctionNft(auctionId, owner)
npm i @uniblock/launcher-multisend
or
yarn add @uniblock/launcher-multisend