# How to implement ERC721 batchBuy

**URL:** <https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030>\
**Category:** Contracts\
**Created:** [March 1, 2021, 9:40am UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030 "2021-03-01T09:40:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExeciN](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/execin/32/2803_2.png) [@ExeciN](https://forum.openzeppelin.com/u/ExeciN)\
**Post date:** [March 1, 2021, 9:40am UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030/1 "2021-03-01T09:40:04Z")

</div>

I’m trying to implement a batchBuy function on 4.0 but I’m getting hit by scoping issues. More specifically, I’m in need to modify `_balances` and `_owners` but they are declared as private.

Here is a non-complete implementation:

```auto
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.1;

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";

//...

contract Abc is ERC721Upgradeable {

    //...

    function buyBatch(uint256[] memory tokenIds) public payable {
        require(msg.value >= COST * tokenIds.length, "not enough funds to cover the cost of each token");

        _balances[msg.sender] += tokenIds.length;

        for (uint i = 0; i < tokenIds.length; i++) {
            _beforeTokenTransfer(address(0), msg.sender, tokenIds[i]);

            _owners[tokenIds[i]] = msg.sender;

            emit Transfer(address(0), msg.sender, tokenIds[i]);
        }
    }
}

```

Also, I’m thinking that `_balances[msg.sender] += tokenIds.length;` could be moved inside the `for` loop, below `_beforeTokenTransfer` and change to `_balances[msg.sender] += 1`

I’m trying to keep the parent contract intact so I’m not sure if there is a way to do it.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [March 1, 2021, 4:59pm UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030/2 "2021-03-01T16:59:22Z")

</div>

Hi @ExeciN, you should be using ERC721’s [`_mint`](https://docs.openzeppelin.com/contracts/3.x/api/token/erc721#ERC721-_mint-address-uint256-) or [`_safeMint`](https://docs.openzeppelin.com/contracts/3.x/api/token/erc721#ERC721-_safeMint-address-uint256-) functions.

---

<div class="post-metadata">

**Author:** ![ExeciN](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/execin/32/2803_2.png) [@ExeciN](https://forum.openzeppelin.com/u/ExeciN)\
**Post date:** [March 1, 2021, 9:01pm UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030/3 "2021-03-01T21:01:29Z")

</div>

I’m trying to conserve gas usage.  
This is why I don’t just call `_mint` inside the `for` loop.  
For the same reason I’m also trying to get as many things outside of the `for` loop.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [March 2, 2021, 3:47pm UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030/4 "2021-03-02T15:47:36Z")

</div>

Okay, I understand.

It’s not possible to access the owners mapping, this is by design and it won’t change.

My suggestion to conserve gas would be to first update to version 4.0 Beta, which has improved the gas efficiency of ERC721. (See [announcement](https://forum.openzeppelin.com/t/openzeppelin-contracts-4-0-beta/5878) for details.)

However, you have to use `_mint` in the for loop. Take a look at the gas costs after upgrading to 4.0 Beta and see if you still consider that it needs optimization.

---

<div class="post-metadata">

**Author:** ![ExeciN](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/execin/32/2803_2.png) [@ExeciN](https://forum.openzeppelin.com/u/ExeciN)\
**Post date:** [March 2, 2021, 4:04pm UTC](https://forum.openzeppelin.com/t/how-to-implement-erc721-batchbuy/6030/5 "2021-03-02T16:04:17Z")

</div>

This is already build on top of 4.0 beta hence the references to `_balances` and `_owners`.  
In any case I see that the `_owners` mapping stays private so I guess my quest stops here.

Thanks for the inputs.
