I'm running an experiment where I'm trying to create the following behavior:
- Service owner deploys Experiment1 to the blockchain and is recorded as the owner.
- Owner then adds a user address, a user-defined uint, and a user-defined bytes32.
- The information is pushed to the arrA array, and the index of the record is mapped to the user's address and pushed to the arrAIndices array.
The problem is in the addToArrA function: it seems that state variables are being overwritten, and I can't understand how to prevent that from happening. In particular, owner is overwritten by _user, and arrA is set to be _AdataA in length. Completely at a loss on how to fix this.
pragma solidity ^0.4.17;
contract Experiment1 {
address public owner;
modifier onlyOwner {require(msg.sender == owner); _;}
struct structA {
address user;
uint AdataA;
bool AdataB;
bytes32 AdataC;
structB[] arrB;
}
struct structB {
uint BdataA;
uint BdataB;
}
structA[] public arrA;
// Solidity cannot return structs, and can only return arrays of addresses, bools, or uints
mapping (address => uint256[]) private arrAIndices;
// CurrencyHedge: Contract creator.
function Experiment1() {
owner = msg.sender;
}
// addToStructAArr: add an element to structAArr
function addToArrA(address _user, uint _AdataA, bytes32 _AdataC) public onlyOwner {
structA tempStructA;
// Populate the information in the new structA. This method must be used,
// since struct arrays cannot be passed as parameters.
// Refer to https://ethereum.stackexchange.com/questions/3525/how-to-initialize-empty-struct-array-in-new-struct
tempStructA.user = _user;
tempStructA.AdataA = _AdataA;
tempStructA.AdataB = false;
tempStructA.AdataC = _AdataC;
// Add the structA to the global list, and associate the index of the hedge with the beneficiary
arrA.push(tempStructA);
arrAIndices[_user].push(arrA.length - 1);
}
// getArrALength: Check on length of arrA
function getArrALength() public onlyOwner returns (uint) {
uint length = arrA.length;
return length;
}
// getArrAIndices: Retrieve a list of indices of the allHedges array associated with a particular beneficiary's
// Refer to https://ethereum.stackexchange.com/questions/3589/how-can-i-return-an-array-of-struct-from-a-function
function getArrAIndices(address _user) public onlyOwner returns (uint256[]) {
return arrAIndices[_user];
}
}