1

I have a smart contract that has the following Struct Array in its constructor:

struct Details {
        string tokenType;
        address tokenAddress;
        uint256 tokenId;
        uint256 amount;
}

constructor(Details[] memory _details)

How do I create the migration.js for it?

I know I have to pass the arguments for it as a second parameter on the deployer.deploy function, but I don't know how truffle recognizes, for example, the address type in the Struct since it is not Solidity but JavaScript:

const SmartWallet = artifacts.require("SmartWallet");

module.exports = function(deployer) {
  deployer.deploy(SmartNFTFactory);
};

2 Answers 2

1

Alright hopefully last edit haha I didn't see that it was an array so I appologize.

I wrote a little sample contract to show you how to do the constructor, since you cannot pass an array of structs as a function param.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

contract TEST {
    // declare struct
    struct Details {
        string tokenType;
        address tokenAddress;
        uint256 tokenId;
        uint256 amount;
    }

    // create mapping to store structs by their address
    mapping(address => Details) public tokenDetails;
    // or ID
    // mapping(uint256 => Details) public tokenDetails;

    // Pass array of each value into constructor
    constructor(
        string[] memory _tokenTypes,
        address[] memory _tokenAddresses,
        uint256[] memory _tokenIds,
        uint256[] memory _tokenAmounts
    ) {
        // make sure all arrays are the same length
         require(
            _tokenTypes.length == _tokenAddresses.length &&
                _tokenTypes.length == _tokenIds.length &&
                _tokenTypes.length == _tokenAmounts.length,
            "Array length mismatch"
        ); 
        // loop through arrays and add each value to mapping
        for (uint256 i; i < _tokenTypes.length; i++) {
            tokenDetails[_tokenAddresses[i]].tokenType = _tokenTypes[i];
            tokenDetails[_tokenAddresses[i]].tokenAddress = _tokenAddresses[i];
            tokenDetails[_tokenAddresses[i]].tokenId = _tokenIds[i];
            tokenDetails[_tokenAddresses[i]].amount = _tokenAmounts[i];
        }
    }
}

and then for the migrations you would set it up like this :

const SmartWallet = artifacts.require("SmartWallet");
    
module.exports = function(deployer) {
   const tokenTypes = ["foo","bar"];
   const tokenAddresses = ["0x1234..","0x5678..."];
   const tokenIds = [0,1];
   const amounts = [10000, 10001];
   deployer.deploy(SmartNFTFactory, tokenTypes, tokenAddresses, tokenIds, amounts);
};
6
  • Thank you but that's not what I am asking. I am asking about the migration file in truffle for that contract. Nov 8, 2022 at 21:27
  • I have edited my answer to show you how to use the constructor arguments in migrations! Hope that was what you were looking for. Nov 9, 2022 at 13:52
  • Thank you! But do you see that the constructor of the smart contract expects only one argument with an array? How do I pass that to the contract? It is a an array of structs. Nov 9, 2022 at 22:36
  • alright hopefully final edit now that I understand your question better, my apologies but hope this helps! Nov 10, 2022 at 15:14
  • Thank you! Are you sure you cannot pass and array of structs as a constructor now? Check this post: stackoverflow.com/questions/71558241/…. The last comment says that you can do it with Solidity 0.8.9. I also have a contract myself and it compiles with no problems. Nov 10, 2022 at 23:05
0

The right way to do this is as follows:

const SmartWallet = artifacts.require("SmartWallet");

module.exports = function(deployer) {
  deployer.deploy(SmartNFTFactory, [["ERC20", "0xfe02781cc0fe76Bfd2D211430bfa97D2889fd853", 0, 0]]);
};

The second argument for the deploy function is the Details[] array that the smart contract expects.

If you have any extra doubt, here is a link to a hardhat project that implements a very simple functionality to show how this may work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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