An approach to automate the minting and selling of NFTs on rarible
Tags: nft, minting, rarible, opensea
Published: 2022-01-15 20:17:17

If you create an NFT collection, you will eventually get to the point where you need to upload your collection to a marketplace (e.g. Rarible, OpenSea). And if you are aiming for a 10k collection, you'll realize that you can not do this by hand. There are several approaches to automate this task. In this article I will show you how to approach this task with rarepress.

[Rarepress](https://rarepress.org/) is a decentralized NFT operating system. This means that it has all the tools to interact with NFTs (i.e. create, mint, sell, trade), marketplaces and much more. This includes a file store (i.e. ipfs), database, API, and wallet management. You can easily access it via [npm](https://npmjs.com). With this, we push an NFT to the ipfs, upload it to Rarible.com (including metadata, traits and royalties) and make it available for sale. Please note that there is an [amazing article by Skøgard](https://medium.com/rarible-dao/how-to-mint-a-10-000-item-generative-art-nft-collection-with-21-lines-of-javascript-dfa603cc56d5). However, I have found that there is a lot of trial and error involved here as well. So I would like to show you my approach based on the lessons learned from Skøgard.

Start a new terminal and add some boilerplate
```
mkdir rarepress-test
cd rarepress-test
npm init
```
Add all relevant details and you have a clean project. Run `npm i rarepress` to install rarepress. Also, create a file that you want to mint and upload to Rarible. We will have two functions, namely `mint` and `sell`. But let's start with some boilerplate code:
```
const Rarepress = require('rarepress');
const fs = require('fs');

const filePath = '.../path/to/your/nft.png';
const file = fs.readFileSync(filePath);

const rarepress = new Rarepress();
const account = await rarepress.init({ network: 'mainnet', host: 'https://eth.rarenet.app/v1' });
```
Next, you should connect your wallet to rarepress. As mentioned earlier, rarepress has a tool called mushie for this. Run `npx mushie import` in your terminal. You will need to enter your secret key phrase (e.g. prau pebbly carditis bastardy oeuvre salute) and your decryption password. Now your wallet is connected to rarepress. If you run the script again later, you will have to enter your decryption password again.

Now for the mint function
```
const mint = async (file, name, description, attributes, royalties) => {
  // add file to file system
  const cid = await rarepress.fs.add(file); 

  // create token
  const token = await rarepress.token.create({
    metadata: {
      name,
      description,
      image: `/ipfs/${cid}`,
      attributes,
    },
    royalties,
  });

  await rarepress.fs.push(cid);
  await rarepress.fs.push(token.uri);

  const response = await rarepress.token.send(token);

  return { cid, token, response };
};
```
There's already a lot going on. Let's break it down. First, we need to add the file to ipfs (inter planetary file system). This is a decentralized storage like blockchain, but for files. So the file is stored on many computers rather than just one cloud server, making it more durable. When we add this, we get a hash of the file in return. This is a crucial part. Since it is a file hash, you always get the same hash for the same file. You can do great things with this (like check if someone added the same file as you to ipfs).

After that, we create the actual token. We specify a name, description, the hash of our file in ipfs, attributes, and royalties. For a complete reference of the parameters, see the [docs](https://api.rarepress.org/#/?id=rarepresstokenbuild). If you specify the attributes, users can filter and search for your items later on Rarible (or OpenSea) by them. This is important because it allows flippers to see which NFTs are rarer than others and decide whether to invest based on that information. Royalties mean that a portion of future sales goes to the original creator. You can specify multiple royalty recipients with their respective shares. Note that each marketplace may have its own royalty caps (e.g. Rarible is at 50%). A share of 1000 is equal to 10.00%. So if someone sells our NFT for 1ETH, we get 0.1ETH because of the royalty. Finally, we push our file and token to ifps and make a request to Rarible to upload our token. From the `response` we can construct a valid rarible.com URL (see later section in this article).

Note that the second parameter of `rarepress.token.send` is optional and can be changed to point to any marketplace you want. If you skip this parameter it points to rarible.com.  

Now for the sell function
```
const sell = async (token) => {
  const trade = await rarepress.trade.create({
    what: {
      type: 'ERC721',
      id: token.tokenId
    },
    with: {
      type: 'ETH',
      value: 10**18
    }
  });

  const tradeResponse = await rarepress.trade.send(trade);
  return { trade, tradeResponse };
}
```
First, we create a trade with the token we just minted. It takes two parameters. The `what` describes what the subject of the trade is (i.e. our token). We need to specify the type, which is always `ERC721` for NFTs, and the ID of the token. The `with` parameter describes what we want in exchange for our token. The value is specified in the smallest possible unit, so Wei for ETH. We want 1ETH, which is equal to 10^18 Wei (see https://academy.binance.com/en/glossary/wei for more information). Finally, we need to send the trade and return both the trade and the trade response.

Now we need to put the parts together:
```
(async () => {
  const { token, response } = await mint(
    file, // file
    'Frame 208', // name
    'A frame from the game', // description
    [ // attributes
      { trait_type: 'rarity', value: 'unique' },
      { trait_type: 'background', value: 'green' },
      { trait_type: 'mouth', value: 'closed' },
      // ...
    ],
    [ // royalties
      { account, value: 500 }
    ]);

  await sell(token);

  console.log(`Done. You can find your nft at https://rarible.com/token/${response.id}`);
})();
```
When you visit the rarible link you should see something like this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85wwgpfek72675g9k9ur.png)

And that's it. Automatically minting an NFT including attributes and royalties, uploading to ipfs and selling the NFT. I have tried to go into as much detail as necessary to understand what's going on. But I barely scratched the surface. In fact, many more things are possible with rarepress. You can even run your own NFT marketplace.

Feel free to leave comments. I hope this helps you in your digital endeavour. If not, drop me a line and I'll be happy to help :)