0

I have been practicing writing smart contracts using solidity and testrpc. How can I write and test a simple script that will send a message (e.g. "hello world") to another user on the blockchain. All the smart contracts I've written so far just involve mutating data from one address. Where will the messages get stored, and how will users read the messaged? I'm having great confusion understanding the high level architecture of this problem, and would greatly appreciate any input.

2 Answers 2

0

Data is always stored in the smart contract. So, technically, you won't be sending a message to another address, but you would be sending said message to the contract, and then the recipient would be able to access it.

Here's an extremely simplified example:

contract Messenger {

  mapping (address => string) message;

  function sendMessage(address _recipient, string _message){
    message[_recipient] = _message;
  }

  function readMessage() returns (string) {
    return message[msg.sender];
  }

}

As I mentioned, this is an extremely simplified example to illustrate what I'm saying and it won't do as a "chat app", having said that...

In this example you have a mapping which stores a given message for a user. Basically, anyone could call the sendMessage function, providing an address (the recipient of the message) and the message itself.

Then, the receiver simply calls readMessage() which will lookup the message mapped to his address.

So, as you can see, address A is not sending a message to address B directly, as a matter of fact, you can't "communicate" from an External Account (a human) to another External Account directly, except for sending ether. There has to be a smart contract in between which will receive a command and do whatever it was programmed for. In this case, it receives a message and recipient and stores that data so the recipient can later access it.

3
  • Thank you for the great and concise explanation! This clears up a lot of my confusion.
    – Abhas Arya
    Oct 31, 2017 at 3:17
  • I got a solidity script to work based on your solution. For scalability purposes, is it efficient to store messages in contract storage or should they be stored elsewhere such as ipfs?
    – Abhas Arya
    Oct 31, 2017 at 17:03
  • you can't "communicate" from an External Account (a human) to another External Account directly but there is a Transaction Data (Optional) input field in MetaMask, for example, that is data that is sent along, right? Aug 30, 2019 at 11:58
0

Your Question is: How to send data to another address on the blockchain? Am i right! normally we can store any data in blockchain! I think you ask a sample like a messenger... I think there is above code example is not good for a messenger because we can store only one data on that method..

This method is another sample for messenger

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;          

contract Messenger{
    mapping(address=>string[])public message;      
    function sendMessage(address _address,string memory _msg) public{   
   message[_address].push(_msg);  
    }  
    function readMessage(address _add)public view returns(string memory){  
        return message[_add];  
    }  
}  

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.