0

I read over few website, tutorials and answered question here on SE. There is a huge warning about deploying new contract just to store datas.

i.e. :

bad

  uint256 public amount;
  bool public isAdmin;
  function User(uint256 _amount, bool _isAdmin) {
    amount = _amount;
    isAdmin = _isAdmin;
  }
}

good

  mapping(address => uint256) amount;
  mapping(address => bool) isAdmin;
}

In my case I need to store much more complex data more fields and dedicated methods.

Is it better to go through new contract deployment (and the need to propagate the original sender) or to use several mappings and functions with new parameter "hash" ?

i.e.

Contract style

contract myObject {

address owner;
uint4 stage;
uint256 someOtherData;
mapping(address => uint4) someUserInteraction;

constructor(address user) {
owner=user;
stage=0;
}

function interact(address user, uint4 data)
external
...

function changeStage(uint4 new stage)
external
....

}

contract myContract {
...
address[] objects

function createObject(someData) {
myObject o = myObject(msg.sender,someData);
objects.push(o);
}

function interact (address object, uint4 interaction)
{
myObject o = Object(object);
myObject.interact(msg.sender,interaction);
}
//now we can get the myObject through its address and interact by calling its methods

}

Mapping Style


contract myContract{

uint256[] objects
mapping (uint256 => uint4) objectStage
mapping (uint256 => mapping(address => uint4)) objectInteraction
mapping (uint256 => uint256) objectData
mapping (uint256 => address) objectOwner


function createObject(uint256 data)
external
{
uint256 newObjectIndex = objects.length;
objects.push(newObjectIndex );
objectOwner[newObjectIndex ]=msg.send;
objectData[newObjectIndex ]=data;
}

function interactWithObject(uint256 index, uint4 data)
external
{
objectInteraction[index][msg.sender]=data;
}

//I must always be aware of the index of my Object to interact with it but I don't need another contract

All my objects have some properties and some methods to interact through an independant lifeCycle.

To be less abstract let's imagine myContract is a groupOfPeople and my Object is a decision to be made.

A decision have a stage (New, Reviewed, PendingApproval, Approved or Canceled), a description, an impact, a price, etc. Each member of the groupOfPeople, can create a new Decision then submit to other for approval. Other user can review, suggest changes or votes. the author of the decision request can change it, stop vote, etc.

Does each decision should be a contract or just a bunch of mapping known by a key (index or hash of something).

Thanks for your help, advice and experience feedbacks.

Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.