Garfield
Garfield

Reputation: 3

Type Error: Identifier is not an contract

I am trying to create an new Instance of the contract, but it doesn`t work.

contract Campaign {
    struct Promotion {
        string description;
        uint max_influencer;
        uint jobvalue;
        bool achievement;
    }
    
    address[] public deployedPromotions;
    uint budget = msg.value;

    function createPromotion(string description, uint max_influencer) public payable {
        address newPromotion = new Promotion(description, budget, max_influencer);
        deployedPromotions.push(newPromotion);
        newPromotion.transfer(budget);
    }
}

Upvotes: 0

Views: 928

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43591

Your code has few logical logical errors:

  1. If you want to deploy the Promotion contract to a new address, it needs to be defined as a separate contract - not as a struct.

  2. Assigning uint budget = <some value>; only assigns the value when the variable is defined. So if you want to use the msg.value as a part of the createPromotion() scope, you need to assign in within the function.

Rest of the smaller issues are pointed out in the code comments

pragma solidity ^0.8;

contract Promotion {
    string description;
    uint max_influencer;
    uint jobvalue;
    bool achievement;
    
    // since you're passing 3 values from the `createPromotion()`
    // the constructor also receives 3 values here
    // it also needs to be `payable` to accept native tokens in the constructor
    constructor(string memory _description, uint _budget, uint _max_influencer) payable {
        description = _description;
        max_influencer = _max_influencer;
    }
    
    // the contract needs to have the `receive()` function to accept native tokens
    receive() external payable {
    }
}

// This is the main contract
contract Campaign {
    address[] public deployedPromotions;
    
    uint budget;
    
    // don't forget about the data location (in this case `memory`) with reference types
    function createPromotion(string memory description, uint max_influencer) public payable{
        budget = msg.value; // moved the assigning here
        address newPromotion = address(new Promotion(description, budget, max_influencer));
        deployedPromotions.push(newPromotion);
        payable(newPromotion).transfer(budget); // address needs to be `payable` to accept native tokens
    }
}

Upvotes: 1

Related Questions