I am a total Solidity noob, and I want to know if I can use my set function to initialize my struct members as well as assign a value to a state variable:

pragma solidity ^0.4.21;
contract Govtcontract {

    uint256 public allocbudget;
    uint256 public rembudget;

    struct transaction{
        uint  expend;
        uint dept;
        string item;
        uint date;
        string location;

 transaction public trx;

constructor (uint256 initbudget) public {
    allocbudget = initbudget;
    rembudget = initbudget;

function settx(uint amt, uint dep, string thing, uint date, string loc) public {
    rembudget = rembudget - trx.expend;

function get() public constant returns (uint) {
    return rembudget;


Also, how do I write the web3.js file to take multiple inputs for set()? I can't seem to find any resources online. TIA!


Yes you can! You can validate this with Truffle:

  1. Init project via truffle init and add your contract and deployment file
  2. truffle develop
  3. compile
  4. migrate
  5. Govtcontract.deployed().then(instance => govt = instance.contract)
  6. govt.settx(1000, 1000, "test", 1000, "test", { gas: 500000 })

I arbitrarily set 500,000 because storing so many variables in one batch is not cheap in gas costs.

Optionally, you could try Remix or EthFiddle for contract compilation.

| improve this answer | |

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.