Skip to content

Commit

Permalink
Merge pull request #130 from quezak/master
Browse files Browse the repository at this point in the history
[legacy] correctly pass boolean arguments in methods
  • Loading branch information
krzkaczor committed Dec 17, 2018
2 parents 7e06b15 + 8af4250 commit 667d07a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
21 changes: 18 additions & 3 deletions __snapshots__/DumbContract.spec.ts.js
Expand Up @@ -112,6 +112,15 @@ export class DumbContract extends TC.TypeChainContract {
stateMutability: "nonpayable",
type: "function",
},
{
constant: true,
inputs: [{ name: "boolArrayParam", type: "bool[]" }],
name: "callWithBooleanArray",
outputs: [{ name: "", type: "bool[]" }],
payable: false,
stateMutability: "pure",
type: "function",
},
{
constant: true,
inputs: [{ name: "", type: "uint256" }],
Expand Down Expand Up @@ -226,12 +235,12 @@ export class DumbContract extends TC.TypeChainContract {
}
public callWithBoolean(boolParam: boolean): Promise<boolean> {
return TC.promisify(this.rawWeb3Contract.callWithBoolean, [boolParam.toString()]);
return TC.promisify(this.rawWeb3Contract.callWithBoolean, [boolParam]);
}
public callWithArray2(arrayParam: BigNumber[]): Promise<BigNumber[]> {
return TC.promisify(this.rawWeb3Contract.callWithArray2, [
arrayParam.map(val => val.toString()),
arrayParam.map(arrayParamElem => arrayParamElem.toString()),
]);
}
Expand All @@ -243,6 +252,12 @@ export class DumbContract extends TC.TypeChainContract {
return TC.promisify(this.rawWeb3Contract.testString, [a.toString()]);
}
public callWithBooleanArray(boolArrayParam: boolean[]): Promise<boolean[]> {
return TC.promisify(this.rawWeb3Contract.callWithBooleanArray, [
boolArrayParam.map(boolArrayParamElem => boolArrayParamElem),
]);
}
public counterArray(arg0: BigNumber | number): Promise<BigNumber> {
return TC.promisify(this.rawWeb3Contract.counterArray, [arg0.toString()]);
}
Expand Down Expand Up @@ -279,7 +294,7 @@ export class DumbContract extends TC.TypeChainContract {
}
public callWithArrayTx(arrayParam: BigNumber[]): TC.DeferredTransactionWrapper<TC.ITxParams> {
return new TC.DeferredTransactionWrapper<TC.ITxParams>(this, "callWithArray", [
arrayParam.map(val => val.toString()),
arrayParam.map(arrayParamElem => arrayParamElem.toString()),
]);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/targets/legacy/generation.ts
Expand Up @@ -146,9 +146,13 @@ function codeGenForParams(param: AbiParameter, index: number): string {
}

function codeGenForArgs(param: AbiParameter, index: number): string {
const isArray = param.type instanceof ArrayType;
const paramName = param.name || `arg${index}`;
return isArray ? `${paramName}.map(val => val.toString())` : `${paramName}.toString()`;
if (param.type instanceof ArrayType) {
const elemParam = { name: `${paramName}Elem`, type: param.type.itemType };
return `${paramName}.map(${elemParam.name} => ${codeGenForArgs(elemParam, 0)})`;
}
if (param.type instanceof BooleanType) return paramName;
return `${paramName}.toString()`;
}

function codeGenForOutputTypeList(output: Array<AbiParameter>): string {
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Expand Up @@ -32,4 +32,4 @@ yarn tsc:truffle
if [ "$mode" = "COVERAGE" ]; then
echo "Sending coverage report"
yarn coveralls
fi
fi
5 changes: 5 additions & 0 deletions test/integration/contracts/DumbContract.sol
Expand Up @@ -67,6 +67,11 @@ contract DumbContract {
return boolParam;
}

function callWithBooleanArray(bool[] boolArrayParam) public pure returns (bool[]) {
boolArrayParam = boolArrayParam;
return boolArrayParam;
}

function testAddress(address a) pure public returns (address) {
return a;
}
Expand Down
16 changes: 15 additions & 1 deletion test/integration/targets/legacy/DumbContract.spec.ts
Expand Up @@ -195,13 +195,27 @@ describe("DumbContract", () => {
expect(result).to.eq(byteString);
});

it("should correctly pass both boolean values in args", async () => {
const dumbContract = await DumbContract.createAndValidate(web3, contractAddress);

expect(await dumbContract.callWithBoolean(true)).to.be.deep.eq(true);
expect(await dumbContract.callWithBoolean(false)).to.be.deep.eq(false);
});

it("should correctly pass a boolean array argument", async () => {
const dumbContract = await DumbContract.createAndValidate(web3, contractAddress);
const array = [true, false];

expect(await dumbContract.callWithBooleanArray(array)).to.be.deep.eq(array);
});

describe("estimateGas", () => {
it("should work", async () => {
const dumbContract = await DumbContract.createAndValidate(web3, contractAddress);

const estimatedGas = await dumbContract.countupTx(1).estimateGas({ from: accounts[0] });

expect(estimatedGas).to.be.eq(82683);
expect(estimatedGas).to.be.eq(82705);
});
});
});

0 comments on commit 667d07a

Please sign in to comment.