1

I am getting the following error. Could you please help me in solving by looking into the code.

/////ParserError: Expected pragma, import directive or contract/interface/library definition.
function unlockMessage(string name) public payable returns (bool) {
^------^  ///// 
pragma solidity ^0.4.25;

contract TrojanSecret {
// Data Types
uint memberCount;

string public name;
string public symbol;

mapping (string => address) Trojans;
mapping (string => string) secrets;
mapping (string => address[]) access;
mapping (string => uint) balance;
uint amount_for_unlock;

//constructor

constructor() public{
        name   = "Secrets"; 
        symbol = "S" ;
        amount_for_unlock = 1 ether;

}

function registerTrojan(string name) public returns(bool){
    if( Trojans[name] == address(0))  
    {
        memberCount ++;
        Trojans[name] = msg.sender;
        return True;
    } else {
        return false;
    }
}
function unregisterTrojan(string name) public {
    require( Trojans[name] != address(0));
    Trojans[name] = address(0);
    memberCount --;
}
function setSecret(string name,string message) public {
   require (Trojans[name] != address(0)&& Trojans[name] == msg.sender);
   secrets[name] = message;
}
function getSecret(string name) public view returns(string){
    require( Trojans[name] != address(0));
    bool flag = false;

    for(uint i = 0; i < access[name].length;i++)
    {
        if(access[name][i] == msg.sender){
            flag = true;
            break;

        }
    }
     if(flag){
         return secrets[name];
     }
     return "message is locked";
}

}
function unlockMessage(string name) public payable returns (bool) {
    require( Trojans[name] != address(0));
    if(msg.value == amount_for_unlock){

        balance[Trojans[name]] += msg.value;
        Trojans[name].transfer(msg.value);
        access[name].push(msg.sender);

        return true;
    } 
    else{
        return false;

    }    
}

}

  • seems like you have two closing curly brackets } for the function getSecret just before function unlockMessage – Greg Jeanmart Jan 28 at 18:18
  • Thank you so much for your quick response. I have another compile error after clearing the extra curly braces. TypeError: Type address is not implicitly convertible to expected type string memory. balance[Trojans[name]] += msg.value; – udayasree reddy Jan 28 at 18:28
  • Just submitted a complete answer for your question. – Greg Jeanmart Jan 28 at 18:39
1

Several errors here:

i. double closing curly brackets for the function getSecret just before function unlockMessage

ii. Boolean identifier True rather than true

function registerTrojan(string name) public returns(bool){
    if( Trojans[name] == address(0))  
    {
        memberCount ++;
        Trojans[name] = msg.sender;
        return True; // <<<<< true
    } else {
        return false;
    }
}

iii. Type issue

  • balance is map of type String -> uint. So when you call Trojans[name] it return an address.
  • Trojans is map of type String -> address. When you call balance[???], ??? is expected t be a String a per the definition of balance

Replace

balance[Trojans[name]] += msg.value;

By

balance[name] += msg.value;

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.