0

I am trying to build some Javascript using Web3js to list out nested structs. Specifically, I have designed a customer struct through a mapping. I have nested in the customer struct two additional mappings - billto and shipto information. I want to build a UI in Javascript using Web3js to list out all the customer's bill to and ship to information. I am struggling to figure out how to get it to display through Javascript. Here is the code to the contract:

pragma solidity ^0.4.11;

contract Customer {

function Customer() public{}

struct CustomerData {
    address custAddress;
    bytes32 customerName;
    bytes32 physicalAddr1;
    bytes32 physicalAddr2;
    bytes16 physicalCity;
    bytes8 physicalState;
    bytes8 physicalZipCode;
    bytes8 physicalCountry;
    int CreditLimit;  //Customer credit limit
    int CreditBalance;  //running balance the customer has

    //address[] billtoIndex; //billto index
    //uint256 custIndex;
    mapping(address=>Billto) billto;
    mapping(address=>Shipto) shipto;
    bool isEntity; //flag to show if entity already in blockchain
}

struct Billto {
    address BilltoAddr;
    bytes32 BilltoName;
    bytes32 physicalAddr1;
    bytes32 physicalAddr2;
    bytes16 physicalCity;
    bytes8 physicalState;
    bytes8 physicalZipCode;
    bytes8 physicalCountry;
    bool isEntity;


}

struct Shipto {
    address ShiptoAddr;
    bytes32 ShiptoName;
    bytes32 physicalAddr1;
    bytes32 physicalAddr2;
    bytes16 physicalCity;
    bytes8 physicalState;
    bytes8 physicalZipCode;
    bytes8 physicalCountry;
    bool isEntity;
}

    mapping(address => CustomerData) public customers;
    event addNewCustomer (address indexed CustAddr, bytes32 indexed CustName) ;  //add customer info to blockchain log
    event addBillto(address indexed CustAddr, address indexed BillAddr, bytes32 Name, bytes32 Addr1, bytes32 Addr2, bytes16 City, bytes8 State, bytes8 Zip, bytes8 Country) ; 
    event addShipto(address indexed CustAddr, address indexed ShipAddress, bytes32 Name, bytes32 Addr1, bytes32 Addr2, bytes16 City, bytes8 State, bytes8 Zip, bytes8 Country) ; 

// Add a new customer w/ relevant information
function newCustomer(address CustAddr, bytes32 Name, bytes32 Addr1, bytes32 Addr2, bytes16 City, bytes8 State, bytes8 Zip, bytes8 Country) public {

    customers[CustAddr].custAddress = CustAddr;
    customers[CustAddr].customerName = Name;
    customers[CustAddr].physicalAddr1 = Addr1;
    customers[CustAddr].physicalAddr2 = Addr2;
    customers[CustAddr].physicalCity = City;
    customers[CustAddr].physicalState = State;
    customers[CustAddr].physicalZipCode = Zip;
    customers[CustAddr].physicalCountry = Country;
    customers[CustAddr].CreditLimit = 0;
    customers[CustAddr].CreditBalance = 0;
    customers[CustAddr].isEntity = true;
    addNewCustomer(CustAddr, Name);

}
//add Bill to address
function newBillto(address CustAddr, address BillAddr, bytes32 Name, bytes32 Addr1, bytes32 Addr2, bytes16 City, bytes8 State, bytes8 Zip, bytes8 Country) public returns (string confirmed) {

        customers[CustAddr].billto[BillAddr].BilltoAddr = BillAddr;
        customers[CustAddr].billto[BillAddr].BilltoName = Name;
        customers[CustAddr].billto[BillAddr].physicalAddr1 = Addr1;
        customers[CustAddr].billto[BillAddr].physicalAddr2 = Addr2;
        customers[CustAddr].billto[BillAddr].physicalCity = City;
        customers[CustAddr].billto[BillAddr].physicalState = State;
        customers[CustAddr].billto[BillAddr].physicalZipCode = Zip;
        customers[CustAddr].billto[BillAddr].physicalCountry = Country;
        customers[CustAddr].billto[BillAddr].isEntity = true;
        confirmed = "Bill to address added";
        addBillto(CustAddr, BillAddr, Name, Addr1, Addr2, City, State, Zip, Country);
//    }



}
  //add Ship to address
function newShipto(address CustAddr, address ShipAddr, bytes32 Name, bytes32 Addr1, bytes32 Addr2, bytes16 City, bytes8 State, bytes8 Zip, bytes8 Country) 
public returns (string confirmed) {

        customers[CustAddr].shipto[ShipAddr].ShiptoName = Name;
        customers[CustAddr].shipto[ShipAddr].physicalAddr1 = Addr1;
        customers[CustAddr].shipto[ShipAddr].physicalAddr2 = Addr2;
        customers[CustAddr].shipto[ShipAddr].physicalCity = City;
        customers[CustAddr].shipto[ShipAddr].physicalState = State;
        customers[CustAddr].shipto[ShipAddr].physicalZipCode = Zip;
        customers[CustAddr].shipto[ShipAddr].physicalCountry = Country;
        addShipto(CustAddr, ShipAddr, Name, Addr1, Addr2, City, State, Zip, Country);
        return "Shipto added.";

}    


function adjustCreditLimit(address CustAddr, int newLimit)public returns (string confirmed){
    customers[CustAddr].CreditLimit = newLimit;    
    confirmed = "Customer credit limit changed";
    }    


}
0

You can't return an array of structs in Solidity yet, so you have at least two options:

1- Have your function return multiple arrays like so:

function getMultipleRecords() returns (uint[], uint[], address[]){
  //pseudocode
  uint[] integerData = [];
  uint[] someOtherData = [];
  address[] addressData = [];
  for each struct in your struct array {
    integerData.push(struct.intData);
    someOtherData.push(struct.someOtherData);
    addressData.push(struct.addressData);
  }  

  return (integerData,someOtherData,addressData);

}

Then in JS you could piece back the struct together.

2- You could have a function that returns the array length and then in JS for each element call a function that returns that given element.

function getOneRecord(uint index) returns (uint, uint, address){
  return (array[index].integerData, array[index].someOtherData, array[index].addressData);
}

Then you just piece the struct back together.

Method 1 requires more gas to execute and looks messy, but will retrieve the data in just one call. Method 2 will execute multiple calls (1 call per record) to the contract, but its simpler to code and will consume little gas per call , compared to Method 1, no matter the amount of records.

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.