we are experiencing an issue dealing with structs. We expect to modify a struct property after getting it from an event in web3js, but, it seems that the modify does not apply.
Any ideas?
Contract code:
pragma solidity ^0.5.3;
pragma experimental ABIEncoderV2;
contract Example {
struct Structure {
uint value;
}
event Created(
Structure structure
);
event Modified(
Structure structure
);
constructor() public {}
function create() public {
emit Created(Structure(42));
}
function modify(Structure memory structure) public {
emit Modified(structure);
}
}
web3js code:
let Example = artifacts.require('Example')
let accounts = await web3.eth.getAccounts()
Example.deployed()
.then(instance => {
instance.Created(async (err, evt) => {
let structure = evt.returnValues.structure
console.log('Created: ' + structure.value)
structure.value = 21
console.log('Modify: ' + structure.value)
await instance.modify(structure, { from: accounts[0] })
})
instance.Modified((err, evt) => {
let structure = evt.returnValues.structure
console.log('Modified: ' + structure.value)
callback()
})
instance.create({ from: accounts[0] })
})
log:
Created: 42
Modify: 21
Modified: 42
We expected the modified value equals to 21
structure[0] = 21
.instance.modify([ structure.foo, structure.bar, ...], { from: accounts[0] })
. You still have to list all the properties in the right order somewhere, but you can centralize that code.