[legacy] invalid passing of bool contract method arguments
See original GitHub issueTL;DR
With the legacy
target for web3-0.2.x, bool
arguments in contract methods are passed through a .toString()
in the generated TS class, which results in the argument always being set to true
on the blockchain side.
Did not test with other targets.
Minimal reproduction
Contract
contract TestContract {
bool public field;
event FieldSet(bool value);
function setField(bool _field) public {
field = _field;
emit FieldSet(field);
}
}
Test
import { artifacts, contract, web3 } from './truffle-globals';
import { TestContract } from '@contracts/TestContract';
import { expect } from 'chai';
import { it } from 'mocha';
contract('TestContract', accounts => {
let testInstance: TestContract;
before(async () => {
const truffleDeployedContract = await artifacts
.require(TestContract.name)
.new({ from: accounts[0] });
testInstance = await TestContract.createAndValidate(
web3,
truffleDeployedContract.address,
);
});
it('tests setting the field', async () => {
expect(await testInstance.field).to.eq(false);
await testInstance.setFieldTx(true).send({ from: accounts[0] });
expect(await testInstance.field).to.eq(true);
});
it('tests unsetting the field', async () => {
expect(await testInstance.field).to.eq(true);
await testInstance.setFieldTx(false).send({ from: accounts[0] });
expect(await testInstance.field).to.eq(false);
});
});
Test output
Most important detail: during the second test the emitted event says value: true
, even though the method was called with false
:
Contract: TestContract
✓ tests setting the field (68ms)
1) tests unsetting the field
Events emitted during test:
---------------------------
FieldSet(value: true)
---------------------------
1 passing (264ms)
1 failing
1) Contract: TestContract
tests unsetting the field:
AssertionError: expected true to equal false
+ expected - actual
-true
+false
at Object.<anonymous> (test/asdf.spec.ts:27:45)
at Generator.next (<anonymous>)
at fulfilled (test/asdf.spec.ts:4:58)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
Culprit
The _field.toString()
argument in TestContract.ts
.
When the .toString()
conversion is removed manually, the test passes as expected.
public setFieldTx(
_field: boolean
): TC.DeferredTransactionWrapper<TC.ITxParams> {
return new TC.DeferredTransactionWrapper<TC.ITxParams>(this, "setField", [
_field.toString()
]);
}
env
typechain 0.3.8
web3 0.20.7
ganache-cli 6.1.8
truffle 4.1.13
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top Results From Across the Web
[legacy] invalid passing of bool contract method arguments #120
TL;DR With the legacy target for web3-0.2.x, bool arguments in contract methods are passed through a .toString() in the generated TS class, ...
Read more >[legacy] invalid passing of bool contract method arguments
TL;DR With the `legacy` target for web3-0.2.x, `bool` arguments in contract methods are passed through a `.toString()` in the generated TS class, ...
Read more >What is wrong with boolean parameters?
A boolean argument indicates the function has 2 different behaviors. These 2 behaviors are probably tangled together. And this just calls for more....
Read more >Error: Error encoding arguments: Error: invalid arrayify value
In Solidity we can assign String literal to a byte32 type variable easily. Solidity considers it as a byte32 literal. contract SolidityTest { ......
Read more >8.2 Function Contracts - Racket Documentation
A function contract wraps a procedure to delay checks for its arguments and ... argument must be an integer, and the second argument...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @quezak giving this to you since you opened the issue 😃
Issue Status: 1. Open 2. Started 3. Submitted 4. Done
The funding of 100.0 DAI (100.0 USD @ $1.0/DAI) attached to this issue has been approved & issued to @quezak.