0

Good day to everyone, I've been working with brownie for sometime now and have been running into a lot of challenges. I created a struct basically and a mapping to that struct, however when I deploy my contract and assign information in the struct, I'm not able to fetch it from the mapping, I always get a null information. Kindly see my code below. P.S I've run it using brownie, a sepolia forked dev and my ganache cli. Also I have tested my code with remix and it works perfectly fine. I would really appreciate assistance.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract CrowdFunding{
    struct Project{
        uint256 totalCollated;
        address[] contributors;
        address owner;
        uint256 dateCreated;
        uint256[] contributions;
        string description;
        string name;
        uint256 totalNeeded;

    }

    mapping(uint256 => Project) public projects;

    mapping(address => uint256) public giveramount;

    mapping(address => string) public contributorNames;

    uint256 public totalProjects = 0;


    function createProject(address _owner, string memory _name, string memory _description, uint256 _dateCreated, uint256 _totalNeeded) public returns (uint256, address, string memory, string memory, uint256, uint256, uint256){
        Project storage newProject = projects[totalProjects];
        newProject.owner = _owner;
        newProject.name = _name;
        newProject.description = _description;
        newProject.dateCreated = _dateCreated;
        newProject.totalNeeded = _totalNeeded;
        newProject.totalCollated = 0;


        totalProjects++;    
        return (totalProjects - 1, newProject.owner, newProject.name, newProject.description, newProject.dateCreated, newProject.totalNeeded, newProject.totalCollated);            
    }
    
    function fundProject(uint256 id, string memory _contributorName) public payable{
        uint256 donation = msg.value;

        Project storage project = projects[id];
        

        project.contributors.push(msg.sender);

        giveramount[msg.sender] = donation;
        contributorNames[msg.sender] = _contributorName;


        project.contributions.push(donation);

        bool success = payable(project.owner).send(donation);
        if(success){
            project.totalCollated = project.totalCollated+donation;
            
        }
    }
    function getContributors(uint256 _val) view public returns(address[] memory, uint256[] memory){
        return(projects[_val].contributors, projects[_val].contributions);
        
        

        
    }
    function getAllProjects() public view returns(Project[] memory){
        Project[] memory allProjects = new Project[](totalProjects);

        for(uint i=0; i<totalProjects; i++){
            Project storage item = projects[i];
            allProjects[i] = item;
        }

        return allProjects;


    }
    
}

This is my brownie deploy.py function

from brownie import accounts, config, CrowdFunding, network
import os, time
from scripts.helpful_scripts import get_account


def deploy_crowdfunding():
    account = get_account()
    crowd_fund = CrowdFunding.deploy({"from": account})

    print(crowd_fund)


def crowdfund_create():
    crowdfund = CrowdFunding[-1]
    account = get_account()
    value = crowdfund.createProject.call(
        account,
        "Toluwani",
        "Hello",
        16,
        200,
    )
    time.sleep(6)
    print(value)


def fundProject_Crowd():
    crowdfund = CrowdFunding[-1]
    account = get_account()
    val = crowdfund.fundProject(
        0,
        "Amaechi",
        {
            "from": account,
            "value": 200,
        },
    )
    time.sleep(6)
    print(val)


def getProjects():
    account = get_account()
    crowdfund = CrowdFunding[-1]

    vals = crowdfund.projects(0)
    for key, value in vals.items():
        print(f"{key}: {value}")

    time.sleep(6)


def main():
    deploy_crowdfunding()
    crowdfund_create()
    fundProject_Crowd()
    getProjects()

When I run it on ganache locally this is what I get

CrowdfundingProject is the active project.

Running 'scripts\deploy.py::main'...
Transaction sent: 0x0f6a1c083581d51a5c53dd4dc8df063784c48c31900e0c5c1989d211b70ac840
  Gas price: 20.0 gwei   Gas limit: 1096294   Nonce: 0
  CrowdFunding.constructor confirmed   Block: 1   Gas used: 996631 (90.91%)
  CrowdFunding deployed at: 0x47AF73d262e404A3c3EFBF328BEF14e0ad0602a7

0x47AF73d262e404A3c3EFBF328BEF14e0ad0602a7
(0, '0xBbf0f26f7fA523d64AE3026Ff4938E30273d4B9E', 'Toluwani', 'Hello', 16, 200, 0)
Transaction sent: 0x7f61a217dcb92d9519432620a8f8b11eb46f07634ebb486f50fa69764b4faebc
  Gas price: 20.0 gwei   Gas limit: 208409   Nonce: 1
  CrowdFunding.fundProject confirmed   Block: 2   Gas used: 189463 (90.91%)

<Transaction '0x7f61a217dcb92d9519432620a8f8b11eb46f07634ebb486f50fa69764b4faebc'>
totalCollated: 200
owner: 0x0000000000000000000000000000000000000000
dateCreated: 0
description:
name:
totalNeeded: 0

I supposed to get a return value when I run my createProject function which I do. However my projects mapping is also supposed to show that I have registered my new project in it. But it just returns null values

2
  • Can you add more details about what is the expected output and what are you getting? It could be that some of the transactions failed, or it takes longer to and you are querying before the transaction is processed.
    – Ismael
    Commented May 15, 2023 at 22:03
  • I just updated my question
    – Tolu
    Commented May 17, 2023 at 0:30

1 Answer 1

0

When creating the project it uses .call()

value = crowdfund.createProject.call(
    account,
    "Toluwani",
    "Hello",
    16,
    200,
)

From the Brownie documentation

If you wish to call the contract method without a transaction, use the ContractTx.call method.

Using .call() won't persist the changes to the contract. It is a simulation.

Removing the .call() should be enough

value = crowdfund.createProject(
    account,
    "Toluwani",
    "Hello",
    16,
    200,
)

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.