0

I have a smart contract function which receives an array of Structs as input. The struct is this.

struct Bgd {
        uint id;
        uint tanslationCode;
        string description;
        string image;
        string layer;
        uint supply;
        string createdBy;
        address revenue;
        uint price;
        bool approved;
    }

The function is below and simply takes the array of structs loops through and adds them to mappings.

mapping(uint => Bgd) _idToMetadata; 
mapping(uint => uint) _maxSupply; 
mapping(uint => uint) _itemsMinted;  
mapping(uint => bool) _approved;  

function addAttachedBackgrounds(Bgd[] calldata bgds)external onlyRole(UPDATER_ROLE){
        for (uint i; i < bgds.length; i++){
            uint id = bgds[i].id;
            _idToMetadata[id] = bgds[i];
            uint s = bgds[i].supply;
            _maxSupply[id] = s;//bgds[i].supply;
            if (id > bgdCurrentid){
                bgdCurrentid = id + 1; 
            }
            _approved[id] = true;
        }
    }

I am now running some unit tests with Hardhat and ethers. But I get an error when I try to add data to this function.

Error: invalid BigNumber value (argument="value", value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.6.2)

The code I use to create the input array is as follows.

let bgdData = [];
const accessories = require("./accessories.json");
  for (i = 0; i < accessories.length; i++) {
    let a = accessories[i];
    let a_price = Number(a["price"]).toFixed(4);
    let weiPrice = ethers.utils.parseEther(a_price);
 
    a["price"] = weiPrice;
    a["revenue"] = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
    if (a["layer"] == "background") {
      bgdData.push(a);
      // console.log(a);
    } else {
      a["avType"] = 1;
      a["city"] = 1;
      accessoryData.push(a);
    }
  }

Then to add it to the contract.

const addBgsData = async () => {
    await bgDataContract.addAttachedBackgrounds(bgdData);
  };

The other array created in the same way works fine. When I console log the array of bgdData is get the following objects.

{
    id: 6,
    translationCode: 6,
    description: '',
    image: 'https://avatarimg.metropolisworld.net/img/accessories/1/none/6',
    layer: 'background',
    supply: 10,
    createdBy: 'Puks',
    revenue: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
    price: BigNumber { value: "3255100000000000000" },
    approved: true
  }

So my question is why am I getting this error? Any help much appreciated.

2 Answers 2

0

This error usually occurs when the wrong type of data is passed as an argument.

I guess in your case, an undefined value is passed in the struct.

Also on a side note, while calling smart contract methods, if the number is long, consider passing it as a string. i.e. Your updated bgdData object may look something like the below:

{
    id: 6,
    translationCode: 6,
    description: '',
    image: 'https://avatarimg.metropolisworld.net/img/accessories/1/none/6',
    layer: 'background',
    supply: 10,
    createdBy: 'Puks',
    revenue: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
    price: "3255100000000000000",
    approved: true
}
2
  • Yeah I have tried as a string but I still get the same error. Nov 7 at 10:11
  • Try checking the values, so that correct values are passed and no undefined is being passed. Nov 7 at 12:40
0

Turns out there is a spelling error in the struct which was causing the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.